View Javadoc
1   /*
2    * Copyright 2015 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *       http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.openehealth.ipf.commons.ihe.fhir.iti83;
18  
19  import ca.uhn.fhir.rest.annotation.IdParam;
20  import ca.uhn.fhir.rest.annotation.Operation;
21  import ca.uhn.fhir.rest.annotation.OperationParam;
22  import ca.uhn.fhir.rest.param.TokenParam;
23  import ca.uhn.fhir.rest.param.UriParam;
24  import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException;
25  import org.hl7.fhir.dstu3.model.IdType;
26  import org.hl7.fhir.dstu3.model.Identifier;
27  import org.hl7.fhir.dstu3.model.Parameters;
28  import org.hl7.fhir.dstu3.model.Patient;
29  import org.hl7.fhir.dstu3.model.UriType;
30  import org.openehealth.ipf.commons.ihe.fhir.AbstractPlainProvider;
31  
32  import javax.servlet.http.HttpServletRequest;
33  import javax.servlet.http.HttpServletResponse;
34  
35  import static org.openehealth.ipf.commons.ihe.fhir.Constants.SOURCE_IDENTIFIER_NAME;
36  import static org.openehealth.ipf.commons.ihe.fhir.Constants.TARGET_SYSTEM_NAME;
37  
38  /**
39   * According to the PIXM specification, this resource provider must handle requests in the form
40   * GET [base]/Patient/$ihe-pix?sourceIdentifier=[token]]{&targetSystem=[uri]}{&_format=[mime-type]}
41   *
42   * @author Christian Ohr
43   * @since 3.4
44   */
45  public class Iti83ResourceProvider extends AbstractPlainProvider {
46  
47      /**
48       * Handles the PIXm Query
49       *
50       * @param sourceIdentifierParam Identifier to search for. Should be an {@link Identifier}, but obviously
51       *                              non-primitive types are forbidden in GET operations
52       * @param targetSystemParam     target system URI
53       * @return {@link Parameters} containing found identifiers
54       */
55      @SuppressWarnings("unused")
56      @Operation(name = Iti83Constants.PIXM_OPERATION_NAME, type = Patient.class, idempotent = true, returnParameters = {@OperationParam(name = "return", type = Identifier.class, max = 100)})
57      public Parameters pixmQuery(
58              @OperationParam(name = SOURCE_IDENTIFIER_NAME) TokenParam sourceIdentifierParam,
59              @OperationParam(name = TARGET_SYSTEM_NAME) UriParam targetSystemParam,
60              HttpServletRequest httpServletRequest,
61              HttpServletResponse httpServletResponse) {
62  
63          Identifier sourceIdentifier = new Identifier()
64                  .setSystem(sourceIdentifierParam.getSystem())
65                  .setValue(sourceIdentifierParam.getValue());
66          UriType targetUri = targetSystemParam == null ? null : new UriType(targetSystemParam.getValue());
67  
68          Parameters inParams = new Parameters();
69          inParams.addParameter().setName(SOURCE_IDENTIFIER_NAME).setValue(sourceIdentifier);
70          inParams.addParameter().setName(TARGET_SYSTEM_NAME).setValue(targetUri);
71  
72          // Run down the route
73          return requestResource(inParams, Parameters.class, httpServletRequest, httpServletResponse);
74      }
75  
76      /**
77       * Handles the PIXm Retrieve. Note that this is not part of the specification, but a useful variant that allows to use resource IDs
78       * in requests such as .../Patient/4711/$ihe-pix would be equivalent with .../Patient/$ihe-pix?sourceIdentifier=URI|4711 where URI
79       * identifies the NamingSystem used for resource IDs
80       *
81       * @param resourceId          resource ID
82       * @param targetSystemParam   target system URI
83       * @param httpServletRequest  servlet request
84       * @param httpServletResponse servlet response
85       * @return {@link Parameters} containing found identifiers
86       */
87      @Operation(name = Iti83Constants.PIXM_OPERATION_NAME, type = Patient.class, idempotent = true, returnParameters = {@OperationParam(name = "return", type = Identifier.class, max = 100)})
88      public Parameters pixmRetrieve(
89              @IdParam IdType resourceId,
90              @OperationParam(name = TARGET_SYSTEM_NAME) UriParam targetSystemParam,
91              HttpServletRequest httpServletRequest,
92              HttpServletResponse httpServletResponse) {
93          if (resourceId == null) throw new InvalidRequestException("Must provide ID with READ request");
94          UriType targetUri = targetSystemParam == null ? null : new UriType(targetSystemParam.getValue());
95  
96          Parameters inParams = new Parameters();
97          inParams.addParameter().setName(SOURCE_IDENTIFIER_NAME).setValue(new Identifier().setValue(resourceId.getIdPart()));
98          inParams.addParameter().setName(TARGET_SYSTEM_NAME).setValue(targetUri);
99  
100         // Run down the route
101         return requestResource(inParams, Parameters.class, httpServletRequest, httpServletResponse);
102     }
103 }