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

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

ref --> href
+ xml:id for instances
refactoring

File size: 4.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.backend.NotInDataBaseException;
22import eu.dasish.annotation.backend.PrincipalExists;
23import eu.dasish.annotation.backend.dao.DBDispatcher;
24import eu.dasish.annotation.schema.Principal;
25import java.io.IOException;
26import javax.servlet.ServletContext;
27import javax.servlet.http.HttpServletRequest;
28import javax.servlet.http.HttpServletResponse;
29import javax.ws.rs.core.Context;
30import javax.ws.rs.ext.Providers;
31import org.slf4j.Logger;
32import org.slf4j.LoggerFactory;
33import org.springframework.beans.factory.annotation.Autowired;
34
35/**
36 *
37 * @author olhsha
38 */
39public class ResourceResource<T> {
40
41    @Autowired
42    protected DBDispatcher dbDispatcher;
43    @Context
44    protected HttpServletRequest httpServletRequest;
45    @Context
46    protected HttpServletResponse httpServletResponse;   
47    @Context
48    protected Providers providers;
49    @Context
50    protected ServletContext context;
51    protected Logger loggerServer = LoggerFactory.getLogger(HttpServletResponse.class);
52    protected String admin = "admin";
53    protected String anonym = "anonymous";
54    protected String defaultAccess = "read";
55    protected String[] admissibleAccess = {"read", "write", "owner"};
56
57    public Number getPrincipalID() throws IOException {
58       
59        dbDispatcher.setResourcesPaths(this.getRelativeServiceURI());
60        String remotePrincipal = httpServletRequest.getRemoteUser();
61        if (remotePrincipal != null) {
62            if (!remotePrincipal.equals(anonym)) {
63                try {
64                    return dbDispatcher.getPrincipalInternalIDFromRemoteID(remotePrincipal);
65                } catch (NotInDataBaseException e) {
66                    loggerServer.info(e.toString());
67                    loggerServer.info("The record for the user with the id " + remotePrincipal + " will be generated now automatically.");
68                    try {
69                        try {
70                            Principal newPrincipal = Helpers.createPrincipalElement(remotePrincipal, remotePrincipal);
71                            return dbDispatcher.addPrincipal(newPrincipal, remotePrincipal);
72                        } catch (PrincipalExists e2) {
73                            loggerServer.info(e2.toString());
74                            httpServletResponse.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e2.toString());
75                            return null;
76                        }
77                    } catch (NotInDataBaseException e1) {
78                        loggerServer.info(e1.toString());
79                        httpServletResponse.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e1.toString());
80                        return null;
81                    }
82                }
83            } else {
84                loggerServer.info("Shibboleth fall-back.  Logged in as 'anonymous' with no rights.");
85                httpServletResponse.sendError(HttpServletResponse.SC_UNAUTHORIZED, " Shibboleth fall-back.  Logged in as 'anonymous' with no rights.");
86                return null;
87            }
88        } else {
89            loggerServer.info("Null principal");
90            httpServletResponse.sendError(HttpServletResponse.SC_UNAUTHORIZED, " Null principal");
91            return null;
92        }
93    }
94
95    protected void ADMIN_RIGHTS_EXPECTED() throws IOException {
96        loggerServer.debug("The request can be performed only by the principal with the admin rights.");
97    }
98
99    protected void INVALID_ACCESS_MODE(String accessMode) throws IOException {
100        loggerServer.debug(accessMode + " is an invalid access value, which must be either owner, or read, or write.");
101    }
102   
103    protected String getRelativeServiceURI(){
104        return httpServletRequest.getContextPath()+httpServletRequest.getServletPath();
105    }
106}
Note: See TracBrowser for help on using the repository browser.