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