View Javadoc
1   /*
2    * Copyright 2009 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.xds.core.validate.responses;
17  
18  import org.openehealth.ipf.commons.core.modules.api.Validator;
19  import org.openehealth.ipf.commons.ihe.xds.core.ebxml.EbXMLRetrieveDocumentSetResponse;
20  import org.openehealth.ipf.commons.ihe.xds.core.requests.DocumentReference;
21  import org.openehealth.ipf.commons.ihe.xds.core.responses.RetrievedDocument;
22  import org.openehealth.ipf.commons.ihe.xds.core.validate.HomeCommunityIdValidator;
23  import org.openehealth.ipf.commons.ihe.xds.core.validate.ValidationProfile;
24  
25  import static org.apache.commons.lang3.Validate.notNull;
26  import static org.openehealth.ipf.commons.ihe.xds.core.validate.ValidationMessage.DOC_ID_MUST_BE_SPECIFIED;
27  import static org.openehealth.ipf.commons.ihe.xds.core.validate.ValidationMessage.MIME_TYPE_MUST_BE_SPECIFIED;
28  import static org.openehealth.ipf.commons.ihe.xds.core.validate.ValidationMessage.MISSING_DOCUMENT_FOR_DOC_ENTRY;
29  import static org.openehealth.ipf.commons.ihe.xds.core.validate.ValidationMessage.ON_DEMAND_DOC_ID_MUST_DIFFER;
30  import static org.openehealth.ipf.commons.ihe.xds.core.validate.ValidationMessage.REPO_ID_MUST_BE_SPECIFIED;
31  import static org.openehealth.ipf.commons.ihe.xds.core.validate.ValidatorAssertions.metaDataAssert;
32  
33  /**
34   * Validates {@link EbXMLRetrieveDocumentSetResponse}.
35   *
36   * @author Jens Riemschneider
37   */
38  public class RetrieveDocumentSetResponseValidator implements Validator<EbXMLRetrieveDocumentSetResponse, ValidationProfile> {
39      private final RegistryResponseValidator regResponseValidator = new RegistryResponseValidator();
40      private final HomeCommunityIdValidator hcValidator = new HomeCommunityIdValidator(true);
41  
42      @Override
43      public void validate(EbXMLRetrieveDocumentSetResponse response, ValidationProfile profile) {
44          notNull(response, "response cannot be null");
45  
46          regResponseValidator.validate(response, profile);
47  
48          for (RetrievedDocument doc : response.getDocuments()) {
49              DocumentReference requestData = doc.getRequestData();
50  
51              String repoId = requestData.getRepositoryUniqueId();
52              metaDataAssert(repoId != null && !repoId.isEmpty(), REPO_ID_MUST_BE_SPECIFIED);
53  
54              String docId = requestData.getDocumentUniqueId();
55              metaDataAssert(docId != null && !docId.isEmpty(), DOC_ID_MUST_BE_SPECIFIED);
56  
57              String newDocId = doc.getNewDocumentUniqueId();
58              metaDataAssert(!docId.equals(newDocId), ON_DEMAND_DOC_ID_MUST_DIFFER);
59  
60              String mimeType = doc.getMimeType();
61              metaDataAssert(mimeType != null && !mimeType.isEmpty(), MIME_TYPE_MUST_BE_SPECIFIED);
62  
63              if (profile.getInteractionProfile().requiresHomeCommunityId()) {
64                  hcValidator.validate(requestData.getHomeCommunityId());
65              }
66  
67              metaDataAssert(doc.getDataHandler() != null, MISSING_DOCUMENT_FOR_DOC_ENTRY, docId);
68          }
69      }
70  }