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

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

shibbolized

File size: 4.2 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 eu.dasish.annotation.backend.BackendConstants;
21import eu.dasish.annotation.backend.dao.DBIntegrityService;
22import eu.dasish.annotation.schema.CachedRepresentationInfo;
23import eu.dasish.annotation.schema.ObjectFactory;
24import java.awt.image.BufferedImage;
25import java.io.IOException;
26import java.io.InputStream;
27import java.sql.SQLException;
28import java.util.UUID;
29import javax.imageio.ImageIO;
30import javax.servlet.http.HttpServletRequest;
31import javax.servlet.http.HttpServletResponse;
32import javax.ws.rs.GET;
33import javax.ws.rs.Path;
34import javax.ws.rs.PathParam;
35import javax.ws.rs.Produces;
36import javax.ws.rs.core.Context;
37import javax.ws.rs.core.MediaType;
38import javax.ws.rs.core.UriInfo;
39import javax.xml.bind.JAXBElement;
40import javax.xml.parsers.ParserConfigurationException;
41import org.springframework.beans.factory.annotation.Autowired;
42import org.springframework.stereotype.Component;
43import 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})
52public class CachedRepresentationResource {
53
54    @Autowired
55    private DBIntegrityService dbIntegrityService;
56    @Context
57    private HttpServletRequest httpServletRequest;
58    @Context
59    private HttpServletResponse httpServletResponse;
60    @Context
61    private UriInfo uriInfo;
62
63    public void setHttpRequest(HttpServletRequest request) {
64        this.httpServletRequest = request;
65    }
66
67    // TODOD both unit tests
68    //changed path, /Target/cached part is removed
69    @GET
70    @Produces(MediaType.TEXT_XML)
71    @Path("{cachedid: " + BackendConstants.regExpIdentifier + "}/metadata")
72    @Transactional(readOnly = true)
73    public JAXBElement<CachedRepresentationInfo> getCachedRepresentationInfo(@PathParam("cachedid") String externalId) throws SQLException, IOException {
74        dbIntegrityService.setServiceURI(uriInfo.getBaseUri().toString());
75        final Number cachedID = dbIntegrityService.getCachedRepresentationInternalIdentifier(UUID.fromString(externalId));
76        if (cachedID != null) {
77            final CachedRepresentationInfo cachedInfo = dbIntegrityService.getCachedRepresentationInfo(cachedID);
78            return new ObjectFactory().createCashedRepresentationInfo(cachedInfo);
79        } else {
80            httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, "The cached representation with the given id is not found in the database");
81            return null;
82        }
83    }
84
85    @GET
86    @Produces({"image/jpeg", "image/png"})
87    @Path("{cachedid: " + BackendConstants.regExpIdentifier + "}/content")
88    @Transactional(readOnly = true)
89    public BufferedImage getCachedRepresentationContent(@PathParam("cachedid") String externalId) throws SQLException, IOException {
90        dbIntegrityService.setServiceURI(uriInfo.getBaseUri().toString());
91        final Number cachedID = dbIntegrityService.getCachedRepresentationInternalIdentifier(UUID.fromString(externalId));
92        if (cachedID != null) {
93            InputStream dbRespond = dbIntegrityService.getCachedRepresentationBlob(cachedID);
94            ImageIO.setUseCache(false);
95            BufferedImage result = ImageIO.read(dbRespond);
96            return result;
97        } else {
98            httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, "The cached representation  with the given id is not found in the database");
99            return null;
100        }
101    }
102}
Note: See TracBrowser for help on using the repository browser.