1 /*** 2 * Copyright (C) 2009 TM-Search Community. 3 * 4 * This file is part of TM-Search Services. 5 * 6 * Foobar is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation, either version 3 of the License, or 9 * (at your option) any later version. 10 * 11 * Foobar is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with Foobar. If not, see <http://www.gnu.org/licenses/>. 18 */ 19 20 package eu.europa.tmsearch.services.resources; 21 22 import javax.ws.rs.GET; 23 import javax.ws.rs.Path; 24 import javax.ws.rs.PathParam; 25 import javax.ws.rs.core.Context; 26 import javax.ws.rs.core.HttpHeaders; 27 import javax.ws.rs.core.Request; 28 import javax.ws.rs.core.Response; 29 import javax.ws.rs.core.UriInfo; 30 31 import org.apache.log4j.Logger; 32 import org.springframework.context.annotation.Scope; 33 import org.springframework.stereotype.Component; 34 35 import eu.europa.tmsearch.services.business.TradeMarkThumbnailService; 36 import eu.europa.tmsearch.services.business.exception.BusinessServiceException; 37 import eu.europa.tmsearch.services.model.TradeMarkImage; 38 import eu.europa.tmsearch.services.resources.exceptions.ResourceNotFoundException; 39 import eu.europa.tmsearch.services.resources.util.CacheableResponseSupport; 40 41 /*** 42 * An implementation of Thumbnail resource as per TM-Search RFC v1.0 using 43 * JAX-RS 44 * 45 * 46 * @see TradeMarkImage 47 */ 48 @Path("/trademark/thumbnail") 49 @Component 50 @Scope("request") 51 public class TradeMarkThumbnailResource extends CacheableResponseSupport { 52 53 private static Logger log = Logger.getLogger(TradeMarkThumbnailResource.class); 54 55 /*** 56 * A JAX-RS request object. Injected by Jersey on each request 57 */ 58 @Context 59 private Request request; 60 61 /*** 62 * A JAX-RS UriInfo instance. Injected by Jersey on each request 63 */ 64 @Context 65 private UriInfo uriInfo; 66 67 /*** 68 * A JAX-RS HttpHeaders instance. Injected by Jersey on each request 69 */ 70 @Context 71 private HttpHeaders httpHeaders; 72 73 /*** 74 * A business service to get a TradeMark thumbnail image by its ST13 id 75 */ 76 private TradeMarkThumbnailService tradeMarkThumbnailService; 77 78 /*** 79 * An TradeMarkImage instance 80 */ 81 private TradeMarkImage image; 82 83 /*** 84 * Dereferences a Trade Mark Image resource to its binary representation by 85 * its ST13 id. 86 * 87 * @param tradeMarkId 88 * An ST13 trade mark image ID 89 * @return The trade mark image 90 * @throws {@link ResourceNotFoundException} 91 * {@link BusinessServiceException} 92 */ 93 @GET 94 @Path("/{tradeMarkId}") 95 public Object getTradeMarkThumbnail(@PathParam("tradeMarkId") final String tradeMarkId) 96 throws BusinessServiceException, ResourceNotFoundException { 97 98 if (log.isDebugEnabled()) { 99 log.debug("Retrieving Image for Trademark With ID --> " + tradeMarkId); 100 } 101 102 this.setLastModified(this.getTradeMarkThumbnailService().getLastModified(tradeMarkId)); 103 104 Response response = this.checkForStaleResource(this.request); 105 if (response != null) 106 return response; 107 108 this.image = this.getTradeMarkThumbnailService().getThumbnail(tradeMarkId); 109 this.getETagGenerator().setSourceObject(image); 110 111 response = this.checkForValidResource(this.request); 112 if (response != null) 113 return response; 114 115 this.setExpires(this.getTradeMarkThumbnailService().getExpires(tradeMarkId)); 116 117 return Response.ok(image).lastModified(this.getLastModified()).expires(this.getExpires()).tag( 118 this.getETagGenerator().getETag()).build(); 119 } 120 121 public Request getRequest() { 122 return request; 123 } 124 125 public void setRequest(Request request) { 126 this.request = request; 127 } 128 129 public UriInfo getUriInfo() { 130 return uriInfo; 131 } 132 133 public void setUriInfo(UriInfo uriInfo) { 134 this.uriInfo = uriInfo; 135 } 136 137 public HttpHeaders getHttpHeaders() { 138 return httpHeaders; 139 } 140 141 public void setHttpHeaders(HttpHeaders httpHeaders) { 142 this.httpHeaders = httpHeaders; 143 } 144 145 public TradeMarkThumbnailService getTradeMarkThumbnailService() { 146 return tradeMarkThumbnailService; 147 } 148 149 public void setTradeMarkThumbnailService(TradeMarkThumbnailService tradeMarkThumbnailService) { 150 this.tradeMarkThumbnailService = tradeMarkThumbnailService; 151 } 152 153 public TradeMarkImage getImage() { 154 return image; 155 } 156 157 public void setImage(TradeMarkImage image) { 158 this.image = image; 159 } 160 161 }