1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
42
43 @Path("")
44 public class ApiResource
45 {
46
47
48
49
50
51
52
53 public static final ServiceDescriptor CONFIGURATIONS = new ServiceDescriptor(
54 "configurations", ConfigurationKeysResource.class);
55
56
57
58
59 public static final ServiceDescriptor SECURITY = new ServiceDescriptor(
60 "security", SecurityResource.class);
61
62
63
64
65 public static final ServiceDescriptor JNDI = new ServiceDescriptor("jndi",
66 JndiResource.class);
67
68
69
70
71 private static final ApiServices SERVICES;
72
73
74
75
76 public static final ServiceDescriptor DESCRIPTORS = new ServiceDescriptor(
77 "descriptors", PropertyDescriptorsResource.class);
78
79
80
81
82 public static final ServiceDescriptor DESCRIPTOR = new ServiceDescriptor(
83 "descriptor", PropertyDescriptorResource.class);;
84
85
86
87
88
89
90 @Context
91 private HttpServletRequest request;
92
93
94
95
96 @Context
97 private UriInfo uriInfo;
98
99
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
114
115
116
117
118
119
120
121
122
123
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
140 }
141 }
142 return null;
143 }
144
145
146
147
148
149
150 @GET
151 @Produces(MediaType.TEXT_HTML)
152 public Response getAsHtml()
153 {
154 return get();
155 }
156
157
158
159
160
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
184
185
186
187
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
217
218 }