View Javadoc

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.dom.DOMSource;
31  import javax.xml.transform.stream.StreamResult;
32  import javax.xml.ws.BindingProvider;
33  import javax.xml.ws.soap.MTOMFeature;
34  
35  import org.apache.chemistry.opencmis.client.bindings.impl.CmisBindingsHelper;
36  import org.apache.chemistry.opencmis.commons.SessionParameter;
37  import org.apache.chemistry.opencmis.commons.exceptions.CmisBaseException;
38  import org.apache.chemistry.opencmis.commons.exceptions.CmisConnectionException;
39  import org.apache.chemistry.opencmis.commons.impl.XMLUtils;
40  import org.apache.chemistry.opencmis.commons.spi.AuthenticationProvider;
41  import org.slf4j.Logger;
42  import org.slf4j.LoggerFactory;
43  import org.w3c.dom.Element;
44  
45  /**
46   * WebSphere JAX-WS implementation
47   */
48  public class WebSpherePortProvider extends AbstractPortProvider {
49      private static final Logger LOG = LoggerFactory.getLogger(WebSpherePortProvider.class);
50  
51      /**
52       * Creates a port object.
53       */
54      @Override
55      protected BindingProvider createPortObject(CmisServiceHolder serviceHolder) {
56          if (LOG.isDebugEnabled()) {
57              LOG.debug("Creating Web Service port object of " + serviceHolder.getServiceName() + "...");
58          }
59  
60          try {
61              // create port object
62              BindingProvider portObject = createPortObjectFromServiceHolder(serviceHolder, new MTOMFeature());
63  
64              // add SOAP and HTTP authentication headers
65              AuthenticationProvider authProvider = CmisBindingsHelper.getAuthenticationProvider(getSession());
66              Map<String, List<String>> httpHeaders = null;
67              if (authProvider != null) {
68                  // SOAP header
69                  Element soapHeader = authProvider.getSOAPHeaders(portObject);
70                  if (soapHeader != null) {
71                      Transformer transformer = XMLUtils.newTransformer();
72                      StringWriter headerXml = new StringWriter();
73                      transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
74                      transformer.transform(new DOMSource(soapHeader), new StreamResult(headerXml));
75  
76                      Map<QName, List<String>> header = new HashMap<QName, List<String>>();
77                      header.put(new QName(soapHeader.getNamespaceURI(), soapHeader.getLocalName()),
78                              Collections.singletonList(headerXml.toString()));
79                      portObject.getRequestContext().put("jaxws.binding.soap.headers.outbound", header);
80                  }
81  
82                  // HTTP header
83                  String url = (serviceHolder.getEndpointUrl() != null ? serviceHolder.getEndpointUrl().toString()
84                          : serviceHolder.getServiceObject().getWSDLDocumentLocation().toString());
85                  httpHeaders = authProvider.getHTTPHeaders(url);
86  
87                  // TODO: set SSL Factory
88  
89                  // TODO: set Hostname Verifier
90              }
91  
92              // set HTTP headers
93              setHTTPHeaders(portObject, httpHeaders);
94  
95              // set endpoint URL
96              setEndpointUrl(portObject, serviceHolder.getEndpointUrl());
97  
98              // timeouts
99              int connectTimeout = getSession().get(SessionParameter.CONNECT_TIMEOUT, -1);
100             if (connectTimeout >= 0) {
101                 portObject.getRequestContext().put("connection_timeout", connectTimeout);
102             }
103 
104             int readTimeout = getSession().get(SessionParameter.READ_TIMEOUT, -1);
105             if (readTimeout >= 0) {
106                 portObject.getRequestContext().put("request_timeout", readTimeout);
107             }
108 
109             return portObject;
110         } catch (CmisBaseException ce) {
111             throw ce;
112         } catch (Exception e) {
113             throw new CmisConnectionException("Cannot initalize Web Services port object: " + e.getMessage(), e);
114         }
115     }
116 }