1 | /* |
---|
2 | * Copyright (C) 2013 DASISH |
---|
3 | * |
---|
4 | * This program is free software; you can redistribute it and/or |
---|
5 | * modify it under the terms of the GNU General Public License |
---|
6 | * as published by the Free Software Foundation; either version 2 |
---|
7 | * of the License, or (at your option) any later version. |
---|
8 | * |
---|
9 | * This program is distributed in the hope that it will be useful, |
---|
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
12 | * GNU General Public License for more details. |
---|
13 | * |
---|
14 | * You should have received a copy of the GNU General Public License |
---|
15 | * along with this program; if not, write to the Free Software |
---|
16 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
---|
17 | */ |
---|
18 | package eu.dasish.annotation.backend.rest; |
---|
19 | |
---|
20 | import eu.dasish.annotation.backend.BackendConstants; |
---|
21 | import eu.dasish.annotation.backend.Resource; |
---|
22 | import eu.dasish.annotation.backend.dao.DBIntegrityService; |
---|
23 | import eu.dasish.annotation.schema.CurrentUserInfo; |
---|
24 | import eu.dasish.annotation.schema.ObjectFactory; |
---|
25 | import eu.dasish.annotation.schema.User; |
---|
26 | import java.io.IOException; |
---|
27 | import java.sql.SQLException; |
---|
28 | import java.util.UUID; |
---|
29 | import javax.servlet.http.HttpServletRequest; |
---|
30 | import javax.servlet.http.HttpServletResponse; |
---|
31 | import javax.ws.rs.Consumes; |
---|
32 | import javax.ws.rs.DELETE; |
---|
33 | import javax.ws.rs.GET; |
---|
34 | import javax.ws.rs.POST; |
---|
35 | import javax.ws.rs.PUT; |
---|
36 | import javax.ws.rs.Path; |
---|
37 | import javax.ws.rs.PathParam; |
---|
38 | import javax.ws.rs.Produces; |
---|
39 | import javax.ws.rs.QueryParam; |
---|
40 | import javax.ws.rs.core.Context; |
---|
41 | import javax.ws.rs.core.MediaType; |
---|
42 | import javax.ws.rs.core.UriInfo; |
---|
43 | import javax.xml.bind.JAXBElement; |
---|
44 | import javax.xml.parsers.ParserConfigurationException; |
---|
45 | import org.slf4j.Logger; |
---|
46 | import org.slf4j.LoggerFactory; |
---|
47 | import org.springframework.beans.factory.annotation.Autowired; |
---|
48 | import org.springframework.stereotype.Component; |
---|
49 | import org.springframework.transaction.annotation.Transactional; |
---|
50 | |
---|
51 | /** |
---|
52 | * |
---|
53 | * @author olhsha |
---|
54 | */ |
---|
55 | @Component |
---|
56 | @Path("/users") |
---|
57 | @Transactional(rollbackFor = {Exception.class, SQLException.class, IOException.class, ParserConfigurationException.class}) |
---|
58 | public class UserResource { |
---|
59 | |
---|
60 | @Autowired |
---|
61 | private DBIntegrityService dbIntegrityService; |
---|
62 | @Context |
---|
63 | private HttpServletRequest httpServletRequest; |
---|
64 | @Context |
---|
65 | private HttpServletResponse httpServletResponse; |
---|
66 | @Context |
---|
67 | private UriInfo uriInfo; |
---|
68 | public static final Logger loggerServer = LoggerFactory.getLogger(HttpServletResponse.class); |
---|
69 | private final VerboseOutput verboseOutput = new VerboseOutput(httpServletResponse, loggerServer); |
---|
70 | |
---|
71 | final private String admin = "admin"; |
---|
72 | |
---|
73 | public void setHttpRequest(HttpServletRequest request) { |
---|
74 | this.httpServletRequest = request; |
---|
75 | } |
---|
76 | |
---|
77 | public UserResource() { |
---|
78 | } |
---|
79 | |
---|
80 | @GET |
---|
81 | @Produces(MediaType.TEXT_XML) |
---|
82 | @Path("{userid}") |
---|
83 | @Transactional(readOnly = true) |
---|
84 | public JAXBElement<User> getUser(@PathParam("userid") String externalIdentifier) throws SQLException, IOException { |
---|
85 | String remoteUser = httpServletRequest.getRemoteUser(); |
---|
86 | Number remoteUserID = dbIntegrityService.getUserInternalIDFromRemoteID(remoteUser); |
---|
87 | if (remoteUserID != null) { |
---|
88 | dbIntegrityService.setServiceURI(uriInfo.getBaseUri().toString()); |
---|
89 | try { |
---|
90 | final Number userID = dbIntegrityService.getResourceInternalIdentifier(UUID.fromString(externalIdentifier), Resource.PRINCIPAL); |
---|
91 | if (userID != null) { |
---|
92 | final User user = dbIntegrityService.getUser(userID); |
---|
93 | return new ObjectFactory().createUser(user); |
---|
94 | } else { |
---|
95 | verboseOutput.PRINCIPAL_NOT_FOUND(externalIdentifier); |
---|
96 | } |
---|
97 | } catch (IllegalArgumentException e) { |
---|
98 | verboseOutput.ILLEGAL_UUID(externalIdentifier); |
---|
99 | } |
---|
100 | } else { |
---|
101 | verboseOutput.REMOTE_PRINCIPAL_NOT_FOUND(remoteUser); |
---|
102 | } |
---|
103 | return new ObjectFactory().createUser(new User()); |
---|
104 | } |
---|
105 | |
---|
106 | @GET |
---|
107 | @Produces(MediaType.TEXT_XML) |
---|
108 | @Path("/info") |
---|
109 | @Transactional(readOnly = true) |
---|
110 | public JAXBElement<User> getUserByInfo(@QueryParam("email") String email) throws SQLException, IOException { |
---|
111 | String remoteUser = httpServletRequest.getRemoteUser(); |
---|
112 | Number remoteUserID = dbIntegrityService.getUserInternalIDFromRemoteID(remoteUser); |
---|
113 | if (remoteUserID != null) { |
---|
114 | dbIntegrityService.setServiceURI(uriInfo.getBaseUri().toString()); |
---|
115 | final User user = dbIntegrityService.getUserByInfo(email); |
---|
116 | if (user != null) { |
---|
117 | return new ObjectFactory().createUser(user); |
---|
118 | } else { |
---|
119 | verboseOutput.PRINCIPAL_NOT_FOUND_BY_INFO(email); |
---|
120 | } |
---|
121 | } else { |
---|
122 | verboseOutput.REMOTE_PRINCIPAL_NOT_FOUND(remoteUser); |
---|
123 | } |
---|
124 | return new ObjectFactory().createUser(new User()); |
---|
125 | } |
---|
126 | |
---|
127 | @GET |
---|
128 | @Produces(MediaType.TEXT_XML) |
---|
129 | @Path("{userid}/current") |
---|
130 | @Transactional(readOnly = true) |
---|
131 | public JAXBElement<CurrentUserInfo> getCurrentUserInfo(@PathParam("userid") String externalIdentifier) throws IOException { |
---|
132 | String remoteUser = httpServletRequest.getRemoteUser(); |
---|
133 | Number remoteUserID = dbIntegrityService.getUserInternalIDFromRemoteID(remoteUser); |
---|
134 | if (remoteUserID != null) { |
---|
135 | dbIntegrityService.setServiceURI(uriInfo.getBaseUri().toString()); |
---|
136 | try { |
---|
137 | final Number userID = dbIntegrityService.getResourceInternalIdentifier(UUID.fromString(externalIdentifier), Resource.PRINCIPAL); |
---|
138 | if (userID != null) { |
---|
139 | final CurrentUserInfo userInfo = new CurrentUserInfo(); |
---|
140 | userInfo.setRef(dbIntegrityService.getResourceURI(userID, Resource.PRINCIPAL)); |
---|
141 | userInfo.setCurrentUser(ifLoggedIn(userID)); |
---|
142 | return new ObjectFactory().createCurrentUserInfo(userInfo); |
---|
143 | } else { |
---|
144 | verboseOutput.PRINCIPAL_NOT_FOUND(externalIdentifier); |
---|
145 | } |
---|
146 | } catch (IllegalArgumentException e) { |
---|
147 | verboseOutput.ILLEGAL_UUID(externalIdentifier); |
---|
148 | } |
---|
149 | } else { |
---|
150 | verboseOutput.REMOTE_PRINCIPAL_NOT_FOUND(remoteUser); |
---|
151 | } |
---|
152 | |
---|
153 | return new ObjectFactory().createCurrentUserInfo(new CurrentUserInfo()); |
---|
154 | } |
---|
155 | |
---|
156 | @POST |
---|
157 | @Consumes(MediaType.APPLICATION_XML) |
---|
158 | @Produces(MediaType.APPLICATION_XML) |
---|
159 | @Path("{remoteId}") |
---|
160 | public JAXBElement<User> addUser(@PathParam("remoteId") String remoteId, User user) throws SQLException, IOException { |
---|
161 | String remoteUser = httpServletRequest.getRemoteUser(); |
---|
162 | Number remoteUserID = dbIntegrityService.getUserInternalIDFromRemoteID(remoteUser); |
---|
163 | if (remoteUserID != null) { |
---|
164 | if (dbIntegrityService.getTypeOfUserAccount(remoteUserID).equals(admin)) { |
---|
165 | dbIntegrityService.setServiceURI(uriInfo.getBaseUri().toString()); |
---|
166 | final Number userID = dbIntegrityService.addUser(user, remoteId); |
---|
167 | if (userID != null) { |
---|
168 | final User addedUser = dbIntegrityService.getUser(userID); |
---|
169 | return new ObjectFactory().createUser(addedUser); |
---|
170 | } else { |
---|
171 | verboseOutput.PRINCIPAL_IS_NOT_ADDED_TO_DB(); |
---|
172 | } |
---|
173 | } else { |
---|
174 | verboseOutput.ADMIN_RIGHTS_EXPECTED(); |
---|
175 | } |
---|
176 | } else { |
---|
177 | verboseOutput.REMOTE_PRINCIPAL_NOT_FOUND(remoteUser); |
---|
178 | } |
---|
179 | return new ObjectFactory().createUser(new User()); |
---|
180 | } |
---|
181 | |
---|
182 | @PUT |
---|
183 | @Consumes(MediaType.APPLICATION_XML) |
---|
184 | @Produces(MediaType.APPLICATION_XML) |
---|
185 | @Path("") |
---|
186 | public JAXBElement<User> updateUser(User user) throws IOException { |
---|
187 | String remoteUser = httpServletRequest.getRemoteUser(); |
---|
188 | Number remoteUserID = dbIntegrityService.getUserInternalIDFromRemoteID(remoteUser); |
---|
189 | if (remoteUserID != null) { |
---|
190 | if (dbIntegrityService.getTypeOfUserAccount(remoteUserID).equals(admin)) { |
---|
191 | dbIntegrityService.setServiceURI(uriInfo.getBaseUri().toString()); |
---|
192 | final Number userID = dbIntegrityService.updateUser(user); |
---|
193 | if (userID != null) { |
---|
194 | final User addedUser = dbIntegrityService.getUser(userID); |
---|
195 | return new ObjectFactory().createUser(addedUser); |
---|
196 | } else { |
---|
197 | verboseOutput.PRINCIPAL_NOT_FOUND(user.getURI()); |
---|
198 | } |
---|
199 | } else { |
---|
200 | verboseOutput.ADMIN_RIGHTS_EXPECTED(); |
---|
201 | } |
---|
202 | } else { |
---|
203 | verboseOutput.REMOTE_PRINCIPAL_NOT_FOUND(remoteUser); |
---|
204 | } |
---|
205 | |
---|
206 | return new ObjectFactory().createUser(new User()); |
---|
207 | } |
---|
208 | |
---|
209 | @PUT |
---|
210 | @Produces(MediaType.TEXT_PLAIN) |
---|
211 | @Path("{externalId}/account/{accountType}") |
---|
212 | public String updateUserAccount(@PathParam("externalId") String externalId, @PathParam("accountType") String accountType) throws IOException { |
---|
213 | String remoteUser = httpServletRequest.getRemoteUser(); |
---|
214 | Number remoteUserID = dbIntegrityService.getUserInternalIDFromRemoteID(remoteUser); |
---|
215 | if (remoteUserID != null) { |
---|
216 | if (dbIntegrityService.getTypeOfUserAccount(remoteUserID).equals(admin)) { |
---|
217 | dbIntegrityService.setServiceURI(uriInfo.getBaseUri().toString()); |
---|
218 | final boolean updated = dbIntegrityService.updateAccount(UUID.fromString(externalId), accountType); |
---|
219 | if (updated) { |
---|
220 | return "The account was updated to "+dbIntegrityService.getTypeOfUserAccount(dbIntegrityService.getResourceInternalIdentifier(UUID.fromString(externalId), Resource.PRINCIPAL)); |
---|
221 | } else { |
---|
222 | verboseOutput.ACCOUNT_IS_NOT_UPDATED(); |
---|
223 | } |
---|
224 | } else { |
---|
225 | verboseOutput.ADMIN_RIGHTS_EXPECTED(); |
---|
226 | } |
---|
227 | } else { |
---|
228 | verboseOutput.REMOTE_PRINCIPAL_NOT_FOUND(remoteUser); |
---|
229 | } |
---|
230 | |
---|
231 | return " "; |
---|
232 | } |
---|
233 | |
---|
234 | @DELETE |
---|
235 | @Path("{userId}") |
---|
236 | public String deleteUser(@PathParam("userId") String externalIdentifier) throws IOException { |
---|
237 | String remoteUser = httpServletRequest.getRemoteUser(); |
---|
238 | Number remoteUserID = dbIntegrityService.getUserInternalIDFromRemoteID(remoteUser); |
---|
239 | if (remoteUserID != null) { |
---|
240 | if (dbIntegrityService.getTypeOfUserAccount(remoteUserID).equals(admin)) { |
---|
241 | dbIntegrityService.setServiceURI(uriInfo.getBaseUri().toString()); |
---|
242 | final Number userID = dbIntegrityService.getResourceInternalIdentifier(UUID.fromString(externalIdentifier), Resource.PRINCIPAL); |
---|
243 | if (userID != null) { |
---|
244 | final Integer result = dbIntegrityService.deleteUser(userID); |
---|
245 | return "There is " + result.toString() + " row deleted"; |
---|
246 | } else { |
---|
247 | verboseOutput.PRINCIPAL_NOT_FOUND(externalIdentifier); |
---|
248 | } |
---|
249 | } else { |
---|
250 | verboseOutput.ADMIN_RIGHTS_EXPECTED(); |
---|
251 | } |
---|
252 | } else { |
---|
253 | verboseOutput.REMOTE_PRINCIPAL_NOT_FOUND(remoteUser); |
---|
254 | } |
---|
255 | |
---|
256 | return " "; |
---|
257 | } |
---|
258 | |
---|
259 | @DELETE |
---|
260 | @Path("{userId}/safe") |
---|
261 | public String deleteUserSafe(@PathParam("userId") String externalIdentifier) throws IOException { |
---|
262 | String remoteUser = httpServletRequest.getRemoteUser(); |
---|
263 | Number remoteUserID = dbIntegrityService.getUserInternalIDFromRemoteID(remoteUser); |
---|
264 | if (remoteUserID != null) { |
---|
265 | if (dbIntegrityService.getTypeOfUserAccount(remoteUserID).equals(admin)) { |
---|
266 | dbIntegrityService.setServiceURI(uriInfo.getBaseUri().toString()); |
---|
267 | final Number userID = dbIntegrityService.getResourceInternalIdentifier(UUID.fromString(externalIdentifier), Resource.PRINCIPAL); |
---|
268 | if (userID != null) { |
---|
269 | final Integer result = dbIntegrityService.deleteUserSafe(userID); |
---|
270 | return "There is " + result.toString() + " row deleted"; |
---|
271 | } else { |
---|
272 | verboseOutput.PRINCIPAL_NOT_FOUND(externalIdentifier); |
---|
273 | } |
---|
274 | } else { |
---|
275 | verboseOutput.ADMIN_RIGHTS_EXPECTED(); |
---|
276 | } |
---|
277 | } else { |
---|
278 | verboseOutput.REMOTE_PRINCIPAL_NOT_FOUND(remoteUser); |
---|
279 | } |
---|
280 | return " "; |
---|
281 | } |
---|
282 | |
---|
283 | private boolean ifLoggedIn(Number userID) { |
---|
284 | return httpServletRequest.getRemoteUser().equals(dbIntegrityService.getUserRemoteID(userID)); |
---|
285 | } |
---|
286 | |
---|
287 | |
---|
288 | } |
---|