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.backend.NotInDataBaseException; |
---|
21 | import eu.dasish.annotation.backend.PrincipalExists; |
---|
22 | import eu.dasish.annotation.backend.Resource; |
---|
23 | import eu.dasish.annotation.backend.ResourceAction; |
---|
24 | import eu.dasish.annotation.backend.dao.ILambda; |
---|
25 | import eu.dasish.annotation.backend.dao.ILambdaPrincipal; |
---|
26 | import eu.dasish.annotation.schema.ObjectFactory; |
---|
27 | import eu.dasish.annotation.schema.Principal; |
---|
28 | import java.io.IOException; |
---|
29 | import java.util.Map; |
---|
30 | import java.util.UUID; |
---|
31 | import javax.servlet.http.HttpServletResponse; |
---|
32 | import javax.xml.bind.JAXBElement; |
---|
33 | |
---|
34 | /** |
---|
35 | * |
---|
36 | * @author olhsha |
---|
37 | */ |
---|
38 | public class RequestWrappers<T> { |
---|
39 | |
---|
40 | private ResourceResource resourceResource; |
---|
41 | private String _internalID = "internalID"; |
---|
42 | private String _principalID = "principalID"; |
---|
43 | private String _externalId = "externalID"; |
---|
44 | private String _resourceType = "resourceType"; |
---|
45 | |
---|
46 | public RequestWrappers(ResourceResource resourceResource) { |
---|
47 | this.resourceResource = resourceResource; |
---|
48 | } |
---|
49 | |
---|
50 | public String FORBIDDEN_RESOURCE_ACTION(String identifier, String resource, String action) { |
---|
51 | return " The logged-in principal cannot " + action + " the " + resource + " with the identifier " + identifier; |
---|
52 | } |
---|
53 | |
---|
54 | public T wrapRequestResource(Map params, ILambda<Map, T> dbRequestor) throws IOException { |
---|
55 | Number remotePrincipalID = resourceResource.getPrincipalID(); |
---|
56 | if (remotePrincipalID == null) { |
---|
57 | return null; |
---|
58 | } |
---|
59 | params.put(_principalID, remotePrincipalID); |
---|
60 | try { |
---|
61 | return dbRequestor.apply(params); |
---|
62 | } catch (NotInDataBaseException e1) { |
---|
63 | resourceResource.loggerServer.debug(e1.toString()); |
---|
64 | resourceResource.httpServletResponse.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e1.toString()); |
---|
65 | return null; |
---|
66 | } |
---|
67 | } |
---|
68 | |
---|
69 | public T wrapRequestResource(Map params, ILambda<Map, T> dbRequestor, Resource resource, ResourceAction action, String externalId) throws IOException { |
---|
70 | Number principalID = resourceResource.getPrincipalID(); |
---|
71 | if (principalID == null) { |
---|
72 | return null; |
---|
73 | } |
---|
74 | params.put(_principalID, principalID); |
---|
75 | try { |
---|
76 | final Number resourceID = resourceResource.dbDispatcher.getResourceInternalIdentifier(UUID.fromString(externalId), resource); |
---|
77 | if (resourceResource.dbDispatcher.canDo(action, principalID, resourceID, resource)) { |
---|
78 | params.put(_externalId, externalId); |
---|
79 | params.put(_internalID, resourceID); |
---|
80 | params.put(_resourceType, resource); |
---|
81 | return dbRequestor.apply(params); |
---|
82 | } else { |
---|
83 | this.FORBIDDEN_RESOURCE_ACTION(externalId, resource.name(), action.name()); |
---|
84 | resourceResource.loggerServer.debug("Principal " + resourceResource.dbDispatcher.getResourceExternalIdentifier(principalID, Resource.PRINCIPAL) + " cannot " + action.name() + " " + resource.name() + " with the id " + externalId); |
---|
85 | resourceResource.httpServletResponse.sendError(HttpServletResponse.SC_FORBIDDEN); |
---|
86 | return null; |
---|
87 | } |
---|
88 | } catch (NotInDataBaseException e2) { |
---|
89 | resourceResource.loggerServer.debug(e2.toString()); |
---|
90 | resourceResource.httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, e2.toString()); |
---|
91 | return null; |
---|
92 | } |
---|
93 | } |
---|
94 | |
---|
95 | public JAXBElement<Principal> wrapAddPrincipalRequest(Map params, ILambdaPrincipal<Map, Principal> dbRequestor) throws IOException { |
---|
96 | |
---|
97 | try { |
---|
98 | try { |
---|
99 | return new ObjectFactory().createPrincipal(dbRequestor.apply(params)); |
---|
100 | } catch (NotInDataBaseException e1) { |
---|
101 | resourceResource.loggerServer.debug(e1.toString()); |
---|
102 | resourceResource.httpServletResponse.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e1.toString()); |
---|
103 | return new ObjectFactory().createPrincipal(new Principal()); |
---|
104 | } |
---|
105 | } catch (PrincipalExists e) { |
---|
106 | resourceResource.loggerServer.debug(e.toString()); |
---|
107 | resourceResource.httpServletResponse.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.toString()); |
---|
108 | return new ObjectFactory().createPrincipal(new Principal()); |
---|
109 | } |
---|
110 | |
---|
111 | } |
---|
112 | } |
---|