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.dao.DBIntegrityService; |
---|
22 | import eu.dasish.annotation.schema.CurrentUserInfo; |
---|
23 | import eu.dasish.annotation.schema.ObjectFactory; |
---|
24 | import eu.dasish.annotation.schema.User; |
---|
25 | import java.io.IOException; |
---|
26 | import java.sql.SQLException; |
---|
27 | import java.util.UUID; |
---|
28 | import javax.servlet.http.HttpServletRequest; |
---|
29 | import javax.servlet.http.HttpServletResponse; |
---|
30 | import javax.ws.rs.Consumes; |
---|
31 | import javax.ws.rs.DELETE; |
---|
32 | import javax.ws.rs.GET; |
---|
33 | import javax.ws.rs.POST; |
---|
34 | import javax.ws.rs.PUT; |
---|
35 | import javax.ws.rs.Path; |
---|
36 | import javax.ws.rs.PathParam; |
---|
37 | import javax.ws.rs.Produces; |
---|
38 | import javax.ws.rs.QueryParam; |
---|
39 | import javax.ws.rs.core.Context; |
---|
40 | import javax.ws.rs.core.MediaType; |
---|
41 | import javax.ws.rs.core.UriInfo; |
---|
42 | import javax.xml.bind.JAXBElement; |
---|
43 | import javax.xml.parsers.ParserConfigurationException; |
---|
44 | import org.slf4j.Logger; |
---|
45 | import org.slf4j.LoggerFactory; |
---|
46 | import org.springframework.beans.factory.annotation.Autowired; |
---|
47 | import org.springframework.stereotype.Component; |
---|
48 | import org.springframework.transaction.annotation.Transactional; |
---|
49 | |
---|
50 | /** |
---|
51 | * |
---|
52 | * @author olhsha |
---|
53 | */ |
---|
54 | @Component |
---|
55 | @Path("/users") |
---|
56 | @Transactional(rollbackFor = {Exception.class, SQLException.class, IOException.class, ParserConfigurationException.class}) |
---|
57 | public class UserResource { |
---|
58 | |
---|
59 | @Autowired |
---|
60 | private DBIntegrityService dbIntegrityService; |
---|
61 | @Context |
---|
62 | private HttpServletRequest httpServletRequest; |
---|
63 | @Context |
---|
64 | private HttpServletResponse httpServletResponse; |
---|
65 | @Context |
---|
66 | private UriInfo uriInfo; |
---|
67 | public static final Logger loggerServer = LoggerFactory.getLogger(HttpServletResponse.class); |
---|
68 | private final VerboseOutput verboseOutput = new VerboseOutput(httpServletResponse, loggerServer); |
---|
69 | |
---|
70 | final private String admin = "admin"; |
---|
71 | |
---|
72 | public void setHttpRequest(HttpServletRequest request) { |
---|
73 | this.httpServletRequest = request; |
---|
74 | } |
---|
75 | |
---|
76 | public UserResource() { |
---|
77 | } |
---|
78 | |
---|
79 | @GET |
---|
80 | @Produces(MediaType.TEXT_XML) |
---|
81 | @Path("{userid}") |
---|
82 | @Transactional(readOnly = true) |
---|
83 | public JAXBElement<User> getUser(@PathParam("userid") String externalIdentifier) throws SQLException, IOException { |
---|
84 | String remoteUser = httpServletRequest.getRemoteUser(); |
---|
85 | Number remoteUserID = dbIntegrityService.getUserInternalIDFromRemoteID(remoteUser); |
---|
86 | if (remoteUserID != null) { |
---|
87 | dbIntegrityService.setServiceURI(uriInfo.getBaseUri().toString()); |
---|
88 | try { |
---|
89 | final Number userID = dbIntegrityService.getUserInternalIdentifier(UUID.fromString(externalIdentifier)); |
---|
90 | if (userID != null) { |
---|
91 | final User user = dbIntegrityService.getUser(userID); |
---|
92 | return new ObjectFactory().createUser(user); |
---|
93 | } else { |
---|
94 | verboseOutput.PRINCIPAL_NOT_FOUND(externalIdentifier); |
---|
95 | } |
---|
96 | } catch (IllegalArgumentException e) { |
---|
97 | verboseOutput.ILLEGAL_UUID(externalIdentifier); |
---|
98 | } |
---|
99 | } else { |
---|
100 | verboseOutput.REMOTE_PRINCIPAL_NOT_FOUND(remoteUser); |
---|
101 | } |
---|
102 | return new ObjectFactory().createUser(new User()); |
---|
103 | } |
---|
104 | |
---|
105 | @GET |
---|
106 | @Produces(MediaType.TEXT_XML) |
---|
107 | @Path("/info") |
---|
108 | @Transactional(readOnly = true) |
---|
109 | public JAXBElement<User> getUserByInfo(@QueryParam("email") String email) throws SQLException, IOException { |
---|
110 | String remoteUser = httpServletRequest.getRemoteUser(); |
---|
111 | Number remoteUserID = dbIntegrityService.getUserInternalIDFromRemoteID(remoteUser); |
---|
112 | if (remoteUserID != null) { |
---|
113 | dbIntegrityService.setServiceURI(uriInfo.getBaseUri().toString()); |
---|
114 | final User user = dbIntegrityService.getUserByInfo(email); |
---|
115 | if (user != null) { |
---|
116 | return new ObjectFactory().createUser(user); |
---|
117 | } else { |
---|
118 | verboseOutput.PRINCIPAL_NOT_FOUND_BY_INFO(email); |
---|
119 | } |
---|
120 | } else { |
---|
121 | verboseOutput.REMOTE_PRINCIPAL_NOT_FOUND(remoteUser); |
---|
122 | } |
---|
123 | return new ObjectFactory().createUser(new User()); |
---|
124 | } |
---|
125 | |
---|
126 | @GET |
---|
127 | @Produces(MediaType.TEXT_XML) |
---|
128 | @Path("{userid}/current") |
---|
129 | @Transactional(readOnly = true) |
---|
130 | public JAXBElement<CurrentUserInfo> getCurrentUserInfo(@PathParam("userid") String externalIdentifier) throws IOException { |
---|
131 | String remoteUser = httpServletRequest.getRemoteUser(); |
---|
132 | Number remoteUserID = dbIntegrityService.getUserInternalIDFromRemoteID(remoteUser); |
---|
133 | if (remoteUserID != null) { |
---|
134 | dbIntegrityService.setServiceURI(uriInfo.getBaseUri().toString()); |
---|
135 | try { |
---|
136 | final Number userID = dbIntegrityService.getUserInternalIdentifier(UUID.fromString(externalIdentifier)); |
---|
137 | if (userID != null) { |
---|
138 | final CurrentUserInfo userInfo = new CurrentUserInfo(); |
---|
139 | userInfo.setRef(dbIntegrityService.getUserURI(userID)); |
---|
140 | userInfo.setCurrentUser(ifLoggedIn(userID)); |
---|
141 | return new ObjectFactory().createCurrentUserInfo(userInfo); |
---|
142 | } else { |
---|
143 | verboseOutput.PRINCIPAL_NOT_FOUND(externalIdentifier); |
---|
144 | } |
---|
145 | } catch (IllegalArgumentException e) { |
---|
146 | verboseOutput.ILLEGAL_UUID(externalIdentifier); |
---|
147 | } |
---|
148 | } else { |
---|
149 | verboseOutput.REMOTE_PRINCIPAL_NOT_FOUND(remoteUser); |
---|
150 | } |
---|
151 | |
---|
152 | return new ObjectFactory().createCurrentUserInfo(new CurrentUserInfo()); |
---|
153 | } |
---|
154 | |
---|
155 | @POST |
---|
156 | @Consumes(MediaType.APPLICATION_XML) |
---|
157 | @Produces(MediaType.APPLICATION_XML) |
---|
158 | @Path("{remoteId}") |
---|
159 | public JAXBElement<User> addUser(@PathParam("remoteId") String remoteId, User user) throws SQLException, IOException { |
---|
160 | String remoteUser = httpServletRequest.getRemoteUser(); |
---|
161 | Number remoteUserID = dbIntegrityService.getUserInternalIDFromRemoteID(remoteUser); |
---|
162 | if (remoteUserID != null) { |
---|
163 | if (dbIntegrityService.getTypeOfUserAccount(remoteUserID).equals(admin)) { |
---|
164 | dbIntegrityService.setServiceURI(uriInfo.getBaseUri().toString()); |
---|
165 | final Number userID = dbIntegrityService.addUser(user, remoteId); |
---|
166 | if (userID != null) { |
---|
167 | final User addedUser = dbIntegrityService.getUser(userID); |
---|
168 | return new ObjectFactory().createUser(addedUser); |
---|
169 | } else { |
---|
170 | verboseOutput.PRINCIPAL_IS_NOT_ADDED_TO_DB(); |
---|
171 | } |
---|
172 | } else { |
---|
173 | verboseOutput.ADMIN_RIGHTS_EXPECTED(); |
---|
174 | } |
---|
175 | } else { |
---|
176 | verboseOutput.REMOTE_PRINCIPAL_NOT_FOUND(remoteUser); |
---|
177 | } |
---|
178 | return new ObjectFactory().createUser(new User()); |
---|
179 | } |
---|
180 | |
---|
181 | @PUT |
---|
182 | @Consumes(MediaType.APPLICATION_XML) |
---|
183 | @Produces(MediaType.APPLICATION_XML) |
---|
184 | @Path("") |
---|
185 | public JAXBElement<User> updateUser(User user) throws IOException { |
---|
186 | String remoteUser = httpServletRequest.getRemoteUser(); |
---|
187 | Number remoteUserID = dbIntegrityService.getUserInternalIDFromRemoteID(remoteUser); |
---|
188 | if (remoteUserID != null) { |
---|
189 | if (dbIntegrityService.getTypeOfUserAccount(remoteUserID).equals(admin)) { |
---|
190 | dbIntegrityService.setServiceURI(uriInfo.getBaseUri().toString()); |
---|
191 | final Number userID = dbIntegrityService.updateUser(user); |
---|
192 | if (userID != null) { |
---|
193 | final User addedUser = dbIntegrityService.getUser(userID); |
---|
194 | return new ObjectFactory().createUser(addedUser); |
---|
195 | } else { |
---|
196 | verboseOutput.PRINCIPAL_NOT_FOUND(user.getURI()); |
---|
197 | } |
---|
198 | } else { |
---|
199 | verboseOutput.ADMIN_RIGHTS_EXPECTED(); |
---|
200 | } |
---|
201 | } else { |
---|
202 | verboseOutput.REMOTE_PRINCIPAL_NOT_FOUND(remoteUser); |
---|
203 | } |
---|
204 | |
---|
205 | return new ObjectFactory().createUser(new User()); |
---|
206 | } |
---|
207 | |
---|
208 | @PUT |
---|
209 | @Produces(MediaType.TEXT_PLAIN) |
---|
210 | @Path("{externalId}/account/{accountType}") |
---|
211 | public String updateUserAccount(@PathParam("externalId") String externalId, @PathParam("accountType") String accountType) throws IOException { |
---|
212 | String remoteUser = httpServletRequest.getRemoteUser(); |
---|
213 | Number remoteUserID = dbIntegrityService.getUserInternalIDFromRemoteID(remoteUser); |
---|
214 | if (remoteUserID != null) { |
---|
215 | if (dbIntegrityService.getTypeOfUserAccount(remoteUserID).equals(admin)) { |
---|
216 | dbIntegrityService.setServiceURI(uriInfo.getBaseUri().toString()); |
---|
217 | final boolean updated = dbIntegrityService.updateAccount(UUID.fromString(externalId), accountType); |
---|
218 | if (updated) { |
---|
219 | return "The account was updated to "+dbIntegrityService.getTypeOfUserAccount(dbIntegrityService.getUserInternalIdentifier(UUID.fromString(externalId))); |
---|
220 | } else { |
---|
221 | verboseOutput.ACCOUNT_IS_NOT_UPDATED(); |
---|
222 | } |
---|
223 | } else { |
---|
224 | verboseOutput.ADMIN_RIGHTS_EXPECTED(); |
---|
225 | } |
---|
226 | } else { |
---|
227 | verboseOutput.REMOTE_PRINCIPAL_NOT_FOUND(remoteUser); |
---|
228 | } |
---|
229 | |
---|
230 | return " "; |
---|
231 | } |
---|
232 | |
---|
233 | @DELETE |
---|
234 | @Path("{userId}") |
---|
235 | public String deleteUser(@PathParam("userId") String externalIdentifier) throws IOException { |
---|
236 | String remoteUser = httpServletRequest.getRemoteUser(); |
---|
237 | Number remoteUserID = dbIntegrityService.getUserInternalIDFromRemoteID(remoteUser); |
---|
238 | if (remoteUserID != null) { |
---|
239 | if (dbIntegrityService.getTypeOfUserAccount(remoteUserID).equals(admin)) { |
---|
240 | dbIntegrityService.setServiceURI(uriInfo.getBaseUri().toString()); |
---|
241 | final Number userID = dbIntegrityService.getUserInternalIdentifier(UUID.fromString(externalIdentifier)); |
---|
242 | if (userID != null) { |
---|
243 | final Integer result = dbIntegrityService.deleteUser(userID); |
---|
244 | return "There is " + result.toString() + " row deleted"; |
---|
245 | } else { |
---|
246 | verboseOutput.PRINCIPAL_NOT_FOUND(externalIdentifier); |
---|
247 | } |
---|
248 | } else { |
---|
249 | verboseOutput.ADMIN_RIGHTS_EXPECTED(); |
---|
250 | } |
---|
251 | } else { |
---|
252 | verboseOutput.REMOTE_PRINCIPAL_NOT_FOUND(remoteUser); |
---|
253 | } |
---|
254 | |
---|
255 | return " "; |
---|
256 | } |
---|
257 | |
---|
258 | @DELETE |
---|
259 | @Path("{userId}/safe") |
---|
260 | public String deleteUserSafe(@PathParam("userId") String externalIdentifier) throws IOException { |
---|
261 | String remoteUser = httpServletRequest.getRemoteUser(); |
---|
262 | Number remoteUserID = dbIntegrityService.getUserInternalIDFromRemoteID(remoteUser); |
---|
263 | if (remoteUserID != null) { |
---|
264 | if (dbIntegrityService.getTypeOfUserAccount(remoteUserID).equals(admin)) { |
---|
265 | dbIntegrityService.setServiceURI(uriInfo.getBaseUri().toString()); |
---|
266 | final Number userID = dbIntegrityService.getUserInternalIdentifier(UUID.fromString(externalIdentifier)); |
---|
267 | if (userID != null) { |
---|
268 | final Integer result = dbIntegrityService.deleteUserSafe(userID); |
---|
269 | return "There is " + result.toString() + " row deleted"; |
---|
270 | } else { |
---|
271 | verboseOutput.PRINCIPAL_NOT_FOUND(externalIdentifier); |
---|
272 | } |
---|
273 | } else { |
---|
274 | verboseOutput.ADMIN_RIGHTS_EXPECTED(); |
---|
275 | } |
---|
276 | } else { |
---|
277 | verboseOutput.REMOTE_PRINCIPAL_NOT_FOUND(remoteUser); |
---|
278 | } |
---|
279 | return " "; |
---|
280 | } |
---|
281 | |
---|
282 | private boolean ifLoggedIn(Number userID) { |
---|
283 | return httpServletRequest.getRemoteUser().equals(dbIntegrityService.getUserRemoteID(userID)); |
---|
284 | } |
---|
285 | |
---|
286 | |
---|
287 | } |
---|