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.ForbiddenException; |
---|
24 | import eu.dasish.annotation.backend.NotInDataBaseException; |
---|
25 | import eu.dasish.annotation.backend.Resource; |
---|
26 | import eu.dasish.annotation.backend.ResourceAction; |
---|
27 | import eu.dasish.annotation.backend.dao.ILambda; |
---|
28 | import eu.dasish.annotation.schema.CachedRepresentationInfo; |
---|
29 | import eu.dasish.annotation.schema.ObjectFactory; |
---|
30 | import java.awt.image.BufferedImage; |
---|
31 | import java.io.FileInputStream; |
---|
32 | import java.io.IOException; |
---|
33 | import java.io.InputStream; |
---|
34 | import java.net.URL; |
---|
35 | import java.sql.SQLException; |
---|
36 | import java.util.HashMap; |
---|
37 | import java.util.Map; |
---|
38 | import javax.imageio.ImageIO; |
---|
39 | import javax.servlet.http.HttpServletRequest; |
---|
40 | import javax.servlet.http.HttpServletResponse; |
---|
41 | import javax.ws.rs.Consumes; |
---|
42 | import javax.ws.rs.GET; |
---|
43 | import javax.ws.rs.PUT; |
---|
44 | import javax.ws.rs.Path; |
---|
45 | import javax.ws.rs.PathParam; |
---|
46 | import javax.ws.rs.Produces; |
---|
47 | import javax.ws.rs.core.MediaType; |
---|
48 | import javax.xml.bind.JAXBElement; |
---|
49 | import javax.xml.parsers.ParserConfigurationException; |
---|
50 | import org.springframework.stereotype.Component; |
---|
51 | import org.springframework.transaction.annotation.Transactional; |
---|
52 | |
---|
53 | /** |
---|
54 | * |
---|
55 | * @author olhsha |
---|
56 | */ |
---|
57 | @Component |
---|
58 | @Path("/cached") |
---|
59 | @Transactional(rollbackFor = {Exception.class, SQLException.class, IOException.class, ParserConfigurationException.class}) |
---|
60 | public class CachedRepresentationResource extends ResourceResource { |
---|
61 | |
---|
62 | public void setHttpRequest(HttpServletRequest request) { |
---|
63 | this.httpServletRequest = request; |
---|
64 | } |
---|
65 | |
---|
66 | @GET |
---|
67 | @Produces(MediaType.TEXT_XML) |
---|
68 | @Path("{cachedid: " + BackendConstants.regExpIdentifier + "}/metadata") |
---|
69 | @Transactional(readOnly = true) |
---|
70 | public JAXBElement<CachedRepresentationInfo> getCachedRepresentationInfo(@PathParam("cachedid") String externalId) throws IOException { |
---|
71 | Map params = new HashMap(); |
---|
72 | try { |
---|
73 | CachedRepresentationInfo result = (CachedRepresentationInfo) (new RequestWrappers(this)).wrapRequestResource(params, new GetCachedRepresentationInfo(), Resource.CACHED_REPRESENTATION, ResourceAction.READ, externalId); |
---|
74 | if (result != null) { |
---|
75 | return (new ObjectFactory()).createCashedRepresentationInfo(result); |
---|
76 | } else { |
---|
77 | return (new ObjectFactory()).createCashedRepresentationInfo(new CachedRepresentationInfo()); |
---|
78 | } |
---|
79 | } catch (NotInDataBaseException e1) { |
---|
80 | httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, e1.getMessage()); |
---|
81 | return new ObjectFactory().createCashedRepresentationInfo(new CachedRepresentationInfo()); |
---|
82 | } catch (ForbiddenException e2) { |
---|
83 | httpServletResponse.sendError(HttpServletResponse.SC_FORBIDDEN, e2.getMessage()); |
---|
84 | return new ObjectFactory().createCashedRepresentationInfo(new CachedRepresentationInfo()); |
---|
85 | } |
---|
86 | } |
---|
87 | |
---|
88 | private class GetCachedRepresentationInfo implements ILambda<Map, CachedRepresentationInfo> { |
---|
89 | |
---|
90 | @Override |
---|
91 | public CachedRepresentationInfo apply(Map params) throws NotInDataBaseException { |
---|
92 | Number cachedID = (Number) params.get("internalID"); |
---|
93 | return dbDispatcher.getCachedRepresentationInfo(cachedID); |
---|
94 | } |
---|
95 | } |
---|
96 | |
---|
97 | //////////////////////////////////////////// |
---|
98 | @GET |
---|
99 | @Produces({"image/jpeg", "image/png"}) |
---|
100 | @Path("{cachedid: " + BackendConstants.regExpIdentifier + "}/content") |
---|
101 | @Transactional(readOnly = true) |
---|
102 | public BufferedImage getCachedRepresentationContent(@PathParam("cachedid") String externalId) throws IOException { |
---|
103 | Map params = new HashMap(); |
---|
104 | try { |
---|
105 | InputStream result = (InputStream) (new RequestWrappers(this)).wrapRequestResource(params, new GetCachedRepresentationInputStream(), Resource.CACHED_REPRESENTATION, ResourceAction.READ, externalId); |
---|
106 | if (result != null) { |
---|
107 | ImageIO.setUseCache(false); |
---|
108 | try { |
---|
109 | BufferedImage retVal = ImageIO.read(result); |
---|
110 | return retVal; |
---|
111 | } catch (IOException e1) { |
---|
112 | loggerServer.info(e1.toString()); |
---|
113 | |
---|
114 | return null; |
---|
115 | } |
---|
116 | } else { |
---|
117 | loggerServer.info(" The cached representation with the id " + externalId + " has null blob."); |
---|
118 | return null; |
---|
119 | } |
---|
120 | } catch (NotInDataBaseException e1) { |
---|
121 | httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, e1.getMessage()); |
---|
122 | return null; |
---|
123 | } catch (ForbiddenException e2) { |
---|
124 | httpServletResponse.sendError(HttpServletResponse.SC_FORBIDDEN, e2.getMessage()); |
---|
125 | return null; |
---|
126 | } |
---|
127 | |
---|
128 | } |
---|
129 | |
---|
130 | @GET |
---|
131 | //@Produces({"text/plain", "text/html", "text/xml", "application/zip", "image/png", "image/jpg"}) |
---|
132 | @Path("{cachedid: " + BackendConstants.regExpIdentifier + "}/stream") |
---|
133 | @Transactional(readOnly = true) |
---|
134 | public InputStream getCachedRepresentationContentStream(@PathParam("cachedid") String externalId) throws IOException { |
---|
135 | Map params = new HashMap(); |
---|
136 | try { |
---|
137 | return (InputStream) (new RequestWrappers(this)).wrapRequestResource(params, new GetCachedRepresentationInputStream(), Resource.CACHED_REPRESENTATION, ResourceAction.READ, externalId); |
---|
138 | } catch (NotInDataBaseException e1) { |
---|
139 | httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, e1.getMessage()); |
---|
140 | return null; |
---|
141 | } catch (ForbiddenException e2) { |
---|
142 | httpServletResponse.sendError(HttpServletResponse.SC_FORBIDDEN, e2.getMessage()); |
---|
143 | return null; |
---|
144 | } |
---|
145 | } |
---|
146 | |
---|
147 | private class GetCachedRepresentationInputStream implements ILambda<Map, InputStream> { |
---|
148 | |
---|
149 | @Override |
---|
150 | public InputStream apply(Map params) throws NotInDataBaseException { |
---|
151 | Number cachedID = (Number) params.get("internalID"); |
---|
152 | return dbDispatcher.getCachedRepresentationBlob(cachedID); |
---|
153 | } |
---|
154 | } |
---|
155 | |
---|
156 | /////////////////////////////////////////////////////////////////// |
---|
157 | @PUT |
---|
158 | @Consumes("multipart/mixed") |
---|
159 | @Produces(MediaType.APPLICATION_XML) |
---|
160 | @Path("{cachedid: " + BackendConstants.regExpIdentifier + "}/stream") |
---|
161 | public String updateCachedBlob(@PathParam("cachedid") String cachedIdentifier, |
---|
162 | MultiPart multiPart) throws IOException { |
---|
163 | Map params = new HashMap(); |
---|
164 | BodyPartEntity bpe = (BodyPartEntity) multiPart.getBodyParts().get(0).getEntity(); |
---|
165 | params.put("stream", bpe.getInputStream()); |
---|
166 | try { |
---|
167 | Integer result = (Integer) (new RequestWrappers(this)).wrapRequestResource(params, new UpdateCachedBlob(), Resource.CACHED_REPRESENTATION, ResourceAction.WRITE, cachedIdentifier); |
---|
168 | if (result != null) { |
---|
169 | return result + "rows are updated"; |
---|
170 | } else { |
---|
171 | return "Nothing is updated. "; |
---|
172 | } |
---|
173 | } catch (NotInDataBaseException e1) { |
---|
174 | httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, e1.getMessage()); |
---|
175 | return e1.getMessage(); |
---|
176 | } catch (ForbiddenException e2) { |
---|
177 | httpServletResponse.sendError(HttpServletResponse.SC_FORBIDDEN, e2.getMessage()); |
---|
178 | return e2.getMessage(); |
---|
179 | } |
---|
180 | |
---|
181 | } |
---|
182 | |
---|
183 | @PUT |
---|
184 | @Consumes("text/plain") |
---|
185 | @Produces(MediaType.APPLICATION_XML) |
---|
186 | @Path("{cachedid: " + BackendConstants.regExpIdentifier + "}/path/{isurl}") |
---|
187 | public String updateCachedBlobFromFile(@PathParam("cachedid") String cachedIdentifier, |
---|
188 | @PathParam("isurl") String isURL, String blobPath) throws IOException { |
---|
189 | Map params = new HashMap(); |
---|
190 | InputStream input; |
---|
191 | |
---|
192 | if (isURL.equals("URL")) { |
---|
193 | URL blob = new URL(blobPath); |
---|
194 | input = blob.openStream(); |
---|
195 | } else { |
---|
196 | input = new FileInputStream(blobPath); |
---|
197 | } |
---|
198 | |
---|
199 | params.put("stream", input); |
---|
200 | try { |
---|
201 | Integer result = (Integer) (new RequestWrappers(this)).wrapRequestResource(params, new UpdateCachedBlob(), Resource.CACHED_REPRESENTATION, ResourceAction.WRITE, cachedIdentifier); |
---|
202 | input.close(); |
---|
203 | if (result != null) { |
---|
204 | return result + "rows are updated"; |
---|
205 | } else { |
---|
206 | return "Nothing is updated. "; |
---|
207 | } |
---|
208 | } catch (NotInDataBaseException e1) { |
---|
209 | httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, e1.getMessage()); |
---|
210 | return e1.getMessage(); |
---|
211 | } catch (ForbiddenException e2) { |
---|
212 | httpServletResponse.sendError(HttpServletResponse.SC_FORBIDDEN, e2.getMessage()); |
---|
213 | return e2.getMessage(); |
---|
214 | } |
---|
215 | |
---|
216 | } |
---|
217 | |
---|
218 | private class UpdateCachedBlob implements ILambda<Map, Integer> { |
---|
219 | |
---|
220 | @Override |
---|
221 | public Integer apply(Map params) throws NotInDataBaseException { |
---|
222 | Number cachedID = (Number) params.get("internalID"); |
---|
223 | InputStream stream = (InputStream) params.get("stream"); |
---|
224 | try { |
---|
225 | return dbDispatcher.updateCachedBlob(cachedID, stream); |
---|
226 | } catch (IOException e) { |
---|
227 | loggerServer.info(e.toString()); |
---|
228 | return 0; |
---|
229 | } |
---|
230 | } |
---|
231 | } |
---|
232 | |
---|
233 | /////////////////////////////////////////////////////////// |
---|
234 | @PUT |
---|
235 | @Consumes(MediaType.TEXT_XML) |
---|
236 | @Produces(MediaType.APPLICATION_XML) |
---|
237 | @Path("metadata") |
---|
238 | public String updateCachedMetadata(CachedRepresentationInfo cachedInfo) throws IOException { |
---|
239 | Map params = new HashMap(); |
---|
240 | params.put("info", cachedInfo); |
---|
241 | try { |
---|
242 | Integer result = (Integer) (new RequestWrappers(this)).wrapRequestResource(params, new UpdateCachedMetadata(), Resource.CACHED_REPRESENTATION, ResourceAction.WRITE_W_METAINFO, cachedInfo.getId()); |
---|
243 | if (result != null) { |
---|
244 | return result + "rows are updated"; |
---|
245 | } else { |
---|
246 | return "Nothing is updated. "; |
---|
247 | } |
---|
248 | } catch (NotInDataBaseException e1) { |
---|
249 | httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, e1.getMessage()); |
---|
250 | return e1.getMessage(); |
---|
251 | } catch (ForbiddenException e2) { |
---|
252 | httpServletResponse.sendError(HttpServletResponse.SC_FORBIDDEN, e2.getMessage()); |
---|
253 | return e2.getMessage(); |
---|
254 | } |
---|
255 | } |
---|
256 | |
---|
257 | private class UpdateCachedMetadata implements ILambda<Map, Integer> { |
---|
258 | |
---|
259 | @Override |
---|
260 | public Integer apply(Map params) throws NotInDataBaseException { |
---|
261 | CachedRepresentationInfo cachedInfo = (CachedRepresentationInfo) params.get("info"); |
---|
262 | return dbDispatcher.updateCachedMetada(cachedInfo); |
---|
263 | } |
---|
264 | } |
---|
265 | } |
---|