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

Last change on this file since 6013 was 6013, checked in by olhsha@mpi.nl, 9 years ago

Javadoc are updated for AuthenticationResource?.java

File size: 3.5 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.Helpers;
21import eu.dasish.annotation.schema.ObjectFactory;
22import eu.dasish.annotation.schema.Principal;
23import java.io.IOException;
24import javax.servlet.ServletException;
25import javax.ws.rs.GET;
26import javax.ws.rs.Path;
27import javax.ws.rs.Produces;
28import javax.ws.rs.core.Context;
29import javax.ws.rs.core.MediaType;
30import javax.ws.rs.core.UriInfo;
31import javax.xml.bind.JAXBElement;
32import javax.xml.parsers.ParserConfigurationException;
33import org.springframework.stereotype.Component;
34import org.springframework.transaction.annotation.Transactional;
35
36/**
37 *
38 * @author olhsha
39 */
40@Component
41@Path("/authentication")
42@Transactional(rollbackFor = {Exception.class, IOException.class, ParserConfigurationException.class})
43public class AutheticationResource extends ResourceResource {
44   
45    /**
46     *
47     * @return the {@link Principal} object representing the current logged-on principal.
48     * @throws IOException if sending the error fails.
49     */
50    @GET
51    @Produces(MediaType.TEXT_XML)
52    @Path("principal")
53    @Transactional(readOnly = true)
54    public JAXBElement<Principal> getCurrentPrincipal() throws IOException {
55        Number principalID = this.getPrincipalID();
56        if (principalID != null) {
57            return new ObjectFactory().createPrincipal(dbDispatcher.getPrincipal(principalID));
58        } else {
59            return new ObjectFactory().createPrincipal(new Principal());
60        }
61    }
62
63   
64    /**
65     * Redirects to shibboleth authentication page.
66     * @return welcome string or error message.
67     */
68    @GET
69    @Produces(MediaType.TEXT_HTML)
70    @Path("login")
71    @Transactional(readOnly = true)
72    public String login() {
73        try {
74            Number principalID = this.getPrincipalID();
75            String remoteID = dbDispatcher.getPrincipalRemoteID(principalID);
76            return Helpers.welcomeString(httpServletRequest.getContextPath(), remoteID);
77        } catch (IOException e) {
78            return e.getMessage();
79        }
80    }
81
82    /**
83     *
84     * @throws IOException if sending redirect fails.
85     */
86    @GET
87    @Produces(MediaType.TEXT_XML)
88    @Path("logout")
89    @Transactional(readOnly = true)
90    public void logout() throws IOException{
91        httpServletRequest.getSession().invalidate();
92        boolean isShibboleth = Boolean.parseBoolean(context.getInitParameter("eu.dasish.annotation.backend.isShibbolethSession"));
93        String redirect = isShibboleth ? context.getInitParameter("eu.dasish.annotation.backend.logout.shibboleth") : 
94                httpServletRequest.getContextPath() + context.getInitParameter("eu.dasish.annotation.backend.logout.basic");       
95        httpServletResponse.sendRedirect(redirect);
96    }
97}
Note: See TracBrowser for help on using the repository browser.