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.util.List;
22  
23  import org.apache.commons.lang3.StringEscapeUtils;
24  
25  import de.smartics.properties.admin.domain.model.ApiServices;
26  import de.smartics.properties.admin.domain.model.ApiServices.ServiceDescriptor;
27  import de.smartics.properties.admin.domain.model.AppRelations;
28  import de.smartics.properties.admin.resources.controller.ApiResource;
29  import de.smartics.properties.admin.resources.representation.share.BreadcrumbHelper;
30  import de.smartics.properties.admin.resources.representation.xml.share.AbstractXmlRepresentationRenderer;
31  import de.smartics.resteasy.hypermedia.renderer.LinkDescriptor;
32  import de.smartics.resteasy.hypermedia.renderer.LinkDescriptorJaxrs;
33  import de.smartics.resteasy.hypermedia.renderer.LinkMetadata;
34  import de.smartics.resteasy.hypermedia.renderer.ResourceContext;
35  
36  /**
37   * Displays the list of services in an XML page.
38   */
39  public final class ApiServicesXmlRepresentationRenderer extends
40      AbstractXmlRepresentationRenderer<ApiServices>
41  {
42    // ********************************* Fields *********************************
43  
44    // --- constants ------------------------------------------------------------
45  
46    // --- members --------------------------------------------------------------
47  
48    /**
49     * The domain object to render.
50     */
51    private final ApiServices domainObject;
52  
53    // ****************************** Initializer *******************************
54  
55    // ****************************** Constructors ******************************
56  
57    /**
58     * Default constructor.
59     *
60     * @param context the context information to render the representation.
61     * @param domainObject the domain object to render.
62     * @param entityStream the stream to write to.
63     */
64    public ApiServicesXmlRepresentationRenderer(final ResourceContext context,
65        final ApiServices domainObject, final OutputStream entityStream)
66    {
67      super(context, entityStream);
68      this.domainObject = domainObject;
69    }
70  
71    // ****************************** Inner Classes *****************************
72  
73    // ********************************* Methods ********************************
74  
75    // --- init -----------------------------------------------------------------
76  
77    // --- get&set --------------------------------------------------------------
78  
79    // --- business -------------------------------------------------------------
80  
81    @Override
82    protected void httpBody() throws IllegalStateException
83    {
84      final String encoding = context.getResponse().getCharacterEncoding();
85      try
86      {
87        final PrintStream output = new PrintStream(entityStream, true, encoding);
88        output.append("<?xml version=\"1.0\" encoding=\"").append(encoding)
89            .append("\"?>\n\n");
90  
91        output.append("<services>\n");
92        for (final ServiceDescriptor service : domainObject.getServices())
93        {
94          if (service == ApiResource.SECURITY || service == ApiResource.JNDI)
95          {
96            continue;
97          }
98          final String id = service.getId();
99          output.append("  <service id=\"")
100             .append(StringEscapeUtils.escapeXml(id)).append("\">\n");
101       }
102       output.append("</services>\n");
103     }
104     catch (final UnsupportedEncodingException e)
105     {
106       throw new IllegalStateException(
107           "Cannot write services to XML file due to unsupported encoding '"
108               + encoding + "'.", e);
109     }
110   }
111 
112   @Override
113   protected List<LinkDescriptor> hyperLinks()
114   {
115     final List<LinkDescriptor> links =
116         BreadcrumbHelper.adjustSelf(breadcrumb.api());
117 
118     for (final ServiceDescriptor service : domainObject.getServices())
119     {
120       if (service == ApiResource.SECURITY || service == ApiResource.JNDI)
121       {
122         continue;
123       }
124 
125       final LinkDescriptor link = createLink(service);
126       links.add(link);
127     }
128 
129     return links;
130   }
131 
132   private LinkDescriptor createLink(final ServiceDescriptor service)
133   {
134     final LinkDescriptor link =
135         linkDescriptorFactory.createDescriptor(service, "getAsXml");
136     link.addRels(getRelByService(service), service.getId());
137 
138     final LinkMetadata metadata = new LinkMetadata();
139 
140     final LinkDescriptorJaxrs descriptor =
141         new LinkDescriptorJaxrs(link, metadata);
142     return descriptor;
143   }
144 
145   private String getRelByService(final ServiceDescriptor service)
146   {
147     if (service == ApiResource.CONFIGURATIONS)
148     {
149       return AppRelations.CONFIGURATIONS;
150     }
151     else if (service == ApiResource.DESCRIPTORS)
152     {
153       return AppRelations.DESCRIPTORS;
154     }
155 
156     return AppRelations.API_HOME;
157   }
158 
159   @Override
160   protected void close()
161   {
162   }
163 
164   // --- object basics --------------------------------------------------------
165 
166 }