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.BackendConstants; |
---|
21 | import eu.dasish.annotation.backend.Resource; |
---|
22 | import eu.dasish.annotation.backend.dao.DBIntegrityService; |
---|
23 | import eu.dasish.annotation.schema.Notebook; |
---|
24 | import eu.dasish.annotation.schema.NotebookInfo; |
---|
25 | import eu.dasish.annotation.schema.NotebookInfoList; |
---|
26 | import eu.dasish.annotation.schema.ObjectFactory; |
---|
27 | import eu.dasish.annotation.schema.Permission; |
---|
28 | import eu.dasish.annotation.schema.ReferenceList; |
---|
29 | import eu.dasish.annotation.schema.ResponseBody; |
---|
30 | import java.io.IOException; |
---|
31 | import java.sql.SQLException; |
---|
32 | import java.util.UUID; |
---|
33 | import javax.servlet.http.HttpServletRequest; |
---|
34 | import javax.servlet.http.HttpServletResponse; |
---|
35 | import javax.ws.rs.Consumes; |
---|
36 | import javax.ws.rs.GET; |
---|
37 | import javax.ws.rs.PUT; |
---|
38 | import javax.ws.rs.Path; |
---|
39 | import javax.ws.rs.PathParam; |
---|
40 | import javax.ws.rs.Produces; |
---|
41 | import javax.ws.rs.QueryParam; |
---|
42 | import javax.ws.rs.core.Context; |
---|
43 | import javax.ws.rs.core.MediaType; |
---|
44 | import javax.ws.rs.core.UriInfo; |
---|
45 | import javax.ws.rs.ext.Providers; |
---|
46 | import javax.xml.bind.JAXBElement; |
---|
47 | import javax.xml.parsers.ParserConfigurationException; |
---|
48 | import org.slf4j.Logger; |
---|
49 | import org.slf4j.LoggerFactory; |
---|
50 | import org.springframework.beans.factory.annotation.Autowired; |
---|
51 | import org.springframework.stereotype.Component; |
---|
52 | import org.springframework.transaction.annotation.Transactional; |
---|
53 | |
---|
54 | /** |
---|
55 | * Created on : Jun 11, 2013, 5:10:55 PM |
---|
56 | * |
---|
57 | * @author Peter Withers <peter.withers@mpi.nl> |
---|
58 | */ |
---|
59 | @Component |
---|
60 | @Path("/notebooks") |
---|
61 | @Transactional(rollbackFor = {Exception.class, SQLException.class, IOException.class, ParserConfigurationException.class}) |
---|
62 | public class NotebookResource extends ResourceResource { |
---|
63 | |
---|
64 | public NotebookResource() { |
---|
65 | } |
---|
66 | |
---|
67 | // changed w.r.t.the spec, query parameter persmission is added |
---|
68 | @GET |
---|
69 | @Produces(MediaType.APPLICATION_XML) |
---|
70 | @Path("") |
---|
71 | @Transactional(readOnly = true) |
---|
72 | public JAXBElement<NotebookInfoList> getNotebookInfos(@QueryParam("permission") String permissionMode) throws IOException { |
---|
73 | dbIntegrityService.setServiceURI(uriInfo.getBaseUri().toString()); |
---|
74 | Number remoteUserID = this.getUserID(); |
---|
75 | if (remoteUserID != null) { |
---|
76 | if (permissionMode.equalsIgnoreCase("reader") || permissionMode.equalsIgnoreCase("writer") || permissionMode.equalsIgnoreCase("owner")) { |
---|
77 | NotebookInfoList notebookInfos = dbIntegrityService.getNotebooks(remoteUserID, permissionMode); |
---|
78 | return new ObjectFactory().createNotebookInfoList(notebookInfos); |
---|
79 | } else { |
---|
80 | verboseOutput.INVALID_PERMISSION_MODE(permissionMode); |
---|
81 | } |
---|
82 | } |
---|
83 | return (new ObjectFactory()).createNotebookInfoList(new NotebookInfoList()); |
---|
84 | } |
---|
85 | |
---|
86 | @GET |
---|
87 | @Produces(MediaType.APPLICATION_XML) |
---|
88 | @Path("owned") |
---|
89 | @Transactional(readOnly = true) |
---|
90 | public JAXBElement<ReferenceList> getOwnedNotebooks() throws IOException { |
---|
91 | Number remoteUserID = this.getUserID(); |
---|
92 | if (remoteUserID != null) { |
---|
93 | ReferenceList references = dbIntegrityService.getNotebooksOwnedBy(remoteUserID); |
---|
94 | return new ObjectFactory().createReferenceList(references); |
---|
95 | } |
---|
96 | return new ObjectFactory().createReferenceList(new ReferenceList()); |
---|
97 | } |
---|
98 | |
---|
99 | @GET |
---|
100 | @Produces(MediaType.APPLICATION_XML) |
---|
101 | @Path("{notebookid: " + BackendConstants.regExpIdentifier + "}/{permission}") |
---|
102 | @Transactional(readOnly = true) |
---|
103 | public JAXBElement<ReferenceList> getPrincipals(@PathParam("notebookid") String externalIdentifier, @PathParam("permission") String permissionMode) throws IOException { |
---|
104 | Number remoteUserID = this.getUserID(); |
---|
105 | if (remoteUserID != null) { |
---|
106 | Number notebookID = dbIntegrityService.getResourceInternalIdentifier(UUID.fromString(externalIdentifier), Resource.NOTEBOOK); |
---|
107 | if (notebookID != null) { |
---|
108 | if (dbIntegrityService.hasAccess(notebookID, remoteUserID, Permission.fromValue("reader"))) { |
---|
109 | ReferenceList principals = dbIntegrityService.getPrincipals(notebookID, permissionMode); |
---|
110 | return new ObjectFactory().createReferenceList(principals); |
---|
111 | } else { |
---|
112 | verboseOutput.FORBIDDEN_NOTEBOOK_READING(externalIdentifier, dbIntegrityService.getAnnotationOwner(notebookID).getDisplayName(), dbIntegrityService.getAnnotationOwner(notebookID).getEMail()); |
---|
113 | } |
---|
114 | } else { |
---|
115 | verboseOutput.NOTEBOOK_NOT_FOUND(externalIdentifier); |
---|
116 | } |
---|
117 | } |
---|
118 | return new ObjectFactory().createReferenceList(new ReferenceList()); |
---|
119 | } |
---|
120 | |
---|
121 | // Notebook and NotebookInfo (metadata) schemata may be changed |
---|
122 | // 1) we do not have information "private notebook" directly in the xml, but we have readers and writers in the schema |
---|
123 | //so if both are empty then we see that it is private for the owner |
---|
124 | // or shall we change the scheme? for notebooks |
---|
125 | // 2) d we need to include the reference list of annotations in teh metadata of the notebook |
---|
126 | @GET |
---|
127 | @Produces(MediaType.APPLICATION_XML) |
---|
128 | @Path("{notebookid: " + BackendConstants.regExpIdentifier + "}/metadata") |
---|
129 | @Transactional(readOnly = true) |
---|
130 | public JAXBElement<Notebook> getNotebook(@PathParam("notebookid") String externalIdentifier) throws IOException { |
---|
131 | Number remoteUserID = this.getUserID(); |
---|
132 | if (remoteUserID != null) { |
---|
133 | Number notebookID = dbIntegrityService.getResourceInternalIdentifier(UUID.fromString(externalIdentifier), Resource.NOTEBOOK); |
---|
134 | if (notebookID != null) { |
---|
135 | if (dbIntegrityService.hasAccess(notebookID, remoteUserID, Permission.fromValue("reader"))) { |
---|
136 | Notebook notebook = dbIntegrityService.getNotebook(notebookID); |
---|
137 | return new ObjectFactory().createNotebook(notebook); |
---|
138 | } else { |
---|
139 | verboseOutput.FORBIDDEN_NOTEBOOK_READING(externalIdentifier, dbIntegrityService.getAnnotationOwner(notebookID).getDisplayName(), dbIntegrityService.getAnnotationOwner(notebookID).getEMail()); |
---|
140 | } |
---|
141 | } else { |
---|
142 | verboseOutput.NOTEBOOK_NOT_FOUND(externalIdentifier); |
---|
143 | } |
---|
144 | } |
---|
145 | return new ObjectFactory().createNotebook(new Notebook()); |
---|
146 | } |
---|
147 | |
---|
148 | @GET |
---|
149 | @Produces(MediaType.APPLICATION_XML) |
---|
150 | @Path("{notebookid: " + BackendConstants.regExpIdentifier + "}") |
---|
151 | @Transactional(readOnly = true) |
---|
152 | public JAXBElement<ReferenceList> getNotebookAnnotations(@PathParam("notebookid") String externalIdentifier, |
---|
153 | @QueryParam("maximumAnnotations") int maximumAnnotations, |
---|
154 | @QueryParam("startAnnotation") int startAnnotations, |
---|
155 | @QueryParam("orderBy") String orderBy, |
---|
156 | @QueryParam("descending") boolean desc) throws IOException { |
---|
157 | |
---|
158 | Number remoteUserID = this.getUserID(); |
---|
159 | if (remoteUserID != null) { |
---|
160 | Number notebookID = dbIntegrityService.getResourceInternalIdentifier(UUID.fromString(externalIdentifier), Resource.NOTEBOOK); |
---|
161 | if (notebookID != null) { |
---|
162 | if (dbIntegrityService.hasAccess(notebookID, remoteUserID, Permission.fromValue("reader"))) { |
---|
163 | ReferenceList annotations = dbIntegrityService.getAnnotationsForNotebook(notebookID, startAnnotations, maximumAnnotations, orderBy, desc); |
---|
164 | return new ObjectFactory().createReferenceList(annotations); |
---|
165 | } else { |
---|
166 | verboseOutput.FORBIDDEN_NOTEBOOK_READING(externalIdentifier, dbIntegrityService.getAnnotationOwner(notebookID).getDisplayName(), dbIntegrityService.getAnnotationOwner(notebookID).getEMail()); |
---|
167 | } |
---|
168 | } else { |
---|
169 | verboseOutput.NOTEBOOK_NOT_FOUND(externalIdentifier); |
---|
170 | } |
---|
171 | } |
---|
172 | return new ObjectFactory().createReferenceList(new ReferenceList()); |
---|
173 | } |
---|
174 | |
---|
175 | @PUT |
---|
176 | @Consumes(MediaType.APPLICATION_XML) |
---|
177 | @Produces(MediaType.APPLICATION_XML) |
---|
178 | @Path("{notebookid: " + BackendConstants.regExpIdentifier + "}") |
---|
179 | public JAXBElement<ResponseBody> updateNotebookInfo(@PathParam("notebookid") String externalIdentifier, NotebookInfo notebookInfo) throws IOException { |
---|
180 | |
---|
181 | Number remoteUserID = this.getUserID(); |
---|
182 | if (remoteUserID != null) { |
---|
183 | String path = uriInfo.getBaseUri().toString(); |
---|
184 | String notebookURI = notebookInfo.getRef(); |
---|
185 | if (!(path + "notebook/" + externalIdentifier).equals(notebookURI)) { |
---|
186 | verboseOutput.IDENTIFIER_MISMATCH(externalIdentifier); |
---|
187 | return new ObjectFactory().createResponseBody(new ResponseBody()); |
---|
188 | } |
---|
189 | |
---|
190 | try { |
---|
191 | final Number notebookID = dbIntegrityService.getResourceInternalIdentifier(UUID.fromString(externalIdentifier), Resource.NOTEBOOK); |
---|
192 | if (notebookID != null) { |
---|
193 | if (remoteUserID.equals(dbIntegrityService.getNotebookOwner(notebookID)) || dbIntegrityService.getTypeOfUserAccount(remoteUserID).equals(admin)) { |
---|
194 | boolean success = dbIntegrityService.updateNotebookMetadata(notebookID, notebookInfo); |
---|
195 | if (success) { |
---|
196 | return new ObjectFactory().createResponseBody(dbIntegrityService.makeNotebookResponseEnvelope(notebookID)); |
---|
197 | } |
---|
198 | } else { |
---|
199 | verboseOutput.FORBIDDEN_PERMISSION_CHANGING(externalIdentifier, dbIntegrityService.getAnnotationOwner(notebookID).getDisplayName(), dbIntegrityService.getAnnotationOwner(notebookID).getEMail()); |
---|
200 | loggerServer.debug(" Ownership changing is the part of the full update of the notebook metadadata."); |
---|
201 | } |
---|
202 | } else { |
---|
203 | verboseOutput.NOTEBOOK_NOT_FOUND(externalIdentifier); |
---|
204 | } |
---|
205 | } catch (IllegalArgumentException e) { |
---|
206 | verboseOutput.ILLEGAL_UUID(externalIdentifier); |
---|
207 | } |
---|
208 | } |
---|
209 | return new ObjectFactory().createResponseBody(new ResponseBody()); |
---|
210 | } |
---|
211 | } |
---|