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.net.URISyntaxException;
20  import java.util.ArrayList;
21  import java.util.List;
22  
23  import javax.servlet.ServletException;
24  import javax.servlet.http.HttpServletRequest;
25  import javax.ws.rs.GET;
26  import javax.ws.rs.POST;
27  import javax.ws.rs.Path;
28  import javax.ws.rs.Produces;
29  import javax.ws.rs.core.Context;
30  import javax.ws.rs.core.MediaType;
31  import javax.ws.rs.core.Response;
32  import javax.ws.rs.core.Response.ResponseBuilder;
33  import javax.ws.rs.core.UriBuilder;
34  import javax.ws.rs.core.UriInfo;
35  
36  import de.smartics.properties.admin.domain.model.ApiServices;
37  import de.smartics.properties.admin.domain.model.ApiServices.ServiceDescriptor;
38  import de.smartics.properties.admin.domain.model.Paths;
39  
40  /**
41   * Provides access information about the API.
42   */
43  @Path("")
44  public class ApiResource
45  {
46    // ********************************* Fields *********************************
47  
48    // --- constants ------------------------------------------------------------
49  
50    /**
51     * The service to configure configurations.
52     */
53    public static final ServiceDescriptor CONFIGURATIONS = new ServiceDescriptor(
54        "configurations", ConfigurationKeysResource.class);
55  
56    /**
57     * The service to en- and decrypt property values.
58     */
59    public static final ServiceDescriptor SECURITY = new ServiceDescriptor(
60        "security", SecurityResource.class);
61  
62    /**
63     * The service configure JNDI properties.
64     */
65    public static final ServiceDescriptor JNDI = new ServiceDescriptor("jndi",
66        JndiResource.class);
67  
68    /**
69     * The list of services provided by this API.
70     */
71    private static final ApiServices SERVICES;
72  
73    /**
74     * The service to browse property descriptors.
75     */
76    public static final ServiceDescriptor DESCRIPTORS = new ServiceDescriptor(
77        "descriptors", PropertyDescriptorsResource.class);
78  
79    /**
80     * The service to view a selected property descriptor.
81     */
82    public static final ServiceDescriptor DESCRIPTOR = new ServiceDescriptor(
83        "descriptor", PropertyDescriptorResource.class);;
84  
85    // --- members --------------------------------------------------------------
86  
87    /**
88     * Provides access to the request to logout the user.
89     */
90    @Context
91    private HttpServletRequest request;
92  
93    /**
94     * Helper to construct paths.
95     */
96    @Context
97    private UriInfo uriInfo;
98  
99    // ****************************** Initializer *******************************
100 
101   static
102   {
103     final List<ServiceDescriptor> services = new ArrayList<ServiceDescriptor>();
104 
105     services.add(DESCRIPTORS);
106     services.add(CONFIGURATIONS);
107     services.add(SECURITY);
108     services.add(JNDI);
109 
110     SERVICES = new ApiServices(services);
111   }
112 
113   // ****************************** Constructors ******************************
114 
115   // ****************************** Inner Classes *****************************
116 
117   // ********************************* Methods ********************************
118 
119   // --- init -----------------------------------------------------------------
120 
121   // --- get&set --------------------------------------------------------------
122 
123   // --- business -------------------------------------------------------------
124 
125   private Response redirect()
126   {
127     final String redirect = request.getParameter("redirect-to");
128     if (redirect != null)
129     {
130       try
131       {
132         final URI uri = new URI(redirect);
133         final ResponseBuilder builder = Response.temporaryRedirect(uri);
134         final Response response = builder.build();
135         return response;
136       }
137       catch (final URISyntaxException e)
138       {
139         // ok, keep default
140       }
141     }
142     return null;
143   }
144 
145   /**
146    * Provides a list of services in HTML.
147    *
148    * @return a response to redirect.
149    */
150   @GET
151   @Produces(MediaType.TEXT_HTML)
152   public Response getAsHtml()
153   {
154     return get();
155   }
156 
157   /**
158    * Provides a list of services in XML.
159    *
160    * @return a response to redirect.
161    */
162   @GET
163   @Produces({ MediaType.TEXT_XML, MediaType.APPLICATION_XML })
164   public Response getAsXml()
165   {
166     return get();
167   }
168 
169   private Response get()
170   {
171     final Response redirect = redirect();
172     if (redirect != null)
173     {
174       return redirect;
175     }
176 
177     final ResponseBuilder builder = Response.ok().entity(SERVICES);
178     final Response response = builder.build();
179     return response;
180   }
181 
182   /**
183    * Allows to logout the user.
184    *
185    * @return the response to render on successful logout.
186    * @throws ServletException on any problem encountered while logging out the
187    *           user.
188    */
189   @POST
190   @Path(Paths.LOGOUT)
191   @Produces({ MediaType.TEXT_HTML, MediaType.TEXT_XML,
192              MediaType.APPLICATION_XML })
193   public Response logout() throws ServletException
194   {
195     if (request.getUserPrincipal() != null)
196     {
197       request.logout();
198     }
199 
200     final UriBuilder builder =
201         uriInfo.getBaseUriBuilder().segment("..").queryParam("logout", true);
202     request.getServletPath();
203     final URI homeUri = builder.build();
204     final String path = request.getContextPath();
205     final Response response =
206         Response
207             .seeOther(homeUri)
208             .header(
209                 "Set-Cookie",
210                 "JSESSIONID=deleted; path=" + path
211                     + "; expires=Thu, 01 Jan 1970 00:00:00 GMT").build();
212 
213     return response;
214   }
215 
216   // --- object basics --------------------------------------------------------
217 
218 }