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.NotInDataBaseException; |
---|
22 | import eu.dasish.annotation.backend.Resource; |
---|
23 | import eu.dasish.annotation.schema.NotebookInfoList; |
---|
24 | import eu.dasish.annotation.schema.ObjectFactory; |
---|
25 | import eu.dasish.annotation.schema.Access; |
---|
26 | import eu.dasish.annotation.schema.Notebook; |
---|
27 | import eu.dasish.annotation.schema.NotebookInfo; |
---|
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.HttpServletResponse; |
---|
34 | import javax.ws.rs.Consumes; |
---|
35 | import javax.ws.rs.GET; |
---|
36 | import javax.ws.rs.PUT; |
---|
37 | import javax.ws.rs.Path; |
---|
38 | import javax.ws.rs.PathParam; |
---|
39 | import javax.ws.rs.Produces; |
---|
40 | import javax.ws.rs.QueryParam; |
---|
41 | import javax.ws.rs.core.MediaType; |
---|
42 | import javax.xml.bind.JAXBElement; |
---|
43 | import javax.xml.parsers.ParserConfigurationException; |
---|
44 | import javax.xml.ws.http.HTTPException; |
---|
45 | import org.springframework.stereotype.Component; |
---|
46 | import org.springframework.transaction.annotation.Transactional; |
---|
47 | |
---|
48 | /** |
---|
49 | * A REST interface for GETting, POSTing, PUTting and DELETing notebooks or their substructures (child elements). |
---|
50 | * Every REST method in the case of successful completion produces the object of the declared output type |
---|
51 | * (a JAXB-element or a message string) or sends a HTTP-error with the corresponding diagnostics otherwise. |
---|
52 | * @author olhsha@mpi.nl |
---|
53 | */ |
---|
54 | @Component |
---|
55 | @Path("/notebooks") |
---|
56 | @Transactional(rollbackFor = {Exception.class, SQLException.class, IOException.class, ParserConfigurationException.class}) |
---|
57 | public class NotebookResource extends ResourceResource { |
---|
58 | |
---|
59 | public NotebookResource() { |
---|
60 | } |
---|
61 | |
---|
62 | /** |
---|
63 | * |
---|
64 | * @param accessMode a string, representing an access mode: "none", "read", "write", "all". |
---|
65 | * @return the {@link NotebookInfoList} element containing the list of {@link NotebookInfo} elements |
---|
66 | * of all the notebooks to which the in-logged principal has "access" access. |
---|
67 | * @throws IOException if sending an error fails. |
---|
68 | */ |
---|
69 | @GET |
---|
70 | @Produces(MediaType.APPLICATION_XML) |
---|
71 | @Path("") |
---|
72 | @Transactional(readOnly = true) |
---|
73 | public JAXBElement<NotebookInfoList> getNotebookInfos(@QueryParam("access") String accessMode) throws IOException { |
---|
74 | dbDispatcher.setResourcesPaths(this.getRelativeServiceURI()); |
---|
75 | Number remotePrincipalID = this.getPrincipalID(); |
---|
76 | if (accessMode.equalsIgnoreCase("read") || accessMode.equalsIgnoreCase("write")) { |
---|
77 | NotebookInfoList notebookInfos = dbDispatcher.getNotebooks(remotePrincipalID, Access.fromValue(accessMode)); |
---|
78 | return new ObjectFactory().createNotebookInfoList(notebookInfos); |
---|
79 | } else { |
---|
80 | this.INVALID_ACCESS_MODE(accessMode); |
---|
81 | httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, "ivalide mode acess " + accessMode); |
---|
82 | return new ObjectFactory().createNotebookInfoList(new NotebookInfoList()); |
---|
83 | } |
---|
84 | } |
---|
85 | |
---|
86 | /** |
---|
87 | * |
---|
88 | * @return the {@link ReferenceList} element containing the list of h-references of all the notebooks owned by the in-logged principal. |
---|
89 | * @throws IOException if sending an error fails. |
---|
90 | */ |
---|
91 | @GET |
---|
92 | @Produces(MediaType.APPLICATION_XML) |
---|
93 | @Path("owned") |
---|
94 | @Transactional(readOnly = true) |
---|
95 | public JAXBElement<ReferenceList> getOwnedNotebooks() throws IOException { |
---|
96 | Number remotePrincipalID = this.getPrincipalID(); |
---|
97 | if (remotePrincipalID == null) { |
---|
98 | return new ObjectFactory().createReferenceList(new ReferenceList()); |
---|
99 | } |
---|
100 | ReferenceList references = dbDispatcher.getNotebooksOwnedBy(remotePrincipalID); |
---|
101 | return new ObjectFactory().createReferenceList(references); |
---|
102 | } |
---|
103 | |
---|
104 | /** |
---|
105 | * |
---|
106 | * @param externalIdentifier the external UUID identifier of a notebook. |
---|
107 | * @param accessMode the access mode on which principals must be filtered; |
---|
108 | * can be "none", "read", "write", "all". |
---|
109 | * @return a {@link ReferenceList} element representing the list of h-references of the |
---|
110 | * principals that have access "accessMode". |
---|
111 | * @throws IOException if sending an error fails. |
---|
112 | */ |
---|
113 | @GET |
---|
114 | @Produces(MediaType.APPLICATION_XML) |
---|
115 | @Path("{notebookid: " + BackendConstants.regExpIdentifier + "}/{access}") |
---|
116 | @Transactional(readOnly = true) |
---|
117 | public JAXBElement<ReferenceList> getPrincipals(@PathParam("notebookid") String externalIdentifier, @PathParam("access") String accessMode) throws IOException { |
---|
118 | Number remotePrincipalID = this.getPrincipalID(); |
---|
119 | if (remotePrincipalID == null) { |
---|
120 | return new ObjectFactory().createReferenceList(new ReferenceList()); |
---|
121 | } |
---|
122 | try { |
---|
123 | Number notebookID = dbDispatcher.getResourceInternalIdentifier(UUID.fromString(externalIdentifier), Resource.NOTEBOOK); |
---|
124 | if (dbDispatcher.hasAccess(notebookID, remotePrincipalID, Access.fromValue("read"))) { |
---|
125 | ReferenceList principals = dbDispatcher.getPrincipals(notebookID, accessMode); |
---|
126 | return new ObjectFactory().createReferenceList(principals); |
---|
127 | } else { |
---|
128 | httpServletResponse.sendError(HttpServletResponse.SC_FORBIDDEN); |
---|
129 | return new ObjectFactory().createReferenceList(new ReferenceList()); |
---|
130 | } |
---|
131 | |
---|
132 | } catch (NotInDataBaseException e) { |
---|
133 | loggerServer.debug(e.toString());; |
---|
134 | httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, e.toString()); |
---|
135 | return new ObjectFactory().createReferenceList(new ReferenceList()); |
---|
136 | } |
---|
137 | } |
---|
138 | |
---|
139 | /** |
---|
140 | * |
---|
141 | * @param externalIdentifier the external UUID identifier of a notebook. |
---|
142 | * @return a {@link Notebook} element representing the notebook with "externalIdentifier"; built up on the whole information |
---|
143 | * (the "notebook" table and the corresponding junction tables) for the notebook with "externalIdentifier". |
---|
144 | * @throws IOException if sending an error fails. |
---|
145 | */ |
---|
146 | |
---|
147 | |
---|
148 | @GET |
---|
149 | @Produces(MediaType.APPLICATION_XML) |
---|
150 | @Path("{notebookid: " + BackendConstants.regExpIdentifier + "}/metadata") |
---|
151 | @Transactional(readOnly = true) |
---|
152 | public JAXBElement<Notebook> getNotebook(@PathParam("notebookid") String externalIdentifier) throws IOException{ |
---|
153 | Number remotePrincipalID = this.getPrincipalID(); |
---|
154 | if (remotePrincipalID == null) { |
---|
155 | return new ObjectFactory().createNotebook(new Notebook()); |
---|
156 | } |
---|
157 | try { |
---|
158 | Number notebookID = dbDispatcher.getResourceInternalIdentifier(UUID.fromString(externalIdentifier), Resource.NOTEBOOK); |
---|
159 | if (dbDispatcher.hasAccess(notebookID, remotePrincipalID, Access.fromValue("read"))) { |
---|
160 | Notebook notebook = dbDispatcher.getNotebook(notebookID); |
---|
161 | return new ObjectFactory().createNotebook(notebook); |
---|
162 | } else { |
---|
163 | httpServletResponse.sendError(HttpServletResponse.SC_FORBIDDEN); |
---|
164 | return new ObjectFactory().createNotebook(new Notebook()); |
---|
165 | } |
---|
166 | |
---|
167 | } catch (NotInDataBaseException e) { |
---|
168 | loggerServer.debug(e.toString());; |
---|
169 | httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, e.toString()); |
---|
170 | return new ObjectFactory().createNotebook(new Notebook()); |
---|
171 | } |
---|
172 | } |
---|
173 | |
---|
174 | /** |
---|
175 | * |
---|
176 | * @param externalIdentifier the external UUID of a notebook. |
---|
177 | * @param maximumAnnotations the maximum amount of annotations from this notebook to output; |
---|
178 | * if the amount of annotations in the notebook is more than (startAnnotations-1) + maximumAnnotations, |
---|
179 | * then exactly maximumAnnotations will be output, otherwise the amount of annotations is limited by "# of annotations in the notebook" - (startAnnotation-1) |
---|
180 | * @param startAnnotations the index of the first annotation, min value is "1". |
---|
181 | * @param orderBy the field in the table "notebook" on which annotations must be ordered. |
---|
182 | * @param desc if true then the annotations in the list must be ordered in descending order, otherwise in ascending order. |
---|
183 | * @return a {@link ReferenceList} element representing the list of annotations filtered according to the parameters. |
---|
184 | * @throws IOException if sending an error fails. |
---|
185 | */ |
---|
186 | @GET |
---|
187 | @Produces(MediaType.APPLICATION_XML) |
---|
188 | @Path("{notebookid: " + BackendConstants.regExpIdentifier + "}") |
---|
189 | @Transactional(readOnly = true) |
---|
190 | public JAXBElement<ReferenceList> getNotebookAnnotations(@PathParam("notebookid") String externalIdentifier, |
---|
191 | @QueryParam("maximumAnnotations") int maximumAnnotations, |
---|
192 | @QueryParam("startAnnotation") int startAnnotations, |
---|
193 | @QueryParam("orderBy") String orderBy, |
---|
194 | @QueryParam("descending") boolean desc) throws IOException{ |
---|
195 | |
---|
196 | Number remotePrincipalID = this.getPrincipalID(); |
---|
197 | if (remotePrincipalID == null) { |
---|
198 | return new ObjectFactory().createReferenceList(new ReferenceList()); |
---|
199 | } |
---|
200 | try { |
---|
201 | Number notebookID = dbDispatcher.getResourceInternalIdentifier(UUID.fromString(externalIdentifier), Resource.NOTEBOOK); |
---|
202 | if (dbDispatcher.hasAccess(notebookID, remotePrincipalID, Access.fromValue("read"))) { |
---|
203 | ReferenceList annotations = dbDispatcher.getAnnotationsForNotebook(notebookID, startAnnotations, maximumAnnotations, orderBy, desc); |
---|
204 | return new ObjectFactory().createReferenceList(annotations); |
---|
205 | } else { |
---|
206 | httpServletResponse.sendError(HttpServletResponse.SC_FORBIDDEN); |
---|
207 | return new ObjectFactory().createReferenceList(new ReferenceList()); |
---|
208 | } |
---|
209 | |
---|
210 | } catch (NotInDataBaseException e) { |
---|
211 | loggerServer.debug(e.toString()); |
---|
212 | httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, e.toString()); |
---|
213 | return new ObjectFactory().createReferenceList(new ReferenceList()); |
---|
214 | } |
---|
215 | |
---|
216 | } |
---|
217 | |
---|
218 | /** |
---|
219 | * |
---|
220 | * @param externalIdentifier the external UUID identifier of a notebook. |
---|
221 | * @param notebookInfo the fresh {@link NotebookInfo} object. |
---|
222 | * @return a {@link ResponseBody} element containing the just updated {@link Notebook} element |
---|
223 | * and the list of actions (which are not yet specified for the notebooks). |
---|
224 | * @throws IOException if sending an error fails. |
---|
225 | */ |
---|
226 | @PUT |
---|
227 | @Consumes(MediaType.APPLICATION_XML) |
---|
228 | @Produces(MediaType.APPLICATION_XML) |
---|
229 | @Path("{notebookid: " + BackendConstants.regExpIdentifier + "}") |
---|
230 | public JAXBElement<ResponseBody> updateNotebookInfo(@PathParam("notebookid") String externalIdentifier, NotebookInfo notebookInfo) throws IOException{ |
---|
231 | Number remotePrincipalID = this.getPrincipalID(); |
---|
232 | if (remotePrincipalID == null) { |
---|
233 | return new ObjectFactory().createResponseBody(new ResponseBody()); |
---|
234 | } |
---|
235 | String path = this.getRelativeServiceURI(); |
---|
236 | String notebookURI = notebookInfo.getHref(); |
---|
237 | if (!(path + "/notebooks/" + externalIdentifier).equals(notebookURI)) { |
---|
238 | httpServletResponse.sendError(HttpServletResponse.SC_BAD_REQUEST); |
---|
239 | return new ObjectFactory().createResponseBody(new ResponseBody()); |
---|
240 | }; |
---|
241 | try { |
---|
242 | final Number notebookID = dbDispatcher.getResourceInternalIdentifier(UUID.fromString(externalIdentifier), Resource.NOTEBOOK); |
---|
243 | try { |
---|
244 | if (remotePrincipalID.equals(dbDispatcher.getNotebookOwner(notebookID)) || dbDispatcher.getTypeOfPrincipalAccount(remotePrincipalID).equals(admin)) { |
---|
245 | boolean success = dbDispatcher.updateNotebookMetadata(notebookID, notebookInfo); |
---|
246 | if (success) { |
---|
247 | return new ObjectFactory().createResponseBody(dbDispatcher.makeNotebookResponseEnvelope(notebookID)); |
---|
248 | } else { |
---|
249 | httpServletResponse.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); |
---|
250 | return new ObjectFactory().createResponseBody(new ResponseBody()); |
---|
251 | } |
---|
252 | } else { |
---|
253 | loggerServer.debug(" Ownership changing is the part of the full update of the notebook metadadata."); |
---|
254 | httpServletResponse.sendError(HttpServletResponse.SC_FORBIDDEN); |
---|
255 | return new ObjectFactory().createResponseBody(new ResponseBody()); |
---|
256 | } |
---|
257 | } catch (NotInDataBaseException e1) { |
---|
258 | loggerServer.debug(e1.toString()); |
---|
259 | httpServletResponse.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e1.toString()); |
---|
260 | return new ObjectFactory().createResponseBody(new ResponseBody()); |
---|
261 | } |
---|
262 | } catch (NotInDataBaseException e) { |
---|
263 | loggerServer.debug(e.toString()); |
---|
264 | httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, e.toString()); |
---|
265 | return new ObjectFactory().createResponseBody(new ResponseBody()); |
---|
266 | } |
---|
267 | } |
---|
268 | } |
---|