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.schema.ObjectFactory; |
---|
21 | import eu.dasish.annotation.schema.User; |
---|
22 | import java.io.IOException; |
---|
23 | import java.sql.SQLException; |
---|
24 | import javax.servlet.ServletException; |
---|
25 | import javax.servlet.http.HttpServletRequestWrapper; |
---|
26 | import javax.ws.rs.GET; |
---|
27 | import javax.ws.rs.Path; |
---|
28 | import javax.ws.rs.Produces; |
---|
29 | import javax.ws.rs.core.MediaType; |
---|
30 | import javax.xml.bind.JAXBElement; |
---|
31 | import javax.xml.parsers.ParserConfigurationException; |
---|
32 | import org.springframework.stereotype.Component; |
---|
33 | import org.springframework.transaction.annotation.Transactional; |
---|
34 | |
---|
35 | /** |
---|
36 | * |
---|
37 | * @author olhsha |
---|
38 | */ |
---|
39 | @Component |
---|
40 | @Path("/authentication") |
---|
41 | @Transactional(rollbackFor = {Exception.class, SQLException.class, IOException.class, ParserConfigurationException.class}) |
---|
42 | public class AutheticationResource extends ResourceResource { |
---|
43 | |
---|
44 | @GET |
---|
45 | @Produces(MediaType.TEXT_XML) |
---|
46 | @Path("user") |
---|
47 | @Transactional(readOnly = true) |
---|
48 | public JAXBElement<User> getCurrentUser() throws IOException { |
---|
49 | Number userID = this.getUserID(); |
---|
50 | if (userID != null) { |
---|
51 | return new ObjectFactory().createUser(dbIntegrityService.getUser(userID)); |
---|
52 | } |
---|
53 | return new ObjectFactory().createUser(new User()); |
---|
54 | } |
---|
55 | |
---|
56 | /* the only request that redirects to the shibboleth login-page |
---|
57 | * |
---|
58 | */ |
---|
59 | @GET |
---|
60 | @Produces(MediaType.TEXT_XML) |
---|
61 | @Path("login") |
---|
62 | @Transactional(readOnly = true) |
---|
63 | public JAXBElement<User> loginAndGet() throws IOException { |
---|
64 | String remoteUser = httpServletRequest.getRemoteUser(); |
---|
65 | verboseOutput = new VerboseOutput(httpServletResponse, loggerServer); |
---|
66 | if (remoteUser != null) { |
---|
67 | if (!remoteUser.equals("anonymous")) { |
---|
68 | dbIntegrityService.setServiceURI(uriInfo.getBaseUri().toString()); |
---|
69 | final Number remoteUserID = dbIntegrityService.getUserInternalIDFromRemoteID(remoteUser); |
---|
70 | if (remoteUserID != null) { |
---|
71 | return new ObjectFactory().createUser(dbIntegrityService.getUser(remoteUserID)); |
---|
72 | } else { |
---|
73 | verboseOutput.REMOTE_PRINCIPAL_NOT_FOUND(remoteUser, dbIntegrityService.getDataBaseAdmin().getDisplayName(), dbIntegrityService.getDataBaseAdmin().getEMail()); |
---|
74 | } |
---|
75 | } else { |
---|
76 | verboseOutput.ANONYMOUS_PRINCIPAL(); |
---|
77 | } |
---|
78 | } |
---|
79 | return new ObjectFactory().createUser(new User()); |
---|
80 | } |
---|
81 | |
---|
82 | @GET |
---|
83 | @Produces(MediaType.TEXT_XML) |
---|
84 | @Path("logout") |
---|
85 | @Transactional(readOnly = true) |
---|
86 | public void logout() throws IOException, ServletException { |
---|
87 | httpServletResponse.sendRedirect("eu.dasish.annotation.backend.logout"); |
---|
88 | } |
---|
89 | } |
---|