View Javadoc
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 java.util.Date;
23  import java.util.Iterator;
24  
25  import javax.ws.rs.Consumes;
26  import javax.ws.rs.GET;
27  import javax.ws.rs.Path;
28  import javax.ws.rs.PathParam;
29  import javax.ws.rs.Produces;
30  import javax.ws.rs.core.Context;
31  import javax.ws.rs.core.HttpHeaders;
32  import javax.ws.rs.core.Request;
33  import javax.ws.rs.core.Response;
34  import javax.ws.rs.core.UriInfo;
35  import javax.xml.datatype.XMLGregorianCalendar;
36  
37  import org.apache.log4j.Logger;
38  import org.springframework.context.annotation.Scope;
39  import org.springframework.stereotype.Component;
40  
41  import eu.europa.tmsearch.services.business.TradeMarkService;
42  import eu.europa.tmsearch.services.business.exception.BusinessServiceException;
43  import eu.europa.tmsearch.services.model.TradeMarkImage;
44  import eu.europa.tmsearch.services.resources.exceptions.ResourceNotFoundException;
45  import eu.europa.tmsearch.services.resources.util.CacheableResponseSupport;
46  import eu.europa.tmsearch.services.schemas.trademark.data.KeyType;
47  import eu.europa.tmsearch.services.schemas.trademark.data.TMSearchTransactionCodeType;
48  import eu.europa.tmsearch.services.schemas.trademark.data.TradeMarkType;
49  import eu.europa.tmsearch.services.schemas.trademark.data.Transaction;
50  import eu.europa.tmsearch.services.schemas.trademark.data.TransactionBodyType;
51  import eu.europa.tmsearch.services.schemas.trademark.data.TransactionDataType;
52  import eu.europa.tmsearch.services.schemas.trademark.data.TransactionHeaderType;
53  import eu.europa.tmsearch.services.schemas.trademark.data.TransactionBodyType.TransactionContentDetails;
54  import eu.europa.tmsearch.services.schemas.trademark.data.TransactionDataType.TradeMarkDetails;
55  import eu.europa.tmsearch.services.schemas.trademark.data.TransactionHeaderType.SenderDetails;
56  
57  /***
58   * A implementation of TradeMark resource as per TM-Search RFC v1.0 using JAX-RS
59   * 
60   * 
61   * @see TradeMarkImage
62   * @see TradeMark
63   */
64  @Path("/trademark/data")
65  @Component
66  @Scope("request")
67  public class TradeMarkResource extends CacheableResponseSupport {
68  
69      private static Logger log = Logger.getLogger(TradeMarkResource.class);
70  
71      /***
72       * A JAX-RS request object. Injected by Jersey on each request
73       */
74      @Context
75      private Request request;
76  
77      /***
78       * A JAX-RS UriInfo instance. Injected by Jersey on each request
79       */
80      @Context
81      private UriInfo uriInfo;
82  
83      /***
84       * A JAX-RS HttpHeaders instance. Injected by Jersey on each request
85       */
86      @Context
87      private HttpHeaders httpHeaders;
88  
89      /***
90       * A TradeMarkService to get a TradeMark by ApplicationNumber
91       */
92      private TradeMarkService tradeMarkService;
93  
94      /***
95       * An ApplicantType instance
96       */
97      private TradeMarkType tradeMark;
98  
99      /***
100      * A String holding the enumeration value for the corresponding transaction
101      * code, depending on the office specific XML schema
102      */
103     private String transactionCode;
104     
105     /***
106      * Dereferences a Trade Mark Data resource to its TM-Search XML
107      * representation by its id.
108      * 
109      * @param tradeMarkId
110      *            A ST13 trade mark ID
111      * @return A TM-View XML Trade Mark Data representation of the Trade Mark
112      *         data
113      * @throws {@link ResourceNotFoundException}
114      *         {@link BusinessServiceException}
115      */
116     @GET
117     @Path("{tradeMarkId}")
118     @Consumes("application/xml")
119     @Produces("application/xml")
120     public Response getTradeMark(@PathParam("tradeMarkId") String tradeMarkId)
121 	    throws BusinessServiceException, ResourceNotFoundException {
122 	if (log.isDebugEnabled()) {
123 	    log.debug("Retrieving Trademark With ID --> " + tradeMarkId);
124 	    log.debug("Headers --> " + httpHeaders.getRequestHeaders());
125 	}
126 
127 	this.setLastModified(this.getTradeMarkService().getLastModified(tradeMarkId));
128 
129 	Response response = this.checkForStaleResource(this.request);
130 	if (response != null)
131 	    return response;
132 
133 	this.tradeMark = this.getTradeMarkService().getTradeMark(tradeMarkId);
134 	this.getETagGenerator().setSourceObject(tradeMark);
135 
136 	response = this.checkForValidResource(this.request);
137 	if (response != null)
138 	    return response;
139 
140 	this.setExpires(this.getTradeMarkService().getExpires(tradeMarkId));
141 
142 	return Response.ok(this.buildResource()).lastModified(this.getLastModified()).expires(
143 		this.getExpires()).tag(this.getETagGenerator().getETag()).build();
144     }
145 
146     /***
147      * Used to inject a service for retrieving a Trade Mark for the underlying
148      * data store.
149      * 
150      * @param tradeMarkService
151      *            Service to access to a Trade Mark
152      */
153     public void setTradeMarkService(TradeMarkService tradeMarkService) {
154 	this.tradeMarkService = tradeMarkService;
155     }
156 
157     public TradeMarkService getTradeMarkService() {
158 	return tradeMarkService;
159     }
160 
161     public Request getRequest() {
162 	return request;
163     }
164 
165     public void setRequest(Request request) {
166 	this.request = request;
167     }
168 
169     public UriInfo getUriInfo() {
170 	return uriInfo;
171     }
172 
173     public void setUriInfo(UriInfo uriInfo) {
174 	this.uriInfo = uriInfo;
175     }
176 
177     public HttpHeaders getHttpHeaders() {
178 	return httpHeaders;
179     }
180 
181     public void setHttpHeaders(HttpHeaders httpHeaders) {
182 	this.httpHeaders = httpHeaders;
183     }
184 
185     public TradeMarkType getTradeMark() {
186 	return tradeMark;
187     }
188 
189     public void setTradeMark(TradeMarkType tradeMark) {
190 	this.tradeMark = tradeMark;
191     }
192 
193     public String getTransactionCode() {
194         return transactionCode;
195     }
196 
197     public void setTransactionCode(String transactionCode) {
198         this.transactionCode = transactionCode;
199     }
200 
201     private Object buildResource() {
202 	TradeMarkDetails tradeMarkDetails = new TradeMarkDetails();
203 	tradeMarkDetails.setTradeMark(tradeMark);
204 	TransactionDataType transactionDataType = new TransactionDataType();
205 	transactionDataType.setTradeMarkDetails(tradeMarkDetails);
206 	TransactionContentDetails transactionContentDetails = new TransactionContentDetails();
207 	transactionContentDetails.setTransactionCode(TMSearchTransactionCodeType.fromValue(transactionCode));
208 	transactionContentDetails.setTransactionData(transactionDataType);
209 	TransactionBodyType transactionBodyType = new TransactionBodyType();
210 	transactionBodyType.setTransactionContentDetails(transactionContentDetails);
211 
212 	SenderDetails senderDetails = new SenderDetails();
213 	TransactionHeaderType transactionHeaderType = new TransactionHeaderType();
214 	transactionHeaderType.setSenderDetails(senderDetails);
215 	Transaction transaction = new Transaction();
216 	transaction.setTradeMarkTransactionBody(transactionBodyType);
217 	transaction.setTransactionHeader(transactionHeaderType);
218 
219 	if (tradeMarkDetails.getTradeMark().getMarkImageDetails() != null) {
220 	    tradeMarkDetails.getTradeMark().getMarkImageDetails().getMarkImage().getMarkImageURI().setValue(
221 		    uriInfo.getBaseUriBuilder().path(TradeMarkImageResource.class).build().toString()
222 			    + "/"
223 			    + tradeMarkDetails.getTradeMark().getMarkImageDetails().getMarkImage()
224 				    .getMarkImageURI().getValue());
225 	}
226 
227 	for (Iterator<KeyType> applicantKeyList = tradeMarkDetails.getTradeMark().getApplicantDetails()
228 		.getApplicantKeies().iterator(); applicantKeyList.hasNext();) {
229 	    KeyType applicantKey = applicantKeyList.next();
230 	    applicantKey.getURI().setValue(
231 		    uriInfo.getBaseUriBuilder().path(ApplicantResource.class).build().toString() + "/"
232 			    + applicantKey.getURI().getValue());
233 	}
234 
235 	XMLGregorianCalendar dateTimeStamp = getDateTimeStamp(this.getLastModified());
236 	transactionContentDetails.setTransactionIdentifier(uriInfo.getAbsolutePath().toString());
237 	senderDetails.setRequestProducerDateTime(dateTimeStamp);
238 
239 	return transaction;
240     }
241 
242 }