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

Last change on this file since 4028 was 4028, checked in by olhsha, 11 years ago

updating body of the annotation. Testing Get methods and verifying their produced xml-s. Ok.

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