1 /*
   2  * Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.xml.internal.ws.binding;
  27 
  28 import com.sun.istack.internal.NotNull;
  29 import com.sun.xml.internal.ws.api.BindingID;
  30 import com.sun.xml.internal.ws.api.SOAPVersion;
  31 import com.sun.xml.internal.ws.client.HandlerConfiguration;
  32 import com.sun.xml.internal.ws.encoding.soap.streaming.SOAP12NamespaceConstants;
  33 import com.sun.xml.internal.ws.resources.ClientMessages;
  34 
  35 import javax.xml.namespace.QName;
  36 import javax.xml.soap.MessageFactory;
  37 import javax.xml.soap.SOAPFactory;
  38 
  39 import javax.xml.ws.WebServiceException;
  40 import javax.xml.ws.WebServiceFeature;
  41 import javax.xml.ws.handler.Handler;
  42 import javax.xml.ws.soap.MTOMFeature;
  43 import javax.xml.ws.soap.SOAPBinding;
  44 import java.util.*;
  45 import java.util.concurrent.locks.Lock;
  46 import java.util.concurrent.locks.ReentrantLock;
  47 
  48 /**
  49  * @author WS Development Team
  50  */
  51 public final class SOAPBindingImpl extends BindingImpl implements SOAPBinding {
  52 
  53     public static final String X_SOAP12HTTP_BINDING =
  54         "http://java.sun.com/xml/ns/jaxws/2003/05/soap/bindings/HTTP/";
  55 
  56     private static final String ROLE_NONE = SOAP12NamespaceConstants.ROLE_NONE;
  57     //protected boolean enableMtom;
  58     protected final SOAPVersion soapVersion;
  59 
  60     private Set<QName> portKnownHeaders = Collections.emptySet();
  61     private Set<QName> bindingUnderstoodHeaders = new HashSet<QName>();
  62     private final Lock lock = new ReentrantLock();
  63 
  64     /**
  65      * Use {@link BindingImpl#create(BindingID)} to create this.
  66      *
  67      * @param bindingId SOAP binding ID
  68      */
  69     SOAPBindingImpl(BindingID bindingId) {
  70         this(bindingId,EMPTY_FEATURES);
  71     }
  72 
  73     /**
  74      * Use {@link BindingImpl#create(BindingID)} to create this.
  75      *
  76      * @param bindingId binding id
  77      * @param features
  78      *      These features have a precedence over
  79      *      {@link BindingID#createBuiltinFeatureList() the implicit features}
  80      *      associated with the {@link BindingID}.
  81      */
  82     SOAPBindingImpl(BindingID bindingId, WebServiceFeature... features) {
  83         super(bindingId, features);
  84         this.soapVersion = bindingId.getSOAPVersion();
  85         //populates with required roles and updates handlerConfig
  86         setRoles(new HashSet<String>());
  87         //Is this still required? comment out for now
  88         //setupSystemHandlerDelegate(serviceName);
  89 
  90         this.features.addAll(bindingId.createBuiltinFeatureList());
  91     }
  92 
  93     /**
  94      *  This method should be called if the binding has SOAPSEIModel
  95      *  The Headers understood by the Port are set, so that they can be used for MU
  96      *  processing.
  97      *
  98      * @param headers SOAP header names
  99      */
 100     public void setPortKnownHeaders(@NotNull Set<QName> headers) {
 101 
 102         try{
 103           lock.lock();
 104           this.portKnownHeaders = headers;
 105                 } finally {
 106                 lock.unlock();
 107         }
 108     }
 109 
 110     /**
 111      * TODO A feature should be created to configure processing of MU headers.
 112      * @param header
 113      * @return
 114      */
 115     public boolean understandsHeader(QName header) {
 116         return serviceMode == javax.xml.ws.Service.Mode.MESSAGE
 117                 || portKnownHeaders.contains(header)
 118                 || bindingUnderstoodHeaders.contains(header);
 119 
 120     }
 121 
 122     /**
 123      * Sets the handlers on the binding and then sorts the handlers in to logical and protocol handlers.
 124      * Creates a new HandlerConfiguration object and sets it on the BindingImpl. Also parses Headers understood by
 125      * Protocol Handlers and sets the HandlerConfiguration.
 126      */
 127     public void setHandlerChain(List<Handler> chain) {
 128         setHandlerConfig(new HandlerConfiguration(getHandlerConfig().getRoles(), chain));
 129     }
 130 
 131     protected void addRequiredRoles(Set<String> roles) {
 132         roles.addAll(soapVersion.requiredRoles);
 133     }
 134 
 135     public Set<String> getRoles() {
 136         return getHandlerConfig().getRoles();
 137     }
 138 
 139     /**
 140      * Adds the next and other roles in case this has
 141      * been called by a user without them.
 142      * Creates a new HandlerConfiguration object and sets it on the BindingImpl.
 143      */
 144     public void setRoles(Set<String> roles) {
 145         if (roles == null) {
 146             roles = new HashSet<String>();
 147         }
 148         if (roles.contains(ROLE_NONE)) {
 149             throw new WebServiceException(ClientMessages.INVALID_SOAP_ROLE_NONE());
 150         }
 151         addRequiredRoles(roles);
 152         setHandlerConfig(new HandlerConfiguration(roles, getHandlerConfig()));
 153     }
 154 
 155 
 156     /**
 157      * Used typically by the runtime to enable/disable Mtom optimization
 158      */
 159     public boolean isMTOMEnabled() {
 160         return isFeatureEnabled(MTOMFeature.class);
 161     }
 162 
 163     /**
 164      * Client application can override if the MTOM optimization should be enabled
 165      */
 166     public void setMTOMEnabled(boolean b) {
 167         features.setMTOMEnabled(b);
 168     }
 169 
 170     public SOAPFactory getSOAPFactory() {
 171         return soapVersion.getSOAPFactory();
 172     }
 173 
 174     public MessageFactory getMessageFactory() {
 175         return soapVersion.getMessageFactory();
 176     }
 177 
 178 }