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 | */ |
---|
18 | package eu.dasish.annotation.backend.rest; |
---|
19 | |
---|
20 | import com.sun.jersey.multipart.BodyPartEntity; |
---|
21 | import com.sun.jersey.multipart.MultiPart; |
---|
22 | import eu.dasish.annotation.backend.BackendConstants; |
---|
23 | import eu.dasish.annotation.backend.dao.DBIntegrityService; |
---|
24 | import eu.dasish.annotation.schema.CachedRepresentationInfo; |
---|
25 | import eu.dasish.annotation.schema.ObjectFactory; |
---|
26 | import eu.dasish.annotation.schema.ReferenceList; |
---|
27 | import eu.dasish.annotation.schema.Target; |
---|
28 | import java.io.InputStream; |
---|
29 | import java.sql.SQLException; |
---|
30 | import java.util.UUID; |
---|
31 | import javax.servlet.http.HttpServletRequest; |
---|
32 | import javax.ws.rs.Consumes; |
---|
33 | import javax.ws.rs.DELETE; |
---|
34 | import javax.ws.rs.GET; |
---|
35 | import javax.ws.rs.POST; |
---|
36 | import javax.ws.rs.Path; |
---|
37 | import javax.ws.rs.PathParam; |
---|
38 | import javax.ws.rs.Produces; |
---|
39 | import javax.ws.rs.core.Context; |
---|
40 | import javax.ws.rs.core.MediaType; |
---|
41 | import javax.ws.rs.core.UriInfo; |
---|
42 | import javax.xml.bind.JAXBElement; |
---|
43 | import org.springframework.beans.factory.annotation.Autowired; |
---|
44 | import org.springframework.security.access.annotation.Secured; |
---|
45 | import org.springframework.stereotype.Component; |
---|
46 | |
---|
47 | /** |
---|
48 | * |
---|
49 | * @author olhsha |
---|
50 | */ |
---|
51 | /** |
---|
52 | * |
---|
53 | * @author olhsha |
---|
54 | */ |
---|
55 | @Component |
---|
56 | @Path("/targets") |
---|
57 | public 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 | } |
---|