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

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

shibbolized

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