View Javadoc

1   /*
2    * Copyright 2013 smartics, Kronseder & Reiner GmbH
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package de.smartics.properties.admin.resources.representation.xml;
17  
18  import java.io.OutputStream;
19  import java.io.PrintStream;
20  import java.io.UnsupportedEncodingException;
21  import java.net.URI;
22  import java.util.List;
23  
24  import javax.ws.rs.core.Link;
25  
26  import org.apache.commons.lang3.StringEscapeUtils;
27  
28  import de.smartics.properties.admin.domain.model.AppRelations;
29  import de.smartics.properties.admin.domain.model.PropertyDescriptors;
30  import de.smartics.properties.admin.resources.controller.PropertyDescriptorResource;
31  import de.smartics.properties.admin.resources.representation.share.BreadcrumbHelper;
32  import de.smartics.properties.admin.resources.representation.xml.share.AbstractXmlRepresentationRenderer;
33  import de.smartics.properties.api.core.domain.PropertyDescriptor;
34  import de.smartics.properties.api.core.domain.PropertyKey;
35  import de.smartics.resteasy.hypermedia.renderer.LinkDescriptor;
36  import de.smartics.resteasy.hypermedia.renderer.LinkDescriptorJaxrs;
37  import de.smartics.resteasy.hypermedia.renderer.LinkMetadata;
38  import de.smartics.resteasy.hypermedia.renderer.ResourceContext;
39  
40  /**
41   * Displays the list of property descriptors belonging to a selected
42   * configuration in an XML page.
43   */
44  public final class PropertyDescriptorsXmlRepresentationRenderer extends
45      AbstractXmlRepresentationRenderer<PropertyDescriptors>
46  {
47    // ********************************* Fields *********************************
48  
49    // --- constants ------------------------------------------------------------
50  
51    // --- members --------------------------------------------------------------
52  
53    /**
54     * The domain object to render.
55     */
56    private final PropertyDescriptors domainObject;
57  
58    // ****************************** Initializer *******************************
59  
60    // ****************************** Constructors ******************************
61  
62    /**
63     * Default constructor.
64     *
65     * @param context the context information to render the representation.
66     * @param domainObject the domain object to render.
67     * @param entityStream the stream to write to.
68     */
69    public PropertyDescriptorsXmlRepresentationRenderer(
70        final ResourceContext context, final PropertyDescriptors domainObject,
71        final OutputStream entityStream)
72    {
73      super(context, entityStream);
74      this.domainObject = domainObject;
75    }
76  
77    // ****************************** Inner Classes *****************************
78  
79    // ********************************* Methods ********************************
80  
81    // --- init -----------------------------------------------------------------
82  
83    // --- get&set --------------------------------------------------------------
84  
85    // --- business -------------------------------------------------------------
86  
87    @Override
88    protected void httpBody() throws IllegalStateException
89    {
90      final String encoding = context.getResponse().getCharacterEncoding();
91      try
92      {
93        final PrintStream output = new PrintStream(entityStream, true, encoding);
94  
95        output.append("<?xml version=\"1.0\" encoding=\"").append(encoding)
96            .append("\"?>\n\n");
97        output.append("<propertySets>\n");
98        for (final String propertySet : domainObject.getPropertyKeys())
99        {
100         final List<PropertyDescriptor> descriptors =
101             domainObject.get(propertySet);
102 
103         output.append("  <propertySet id=\"")
104             .append(StringEscapeUtils.escapeXml(propertySet)).append("\">\n");
105         for (final PropertyDescriptor descriptor : descriptors)
106         {
107           output.append("    <descriptor>");
108           final PropertyKey key = descriptor.getKey();
109           output.append(StringEscapeUtils.escapeXml(key.toString()));
110           output.append("</descriptor>\n");
111         }
112         output.append("  </propertySet>\n");
113       }
114       output.append("</propertySets>\n");
115     }
116     catch (final UnsupportedEncodingException e)
117     {
118       throw new IllegalStateException(
119           "Cannot write descritors to XML file due to unsupported encoding '"
120               + encoding + "'.", e);
121     }
122   }
123 
124   @Override
125   protected List<LinkDescriptor> hyperLinks()
126   {
127     final List<LinkDescriptor> links =
128         BreadcrumbHelper.adjustSelf(breadcrumb.descriptors());
129 
130     for (final String propertySet : domainObject.getPropertyKeys())
131     {
132       final List<PropertyDescriptor> descriptors =
133           domainObject.get(propertySet);
134 
135       for (final PropertyDescriptor descriptor : descriptors)
136       {
137         final PropertyKey key = descriptor.getKey();
138         final LinkDescriptor link = createLink(key.toString());
139         links.add(link);
140       }
141     }
142 
143     return links;
144   }
145 
146   private LinkDescriptor createLink(final String key)
147   {
148     final URI uri =
149         context.getUriInfo().getBaseUriBuilder()
150             .path(PropertyDescriptorResource.class, "getAsXml")
151             .build(new Object[] { key }, false);
152     final Link link =
153         Link.fromUri(uri).rel(AppRelations.DESCRIPTOR + " " + key).build();
154     final LinkMetadata metadata = new LinkMetadata();
155 
156     final LinkDescriptorJaxrs descriptor =
157         new LinkDescriptorJaxrs(link, metadata);
158     return descriptor;
159   }
160 
161   @Override
162   protected void close()
163   {
164   }
165 
166   // --- object basics --------------------------------------------------------
167 
168 }