source: DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/rest/UserResource.java @ 4217

Last change on this file since 4217 was 4217, checked in by olhsha, 10 years ago

files INSTALL, UPDATED, CHANGES and README are corrected. The bug with the wrong server diagnostic (403 instead of 404), when a resource's give id is not found, is fixed.

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