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.IOException; |
---|
29 | import java.io.InputStream; |
---|
30 | import java.sql.SQLException; |
---|
31 | import java.util.UUID; |
---|
32 | import javax.servlet.http.HttpServletRequest; |
---|
33 | import javax.servlet.http.HttpServletResponse; |
---|
34 | import javax.ws.rs.Consumes; |
---|
35 | import javax.ws.rs.DELETE; |
---|
36 | import javax.ws.rs.GET; |
---|
37 | import javax.ws.rs.POST; |
---|
38 | import javax.ws.rs.Path; |
---|
39 | import javax.ws.rs.PathParam; |
---|
40 | import javax.ws.rs.Produces; |
---|
41 | import javax.ws.rs.core.Context; |
---|
42 | import javax.ws.rs.core.MediaType; |
---|
43 | import javax.ws.rs.core.UriInfo; |
---|
44 | import javax.xml.bind.JAXBElement; |
---|
45 | import javax.xml.parsers.ParserConfigurationException; |
---|
46 | import org.springframework.beans.factory.annotation.Autowired; |
---|
47 | import org.springframework.stereotype.Component; |
---|
48 | import 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}) |
---|
61 | public 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 | } |
---|