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

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

Adjusting logging. Loggin null arguments is added

File size: 6.6 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.slf4j.Logger;
42import org.slf4j.LoggerFactory;
43import org.springframework.beans.factory.annotation.Autowired;
44import org.springframework.stereotype.Component;
45import org.springframework.transaction.annotation.Transactional;
46
47/**
48 *
49 * @author olhsha
50 */
51@Component
52@Path("/cached")
53@Transactional(rollbackFor = {Exception.class, SQLException.class, IOException.class, ParserConfigurationException.class})
54public class CachedRepresentationResource {
55
56    @Autowired
57    private DBIntegrityService dbIntegrityService;
58    @Context
59    private HttpServletRequest httpServletRequest;
60    @Context
61    private HttpServletResponse httpServletResponse;
62    @Context
63    private UriInfo uriInfo;
64    private final Logger logger = LoggerFactory.getLogger(CachedRepresentationResource.class);
65
66    public void setHttpRequest(HttpServletRequest request) {
67        this.httpServletRequest = request;
68    }
69
70    // TODOD both unit tests
71    //changed path, /Target/cached part is removed
72    @GET
73    @Produces(MediaType.TEXT_XML)
74    @Path("{cachedid: " + BackendConstants.regExpIdentifier + "}/metadata")
75    @Transactional(readOnly = true)
76    public JAXBElement<CachedRepresentationInfo> getCachedRepresentationInfo(@PathParam("cachedid") String externalId) throws SQLException, IOException {
77
78        String remoteUser = httpServletRequest.getRemoteUser();
79        Number remoteUserID = dbIntegrityService.getUserInternalIDFromRemoteID(remoteUser);
80        if (remoteUserID != null) {
81            dbIntegrityService.setServiceURI(uriInfo.getBaseUri().toString());
82            try {
83                final Number cachedID = dbIntegrityService.getCachedRepresentationInternalIdentifier(UUID.fromString(externalId));
84                if (cachedID != null) {
85                    final CachedRepresentationInfo cachedInfo = dbIntegrityService.getCachedRepresentationInfo(cachedID);
86                    return new ObjectFactory().createCashedRepresentationInfo(cachedInfo);
87                } else {
88                    AnnotationResource.loggerServer.debug(httpServletResponse.SC_NOT_FOUND + ": The cached representation with the given id is not found in the database");
89                    httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, "The cached representation with the given id is not found in the database");
90                    return null;
91                }
92            } catch (IllegalArgumentException e) {
93                AnnotationResource.loggerServer.debug(HttpServletResponse.SC_BAD_REQUEST + ": Illegal argument UUID " + externalId);
94                httpServletResponse.sendError(HttpServletResponse.SC_BAD_REQUEST, "Illegal argument UUID " + externalId);
95                return null;
96            }
97        } else {
98            AnnotationResource.loggerServer.debug(httpServletResponse.SC_NOT_FOUND + ": the logged-in user is not found in the database");
99            httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, "The logged in user is not found in the database");
100            return null;
101        }
102    }
103
104    @GET
105    @Produces({"image/jpeg", "image/png"})
106    @Path("{cachedid: " + BackendConstants.regExpIdentifier + "}/content")
107    @Transactional(readOnly = true)
108    public BufferedImage getCachedRepresentationContent(@PathParam("cachedid") String externalId) throws SQLException, IOException {
109        String remoteUser = httpServletRequest.getRemoteUser();
110        Number remoteUserID = dbIntegrityService.getUserInternalIDFromRemoteID(remoteUser);
111        if (remoteUserID != null) {
112            dbIntegrityService.setServiceURI(uriInfo.getBaseUri().toString());
113            try {
114                final Number cachedID = dbIntegrityService.getCachedRepresentationInternalIdentifier(UUID.fromString(externalId));
115                if (cachedID != null) {
116                    InputStream dbRespond = dbIntegrityService.getCachedRepresentationBlob(cachedID);
117                    ImageIO.setUseCache(false);
118                    BufferedImage result = ImageIO.read(dbRespond);
119                    return result;
120                } else {
121                    AnnotationResource.loggerServer.debug(httpServletResponse.SC_NOT_FOUND + "The cached representation with the given id is not found in the database");
122                    httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, "The cached representation  with the given id   " + externalId + " is not found in the database");
123                    return null;
124                }
125            } catch (IllegalArgumentException e) {
126                AnnotationResource.loggerServer.debug(HttpServletResponse.SC_BAD_REQUEST + ": Illegal argument UUID " + externalId);
127                httpServletResponse.sendError(HttpServletResponse.SC_BAD_REQUEST, "Illegal argument UUID " + externalId);
128                return null;
129            }
130        } else {
131            AnnotationResource.loggerServer.debug(httpServletResponse.SC_NOT_FOUND + ": the logged-in user is not found in the database");
132            httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, "The logged-in user is not found in the database");
133            return null;
134        }
135    }
136}
Note: See TracBrowser for help on using the repository browser.