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.net.URI;
19  import java.util.ArrayList;
20  import java.util.Collection;
21  import java.util.Collections;
22  import java.util.List;
23  
24  import javax.annotation.security.RolesAllowed;
25  import javax.ws.rs.FormParam;
26  import javax.ws.rs.GET;
27  import javax.ws.rs.POST;
28  import javax.ws.rs.Path;
29  import javax.ws.rs.Produces;
30  import javax.ws.rs.core.Context;
31  import javax.ws.rs.core.MediaType;
32  import javax.ws.rs.core.Response;
33  import javax.ws.rs.core.UriInfo;
34  
35  import org.slf4j.Logger;
36  import org.slf4j.LoggerFactory;
37  
38  import de.smartics.properties.admin.domain.model.ConfigurationKeys;
39  import de.smartics.properties.admin.domain.model.ManagedApplication;
40  import de.smartics.properties.admin.domain.model.Paths;
41  import de.smartics.properties.admin.domain.model.Roles;
42  import de.smartics.properties.api.config.domain.key.ConfigurationKey;
43  
44  /**
45   * Provides access to configuration keys to select a configuration.
46   */
47  @Path("")
48  @RolesAllowed(Roles.VIEW)
49  public class ConfigurationKeysResource
50  {
51    // ********************************* Fields *********************************
52  
53    // --- constants ------------------------------------------------------------
54  
55    /**
56     * Reference to the logger for this class.
57     */
58    private static final Logger LOG = LoggerFactory
59        .getLogger(ConfigurationKeysResource.class);
60  
61    // --- members --------------------------------------------------------------
62  
63    /**
64     * Helper to construct paths.
65     */
66    @Context
67    private UriInfo uriInfo;
68  
69    // ****************************** Initializer *******************************
70  
71    // ****************************** Constructors ******************************
72  
73    // ****************************** Inner Classes *****************************
74  
75    // ********************************* Methods ********************************
76  
77    // --- init -----------------------------------------------------------------
78  
79    // --- get&set --------------------------------------------------------------
80  
81    // --- business -------------------------------------------------------------
82  
83    /**
84     * Returns the configuration keys for the configured application.
85     *
86     * @return the string representation.
87     */
88    @GET
89    @Path(Paths.PATH_CONFIGURATIONS)
90    @Produces(MediaType.TEXT_PLAIN)
91    public String getAsText()
92    {
93      final ConfigurationKeys keys = fetchConfigurationKeys();
94  
95      final StringBuilder buffer = new StringBuilder(512);
96      for (final ConfigurationKey<?> key : keys.getKeys())
97      {
98        buffer.append(key).append('\n');
99      }
100     return buffer.toString();
101   }
102 
103   /**
104    * Returns the configuration keys for the configured application.
105    *
106    * @return the HTML representation.
107    */
108   @GET
109   @Path(Paths.PATH_CONFIGURATIONS)
110   @Produces(MediaType.TEXT_HTML)
111   public ConfigurationKeys getAsHtml()
112   {
113     final ConfigurationKeys keys = fetchConfigurationKeys();
114     return keys;
115   }
116 
117   /**
118    * Returns the configuration keys for the configured application.
119    *
120    * @return the SDoc XML representation.
121    */
122   @GET
123   @Path(Paths.PATH_CONFIGURATIONS)
124   @Produces({ MediaType.TEXT_XML, MediaType.APPLICATION_XML })
125   public ConfigurationKeys getAsXml()
126   {
127     final ConfigurationKeys keys = fetchConfigurationKeys();
128     return keys;
129   }
130 
131   @SuppressWarnings({ "rawtypes", "unchecked" })
132   private ConfigurationKeys fetchConfigurationKeys()
133   {
134     if (LOG.isDebugEnabled())
135     {
136       LOG.debug("Fetching all configuration keys.");
137     }
138 
139     final ManagedApplication application = ManagedApplication.getApplication();
140     final Collection<ConfigurationKey<?>> keys =
141         application.getConfigurationKeys();
142     final List sorted = new ArrayList<ConfigurationKey<?>>(keys);
143     Collections.sort(sorted);
144 
145     final ConfigurationKeys configurationKeys =
146         new ConfigurationKeys(application, sorted);
147     return configurationKeys;
148   }
149 
150   /**
151    * Creates a new, empty configuration instance. As long as no properties are
152    * added, the configuration is only stored in-memory and will not be available
153    * after program start.
154    *
155    * @param configurationKey the key to the configuration to create.
156    * @return the HTML representation.
157    */
158   @POST
159   @Path(Paths.PATH_CONFIGURATIONS)
160   @Produces(MediaType.TEXT_HTML)
161   @RolesAllowed(Roles.CREATE_CONFIG)
162   public Response postAsHtml(
163       @FormParam(Paths.PARAM_CONFIGURATION_KEY) final String configurationKey)
164   {
165     final ManagedApplication application = ManagedApplication.getApplication();
166     application.getConfiguration(configurationKey);
167 
168     final ConfigurationKeys keys = fetchConfigurationKeys();
169     final URI self = uriInfo.getRequestUriBuilder().build();
170     final Response response = Response.seeOther(self).entity(keys).build();
171 
172     return response;
173   }
174 
175   // --- object basics --------------------------------------------------------
176 
177 }