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

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

test commit after disaster

File size: 7.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.Resource;
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.ws.rs.Consumes;
34import javax.ws.rs.DELETE;
35import javax.ws.rs.GET;
36import javax.ws.rs.POST;
37import javax.ws.rs.Path;
38import javax.ws.rs.PathParam;
39import javax.ws.rs.Produces;
40import javax.ws.rs.core.MediaType;
41import javax.xml.bind.JAXBElement;
42import javax.xml.parsers.ParserConfigurationException;
43import org.springframework.stereotype.Component;
44import org.springframework.transaction.annotation.Transactional;
45
46/**
47 *
48 * @author olhsha
49 */
50/**
51 *
52 * @author olhsha
53 */
54@Component
55@Path("/targets")
56@Transactional(rollbackFor = {Exception.class, SQLException.class, IOException.class, ParserConfigurationException.class})
57public class TargetResource extends ResourceResource {
58
59    public void setHttpRequest(HttpServletRequest request) {
60        this.httpServletRequest = request;
61    }
62
63    public TargetResource() {
64    }
65
66    // TODOD both unit tests
67    @GET
68    @Produces(MediaType.TEXT_XML)
69    @Path("{targetid: " + BackendConstants.regExpIdentifier + "}")
70    @Transactional(readOnly = true)
71    public JAXBElement<Target> getTarget(@PathParam("targetid") String externalIdentifier) throws SQLException, IOException {
72        Number remoteUserID = this.getUserID();
73        if (remoteUserID != null) {
74            try {
75                final Number targetID = dbIntegrityService.getResourceInternalIdentifier(UUID.fromString(externalIdentifier), Resource.TARGET);
76                if (targetID != null) {
77                    final Target target = dbIntegrityService.getTarget(targetID);
78                    return new ObjectFactory().createTarget(target);
79                } else {
80                    verboseOutput.TARGET_NOT_FOUND(externalIdentifier);
81                }
82            } catch (IllegalArgumentException e) {
83                verboseOutput.ILLEGAL_UUID(externalIdentifier);
84            }
85        }
86        return new ObjectFactory().createTarget(new Target());
87    }
88
89    // TODOD both unit tests
90    @GET
91    @Produces(MediaType.TEXT_XML)
92    @Path("{targetid: " + BackendConstants.regExpIdentifier + "}/versions")
93    @Transactional(readOnly = true)
94    public JAXBElement<ReferenceList> getSiblingTargets(@PathParam("targetid") String externalIdentifier) throws SQLException, IOException {
95        Number remoteUserID = this.getUserID();
96        if (remoteUserID != null) {
97            try {
98                final Number targetID = dbIntegrityService.getResourceInternalIdentifier(UUID.fromString(externalIdentifier), Resource.TARGET);
99                if (targetID != null) {
100                    final ReferenceList siblings = dbIntegrityService.getTargetsForTheSameLinkAs(targetID);
101                    return new ObjectFactory().createReferenceList(siblings);
102                } else {
103                    verboseOutput.TARGET_NOT_FOUND(externalIdentifier);
104                }
105            } catch (IllegalArgumentException e) {
106                verboseOutput.ILLEGAL_UUID(externalIdentifier);
107            }
108        }
109        return new ObjectFactory().createReferenceList(new ReferenceList());
110    }
111
112    @POST
113    @Consumes("multipart/mixed")
114    @Produces(MediaType.APPLICATION_XML)
115    @Path("{targetid: " + BackendConstants.regExpIdentifier + "}/fragment/{fragmentDescriptor}/cached")
116    public JAXBElement<CachedRepresentationInfo> postCached(@PathParam("targetid") String targetIdentifier,
117            @PathParam("fragmentDescriptor") String fragmentDescriptor,
118            MultiPart multiPart) throws SQLException, IOException {
119        Number remoteUserID = this.getUserID();
120        if (remoteUserID != null) {
121            try {
122                final Number targetID = dbIntegrityService.getResourceInternalIdentifier(UUID.fromString(targetIdentifier), Resource.TARGET);
123                if (targetID != null) {
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                } else {
131                    verboseOutput.TARGET_NOT_FOUND(targetIdentifier);
132                }
133            } catch (IllegalArgumentException e) {
134                verboseOutput.ILLEGAL_UUID(targetIdentifier);
135            }
136        }
137        return new ObjectFactory().createCashedRepresentationInfo(new CachedRepresentationInfo());
138    }
139
140    @DELETE
141    @Path("{targetid: " + BackendConstants.regExpIdentifier + "}/cached/{cachedid: " + BackendConstants.regExpIdentifier + "}")
142    public String deleteCachedForTarget(@PathParam("targetid") String targetExternalIdentifier,
143            @PathParam("cachedid") String cachedExternalIdentifier) throws SQLException, IOException {
144        Number remoteUserID = this.getUserID();
145        if (remoteUserID != null) {
146            try {
147                final Number targetID = dbIntegrityService.getResourceInternalIdentifier(UUID.fromString(targetExternalIdentifier), Resource.TARGET);
148                if (targetID != null) {
149                    final Number cachedID = dbIntegrityService.getResourceInternalIdentifier(UUID.fromString(cachedExternalIdentifier), Resource.CACHED_REPRESENTATION);
150                    if (cachedID != null) {
151                        int[] resultDelete = dbIntegrityService.deleteCachedRepresentationOfTarget(targetID, cachedID);
152                        String result = Integer.toString(resultDelete[0]);
153                        return result + " pair(s) target-cached deleted.";
154                    } else {
155                        verboseOutput.CACHED_REPRESENTATION_NOT_FOUND(cachedExternalIdentifier);
156                    }
157                } else {
158                    verboseOutput.TARGET_NOT_FOUND(targetExternalIdentifier);
159                }
160            } catch (IllegalArgumentException e) {
161                verboseOutput.ILLEGAL_UUID(targetExternalIdentifier);
162            }
163        }
164        return " ";
165    }
166}
Note: See TracBrowser for help on using the repository browser.