View Javadoc
1   /*
2    * Copyright 2011 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.platform.camel.ihe.ws;
17  
18  import org.apache.cxf.endpoint.ClientImpl;
19  import org.apache.cxf.frontend.ClientProxy;
20  import org.openehealth.ipf.commons.ihe.ws.JaxWsClientFactory;
21  import org.openehealth.ipf.commons.ihe.ws.WsTransactionConfiguration;
22  import org.openehealth.ipf.commons.ihe.ws.cxf.audit.WsAuditDataset;
23  
24  import javax.jws.WebMethod;
25  import java.lang.reflect.Method;
26  
27  /**
28   * Generic producer for Web Services which have only one operation.
29   * @author Dmytro Rud
30   */
31  public class SimpleWsProducer<
32          AuditDatasetType extends WsAuditDataset,
33          ConfigType extends WsTransactionConfiguration<AuditDatasetType>,
34          InType, OutType> extends AbstractWsProducer<AuditDatasetType, ConfigType, InType, OutType> {
35      private final String operationName;
36  
37      /**
38       * Constructor.
39       * @param endpoint
40       *      Camel endpoint instance.
41       * @param clientFactory
42       *      JAX-WS client object factory.
43       * @param requestClass
44       *          type of request messages.
45       * @param responseClass
46       *          type of response messages.
47       */
48      public SimpleWsProducer(
49              AbstractWsEndpoint<AuditDatasetType, ConfigType> endpoint,
50              JaxWsClientFactory<AuditDatasetType> clientFactory,
51              Class<InType> requestClass,
52              Class<OutType> responseClass)
53      {
54          super(endpoint, clientFactory, requestClass, responseClass);
55  
56          for (Method method : endpoint.getComponent().getWsTransactionConfiguration().getSei().getDeclaredMethods()) {
57              WebMethod annotation = method.getAnnotation(WebMethod.class);
58              if (annotation != null) {
59                  this.operationName = annotation.operationName();
60                  return;
61              }
62          }
63          throw new IllegalStateException("the SEI does not contain any methods annotated with @WebMethod");
64      }
65  
66  
67      @SuppressWarnings("unchecked")
68      @Override
69      protected OutType callService(Object clientObject, InType request) throws Exception {
70          ClientImpl client = (ClientImpl) ClientProxy.getClient(clientObject);
71          Object[] result = client.invoke(operationName, request);
72          return (result != null) ? (OutType) result[0] : null;
73      }
74  }