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 javax.naming.NamingException;
19  import javax.servlet.http.HttpServletRequest;
20  import javax.ws.rs.FormParam;
21  import javax.ws.rs.GET;
22  import javax.ws.rs.POST;
23  import javax.ws.rs.Path;
24  import javax.ws.rs.Produces;
25  import javax.ws.rs.core.Context;
26  import javax.ws.rs.core.MediaType;
27  import javax.ws.rs.core.UriInfo;
28  
29  import org.apache.commons.lang3.StringUtils;
30  
31  import de.smartics.properties.admin.domain.model.JndiConfiguration;
32  import de.smartics.properties.admin.domain.model.ManagedApplication;
33  import de.smartics.properties.admin.domain.model.Paths;
34  
35  /**
36   * Provides access JNDI to edit properties.
37   */
38  @Path("")
39  public class JndiResource
40  {
41    // ********************************* Fields *********************************
42  
43    // --- constants ------------------------------------------------------------
44  
45    // --- members --------------------------------------------------------------
46  
47    /**
48     * Provides access to the request to logout the user.
49     */
50    @Context
51    private HttpServletRequest request;
52  
53    /**
54     * Helper to construct paths.
55     */
56    @Context
57    private UriInfo uriInfo;
58  
59    // ****************************** Initializer *******************************
60  
61    // ****************************** Constructors ******************************
62  
63    // ****************************** Inner Classes *****************************
64  
65    // ********************************* Methods ********************************
66  
67    // --- init -----------------------------------------------------------------
68  
69    // --- get&set --------------------------------------------------------------
70  
71    // --- business -------------------------------------------------------------
72  
73    /**
74     * Provides information about the JNDI properties that may be used by
75     * smartics-properties.
76     *
77     * @return the HTML representation.
78     */
79    @GET
80    @Path(Paths.PATH_JNDI)
81    @Produces(MediaType.TEXT_HTML)
82    public JndiConfiguration getAsHtml()
83    {
84      final JndiConfiguration config = JndiConfiguration.getJndiConfiguration();
85      return config;
86    }
87  
88    // CHECKSTYLE:OFF
89    /**
90     * Provides information about the JNDI properties that may be used by
91     * smartics-properties.
92     *
93     * @param groupId the application's group ID.
94     * @param artifactId the application's artifact ID.
95     * @param version the application's version.
96     * @param archiveType the application's archive type.
97     * @param classifier the application's classifier.
98     * @param cacheName the JNDI mapped name of the cache.
99     * @param dsName the JNDI mapped name of the data source.
100    * @param dsCreateTable flag to create the properties table.
101    * @param dsDropTable flag to drop the properties table.
102    * @param securityKey the security key.
103    * @param securityAlgorithm the security algorithm.
104    * @param securityProvider the security provider.
105    * @param securityTransformation the security transformation.
106    * @return the HTML representation.
107    * @throws NamingException on any problem setting properties.
108    */
109   @POST
110   @Path(Paths.PATH_JNDI)
111   @Produces(MediaType.TEXT_HTML)
112   public JndiConfiguration postAsHtml(
113       @FormParam(Paths.PARAM_JNDI_APP_GROUP_ID) final String groupId,
114       @FormParam(Paths.PARAM_JNDI_APP_ARTIFACT_ID) final String artifactId,
115       @FormParam(Paths.PARAM_JNDI_APP_VERSION) final String version,
116       @FormParam(Paths.PARAM_JNDI_APP_ARCHIVE_TYPE) final String archiveType,
117       @FormParam(Paths.PARAM_JNDI_APP_CLASSIFIER) final String classifier,
118       @FormParam(Paths.PARAM_JNDI_CACHE_MAPPED_NAME) final String cacheName,
119       @FormParam(Paths.PARAM_JNDI_DS_MAPPED_NAME) final String dsName,
120       @FormParam(Paths.PARAM_JNDI_DS_CREATE_TABLE) final String dsCreateTable,
121       @FormParam(Paths.PARAM_JNDI_DS_DROP_TABLE) final String dsDropTable,
122       @FormParam(Paths.PARAM_JNDI_SECURITY_KEY) final String securityKey,
123       @FormParam(Paths.PARAM_JNDI_SECURITY_ALGORITHM) final String securityAlgorithm,
124       @FormParam(Paths.PARAM_JNDI_SECURITY_PROVIDER) final String securityProvider,
125       @FormParam(Paths.PARAM_JNDI_SECURITY_TRANSFORMATION) final String securityTransformation)
126     throws NamingException
127   {
128     final JndiConfiguration config = JndiConfiguration.getJndiConfiguration();
129 
130     if (!config.isInitialized())
131     {
132       config.initialize();
133     }
134 
135     config.setGroupId(norm(groupId));
136     config.setArtifactId(norm(artifactId));
137     config.setVersion(norm(version));
138     config.setArchiveType(norm(archiveType));
139     config.setClassifier(norm(classifier));
140 
141     config.setCacheName(norm(cacheName));
142 
143     config.setDsName(norm(dsName));
144     config.setDsCreateTable(norm(dsCreateTable));
145     config.setDsDropTable(norm(dsDropTable));
146 
147     config.setSecurityKey(norm(securityKey));
148     config.setSecurityAlgorithm(norm(securityAlgorithm));
149     config.setSecurityProvider(norm(securityProvider));
150     config.setSecurityTransformation(norm(securityTransformation));
151 
152     config.update();
153 
154     final ManagedApplication app = ManagedApplication.getApplication();
155     app.clean();
156 
157     return config;
158   }
159 
160   // CHECKSTYLE:ON
161 
162   private static String norm(final String value)
163   {
164     return StringUtils.isBlank(value) ? null : value;
165   }
166 
167   // --- object basics --------------------------------------------------------
168 
169 }