This project has retired. For details please refer to its Attic page.
PortProvider xref

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.chemistry.opencmis.client.bindings.spi.webservices;
20  
21  import java.util.List;
22  import java.util.Map;
23  
24  import javax.xml.ws.BindingProvider;
25  import javax.xml.ws.Service;
26  import javax.xml.ws.soap.MTOMFeature;
27  
28  import org.apache.chemistry.opencmis.client.bindings.impl.CmisBindingsHelper;
29  import org.apache.chemistry.opencmis.commons.SessionParameter;
30  import org.apache.chemistry.opencmis.commons.exceptions.CmisBaseException;
31  import org.apache.chemistry.opencmis.commons.exceptions.CmisConnectionException;
32  import org.apache.chemistry.opencmis.commons.exceptions.CmisRuntimeException;
33  import org.apache.chemistry.opencmis.commons.impl.jaxb.ACLService;
34  import org.apache.chemistry.opencmis.commons.impl.jaxb.DiscoveryService;
35  import org.apache.chemistry.opencmis.commons.impl.jaxb.MultiFilingService;
36  import org.apache.chemistry.opencmis.commons.impl.jaxb.NavigationService;
37  import org.apache.chemistry.opencmis.commons.impl.jaxb.ObjectService;
38  import org.apache.chemistry.opencmis.commons.impl.jaxb.PolicyService;
39  import org.apache.chemistry.opencmis.commons.impl.jaxb.RelationshipService;
40  import org.apache.chemistry.opencmis.commons.impl.jaxb.RepositoryService;
41  import org.apache.chemistry.opencmis.commons.impl.jaxb.VersioningService;
42  import org.apache.chemistry.opencmis.commons.spi.AuthenticationProvider;
43  import org.apache.commons.logging.Log;
44  import org.apache.commons.logging.LogFactory;
45  import org.w3c.dom.Element;
46  
47  import com.sun.xml.ws.api.message.Headers;
48  import com.sun.xml.ws.developer.JAXWSProperties;
49  import com.sun.xml.ws.developer.StreamingAttachmentFeature;
50  import com.sun.xml.ws.developer.WSBindingProvider;
51  
52  /**
53   * Provides CMIS Web Services port objects for JAX-WS RI. Handles authentication
54   * headers.
55   */
56  public class PortProvider extends AbstractPortProvider {
57      private static final Log log = LogFactory.getLog(PortProvider.class);
58  
59      /**
60       * Creates a port object.
61       */
62      protected Object createPortObject(Service service) {
63  
64          if (log.isDebugEnabled()) {
65              log.debug("Creating Web Service port object of " + (service == null ? "???" : service.getServiceName())
66                      + "...");
67          }
68  
69          Object portObject;
70          try {
71              if (service instanceof RepositoryService) {
72                  portObject = ((RepositoryService) service).getRepositoryServicePort(new MTOMFeature());
73              } else if (service instanceof NavigationService) {
74                  portObject = ((NavigationService) service).getNavigationServicePort(new MTOMFeature());
75              } else if (service instanceof ObjectService) {
76                  int threshold = getSession().get(SessionParameter.WEBSERVICES_MEMORY_THRESHOLD, 4 * 1024 * 1024);
77                  portObject = ((ObjectService) service).getObjectServicePort(new MTOMFeature(),
78                          new StreamingAttachmentFeature(null, true, threshold));
79                  ((BindingProvider) portObject).getRequestContext().put(
80                          JAXWSProperties.HTTP_CLIENT_STREAMING_CHUNK_SIZE, CHUNK_SIZE);
81              } else if (service instanceof VersioningService) {
82                  int threshold = getSession().get(SessionParameter.WEBSERVICES_MEMORY_THRESHOLD, 4 * 1024 * 1024);
83                  portObject = ((VersioningService) service).getVersioningServicePort(new MTOMFeature(),
84                          new StreamingAttachmentFeature(null, true, threshold));
85                  ((BindingProvider) portObject).getRequestContext().put(
86                          JAXWSProperties.HTTP_CLIENT_STREAMING_CHUNK_SIZE, CHUNK_SIZE);
87              } else if (service instanceof DiscoveryService) {
88                  portObject = ((DiscoveryService) service).getDiscoveryServicePort(new MTOMFeature());
89              } else if (service instanceof MultiFilingService) {
90                  portObject = ((MultiFilingService) service).getMultiFilingServicePort(new MTOMFeature());
91              } else if (service instanceof RelationshipService) {
92                  portObject = ((RelationshipService) service).getRelationshipServicePort(new MTOMFeature());
93              } else if (service instanceof PolicyService) {
94                  portObject = ((PolicyService) service).getPolicyServicePort(new MTOMFeature());
95              } else if (service instanceof ACLService) {
96                  portObject = ((ACLService) service).getACLServicePort(new MTOMFeature());
97              } else {
98                  throw new CmisRuntimeException("Cannot find Web Services service object!");
99              }
100 
101             // add SOAP and HTTP authentication headers
102             AuthenticationProvider authProvider = CmisBindingsHelper.getAuthenticationProvider(getSession());
103             Map<String, List<String>> httpHeaders = null;
104             if (authProvider != null) {
105                 // SOAP header
106                 Element soapHeader = authProvider.getSOAPHeaders(portObject);
107                 if (soapHeader != null) {
108                     ((WSBindingProvider) portObject).setOutboundHeaders(Headers.create(soapHeader));
109                 }
110 
111                 // HTTP header
112                 httpHeaders = authProvider.getHTTPHeaders(service.getWSDLDocumentLocation().toString());
113             }
114 
115             // set HTTP headers
116             setHTTPHeaders(portObject, httpHeaders);
117 
118             // timeouts
119             int connectTimeout = getSession().get(SessionParameter.CONNECT_TIMEOUT, -1);
120             if (connectTimeout >= 0) {
121                 ((BindingProvider) portObject).getRequestContext().put(JAXWSProperties.CONNECT_TIMEOUT, connectTimeout);
122             }
123 
124             int readTimeout = getSession().get(SessionParameter.READ_TIMEOUT, -1);
125             if (readTimeout >= 0) {
126                 ((BindingProvider) portObject).getRequestContext().put("com.sun.xml.ws.request.timeout", readTimeout);
127             }
128         } catch (CmisBaseException ce) {
129             throw ce;
130         } catch (Exception e) {
131             throw new CmisConnectionException("Cannot initalize Web Services port object: " + e.getMessage(), e);
132         }
133 
134         return portObject;
135     }
136 }