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.ForbiddenException; |
---|
21 | import eu.dasish.annotation.backend.NotInDataBaseException; |
---|
22 | import eu.dasish.annotation.backend.PrincipalExists; |
---|
23 | import eu.dasish.annotation.backend.Resource; |
---|
24 | import eu.dasish.annotation.backend.dao.ILambda; |
---|
25 | import eu.dasish.annotation.backend.dao.ILambdaPrincipal; |
---|
26 | import eu.dasish.annotation.schema.Access; |
---|
27 | import eu.dasish.annotation.schema.ObjectFactory; |
---|
28 | import eu.dasish.annotation.schema.Principal; |
---|
29 | import java.io.IOException; |
---|
30 | import java.util.Map; |
---|
31 | import java.util.UUID; |
---|
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, NotInDataBaseException { |
---|
55 | Number remotePrincipalID = resourceResource.getPrincipalID(); |
---|
56 | if (remotePrincipalID == null) { |
---|
57 | return null; |
---|
58 | } |
---|
59 | params.put(_principalID, remotePrincipalID); |
---|
60 | return dbRequestor.apply(params); |
---|
61 | } |
---|
62 | |
---|
63 | public T wrapRequestResource(Map params, ILambda<Map, T> dbRequestor, Resource resource, Access action, String externalId) throws IOException, ForbiddenException, NotInDataBaseException { |
---|
64 | Number principalID = resourceResource.getPrincipalID(); |
---|
65 | if (principalID == null) { |
---|
66 | return null; |
---|
67 | } |
---|
68 | params.put(_principalID, principalID); |
---|
69 | final Number resourceID = resourceResource.dbDispatcher.getResourceInternalIdentifier(UUID.fromString(externalId), resource); |
---|
70 | if (resourceResource.dbDispatcher.canDo(action, principalID, resourceID, resource)) { |
---|
71 | params.put(_externalId, externalId); |
---|
72 | params.put(_internalID, resourceID); |
---|
73 | params.put(_resourceType, resource); |
---|
74 | return dbRequestor.apply(params); |
---|
75 | } else { |
---|
76 | throw new ForbiddenException(this.FORBIDDEN_RESOURCE_ACTION(externalId, resource.name(), action.name())); |
---|
77 | } |
---|
78 | |
---|
79 | } |
---|
80 | |
---|
81 | |
---|
82 | |
---|
83 | public JAXBElement<Principal> wrapAddPrincipalRequest(Map params, ILambdaPrincipal<Map, Principal> dbRequestor) throws IOException, NotInDataBaseException, PrincipalExists { |
---|
84 | return new ObjectFactory().createPrincipal(dbRequestor.apply(params)); |
---|
85 | } |
---|
86 | } |
---|