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

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

adding trasnactional, refactoring and fixing bugs in updated annotations, removing try-catch from resource methods (The Greek's advice)

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