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  
24  import javax.ws.rs.Consumes;
25  import javax.ws.rs.GET;
26  import javax.ws.rs.Path;
27  import javax.ws.rs.PathParam;
28  import javax.ws.rs.Produces;
29  import javax.ws.rs.core.Context;
30  import javax.ws.rs.core.HttpHeaders;
31  import javax.ws.rs.core.Request;
32  import javax.ws.rs.core.Response;
33  import javax.ws.rs.core.UriInfo;
34  import javax.xml.datatype.XMLGregorianCalendar;
35  
36  import org.apache.log4j.Logger;
37  import org.springframework.context.annotation.Scope;
38  import org.springframework.stereotype.Component;
39  
40  import eu.europa.tmsearch.services.business.RepresentativeService;
41  import eu.europa.tmsearch.services.business.exception.BusinessServiceException;
42  import eu.europa.tmsearch.services.model.TradeMarkImage;
43  import eu.europa.tmsearch.services.resources.exceptions.ResourceNotFoundException;
44  import eu.europa.tmsearch.services.resources.util.CacheableResponseSupport;
45  import eu.europa.tmsearch.services.schemas.trademark.representative.RepresentativeType;
46  import eu.europa.tmsearch.services.schemas.trademark.representative.TMSearchTransactionCodeType;
47  import eu.europa.tmsearch.services.schemas.trademark.representative.Transaction;
48  import eu.europa.tmsearch.services.schemas.trademark.representative.TransactionBodyType;
49  import eu.europa.tmsearch.services.schemas.trademark.representative.TransactionDataType;
50  import eu.europa.tmsearch.services.schemas.trademark.representative.TransactionHeaderType;
51  import eu.europa.tmsearch.services.schemas.trademark.representative.TransactionBodyType.TransactionContentDetails;
52  import eu.europa.tmsearch.services.schemas.trademark.representative.TransactionDataType.RepresentativeDetails;
53  import eu.europa.tmsearch.services.schemas.trademark.representative.TransactionHeaderType.SenderDetails;
54  
55  /***
56   * A implementtation of Applicant resource as per TM-Search RFC v1.0 using
57   * JAX-RS
58   * 
59   * 
60   * @see TradeMarkImage
61   * @see TradeMark
62   */
63  @Path("/trademark/representative")
64  @Component
65  @Scope("request")
66  public class RepresentativeResource extends CacheableResponseSupport {
67  
68      private static Logger log = Logger.getLogger(RepresentativeResource.class);
69  
70      /***
71       * A JAX-RS request object. Injected by Jersey on each request
72       */
73      @Context
74      private Request request;
75  
76      /***
77       * A JAX-RS UriInfo instance. Injected by Jersey on each request
78       */
79      @Context
80      private UriInfo uriInfo;
81  
82      /***
83       * A JAX-RS HttpHeaders instance. Injected by Jersey on each request
84       */
85      @Context
86      private HttpHeaders httpHeaders;
87  
88      /***
89       * A RepresentativeService to get a representative by ID
90       */
91      private RepresentativeService representativeService;
92  
93      /***
94       * An ApplicantType instance
95       */
96      private RepresentativeType representative;
97  
98      /***
99       * A String holding the enumeration value for the corresponding transaction
100      * code, depending on the office specific XML schema
101      */
102     private String transactionCode;
103 
104     /***
105      * Dereferences a Representative resource to TM-Search XML representation by
106      * its id.
107      * 
108      * @param representativeId
109      *            A representative id
110      * @return A TM-View XML Applicant representation of the representative data
111      * @throws {@link ResourceNotFoundException}
112      *         {@link BusinessServiceException}
113      */
114     @GET
115     @Path("/{representativeId}")
116     @Consumes("application/xml")
117     @Produces("application/xml")
118     public Response getRepresentative(@PathParam("representativeId") String representativeId)
119 	    throws BusinessServiceException, ResourceNotFoundException {
120 	if (log.isDebugEnabled()) {
121 	    log.debug("Retrieving Representative With ID --> " + representativeId);
122 	}
123 
124 	this.setLastModified(this.getRepresentativeService().getLastModified(representativeId));
125 
126 	Response response = this.checkForStaleResource(this.request);
127 	if (response != null)
128 	    return response;
129 
130 	this.representative = this.getRepresentativeService().getRepresentative(representativeId);
131 	this.getETagGenerator().setSourceObject(representative);
132 
133 	response = this.checkForValidResource(this.request);
134 	if (response != null)
135 	    return response;
136 
137 	this.setExpires(this.getRepresentativeService().getExpires(representativeId));
138 
139 	return Response.ok(this.buildResource()).lastModified(this.getLastModified()).expires(
140 		this.getExpires()).tag(this.getETagGenerator().getETag()).build();
141     }
142 
143     public Request getRequest() {
144 	return request;
145     }
146 
147     public void setRequest(Request request) {
148 	this.request = request;
149     }
150 
151     public UriInfo getUriInfo() {
152 	return uriInfo;
153     }
154 
155     public void setUriInfo(UriInfo uriInfo) {
156 	this.uriInfo = uriInfo;
157     }
158 
159     public HttpHeaders getHttpHeaders() {
160 	return httpHeaders;
161     }
162 
163     public void setHttpHeaders(HttpHeaders httpHeaders) {
164 	this.httpHeaders = httpHeaders;
165     }
166 
167     public RepresentativeService getRepresentativeService() {
168 	return representativeService;
169     }
170 
171     public void setRepresentativeService(RepresentativeService representativeService) {
172 	this.representativeService = representativeService;
173     }
174 
175     public RepresentativeType getRepresentative() {
176 	return representative;
177     }
178 
179     public void setRepresentative(RepresentativeType representative) {
180 	this.representative = representative;
181     }
182 
183     public String getTransactionCode() {
184         return transactionCode;
185     }
186 
187     public void setTransactionCode(String transactionCode) {
188         this.transactionCode = transactionCode;
189     }
190 
191     private Object buildResource() {
192 	RepresentativeDetails representativeDetails = new RepresentativeDetails();
193 	representativeDetails.getRepresentatives().add(representative);
194 	TransactionDataType transactionDataType = new TransactionDataType();
195 	transactionDataType.setRepresentativeDetails(representativeDetails);
196 	TransactionContentDetails transactionContentDetails = new TransactionContentDetails();
197 	transactionContentDetails.setTransactionCode(TMSearchTransactionCodeType.fromValue(transactionCode));
198 	transactionContentDetails.setTransactionData(transactionDataType);
199 	TransactionBodyType transactionBodyType = new TransactionBodyType();
200 	transactionBodyType.setTransactionContentDetails(transactionContentDetails);
201 
202 	SenderDetails senderDetails = new SenderDetails();
203 	TransactionHeaderType transactionHeaderType = new TransactionHeaderType();
204 	transactionHeaderType.setSenderDetails(senderDetails);
205 	Transaction transaction = new Transaction();
206 	transaction.setTradeMarkTransactionBody(transactionBodyType);
207 	transaction.setTransactionHeader(transactionHeaderType);
208 
209 	XMLGregorianCalendar dateTimeStamp = getDateTimeStamp(getLastModified());
210 	transactionContentDetails.setTransactionIdentifier(uriInfo.getAbsolutePath().toString());
211 	senderDetails.setRequestProducerDateTime(dateTimeStamp);
212 	return transaction;
213     }
214 
215 }