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.sql.SQLException; |
---|
26 | import java.util.UUID; |
---|
27 | import javax.servlet.http.HttpServletRequest; |
---|
28 | import javax.ws.rs.Consumes; |
---|
29 | import javax.ws.rs.GET; |
---|
30 | import javax.ws.rs.POST; |
---|
31 | import javax.ws.rs.PUT; |
---|
32 | import javax.ws.rs.Path; |
---|
33 | import javax.ws.rs.PathParam; |
---|
34 | import javax.ws.rs.Produces; |
---|
35 | import javax.ws.rs.QueryParam; |
---|
36 | import javax.ws.rs.core.Context; |
---|
37 | import javax.ws.rs.core.MediaType; |
---|
38 | import javax.ws.rs.core.UriInfo; |
---|
39 | import javax.xml.bind.JAXBElement; |
---|
40 | import org.springframework.beans.factory.annotation.Autowired; |
---|
41 | import org.springframework.security.access.annotation.Secured; |
---|
42 | import org.springframework.stereotype.Component; |
---|
43 | |
---|
44 | /** |
---|
45 | * |
---|
46 | * @author olhsha |
---|
47 | */ |
---|
48 | @Component |
---|
49 | @Path("/users") |
---|
50 | public class UserResource { |
---|
51 | @Autowired |
---|
52 | private DBIntegrityService dbIntegrityService; |
---|
53 | @Context |
---|
54 | private HttpServletRequest httpServletRequest; |
---|
55 | @Context |
---|
56 | private UriInfo uriInfo; |
---|
57 | |
---|
58 | public void setHttpRequest(HttpServletRequest request) { |
---|
59 | this.httpServletRequest = request; |
---|
60 | } |
---|
61 | |
---|
62 | public UserResource() { |
---|
63 | } |
---|
64 | |
---|
65 | @GET |
---|
66 | @Produces(MediaType.TEXT_XML) |
---|
67 | @Path("{userid: " + BackendConstants.regExpIdentifier + "}") |
---|
68 | @Secured("ROLE_USER") |
---|
69 | public JAXBElement<User> getUser(@PathParam("userid") String ExternalIdentifier) throws SQLException { |
---|
70 | dbIntegrityService.setServiceURI(uriInfo.getBaseUri().toString()); |
---|
71 | final Number userID = dbIntegrityService.getUserInternalIdentifier(UUID.fromString(ExternalIdentifier)); |
---|
72 | final User user = dbIntegrityService.getUser(userID); |
---|
73 | return new ObjectFactory().createUser(user); |
---|
74 | } |
---|
75 | |
---|
76 | @GET |
---|
77 | @Produces(MediaType.TEXT_XML) |
---|
78 | @Path("/info") |
---|
79 | @Secured("ROLE_USER") |
---|
80 | public JAXBElement<User> getUserByInfo(@QueryParam("email") String email) throws SQLException { |
---|
81 | dbIntegrityService.setServiceURI(uriInfo.getBaseUri().toString()); |
---|
82 | final User user = dbIntegrityService.getUserByInfo(email); |
---|
83 | return new ObjectFactory().createUser(user); |
---|
84 | } |
---|
85 | |
---|
86 | @GET |
---|
87 | @Produces(MediaType.TEXT_XML) |
---|
88 | @Path("{userid: " + BackendConstants.regExpIdentifier + "}/current") |
---|
89 | @Secured("ROLE_USER") |
---|
90 | public JAXBElement<CurrentUserInfo> getCurrentUserInfo(@PathParam("userid") String ExternalIdentifier){ |
---|
91 | dbIntegrityService.setServiceURI(uriInfo.getBaseUri().toString()); |
---|
92 | final Number userID = dbIntegrityService.getUserInternalIdentifier(UUID.fromString(ExternalIdentifier)); |
---|
93 | final CurrentUserInfo userInfo = new CurrentUserInfo(); |
---|
94 | userInfo.setRef(dbIntegrityService.getUserURI(userID)); |
---|
95 | userInfo.setCurrentUser(ifLoggedIn(userID)); |
---|
96 | return new ObjectFactory().createCurrentUserInfo(userInfo); |
---|
97 | } |
---|
98 | |
---|
99 | @POST |
---|
100 | @Consumes(MediaType.TEXT_XML) |
---|
101 | @Produces(MediaType.TEXT_XML) |
---|
102 | @Path("{remoteId: " + BackendConstants.regExpIdentifier + "}") |
---|
103 | @Secured("ROLE_ADMIN") |
---|
104 | public JAXBElement<User> addUser(@PathParam("userid") String remoteId, User user) throws SQLException { |
---|
105 | dbIntegrityService.setServiceURI(uriInfo.getBaseUri().toString()); |
---|
106 | final Number userID = dbIntegrityService.addUser(user, remoteId); |
---|
107 | final User addedUser = dbIntegrityService.getUser(userID); |
---|
108 | return new ObjectFactory().createUser(addedUser); |
---|
109 | } |
---|
110 | |
---|
111 | @PUT |
---|
112 | @Consumes(MediaType.TEXT_XML) |
---|
113 | @Produces(MediaType.TEXT_XML) |
---|
114 | @Path("") |
---|
115 | @Secured("ROLE_ADMIN") |
---|
116 | public JAXBElement<User> updateUser(User user){ |
---|
117 | dbIntegrityService.setServiceURI(uriInfo.getBaseUri().toString()); |
---|
118 | final Number userID = dbIntegrityService.updateUser(user); |
---|
119 | final User addedUser = dbIntegrityService.getUser(userID); |
---|
120 | return new ObjectFactory().createUser(addedUser); |
---|
121 | } |
---|
122 | |
---|
123 | |
---|
124 | |
---|
125 | private boolean ifLoggedIn(Number userID){ |
---|
126 | return httpServletRequest.getRemoteUser().equals(dbIntegrityService.getUserRemoteID(userID)); |
---|
127 | } |
---|
128 | } |
---|