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.ConfigurationKeys;
30  import de.smartics.properties.admin.resources.controller.PropertyKeysResource;
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.config.domain.key.ConfigurationKey;
34  import de.smartics.resteasy.hypermedia.renderer.LinkDescriptor;
35  import de.smartics.resteasy.hypermedia.renderer.LinkDescriptorJaxrs;
36  import de.smartics.resteasy.hypermedia.renderer.LinkMetadata;
37  import de.smartics.resteasy.hypermedia.renderer.ResourceContext;
38  
39  /**
40   * Displays the list of configuration keys in an XML page.
41   */
42  public final class ConfigurationKeysXmlRepresentationRenderer extends
43      AbstractXmlRepresentationRenderer<ConfigurationKeys>
44  {
45    // ********************************* Fields *********************************
46  
47    // --- constants ------------------------------------------------------------
48  
49    // --- members --------------------------------------------------------------
50  
51    /**
52     * The domain object to render.
53     */
54    private final ConfigurationKeys domainObject;
55  
56    // ****************************** Initializer *******************************
57  
58    // ****************************** Constructors ******************************
59  
60    /**
61     * Default constructor.
62     *
63     * @param context the context information to render the representation.
64     * @param domainObject the domain object to render.
65     * @param entityStream the stream to write to.
66     */
67    public ConfigurationKeysXmlRepresentationRenderer(
68        final ResourceContext context, final ConfigurationKeys domainObject,
69        final OutputStream entityStream)
70    {
71      super(context, entityStream);
72      this.domainObject = domainObject;
73    }
74  
75    // ****************************** Inner Classes *****************************
76  
77    // ********************************* Methods ********************************
78  
79    // --- init -----------------------------------------------------------------
80  
81    // --- get&set --------------------------------------------------------------
82  
83    // --- business -------------------------------------------------------------
84  
85    @Override
86    protected void httpBody() throws IllegalStateException
87    {
88      final String encoding = context.getResponse().getCharacterEncoding();
89      try
90      {
91        final PrintStream output = new PrintStream(entityStream, true, encoding);
92        output.append("<?xml version=\"1.0\" encoding=\"").append(encoding)
93            .append("\"?>\n\n");
94  
95        output.append("<configurations>\n");
96        for (final ConfigurationKey<?> key : domainObject.getKeys())
97        {
98          final String keyString = key.toString();
99          output.append("  <configuration id=\"")
100             .append(StringEscapeUtils.escapeXml(keyString)).append("\">\n");
101       }
102 
103       output.append("</configurations>\n");
104     }
105     catch (final UnsupportedEncodingException e)
106     {
107       throw new IllegalStateException(
108           "Cannot write configurations to XML file due to unsupported encoding '"
109               + encoding + "'.", e);
110     }
111   }
112 
113   @Override
114   protected List<LinkDescriptor> hyperLinks()
115   {
116     final List<LinkDescriptor> links =
117         BreadcrumbHelper.adjustSelf(breadcrumb.configurations());
118 
119     for (final ConfigurationKey<?> key : domainObject.getKeys())
120     {
121       final LinkDescriptor link = createLink(key.toString());
122       links.add(link);
123     }
124 
125     return links;
126   }
127 
128   private LinkDescriptor createLink(final String key)
129   {
130     final URI uri =
131         context.getUriInfo().getBaseUriBuilder()
132             .path(PropertyKeysResource.class, "getAsXml")
133             .build(new Object[] { key.toString() }, false);
134     final Link link =
135         Link.fromUri(uri).rel(AppRelations.CONFIGURATION + " " + key).build();
136     final LinkMetadata metadata = new LinkMetadata();
137 
138     final LinkDescriptorJaxrs descriptor =
139         new LinkDescriptorJaxrs(link, metadata);
140     return descriptor;
141   }
142 
143   @Override
144   protected void close()
145   {
146   }
147 
148   // --- object basics --------------------------------------------------------
149 
150 }