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

Last change on this file since 4603 was 4603, checked in by olhsha, 10 years ago

test commit after disaster

File size: 10.6 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.BackendConstants;
21import eu.dasish.annotation.backend.Resource;
22import eu.dasish.annotation.backend.dao.DBIntegrityService;
23import eu.dasish.annotation.schema.Notebook;
24import eu.dasish.annotation.schema.NotebookInfo;
25import eu.dasish.annotation.schema.NotebookInfoList;
26import eu.dasish.annotation.schema.ObjectFactory;
27import eu.dasish.annotation.schema.Permission;
28import eu.dasish.annotation.schema.ReferenceList;
29import eu.dasish.annotation.schema.ResponseBody;
30import java.io.IOException;
31import java.sql.SQLException;
32import java.util.UUID;
33import javax.servlet.http.HttpServletRequest;
34import javax.servlet.http.HttpServletResponse;
35import javax.ws.rs.Consumes;
36import javax.ws.rs.GET;
37import javax.ws.rs.PUT;
38import javax.ws.rs.Path;
39import javax.ws.rs.PathParam;
40import javax.ws.rs.Produces;
41import javax.ws.rs.QueryParam;
42import javax.ws.rs.core.Context;
43import javax.ws.rs.core.MediaType;
44import javax.ws.rs.core.UriInfo;
45import javax.ws.rs.ext.Providers;
46import javax.xml.bind.JAXBElement;
47import javax.xml.parsers.ParserConfigurationException;
48import org.slf4j.Logger;
49import org.slf4j.LoggerFactory;
50import org.springframework.beans.factory.annotation.Autowired;
51import org.springframework.stereotype.Component;
52import 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})
62public 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}
Note: See TracBrowser for help on using the repository browser.