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 eu.dasish.annotation.backend.BackendConstants; |
---|
21 | import eu.dasish.annotation.backend.dao.DBIntegrityService; |
---|
22 | import eu.dasish.annotation.schema.CachedRepresentationInfo; |
---|
23 | import eu.dasish.annotation.schema.ObjectFactory; |
---|
24 | import java.awt.image.BufferedImage; |
---|
25 | import java.io.IOException; |
---|
26 | import java.io.InputStream; |
---|
27 | import java.sql.SQLException; |
---|
28 | import java.util.UUID; |
---|
29 | import javax.imageio.ImageIO; |
---|
30 | import javax.servlet.http.HttpServletRequest; |
---|
31 | import javax.ws.rs.GET; |
---|
32 | import javax.ws.rs.Path; |
---|
33 | import javax.ws.rs.PathParam; |
---|
34 | import javax.ws.rs.Produces; |
---|
35 | import javax.ws.rs.core.Context; |
---|
36 | import javax.ws.rs.core.MediaType; |
---|
37 | import javax.ws.rs.core.UriInfo; |
---|
38 | import javax.xml.bind.JAXBElement; |
---|
39 | import javax.xml.parsers.ParserConfigurationException; |
---|
40 | import org.springframework.beans.factory.annotation.Autowired; |
---|
41 | import org.springframework.security.access.annotation.Secured; |
---|
42 | import org.springframework.stereotype.Component; |
---|
43 | import org.springframework.transaction.annotation.Transactional; |
---|
44 | |
---|
45 | /** |
---|
46 | * |
---|
47 | * @author olhsha |
---|
48 | */ |
---|
49 | @Component |
---|
50 | @Path("/cached") |
---|
51 | @Transactional(rollbackFor={Exception.class, SQLException.class, IOException.class, ParserConfigurationException.class}) |
---|
52 | public class CachedRepresentationResource { |
---|
53 | |
---|
54 | @Autowired |
---|
55 | private DBIntegrityService dbIntegrityService; |
---|
56 | @Context |
---|
57 | private HttpServletRequest httpServletRequest; |
---|
58 | @Context |
---|
59 | private UriInfo uriInfo; |
---|
60 | |
---|
61 | public void setHttpRequest(HttpServletRequest request) { |
---|
62 | this.httpServletRequest = request; |
---|
63 | } |
---|
64 | |
---|
65 | // TODOD both unit tests |
---|
66 | //changed path, /Target/cached part is removed |
---|
67 | @GET |
---|
68 | @Produces(MediaType.TEXT_XML) |
---|
69 | @Path("{cachedid: " + BackendConstants.regExpIdentifier + "}/metadata") |
---|
70 | @Secured("ROLE_USER") |
---|
71 | @Transactional(readOnly=true) |
---|
72 | public JAXBElement<CachedRepresentationInfo> getCachedRepresentationInfo(@PathParam("cachedid") String externalId) throws SQLException { |
---|
73 | dbIntegrityService.setServiceURI(uriInfo.getBaseUri().toString()); |
---|
74 | final Number cachedID = dbIntegrityService.getCachedRepresentationInternalIdentifier(UUID.fromString(externalId)); |
---|
75 | final CachedRepresentationInfo cachedInfo = dbIntegrityService.getCachedRepresentationInfo(cachedID); |
---|
76 | return new ObjectFactory().createCashedRepresentationInfo(cachedInfo); |
---|
77 | } |
---|
78 | |
---|
79 | @GET |
---|
80 | @Produces({"image/jpeg", "image/png"}) |
---|
81 | @Path("{cachedid: " + BackendConstants.regExpIdentifier + "}/content") |
---|
82 | @Secured("ROLE_USER") |
---|
83 | @Transactional(readOnly=true) |
---|
84 | public BufferedImage getCachedRepresentationContent(@PathParam("cachedid") String externalId) throws SQLException, IOException { |
---|
85 | dbIntegrityService.setServiceURI(uriInfo.getBaseUri().toString()); |
---|
86 | final Number cachedID = dbIntegrityService.getCachedRepresentationInternalIdentifier(UUID.fromString(externalId)); |
---|
87 | InputStream dbRespond = dbIntegrityService.getCachedRepresentationBlob(cachedID); |
---|
88 | ImageIO.setUseCache(false); |
---|
89 | BufferedImage result = ImageIO.read(dbRespond); |
---|
90 | return result; |
---|
91 | } |
---|
92 | |
---|
93 | |
---|
94 | } |
---|