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

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

files INSTALL, UPDATED, CHANGES and README are corrected. The bug with the wrong server diagnostic (403 instead of 404), when a resource's give id is not found, is fixed.

File size: 8.0 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 com.sun.jersey.multipart.BodyPartEntity;
21import com.sun.jersey.multipart.MultiPart;
22import eu.dasish.annotation.backend.BackendConstants;
23import eu.dasish.annotation.backend.dao.DBIntegrityService;
24import eu.dasish.annotation.schema.CachedRepresentationInfo;
25import eu.dasish.annotation.schema.ObjectFactory;
26import eu.dasish.annotation.schema.ReferenceList;
27import eu.dasish.annotation.schema.Target;
28import java.io.IOException;
29import java.io.InputStream;
30import java.sql.SQLException;
31import java.util.UUID;
32import javax.servlet.http.HttpServletRequest;
33import javax.servlet.http.HttpServletResponse;
34import javax.ws.rs.Consumes;
35import javax.ws.rs.DELETE;
36import javax.ws.rs.GET;
37import javax.ws.rs.POST;
38import javax.ws.rs.Path;
39import javax.ws.rs.PathParam;
40import javax.ws.rs.Produces;
41import javax.ws.rs.core.Context;
42import javax.ws.rs.core.MediaType;
43import javax.ws.rs.core.UriInfo;
44import javax.xml.bind.JAXBElement;
45import javax.xml.parsers.ParserConfigurationException;
46import org.springframework.beans.factory.annotation.Autowired;
47import org.springframework.security.access.annotation.Secured;
48import org.springframework.stereotype.Component;
49import org.springframework.transaction.annotation.Transactional;
50
51/**
52 *
53 * @author olhsha
54 */
55/**
56 *
57 * @author olhsha
58 */
59@Component
60@Path("/targets")
61@Transactional(rollbackFor = {Exception.class, SQLException.class, IOException.class, ParserConfigurationException.class})
62public class TargetResource {
63
64    @Autowired
65    private DBIntegrityService dbIntegrityService;
66    @Context
67    private HttpServletRequest httpServletRequest;
68    @Context
69    private HttpServletResponse httpServletResponse;
70    @Context
71    private UriInfo uriInfo;
72
73    public void setHttpRequest(HttpServletRequest request) {
74        this.httpServletRequest = request;
75    }
76
77    public TargetResource() {
78    }
79
80    // TODOD both unit tests
81    @GET
82    @Produces(MediaType.TEXT_XML)
83    @Path("{targetid: " + BackendConstants.regExpIdentifier + "}")
84    @Secured("ROLE_USER")
85    @Transactional(readOnly = true)
86    public JAXBElement<Target> getTarget(@PathParam("targetid") String ExternalIdentifier) throws SQLException, IOException {
87        dbIntegrityService.setServiceURI(uriInfo.getBaseUri().toString());
88        final Number targetID = dbIntegrityService.getTargetInternalIdentifier(UUID.fromString(ExternalIdentifier));
89        if (targetID != null) {
90            final Target target = dbIntegrityService.getTarget(targetID);
91            return new ObjectFactory().createTarget(target);
92        } else {
93            httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, "The target with the given id is not found in the database");
94            return null;
95        }
96    }
97
98    // TODOD both unit tests
99    @GET
100    @Produces(MediaType.TEXT_XML)
101    @Path("{targetid: " + BackendConstants.regExpIdentifier + "}/versions")
102    @Secured("ROLE_USER")
103    @Transactional(readOnly = true)
104    public JAXBElement<ReferenceList> getSiblingTargets(@PathParam("targetid") String ExternalIdentifier) throws SQLException, IOException {
105        dbIntegrityService.setServiceURI(uriInfo.getBaseUri().toString());
106        final Number targetID = dbIntegrityService.getTargetInternalIdentifier(UUID.fromString(ExternalIdentifier));
107        if (targetID != null) {
108            final ReferenceList siblings = dbIntegrityService.getTargetsForTheSameLinkAs(targetID);
109            return new ObjectFactory().createReferenceList(siblings);
110        } else {
111            httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, "The target with the given id is not found in the database");
112            return null;
113        }
114    }
115
116// TODO both unit tests
117//changed path, /Targetpart is removed
118//how to overwork the input stream to make it downloadable
119// using mime type as well
120//    @DELETE
121//    @Produces(MediaType.TEXT_XML)
122//    @Path("{targetid: " + BackendConstants.regExpIdentifier + "}/cached/{cachedid: " + BackendConstants.regExpIdentifier + "}")
123//    @Secured("ROLE_ADMIN")
124//    public int deleteCached(@PathParam("targetid") String targetIdentifier, @PathParam("cachedid") String cachedIdentifier) throws SQLException {
125//        dbIntegrityService.setServiceURI(uriInfo.getBaseUri().toString());
126//        final Number targetID = dbIntegrityService.getCachedRepresentationInternalIdentifier(UUID.fromString(targetIdentifier));
127//        final Number cachedID = dbIntegrityService.getCachedRepresentationInternalIdentifier(UUID.fromString(cachedIdentifier));
128//        int[] result = dbIntegrityService.deleteCachedRepresentationOfTarget(targetID, cachedID);
129//        return result[1];
130//    }
131    @POST
132    @Consumes("multipart/mixed")
133    @Produces(MediaType.APPLICATION_XML)
134    @Path("{targetid: " + BackendConstants.regExpIdentifier + "}/fragment/{fragmentDescriptor}/cached")
135    @Secured("ROLE_USER")
136    public JAXBElement<CachedRepresentationInfo> postCached(@PathParam("targetid") String targetIdentifier,
137            @PathParam("fragmentDescriptor") String fragmentDescriptor,
138            MultiPart multiPart) throws SQLException {
139        dbIntegrityService.setServiceURI(uriInfo.getBaseUri().toString());
140        final Number targetID = dbIntegrityService.getTargetInternalIdentifier(UUID.fromString(targetIdentifier));
141        CachedRepresentationInfo metadata = multiPart.getBodyParts().get(0).getEntityAs(CachedRepresentationInfo.class);
142        BodyPartEntity bpe = (BodyPartEntity) multiPart.getBodyParts().get(1).getEntity();
143        InputStream cachedSource = bpe.getInputStream();
144        final Number[] respondDB = dbIntegrityService.addCachedForTarget(targetID, fragmentDescriptor, metadata, cachedSource);
145        final CachedRepresentationInfo cachedInfo = dbIntegrityService.getCachedRepresentationInfo(respondDB[1]);
146        return new ObjectFactory()
147                .createCashedRepresentationInfo(cachedInfo);
148
149    }
150
151    @DELETE
152    @Path("{targetid: " + BackendConstants.regExpIdentifier + "}/cached/{cachedid: " + BackendConstants.regExpIdentifier + "}")
153    @Secured("ROLE_ADMIN")
154    public String deleteCachedForTarget(@PathParam("targetid") String targetExternalIdentifier,
155            @PathParam("cachedid") String cachedExternalIdentifier) throws SQLException, IOException {
156        dbIntegrityService.setServiceURI(uriInfo.getBaseUri().toString());
157        final Number targetID = dbIntegrityService.getTargetInternalIdentifier(UUID.fromString(targetExternalIdentifier));
158        if (targetID != null) {
159            final Number cachedID = dbIntegrityService.getCachedRepresentationInternalIdentifier(UUID.fromString(cachedExternalIdentifier));
160            if (cachedID != null) {
161                int[] resultDelete = dbIntegrityService.deleteCachedRepresentationOfTarget(targetID, cachedID);
162                String result = Integer.toString(resultDelete[0]);
163                return result + " pair(s) target-cached deleted.";
164            } else {
165                httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, "The cached representation with the given id is not found in the database");
166                return null;
167            }
168        } else {
169            httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, "The target with the given id is not found in the database");
170            return null;
171        }
172    }
173}
Note: See TracBrowser for help on using the repository browser.