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.ws;
17  
18  import org.apache.cxf.frontend.ServerFactoryBean;
19  import org.apache.cxf.interceptor.InterceptorProvider;
20  import org.openehealth.ipf.commons.audit.AuditContext;
21  import org.openehealth.ipf.commons.ihe.core.atna.AuditStrategy;
22  import org.openehealth.ipf.commons.ihe.ws.correlation.AsynchronyCorrelator;
23  import org.openehealth.ipf.commons.ihe.ws.cxf.audit.AuditResponseInterceptor;
24  import org.openehealth.ipf.commons.ihe.ws.cxf.audit.WsAuditDataset;
25  import org.openehealth.ipf.commons.ihe.ws.cxf.payload.InPayloadExtractorInterceptor;
26  
27  import static java.util.Objects.requireNonNull;
28  import static org.openehealth.ipf.commons.ihe.ws.cxf.payload.StringPayloadHolder.PayloadType.SOAP_BODY;
29  
30  /**
31   * Factory for Web Services serving asynchronous responses
32   */
33  public class JaxWsAsyncResponseServiceFactory<AuditDatasetType extends WsAuditDataset> extends JaxWsServiceFactory<AuditDatasetType> {
34  
35      protected final AsynchronyCorrelator<AuditDatasetType> correlator;
36  
37      /**
38       * Constructs the factory.
39       * @param wsTransactionConfiguration
40       *          the info about the service to produce.
41       * @param serviceAddress
42       *          the address of the service that it should be published with.
43       * @param auditStrategy
44       *          server-side ATNA audit strategy.
45       * @param customInterceptors
46       *          user-defined custom CXF interceptors.
47       * @param correlator
48       *          correlator for asynchronous interactions.
49       */
50      public JaxWsAsyncResponseServiceFactory(
51              WsTransactionConfiguration<AuditDatasetType> wsTransactionConfiguration,
52              String serviceAddress,
53              AuditStrategy<AuditDatasetType> auditStrategy,
54              AuditContext auditContext,
55              InterceptorProvider customInterceptors,
56              AsynchronyCorrelator<AuditDatasetType> correlator)
57      {
58          super(wsTransactionConfiguration, serviceAddress, auditStrategy, auditContext,
59                  customInterceptors, null);
60  
61          requireNonNull(correlator, "Correlator for asynchronous processing must be set.");
62          this.correlator = correlator;
63      }
64  
65      @Override
66      protected void configureInterceptors(ServerFactoryBean svrFactory) {
67          super.configureInterceptors(svrFactory);
68  
69          // install auditing-related interceptors if the user has not switched auditing off
70          if (auditStrategy != null) {
71              if (wsTransactionConfiguration.isAuditRequestPayload()) {
72                  svrFactory.getInInterceptors().add(new InPayloadExtractorInterceptor(SOAP_BODY));
73              }
74  
75              AuditResponseInterceptor<AuditDatasetType> auditInterceptor =
76                      new AuditResponseInterceptor<>(auditStrategy, auditContext,false, correlator, true);
77              svrFactory.getInInterceptors().add(auditInterceptor);
78              svrFactory.getInFaultInterceptors().add(auditInterceptor);
79          }
80      }
81  }