View Javadoc
1   /*
2    * Copyright 2017 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  package org.openehealth.ipf.commons.ihe.hpd;
17  
18  import org.apache.commons.lang3.StringUtils;
19  import org.openehealth.ipf.commons.core.modules.api.ValidationException;
20  import org.openehealth.ipf.commons.ihe.hpd.stub.chpidd.DownloadRequest;
21  import org.openehealth.ipf.commons.ihe.hpd.stub.chpidd.DownloadResponse;
22  import org.openehealth.ipf.commons.ihe.hpd.stub.dsmlv2.BatchRequest;
23  import org.openehealth.ipf.commons.ihe.hpd.stub.dsmlv2.BatchResponse;
24  import org.openehealth.ipf.commons.ihe.hpd.stub.dsmlv2.DsmlMessage;
25  import org.openehealth.ipf.commons.ihe.hpd.stub.dsmlv2.ErrorResponse.ErrorType;
26  import org.openehealth.ipf.commons.ihe.hpd.stub.dsmlv2.SearchRequest;
27  import org.openehealth.ipf.commons.xml.XsdValidator;
28  
29  import javax.naming.InvalidNameException;
30  import javax.naming.ldap.LdapName;
31  import javax.naming.ldap.Rdn;
32  import javax.xml.bind.JAXBContext;
33  import javax.xml.bind.JAXBException;
34  import javax.xml.bind.util.JAXBSource;
35  import javax.xml.transform.Source;
36  
37  /**
38   * @author Dmytro Rud
39   */
40  public class HpdValidator {
41      private static final JAXBContext JAXB_CONTEXT;
42      static {
43          try {
44              JAXB_CONTEXT = JAXBContext.newInstance(
45                      org.openehealth.ipf.commons.ihe.hpd.stub.dsmlv2.ObjectFactory.class,
46                      org.openehealth.ipf.commons.ihe.hpd.stub.chpidd.ObjectFactory.class);
47          } catch (JAXBException e) {
48              throw new RuntimeException(e);
49          }
50      }
51  
52      private static final XsdValidator XSD_VALIDATOR = new XsdValidator();
53  
54      private void check(boolean condition, String message) {
55          if (! condition) {
56              throw new HpdException(message, ErrorType.MALFORMED_REQUEST);
57          }
58      }
59  
60      private static void validateWithXsd(Object object, String schemaName) {
61          try {
62              Source source = new JAXBSource(JAXB_CONTEXT, object);
63              XSD_VALIDATOR.validate(source, schemaName);
64          } catch (ValidationException e) {
65              throw new HpdException(e, ErrorType.MALFORMED_REQUEST);
66          } catch (Exception e) {
67              throw new HpdException(e, ErrorType.OTHER);
68          }
69      }
70  
71      public void validateBatchRequest(BatchRequest request) {
72          validateWithXsd(request, "/schema/DSMLv2.xsd");
73          check(request.getBatchRequests() != null, "Request list is null");
74          check(!request.getBatchRequests().isEmpty(), "Request list is empty");
75          for(DsmlMessage dsml : request.getBatchRequests()) {
76              check(dsml instanceof SearchRequest, "Only SearchRequests are supported");
77              validateSearchRequest((SearchRequest) dsml);
78          }
79      }
80  
81      private void validateSearchRequest(SearchRequest request) {
82          try {
83              LdapName ldapName = new LdapName(request.getDn());
84              boolean oCheck = false;
85              boolean dcCheck = false;
86              for (Rdn rdn : ldapName.getRdns()) {
87                  String value = (String) rdn.getValue();
88                  switch (rdn.getType().toLowerCase()) {
89                      case "o":
90                          oCheck = StringUtils.isNotBlank(value);
91                          break;
92                      case "dc":
93                          dcCheck = "HPD".equals(value);
94                          break;
95                  }
96              }
97              check(oCheck, "Missing DN.O");
98              check(dcCheck, "DN.DC not equal to 'HPD'");
99          } catch (InvalidNameException e) {
100             throw new HpdException(e, ErrorType.MALFORMED_REQUEST);
101         }
102     }
103 
104     public void validateBatchResponse(BatchResponse response) {
105         validateWithXsd(response, "/schema/DSMLv2.xsd");
106     }
107 
108     public void validateDownloadRequest(DownloadRequest request) {
109         validateWithXsd(request, "/schema/PIDD.xsd");
110     }
111 
112     public void validateDownloadResponse(DownloadResponse response) {
113         validateWithXsd(response, "/schema/PIDD.xsd");
114     }
115 }