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

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

Testing. fixing little bug in cacher resource.

File size: 4.5 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.Resource;
22import eu.dasish.annotation.backend.dao.DBIntegrityService;
23import eu.dasish.annotation.schema.CachedRepresentationInfo;
24import eu.dasish.annotation.schema.ObjectFactory;
25import java.awt.image.BufferedImage;
26import java.io.IOException;
27import java.io.InputStream;
28import java.sql.SQLException;
29import java.util.UUID;
30import javax.imageio.ImageIO;
31import javax.servlet.http.HttpServletRequest;
32import javax.servlet.http.HttpServletResponse;
33import javax.ws.rs.GET;
34import javax.ws.rs.Path;
35import javax.ws.rs.PathParam;
36import javax.ws.rs.Produces;
37import javax.ws.rs.core.Context;
38import javax.ws.rs.core.MediaType;
39import javax.ws.rs.core.UriInfo;
40import javax.xml.bind.JAXBElement;
41import javax.xml.parsers.ParserConfigurationException;
42import org.slf4j.Logger;
43import org.slf4j.LoggerFactory;
44import org.springframework.beans.factory.annotation.Autowired;
45import org.springframework.stereotype.Component;
46import org.springframework.transaction.annotation.Transactional;
47
48/**
49 *
50 * @author olhsha
51 */
52@Component
53@Path("/cached")
54@Transactional(rollbackFor = {Exception.class, SQLException.class, IOException.class, ParserConfigurationException.class})
55public class CachedRepresentationResource extends ResourceResource {
56
57    public void setHttpRequest(HttpServletRequest request) {
58        this.httpServletRequest = request;
59    }
60
61    // TODOD both unit tests
62    //changed path, /Target/cached part is removed
63    @GET
64    @Produces(MediaType.TEXT_XML)
65    @Path("{cachedid: " + BackendConstants.regExpIdentifier + "}/metadata")
66    @Transactional(readOnly = true)
67    public JAXBElement<CachedRepresentationInfo> getCachedRepresentationInfo(@PathParam("cachedid") String externalId) throws SQLException, IOException {
68        Number remoteUserID = this.getUserID();
69        if (remoteUserID != null) {
70            try {
71                final Number cachedID = dbIntegrityService.getResourceInternalIdentifier(UUID.fromString(externalId), Resource.CACHED_REPRESENTATION);
72                if (cachedID != null) {
73                    final CachedRepresentationInfo cachedInfo = dbIntegrityService.getCachedRepresentationInfo(cachedID);
74                    return new ObjectFactory().createCashedRepresentationInfo(cachedInfo);
75                } else {
76                    verboseOutput.CACHED_REPRESENTATION_NOT_FOUND(externalId);
77                }
78            } catch (IllegalArgumentException e) {
79                verboseOutput.ILLEGAL_UUID(externalId);
80            }
81        }
82        return new ObjectFactory().createCashedRepresentationInfo(new CachedRepresentationInfo());
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        Number remoteUserID = this.getUserID();
91        if (remoteUserID != null) {
92            try {
93                final Number cachedID = dbIntegrityService.getResourceInternalIdentifier(UUID.fromString(externalId), Resource.CACHED_REPRESENTATION);
94                if (cachedID != null) {
95                    InputStream dbRespond = dbIntegrityService.getCachedRepresentationBlob(cachedID);
96                    ImageIO.setUseCache(false);
97                    BufferedImage result = ImageIO.read(dbRespond);
98                    return result;
99                } else {
100                    verboseOutput.CACHED_REPRESENTATION_NOT_FOUND(externalId);
101                }
102            } catch (IllegalArgumentException e) {
103                verboseOutput.ILLEGAL_UUID(externalId);
104            }
105        } 
106        return null;
107    }
108}
Note: See TracBrowser for help on using the repository browser.