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.lang.ObjectUtils;
27  import org.apache.commons.lang3.StringEscapeUtils;
28  
29  import de.smartics.properties.admin.domain.model.AppRelations;
30  import de.smartics.properties.admin.domain.model.PropertiesConfiguration;
31  import de.smartics.properties.admin.resources.controller.PropertyResource;
32  import de.smartics.properties.admin.resources.representation.share.BreadcrumbHelper;
33  import de.smartics.properties.admin.resources.representation.xml.share.AbstractXmlRepresentationRenderer;
34  import de.smartics.properties.api.config.domain.DescribedProperty;
35  import de.smartics.properties.api.config.domain.ValidatedProperty;
36  import de.smartics.properties.api.config.domain.key.ConfigurationKey;
37  import de.smartics.resteasy.hypermedia.renderer.LinkDescriptor;
38  import de.smartics.resteasy.hypermedia.renderer.LinkDescriptorJaxrs;
39  import de.smartics.resteasy.hypermedia.renderer.LinkMetadata;
40  import de.smartics.resteasy.hypermedia.renderer.ResourceContext;
41  
42  /**
43   * Displays a single property in an XML page.
44   */
45  public final class PropertyKeysXmlRepresentationRenderer extends
46      AbstractXmlRepresentationRenderer<PropertiesConfiguration>
47  {
48    // ********************************* Fields *********************************
49  
50    // --- constants ------------------------------------------------------------
51  
52    // --- members --------------------------------------------------------------
53  
54    /**
55     * The domain object to render.
56     */
57    private final PropertiesConfiguration domainObject;
58  
59    // ****************************** Initializer *******************************
60  
61    // ****************************** Constructors ******************************
62  
63    /**
64     * Default constructor.
65     *
66     * @param context the context information to render the representation.
67     * @param domainObject the domain object to render.
68     * @param entityStream the stream to write to.
69     */
70    public PropertyKeysXmlRepresentationRenderer(final ResourceContext context,
71        final PropertiesConfiguration domainObject,
72        final OutputStream entityStream)
73    {
74      super(context, entityStream);
75      this.domainObject = domainObject;
76    }
77  
78    // ****************************** Inner Classes *****************************
79  
80    // ********************************* Methods ********************************
81  
82    // --- init -----------------------------------------------------------------
83  
84    // --- get&set --------------------------------------------------------------
85  
86    // --- business -------------------------------------------------------------
87  
88    @Override
89    protected void httpBody() throws IllegalStateException
90    {
91      final String encoding = context.getResponse().getCharacterEncoding();
92      try
93      {
94        final PrintStream output = new PrintStream(entityStream, true, encoding);
95  
96        output.append("<?xml version=\"1.0\" encoding=\"").append(encoding)
97            .append("\"?>\n\n");
98        output.append("<properties config=\"")
99            .append(StringEscapeUtils.escapeXml(domainObject.getKey()))
100           .append("\">\n");
101 
102       for (final DescribedProperty property : domainObject.getProperties())
103       {
104         renderRow(output, property);
105       }
106       output.append("</properties>\n");
107     }
108     catch (final UnsupportedEncodingException e)
109     {
110       throw new IllegalStateException(
111           "Cannot write properties to XML file due to unsupported encoding '"
112               + encoding + "'.", e);
113     }
114   }
115 
116   private void renderRow(final PrintStream output,
117       final DescribedProperty property)
118   {
119     final String value;
120     if (property instanceof ValidatedProperty)
121     {
122       value =
123           ObjectUtils.toString(((ValidatedProperty) property)
124               .getValidatedValue());
125     }
126     else
127     {
128       value = property.getValue();
129     }
130 
131     final String key = property.getName();
132     final String source = createConfigSource(property);
133     output.append("  <property name=\"")
134         .append(StringEscapeUtils.escapeXml(key)).append("\" source=\"")
135         .append(StringEscapeUtils.escapeXml(source)).append("\">");
136 
137     if (value != null)
138     {
139       output.append(StringEscapeUtils.escapeXml(value));
140     }
141     output.append("</property>\n");
142   }
143 
144   private String createConfigSource(final DescribedProperty property)
145   {
146     final ConfigurationKey<?> configurationKey = property.getConfigurationKey();
147     final String label =
148         configurationKey == null ? "default" : configurationKey.toString();
149     return label;
150   }
151 
152   @Override
153   protected List<LinkDescriptor> hyperLinks()
154   {
155     final List<LinkDescriptor> links =
156         BreadcrumbHelper.adjustSelf(breadcrumb.configuration(domainObject
157             .getKey()));
158 
159     for (final DescribedProperty property : domainObject.getProperties())
160     {
161       final String name = property.getName();
162       final LinkDescriptor link = createLink(name);
163       links.add(link);
164     }
165 
166     return links;
167   }
168 
169   private LinkDescriptor createLink(final String propertyName)
170   {
171     final URI uri =
172         context.getUriInfo().getBaseUriBuilder()
173             .path(PropertyResource.class, "getAsXml")
174             .build(new Object[] { domainObject.getKey(), propertyName }, false);
175     final Link link =
176         Link.fromUri(uri).rel(AppRelations.PROPERTY + " " + propertyName)
177             .build();
178     final LinkMetadata metadata = new LinkMetadata();
179 
180     final LinkDescriptorJaxrs descriptor =
181         new LinkDescriptorJaxrs(link, metadata);
182     return descriptor;
183   }
184 
185   @Override
186   protected void close()
187   {
188   }
189 
190   // --- object basics --------------------------------------------------------
191 
192 }