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.TradeMarkImageService; 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 Image resource as per TM-Search RFC v1.0 using JAX-RS 43 * 44 * 45 * @see TradeMarkImage 46 */ 47 @Path("/trademark/image") 48 @Component 49 @Scope("request") 50 public class TradeMarkImageResource extends CacheableResponseSupport { 51 52 private static Logger log = Logger.getLogger(TradeMarkImageResource.class); 53 54 /*** 55 * A JAX-RS request object. Injected by Jersey on each request 56 */ 57 @Context 58 private Request request; 59 60 /*** 61 * A JAX-RS UriInfo instance. Injected by Jersey on each request 62 */ 63 @Context 64 private UriInfo uriInfo; 65 66 /*** 67 * A JAX-RS HttpHeaders instance. Injected by Jersey on each request 68 */ 69 @Context 70 private HttpHeaders httpHeaders; 71 72 /*** 73 * A TradeMarkImageService service to get a TradeMark image by ST13 ID 74 */ 75 private TradeMarkImageService tradeMarkImageService; 76 77 /*** 78 * An TradeMarkImage instance 79 */ 80 private TradeMarkImage image; 81 82 /*** 83 * Dereferences a Trade Mark Image resource to its binary representation by its ST13 id. 84 * 85 * @param tradeMarkId 86 * An ST13 trade mark image ID 87 * @return The trade mark image 88 * @throws {@link ResourceNotFoundException} 89 * {@link BusinessServiceException} 90 */ 91 @GET 92 @Path("/{tradeMarkId}") 93 public Object getTradeMarkImage(@PathParam("tradeMarkId") String tradeMarkId) 94 throws BusinessServiceException, ResourceNotFoundException { 95 if (log.isDebugEnabled()) { 96 log.debug("Retrieving Image for Trademark With ID --> " + tradeMarkId); 97 } 98 99 this.setLastModified(this.getTradeMarkImageService().getLastModified(tradeMarkId)); 100 101 Response response = this.checkForStaleResource(this.request); 102 if (response != null) 103 return response; 104 105 this.image = this.getTradeMarkImageService().getImage(tradeMarkId); 106 this.getETagGenerator().setSourceObject(image); 107 108 response = this.checkForValidResource(this.request); 109 if (response != null) 110 return response; 111 112 this.setExpires(this.getTradeMarkImageService().getExpires(tradeMarkId)); 113 114 return Response.ok(image).lastModified(this.getLastModified()).expires(this.getExpires()).tag( 115 this.getETagGenerator().getETag()).build(); 116 } 117 118 public Request getRequest() { 119 return request; 120 } 121 122 public void setRequest(Request request) { 123 this.request = request; 124 } 125 126 public UriInfo getUriInfo() { 127 return uriInfo; 128 } 129 130 public void setUriInfo(UriInfo uriInfo) { 131 this.uriInfo = uriInfo; 132 } 133 134 public HttpHeaders getHttpHeaders() { 135 return httpHeaders; 136 } 137 138 public void setHttpHeaders(HttpHeaders httpHeaders) { 139 this.httpHeaders = httpHeaders; 140 } 141 142 public TradeMarkImageService getTradeMarkImageService() { 143 return tradeMarkImageService; 144 } 145 146 public void setTradeMarkImageService(TradeMarkImageService tradeMarkImageService) { 147 this.tradeMarkImageService = tradeMarkImageService; 148 } 149 150 public TradeMarkImage getImage() { 151 return image; 152 } 153 154 public void setImage(TradeMarkImage image) { 155 this.image = image; 156 } 157 158 }