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.controller;
17  
18  import java.io.IOException;
19  
20  import javax.annotation.security.RolesAllowed;
21  import javax.servlet.http.HttpServletRequest;
22  import javax.ws.rs.GET;
23  import javax.ws.rs.Path;
24  import javax.ws.rs.PathParam;
25  import javax.ws.rs.Produces;
26  import javax.ws.rs.core.Context;
27  import javax.ws.rs.core.MediaType;
28  import javax.ws.rs.core.Response;
29  import javax.ws.rs.core.Response.Status;
30  import javax.ws.rs.core.UriInfo;
31  
32  import de.smartics.properties.admin.domain.model.ManagedApplication;
33  import de.smartics.properties.admin.domain.model.Paths;
34  import de.smartics.properties.admin.domain.model.Roles;
35  import de.smartics.properties.api.core.domain.PropertyDescriptor;
36  import de.smartics.properties.api.core.domain.PropertyDescriptorRegistry;
37  
38  /**
39   * Provides access to properties used by the application.
40   */
41  @Path("")
42  @RolesAllowed(Roles.VIEW)
43  public class PropertyDescriptorResource
44  {
45    // ********************************* Fields *********************************
46  
47    // --- constants ------------------------------------------------------------
48  
49    // --- members --------------------------------------------------------------
50  
51    /**
52     * Provides access to the request to logout the user.
53     */
54    @Context
55    private HttpServletRequest request;
56  
57    /**
58     * Helper to construct paths.
59     */
60    @Context
61    private UriInfo uriInfo;
62  
63    // ****************************** Initializer *******************************
64  
65    // ****************************** Constructors ******************************
66  
67    // ****************************** Inner Classes *****************************
68  
69    // ********************************* Methods ********************************
70  
71    // --- init -----------------------------------------------------------------
72  
73    // --- get&set --------------------------------------------------------------
74  
75    // --- business -------------------------------------------------------------
76  
77    /**
78     * Returns the property descriptor for a given property key.
79     *
80     * @param propertyKey the key to the requested property descriptor.
81     * @return the string representation.
82     * @throws IOException on any problem generating the XML representation.
83     */
84    @GET
85    @Path(Paths.PATH_PROPERTY_DESCRIPTOR + ".xml")
86    @Produces(MediaType.WILDCARD)
87    public Response getAsDotXml(
88        @PathParam(Paths.PARAM_PROPERTY_KEY) final String propertyKey)
89      throws IOException
90    {
91      final Response response = getAsXml(propertyKey);
92      return response;
93    }
94  
95    /**
96     * Returns the property descriptor for a given property key.
97     *
98     * @param propertyKey the key to the requested property descriptor.
99     * @return the string representation.
100    * @throws IOException on any problem generating the XML representation.
101    */
102   @GET
103   @Path(Paths.PATH_PROPERTY_DESCRIPTOR)
104   @Produces({ MediaType.APPLICATION_XML, MediaType.TEXT_XML })
105   public Response getAsXml(
106       @PathParam(Paths.PARAM_PROPERTY_KEY) final String propertyKey)
107     throws IOException
108   {
109     final ManagedApplication app = ManagedApplication.getApplication();
110     final PropertyDescriptorRegistry registry = app.getDescriptorRegistry();
111     final PropertyDescriptor descriptor = registry.get(propertyKey);
112 
113     if (descriptor == null)
114     {
115       final Response response = Response.status(Status.NOT_FOUND).build();
116       return response;
117     }
118 
119     final Response response =
120         Response.ok().type(MediaType.TEXT_XML_TYPE).entity(descriptor).build();
121     return response;
122   }
123 
124   /**
125    * Returns the property descriptor for a given property key as HTML.
126    *
127    * @param propertyKey the key to the requested property descriptor.
128    * @return the string representation.
129    * @throws IOException on any problem generating the XML representation.
130    */
131   @GET
132   @Path(Paths.PATH_PROPERTY_DESCRIPTOR)
133   @Produces({ MediaType.TEXT_HTML })
134   public Response getAsHtml(
135       @PathParam(Paths.PARAM_PROPERTY_KEY) final String propertyKey)
136     throws IOException
137   {
138     final ManagedApplication app = ManagedApplication.getApplication();
139     final PropertyDescriptorRegistry registry = app.getDescriptorRegistry();
140     final PropertyDescriptor descriptor = registry.get(propertyKey);
141     if (descriptor == null && propertyKey.endsWith(".xml"))
142     {
143       return getAsDotXml(propertyKey.substring(0, propertyKey.length() - 4));
144     }
145     else if (descriptor == null)
146     {
147       final Response response = Response.status(Status.NOT_FOUND).build();
148       return response;
149     }
150 
151     final Response response = Response.ok().entity(descriptor).build();
152     return response;
153   }
154 
155   // --- object basics --------------------------------------------------------
156 
157 }