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

Last change on this file since 3949 was 3949, checked in by olhsha, 11 years ago

login/logout and secure REST-method calls works. Further: adapt getting/putting/posting according to permissions of the logged-in user. Adn next: making a list of all currently on-line (logged-in) users from different sessions.

File size: 3.4 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.ws.rs.GET;
32import javax.ws.rs.Path;
33import javax.ws.rs.PathParam;
34import javax.ws.rs.Produces;
35import javax.ws.rs.core.Context;
36import javax.ws.rs.core.MediaType;
37import javax.ws.rs.core.UriInfo;
38import javax.xml.bind.JAXBElement;
39import org.springframework.beans.factory.annotation.Autowired;
40import org.springframework.security.access.annotation.Secured;
41import org.springframework.stereotype.Component;
42
43/**
44 *
45 * @author olhsha
46 */
47@Component
48@Path("/cached")
49public class CachedRepresentationResource {
50
51    @Autowired
52    private DBIntegrityService dbIntegrityService;
53    @Context
54    private HttpServletRequest httpServletRequest;
55    @Context
56    private UriInfo uriInfo;
57
58    public void setHttpRequest(HttpServletRequest request) {
59        this.httpServletRequest = request;
60    }
61
62    // TODOD both unit tests
63    //changed path, /Target/cached part is removed
64    @GET
65    @Produces(MediaType.TEXT_XML)
66    @Path("{cachedid: " + BackendConstants.regExpIdentifier + "}/metadata")
67    @Secured("ROLE_USER")
68    public JAXBElement<CachedRepresentationInfo> getCachedRepresentationInfo(@PathParam("cachedid") String externalId) throws SQLException {
69        dbIntegrityService.setServiceURI(uriInfo.getBaseUri().toString());
70        final Number cachedID = dbIntegrityService.getCachedRepresentationInternalIdentifier(UUID.fromString(externalId));
71        final CachedRepresentationInfo cachedInfo = dbIntegrityService.getCachedRepresentationInfo(cachedID);
72        return new ObjectFactory().createCashedRepresentationInfo(cachedInfo);
73    }
74
75    @GET
76    @Produces({"image/jpeg", "image/png"})
77    @Path("{cachedid: " + BackendConstants.regExpIdentifier + "}/content")
78    @Secured("ROLE_USER")
79    public BufferedImage getCachedRepresentationContent(@PathParam("cachedid") String externalId) throws SQLException, IOException {
80        dbIntegrityService.setServiceURI(uriInfo.getBaseUri().toString());
81        final Number cachedID = dbIntegrityService.getCachedRepresentationInternalIdentifier(UUID.fromString(externalId));
82        InputStream dbRespond = dbIntegrityService.getCachedRepresentationBlob(cachedID);
83        ImageIO.setUseCache(false);
84        BufferedImage result = ImageIO.read(dbRespond);
85        return result;
86    }
87   
88   
89}
Note: See TracBrowser for help on using the repository browser.