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

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

test commit after disaster

File size: 10.6 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.Resource;
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.DELETE;
32import javax.ws.rs.GET;
33import javax.ws.rs.POST;
34import javax.ws.rs.PUT;
35import javax.ws.rs.Path;
36import javax.ws.rs.PathParam;
37import javax.ws.rs.Produces;
38import javax.ws.rs.QueryParam;
39import javax.ws.rs.core.Context;
40import javax.ws.rs.core.MediaType;
41import javax.ws.rs.core.UriInfo;
42import javax.xml.bind.JAXBElement;
43import javax.xml.parsers.ParserConfigurationException;
44import org.slf4j.Logger;
45import org.slf4j.LoggerFactory;
46import org.springframework.beans.factory.annotation.Autowired;
47import org.springframework.stereotype.Component;
48import 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})
57public class UserResource extends ResourceResource{
58
59 
60
61    public void setHttpRequest(HttpServletRequest request) {
62        this.httpServletRequest = request;
63    }
64
65    public UserResource() {
66    }
67
68    @GET
69    @Produces(MediaType.TEXT_XML)
70    @Path("{userid}")
71    @Transactional(readOnly = true)
72    public JAXBElement<User> getUser(@PathParam("userid") String externalIdentifier) throws SQLException, IOException {
73       Number remoteUserID = this.getUserID();
74       if (remoteUserID != null) {
75            try {
76                final Number userID = dbIntegrityService.getResourceInternalIdentifier(UUID.fromString(externalIdentifier), Resource.PRINCIPAL);
77                if (userID != null) {
78                    final User user = dbIntegrityService.getUser(userID);
79                    return new ObjectFactory().createUser(user);
80                } else {
81                    verboseOutput.PRINCIPAL_NOT_FOUND(externalIdentifier);
82                }
83            } catch (IllegalArgumentException e) {
84                verboseOutput.ILLEGAL_UUID(externalIdentifier);
85            }
86        } 
87        return new ObjectFactory().createUser(new User());
88    }
89   
90    @GET
91    @Produces(MediaType.TEXT_XML)
92    @Path("admin")
93    @Transactional(readOnly = true)
94    public String getAdmin() throws IOException {
95       Number remoteUserID = this.getUserID();
96       if (remoteUserID != null) {
97           return "The admin of the server database "+ dbIntegrityService.getDataBaseAdmin().getDisplayName()+" is availiable via e-mail "+dbIntegrityService.getDataBaseAdmin().getEMail();
98        } 
99        return "You are not geconginsed as a registered user.";
100    }
101
102    @GET
103    @Produces(MediaType.TEXT_XML)
104    @Path("/info")
105    @Transactional(readOnly = true)
106    public JAXBElement<User> getUserByInfo(@QueryParam("email") String email) throws SQLException, IOException {
107        Number remoteUserID = this.getUserID();
108        if (remoteUserID != null) {
109            final User user = dbIntegrityService.getUserByInfo(email);
110            if (user != null) {
111                return new ObjectFactory().createUser(user);
112            } else {
113                verboseOutput.PRINCIPAL_NOT_FOUND_BY_INFO(email);
114            }
115        } 
116        return new ObjectFactory().createUser(new User());
117    }
118
119    @GET
120    @Produces(MediaType.TEXT_XML)
121    @Path("{userid}/current")
122    @Transactional(readOnly = true)
123    public JAXBElement<CurrentUserInfo> getCurrentUserInfo(@PathParam("userid") String externalIdentifier) throws IOException {
124        Number remoteUserID = this.getUserID();
125        if (remoteUserID != null) {
126            try {
127                final Number userID = dbIntegrityService.getResourceInternalIdentifier(UUID.fromString(externalIdentifier), Resource.PRINCIPAL);
128                if (userID != null) {
129                    final CurrentUserInfo userInfo = new CurrentUserInfo();
130                    userInfo.setRef(dbIntegrityService.getResourceURI(userID, Resource.PRINCIPAL));
131                    userInfo.setCurrentUser(ifLoggedIn(userID));
132                    return new ObjectFactory().createCurrentUserInfo(userInfo);
133                } else {
134                    verboseOutput.PRINCIPAL_NOT_FOUND(externalIdentifier);
135                }
136            } catch (IllegalArgumentException e) {
137                verboseOutput.ILLEGAL_UUID(externalIdentifier);
138            }
139        }
140        return new ObjectFactory().createCurrentUserInfo(new CurrentUserInfo());
141    }
142
143 
144
145    @POST
146    @Consumes(MediaType.APPLICATION_XML)
147    @Produces(MediaType.APPLICATION_XML)
148    @Path("{remoteId}")
149    public JAXBElement<User> addUser(@PathParam("remoteId") String remoteId, User user) throws SQLException, IOException {
150        Number remoteUserID = this.getUserID();
151        if (remoteUserID != null) {
152            if (dbIntegrityService.getTypeOfUserAccount(remoteUserID).equals(admin)) {
153                final Number userID = dbIntegrityService.addUser(user, remoteId);
154                if (userID != null) {
155                    final User addedUser = dbIntegrityService.getUser(userID);
156                    return new ObjectFactory().createUser(addedUser);
157                } else {
158                    verboseOutput.PRINCIPAL_IS_NOT_ADDED_TO_DB();
159                }
160            } else {
161                verboseOutput.ADMIN_RIGHTS_EXPECTED(dbIntegrityService.getDataBaseAdmin().getDisplayName(), dbIntegrityService.getDataBaseAdmin().getEMail());
162            }
163        } 
164        return new ObjectFactory().createUser(new User());
165    }
166
167    @PUT
168    @Consumes(MediaType.APPLICATION_XML)
169    @Produces(MediaType.APPLICATION_XML)
170    @Path("")
171    public JAXBElement<User> updateUser(User user) throws IOException {
172       Number remoteUserID = this.getUserID();
173        if (remoteUserID != null) {
174            if (dbIntegrityService.getTypeOfUserAccount(remoteUserID).equals(admin)) {
175                final Number userID = dbIntegrityService.updateUser(user);
176                if (userID != null) {
177                    final User addedUser = dbIntegrityService.getUser(userID);
178                    return new ObjectFactory().createUser(addedUser);
179                } else {
180                    verboseOutput.PRINCIPAL_NOT_FOUND(user.getURI());
181                }
182            } else {
183                verboseOutput.ADMIN_RIGHTS_EXPECTED(dbIntegrityService.getDataBaseAdmin().getDisplayName(), dbIntegrityService.getDataBaseAdmin().getEMail());
184            }
185        } 
186        return new ObjectFactory().createUser(new User());
187    }
188
189    @PUT
190    @Produces(MediaType.TEXT_PLAIN)
191    @Path("{externalId}/account/{accountType}")
192    public String updateUserAccount(@PathParam("externalId") String externalId, @PathParam("accountType") String accountType) throws IOException {
193        Number remoteUserID = this.getUserID();
194        if (remoteUserID != null) {
195            if (dbIntegrityService.getTypeOfUserAccount(remoteUserID).equals(admin)) {
196                final boolean updated = dbIntegrityService.updateAccount(UUID.fromString(externalId), accountType);
197                if (updated) {
198                    return "The account was updated to " + dbIntegrityService.getTypeOfUserAccount(dbIntegrityService.getResourceInternalIdentifier(UUID.fromString(externalId), Resource.PRINCIPAL));
199                } else {
200                    verboseOutput.ACCOUNT_IS_NOT_UPDATED();
201                }
202            } else {
203                verboseOutput.ADMIN_RIGHTS_EXPECTED(dbIntegrityService.getDataBaseAdmin().getDisplayName(), dbIntegrityService.getDataBaseAdmin().getEMail());
204            }
205        } 
206        return " ";
207    }
208
209    @DELETE
210    @Path("{userId}")
211    public String deleteUser(@PathParam("userId") String externalIdentifier) throws IOException {
212       Number remoteUserID = this.getUserID();
213        if (remoteUserID != null) {
214            if (dbIntegrityService.getTypeOfUserAccount(remoteUserID).equals(admin)) {
215                final Number userID = dbIntegrityService.getResourceInternalIdentifier(UUID.fromString(externalIdentifier), Resource.PRINCIPAL);
216                if (userID != null) {
217                    final Integer result = dbIntegrityService.deleteUser(userID);
218                    return "There is " + result.toString() + " row deleted";
219                } else {
220                    verboseOutput.PRINCIPAL_NOT_FOUND(externalIdentifier);
221                }
222            } else {
223                verboseOutput.ADMIN_RIGHTS_EXPECTED(dbIntegrityService.getDataBaseAdmin().getDisplayName(), dbIntegrityService.getDataBaseAdmin().getEMail());
224            }
225        } 
226        return " ";
227    }
228
229    @DELETE
230    @Path("{userId}/safe")
231    public String deleteUserSafe(@PathParam("userId") String externalIdentifier) throws IOException {
232       Number remoteUserID = this.getUserID();
233        if (remoteUserID != null) {
234            if (dbIntegrityService.getTypeOfUserAccount(remoteUserID).equals(admin)) {
235                final Number userID = dbIntegrityService.getResourceInternalIdentifier(UUID.fromString(externalIdentifier), Resource.PRINCIPAL);
236                if (userID != null) {
237                    final Integer result = dbIntegrityService.deleteUserSafe(userID);
238                    return "There is " + result.toString() + " row deleted";
239                } else {
240                    verboseOutput.PRINCIPAL_NOT_FOUND(externalIdentifier);
241                }
242            } else {
243                verboseOutput.ADMIN_RIGHTS_EXPECTED(dbIntegrityService.getDataBaseAdmin().getDisplayName(), dbIntegrityService.getDataBaseAdmin().getEMail());
244            }
245        } 
246        return " ";
247    }
248
249    private boolean ifLoggedIn(Number userID) {
250        return (dbIntegrityService.getRemoteUser()).equals(dbIntegrityService.getUserRemoteID(userID));
251    }
252}
Note: See TracBrowser for help on using the repository browser.