This project has retired. For details please refer to its Attic page.
WebSpherePortProvider 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.io.StringWriter;
22  import java.util.Collections;
23  import java.util.HashMap;
24  import java.util.List;
25  import java.util.Map;
26  
27  import javax.xml.namespace.QName;
28  import javax.xml.transform.OutputKeys;
29  import javax.xml.transform.Transformer;
30  import javax.xml.transform.TransformerFactory;
31  import javax.xml.transform.dom.DOMSource;
32  import javax.xml.transform.stream.StreamResult;
33  import javax.xml.ws.BindingProvider;
34  import javax.xml.ws.Service;
35  import javax.xml.ws.soap.MTOMFeature;
36  
37  import org.apache.chemistry.opencmis.client.bindings.impl.CmisBindingsHelper;
38  import org.apache.chemistry.opencmis.commons.SessionParameter;
39  import org.apache.chemistry.opencmis.commons.exceptions.CmisBaseException;
40  import org.apache.chemistry.opencmis.commons.exceptions.CmisConnectionException;
41  import org.apache.chemistry.opencmis.commons.exceptions.CmisRuntimeException;
42  import org.apache.chemistry.opencmis.commons.impl.jaxb.ACLService;
43  import org.apache.chemistry.opencmis.commons.impl.jaxb.DiscoveryService;
44  import org.apache.chemistry.opencmis.commons.impl.jaxb.MultiFilingService;
45  import org.apache.chemistry.opencmis.commons.impl.jaxb.NavigationService;
46  import org.apache.chemistry.opencmis.commons.impl.jaxb.ObjectService;
47  import org.apache.chemistry.opencmis.commons.impl.jaxb.PolicyService;
48  import org.apache.chemistry.opencmis.commons.impl.jaxb.RelationshipService;
49  import org.apache.chemistry.opencmis.commons.impl.jaxb.RepositoryService;
50  import org.apache.chemistry.opencmis.commons.impl.jaxb.VersioningService;
51  import org.apache.chemistry.opencmis.commons.spi.AuthenticationProvider;
52  import org.apache.commons.logging.Log;
53  import org.apache.commons.logging.LogFactory;
54  import org.w3c.dom.Element;
55  
56  /**
57   * Provides CMIS Web Services port objects for WebSphere. Handles authentication
58   * headers.
59   */
60  public class WebSpherePortProvider extends AbstractPortProvider {
61      private static final Log log = LogFactory.getLog(WebSpherePortProvider.class);
62  
63      /**
64       * Creates a port object.
65       */
66      protected Object createPortObject(Service service) {
67  
68          if (log.isDebugEnabled()) {
69              log.debug("Creating Web Service port object of " + (service == null ? "???" : service.getServiceName())
70                      + "...");
71          }
72  
73          Object portObject;
74          try {
75              if (service instanceof RepositoryService) {
76                  portObject = ((RepositoryService) service).getRepositoryServicePort(new MTOMFeature());
77              } else if (service instanceof NavigationService) {
78                  portObject = ((NavigationService) service).getNavigationServicePort(new MTOMFeature());
79              } else if (service instanceof ObjectService) {
80                  portObject = ((ObjectService) service).getObjectServicePort(new MTOMFeature());
81              } else if (service instanceof VersioningService) {
82                  portObject = ((VersioningService) service).getVersioningServicePort(new MTOMFeature());
83              } else if (service instanceof DiscoveryService) {
84                  portObject = ((DiscoveryService) service).getDiscoveryServicePort(new MTOMFeature());
85              } else if (service instanceof MultiFilingService) {
86                  portObject = ((MultiFilingService) service).getMultiFilingServicePort(new MTOMFeature());
87              } else if (service instanceof RelationshipService) {
88                  portObject = ((RelationshipService) service).getRelationshipServicePort(new MTOMFeature());
89              } else if (service instanceof PolicyService) {
90                  portObject = ((PolicyService) service).getPolicyServicePort(new MTOMFeature());
91              } else if (service instanceof ACLService) {
92                  portObject = ((ACLService) service).getACLServicePort(new MTOMFeature());
93              } else {
94                  throw new CmisRuntimeException("Cannot find Web Services service object!");
95              }
96  
97              // add SOAP and HTTP authentication headers
98              AuthenticationProvider authProvider = CmisBindingsHelper.getAuthenticationProvider(getSession());
99              Map<String, List<String>> httpHeaders = null;
100             if (authProvider != null) {
101                 // SOAP header
102                 Element soapHeader = authProvider.getSOAPHeaders(portObject);
103                 if (soapHeader != null) {
104                     TransformerFactory transFactory = TransformerFactory.newInstance();
105                     Transformer transformer = transFactory.newTransformer();
106                     StringWriter headerXml = new StringWriter();
107                     transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
108                     transformer.transform(new DOMSource(soapHeader), new StreamResult(headerXml));
109 
110                     Map<QName, List<String>> header = new HashMap<QName, List<String>>();
111                     header.put(new QName(soapHeader.getNamespaceURI(), soapHeader.getLocalName()),
112                             Collections.singletonList(headerXml.toString()));
113                     ((BindingProvider) portObject).getRequestContext().put("jaxws.binding.soap.headers.outbound",
114                             header);
115                 }
116 
117                 // HTTP header
118                 httpHeaders = authProvider.getHTTPHeaders(service.getWSDLDocumentLocation().toString());
119             }
120 
121             // set HTTP headers
122             setHTTPHeaders(portObject, httpHeaders);
123 
124             // timeouts
125             int connectTimeout = getSession().get(SessionParameter.CONNECT_TIMEOUT, -1);
126             if (connectTimeout >= 0) {
127                 ((BindingProvider) portObject).getRequestContext().put("connection_timeout", connectTimeout);
128             }
129 
130             int readTimeout = getSession().get(SessionParameter.READ_TIMEOUT, -1);
131             if (readTimeout >= 0) {
132                 ((BindingProvider) portObject).getRequestContext().put("request_timeout", readTimeout);
133             }
134         } catch (CmisBaseException ce) {
135             throw ce;
136         } catch (Exception e) {
137             throw new CmisConnectionException("Cannot initalize Web Services port object: " + e.getMessage(), e);
138         }
139 
140         return portObject;
141     }
142 }