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

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

files INSTALL, UPDATED, CHANGES and README are corrected. The bug with the wrong server diagnostic (403 instead of 404), when a resource's give id is not found, is fixed.

File size: 4.3 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.security.access.annotation.Secured;
43import org.springframework.stereotype.Component;
44import org.springframework.transaction.annotation.Transactional;
45
46/**
47 *
48 * @author olhsha
49 */
50@Component
51@Path("/cached")
52@Transactional(rollbackFor = {Exception.class, SQLException.class, IOException.class, ParserConfigurationException.class})
53public class CachedRepresentationResource {
54
55    @Autowired
56    private DBIntegrityService dbIntegrityService;
57    @Context
58    private HttpServletRequest httpServletRequest;
59    @Context
60    private HttpServletResponse httpServletResponse;
61    @Context
62    private UriInfo uriInfo;
63
64    public void setHttpRequest(HttpServletRequest request) {
65        this.httpServletRequest = request;
66    }
67
68    // TODOD both unit tests
69    //changed path, /Target/cached part is removed
70    @GET
71    @Produces(MediaType.TEXT_XML)
72    @Path("{cachedid: " + BackendConstants.regExpIdentifier + "}/metadata")
73    @Secured("ROLE_USER")
74    @Transactional(readOnly = true)
75    public JAXBElement<CachedRepresentationInfo> getCachedRepresentationInfo(@PathParam("cachedid") String externalId) throws SQLException, IOException {
76        dbIntegrityService.setServiceURI(uriInfo.getBaseUri().toString());
77        final Number cachedID = dbIntegrityService.getCachedRepresentationInternalIdentifier(UUID.fromString(externalId));
78        if (cachedID != null) {
79            final CachedRepresentationInfo cachedInfo = dbIntegrityService.getCachedRepresentationInfo(cachedID);
80            return new ObjectFactory().createCashedRepresentationInfo(cachedInfo);
81        } else {
82            httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, "The cached representation with the given id is not found in the database");
83            return null;
84        }
85    }
86
87    @GET
88    @Produces({"image/jpeg", "image/png"})
89    @Path("{cachedid: " + BackendConstants.regExpIdentifier + "}/content")
90    @Secured("ROLE_USER")
91    @Transactional(readOnly = true)
92    public BufferedImage getCachedRepresentationContent(@PathParam("cachedid") String externalId) throws SQLException, IOException {
93        dbIntegrityService.setServiceURI(uriInfo.getBaseUri().toString());
94        final Number cachedID = dbIntegrityService.getCachedRepresentationInternalIdentifier(UUID.fromString(externalId));
95        if (cachedID != null) {
96            InputStream dbRespond = dbIntegrityService.getCachedRepresentationBlob(cachedID);
97            ImageIO.setUseCache(false);
98            BufferedImage result = ImageIO.read(dbRespond);
99            return result;
100        } else {
101            httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, "The cached representation  with the given id is not found in the database");
102            return null;
103        }
104    }
105}
Note: See TracBrowser for help on using the repository browser.