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.GET; |
---|
32 | import javax.ws.rs.POST; |
---|
33 | import javax.ws.rs.PUT; |
---|
34 | import javax.ws.rs.Path; |
---|
35 | import javax.ws.rs.PathParam; |
---|
36 | import javax.ws.rs.Produces; |
---|
37 | import javax.ws.rs.QueryParam; |
---|
38 | import javax.ws.rs.core.Context; |
---|
39 | import javax.ws.rs.core.MediaType; |
---|
40 | import javax.ws.rs.core.UriInfo; |
---|
41 | import javax.xml.bind.JAXBElement; |
---|
42 | import javax.xml.parsers.ParserConfigurationException; |
---|
43 | import org.springframework.beans.factory.annotation.Autowired; |
---|
44 | import org.springframework.stereotype.Component; |
---|
45 | import org.springframework.transaction.annotation.Transactional; |
---|
46 | |
---|
47 | /** |
---|
48 | * |
---|
49 | * @author olhsha |
---|
50 | */ |
---|
51 | @Component |
---|
52 | @Path("/users") |
---|
53 | @Transactional(rollbackFor = {Exception.class, SQLException.class, IOException.class, ParserConfigurationException.class}) |
---|
54 | public class UserResource { |
---|
55 | |
---|
56 | @Autowired |
---|
57 | private DBIntegrityService dbIntegrityService; |
---|
58 | @Context |
---|
59 | private HttpServletRequest httpServletRequest; |
---|
60 | @Context |
---|
61 | private HttpServletResponse httpServletResponse; |
---|
62 | @Context |
---|
63 | private UriInfo uriInfo; |
---|
64 | |
---|
65 | public void setHttpRequest(HttpServletRequest request) { |
---|
66 | this.httpServletRequest = request; |
---|
67 | } |
---|
68 | |
---|
69 | public UserResource() { |
---|
70 | } |
---|
71 | |
---|
72 | @GET |
---|
73 | @Produces(MediaType.TEXT_XML) |
---|
74 | @Path("{userid: " + BackendConstants.regExpIdentifier + "}") |
---|
75 | @Transactional(readOnly = true) |
---|
76 | public JAXBElement<User> getUser(@PathParam("userid") String ExternalIdentifier) throws SQLException, IOException { |
---|
77 | dbIntegrityService.setServiceURI(uriInfo.getBaseUri().toString()); |
---|
78 | final Number userID = dbIntegrityService.getUserInternalIdentifier(UUID.fromString(ExternalIdentifier)); |
---|
79 | if (userID != null) { |
---|
80 | final User user = dbIntegrityService.getUser(userID); |
---|
81 | return new ObjectFactory().createUser(user); |
---|
82 | } else { |
---|
83 | httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, "The user with the given id is not found in the database"); |
---|
84 | return null; |
---|
85 | } |
---|
86 | } |
---|
87 | |
---|
88 | @GET |
---|
89 | @Produces(MediaType.TEXT_XML) |
---|
90 | @Path("/info") |
---|
91 | @Transactional(readOnly = true) |
---|
92 | public JAXBElement<User> getUserByInfo(@QueryParam("email") String email) throws SQLException, IOException { |
---|
93 | dbIntegrityService.setServiceURI(uriInfo.getBaseUri().toString()); |
---|
94 | final User user = dbIntegrityService.getUserByInfo(email); |
---|
95 | if (user != null) { |
---|
96 | return new ObjectFactory().createUser(user); |
---|
97 | } else { |
---|
98 | httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, "The user with the given info is not found in the database"); |
---|
99 | return null; |
---|
100 | } |
---|
101 | } |
---|
102 | |
---|
103 | @GET |
---|
104 | @Produces(MediaType.TEXT_XML) |
---|
105 | @Path("{userid: " + BackendConstants.regExpIdentifier + "}/current") |
---|
106 | @Transactional(readOnly = true) |
---|
107 | public JAXBElement<CurrentUserInfo> getCurrentUserInfo(@PathParam("userid") String ExternalIdentifier) throws IOException { |
---|
108 | dbIntegrityService.setServiceURI(uriInfo.getBaseUri().toString()); |
---|
109 | final Number userID = dbIntegrityService.getUserInternalIdentifier(UUID.fromString(ExternalIdentifier)); |
---|
110 | if (userID != null) { |
---|
111 | final CurrentUserInfo userInfo = new CurrentUserInfo(); |
---|
112 | userInfo.setRef(dbIntegrityService.getUserURI(userID)); |
---|
113 | userInfo.setCurrentUser(ifLoggedIn(userID)); |
---|
114 | return new ObjectFactory().createCurrentUserInfo(userInfo); |
---|
115 | } else { |
---|
116 | httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, "The user with the given id is not found in the database"); |
---|
117 | return null; |
---|
118 | } |
---|
119 | } |
---|
120 | |
---|
121 | @POST |
---|
122 | @Consumes(MediaType.TEXT_XML) |
---|
123 | @Produces(MediaType.TEXT_XML) |
---|
124 | @Path("{remoteId: " + BackendConstants.regExpIdentifier + "}") |
---|
125 | public JAXBElement<User> addUser(@PathParam("userid") String remoteId, User user) throws SQLException { |
---|
126 | dbIntegrityService.setServiceURI(uriInfo.getBaseUri().toString()); |
---|
127 | final Number userID = dbIntegrityService.addUser(user, remoteId); |
---|
128 | final User addedUser = dbIntegrityService.getUser(userID); |
---|
129 | return new ObjectFactory().createUser(addedUser); |
---|
130 | } |
---|
131 | |
---|
132 | @PUT |
---|
133 | @Consumes(MediaType.TEXT_XML) |
---|
134 | @Produces(MediaType.TEXT_XML) |
---|
135 | @Path("") |
---|
136 | public JAXBElement<User> updateUser(User user) throws IOException{ |
---|
137 | dbIntegrityService.setServiceURI(uriInfo.getBaseUri().toString()); |
---|
138 | final Number userID = dbIntegrityService.updateUser(user); |
---|
139 | if (userID != null) { |
---|
140 | final User addedUser = dbIntegrityService.getUser(userID); |
---|
141 | return new ObjectFactory().createUser(addedUser); |
---|
142 | } else { |
---|
143 | httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, "The user with the given id is not found in the database"); |
---|
144 | return null; |
---|
145 | } |
---|
146 | } |
---|
147 | |
---|
148 | private boolean ifLoggedIn(Number userID) { |
---|
149 | return httpServletRequest.getRemoteUser().equals(dbIntegrityService.getUserRemoteID(userID)); |
---|
150 | } |
---|
151 | } |
---|