1 /*
   2  * Copyright (c) 1997, 2013, 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.api.addressing;
  27 
  28 import java.net.URL;
  29 
  30 import com.sun.istack.internal.NotNull;
  31 import com.sun.istack.internal.Nullable;
  32 import com.sun.xml.internal.ws.api.FeatureConstructor;
  33 
  34 import javax.xml.ws.WebServiceFeature;
  35 
  36 import com.sun.org.glassfish.gmbal.ManagedAttribute;
  37 import com.sun.org.glassfish.gmbal.ManagedData;
  38 
  39 /**
  40  * Unsupported RI extension to work around an issue in WSIT.
  41  *
  42  * <p>
  43  * <b>This feature is not meant to be used by a common Web service developer</b> as there
  44  * is no need to send the above mentioned header for a one-way operation. But these
  45  * properties may need to be sent in certain middleware Web services.
  46  *
  47  * <p>
  48  * This feature allows ReplyTo, From and RelatesTo Message Addressing Properties
  49  * to be added for all messages that are sent from the port configured with
  50  * this annotation. All operations are assumed to be one-way, and
  51  * this feature should be used for one-way
  52  * operations only.
  53  *
  54  * If a non-null ReplyTo is specified, then MessageID property is also added.
  55  *
  56  * @author Arun Gupta
  57  */
  58 @ManagedData
  59 public class OneWayFeature extends WebServiceFeature {
  60     /**
  61      * Constant value identifying the {@link OneWayFeature}
  62      */
  63     public static final String ID = "http://java.sun.com/xml/ns/jaxws/addressing/oneway";
  64 
  65     private String messageId;
  66     private WSEndpointReference replyTo;
  67     private WSEndpointReference sslReplyTo;
  68     private WSEndpointReference from;
  69     private WSEndpointReference faultTo;
  70     private WSEndpointReference sslFaultTo;
  71     private String relatesToID;
  72     private boolean useAsyncWithSyncInvoke = false;
  73 
  74     /**
  75      * Create an {@link OneWayFeature}. The instance created will be enabled.
  76      */
  77     public OneWayFeature() {
  78         this.enabled = true;
  79     }
  80 
  81     /**
  82      * Create an {@link OneWayFeature}
  83      *
  84      * @param enabled specifies whether this feature should
  85      *                be enabled or not.
  86      */
  87     public OneWayFeature(boolean enabled) {
  88         this.enabled = enabled;
  89     }
  90 
  91     /**
  92      * Create an {@link OneWayFeature}
  93      *
  94      * @param enabled specifies whether this feature should be enabled or not.
  95      * @param replyTo specifies the {@link WSEndpointReference} of wsa:ReplyTo header.
  96      */
  97     public OneWayFeature(boolean enabled, WSEndpointReference replyTo) {
  98         this.enabled = enabled;
  99         this.replyTo = replyTo;
 100     }
 101 
 102     /**
 103      * Create an {@link OneWayFeature}
 104      *
 105      * @param enabled specifies whether this feature should be enabled or not.
 106      * @param replyTo specifies the {@link WSEndpointReference} of wsa:ReplyTo header.
 107      * @param from specifies the {@link WSEndpointReference} of wsa:From header.
 108      * @param relatesTo specifies the MessageID to be used for wsa:RelatesTo header.
 109      */
 110     @FeatureConstructor({"enabled","replyTo","from","relatesTo"})
 111     public OneWayFeature(boolean enabled, WSEndpointReference replyTo, WSEndpointReference from, String relatesTo) {
 112         this.enabled = enabled;
 113         this.replyTo = replyTo;
 114         this.from = from;
 115         this.relatesToID = relatesTo;
 116     }
 117 
 118     public OneWayFeature(final AddressingPropertySet a, AddressingVersion v) {
 119         this.enabled     = true;
 120         this.messageId   = a.getMessageId();
 121         this.relatesToID = a.getRelatesTo();
 122         this.replyTo     = makeEPR(a.getReplyTo(), v);
 123         this.faultTo     = makeEPR(a.getFaultTo(), v);
 124     }
 125 
 126     private WSEndpointReference makeEPR(final String x, final AddressingVersion v) {
 127         if (x == null) { return null; }
 128         return new WSEndpointReference(x, v);
 129     }
 130 
 131     public String getMessageId() {
 132         return messageId;
 133     }
 134 
 135     /**
 136      * {@inheritDoc}
 137      */
 138     @ManagedAttribute
 139     public String getID() {
 140         return ID;
 141     }
 142 
 143     public boolean
 144     hasSslEprs() {
 145       return sslReplyTo != null || sslFaultTo != null;
 146     }
 147 
 148     /**
 149      * Getter for wsa:ReplyTo header {@link WSEndpointReference} .
 150      *
 151      * @return address of the wsa:ReplyTo header
 152      */
 153     @ManagedAttribute
 154     public WSEndpointReference getReplyTo() {
 155         return replyTo;
 156     }
 157 
 158     public WSEndpointReference getReplyTo(boolean ssl) {
 159         return (ssl && sslReplyTo != null) ? sslReplyTo : replyTo;
 160     }
 161 
 162     /**
 163      * Setter for wsa:ReplyTo header {@link WSEndpointReference}.
 164      *
 165      * @param address
 166      */
 167     public void setReplyTo(WSEndpointReference address) {
 168         this.replyTo = address;
 169     }
 170 
 171     public WSEndpointReference getSslReplyTo() {
 172       return sslReplyTo;
 173     }
 174 
 175     public void setSslReplyTo(WSEndpointReference sslReplyTo) {
 176       this.sslReplyTo = sslReplyTo;
 177     }
 178 
 179   /**
 180      * Getter for wsa:From header {@link WSEndpointReference}.
 181      *
 182      * @return address of the wsa:From header
 183      */
 184     @ManagedAttribute
 185     public WSEndpointReference getFrom() {
 186         return from;
 187     }
 188 
 189     /**
 190      * Setter for wsa:From header {@link WSEndpointReference}.
 191      *
 192      * @param address of the wsa:From header
 193      */
 194     public void setFrom(WSEndpointReference address) {
 195         this.from = address;
 196     }
 197 
 198     /**
 199      * Getter for MessageID for wsa:RelatesTo header.
 200      *
 201      * @return address of the wsa:FaultTo header
 202      */
 203     @ManagedAttribute
 204     public String getRelatesToID() {
 205         return relatesToID;
 206     }
 207 
 208     /**
 209      * Setter for MessageID for wsa:RelatesTo header.
 210      *
 211      * @param id
 212      */
 213     public void setRelatesToID(String id) {
 214         this.relatesToID = id;
 215     }
 216 
 217   /**
 218      * Getter for wsa:FaultTo header {@link WSEndpointReference}.
 219      *
 220      * @return address of the wsa:FaultTo header
 221      */
 222     public WSEndpointReference getFaultTo() {
 223         return faultTo;
 224     }
 225 
 226     public WSEndpointReference getFaultTo(boolean ssl) {
 227         return (ssl && sslFaultTo != null) ? sslFaultTo : faultTo;
 228     }
 229 
 230     /**
 231      * Setter for wsa:FaultTo header {@link WSEndpointReference}.
 232      *
 233      * @param address of the wsa:FaultTo header
 234      */
 235     public void setFaultTo(WSEndpointReference address) {
 236         this.faultTo = address;
 237     }
 238 
 239     public WSEndpointReference getSslFaultTo() {
 240       return sslFaultTo;
 241     }
 242 
 243     public void setSslFaultTo(WSEndpointReference sslFaultTo) {
 244       this.sslFaultTo = sslFaultTo;
 245     }
 246 
 247     /**
 248      * Getter for whether async is to be used with sync invoke
 249      *
 250      * @return whether async is to be used with sync invoke
 251      */
 252     public boolean isUseAsyncWithSyncInvoke() {
 253         return useAsyncWithSyncInvoke;
 254     }
 255 
 256     /**
 257      *  Setter for whether async is to be used with sync invoke
 258      *
 259      * @param useAsyncWithSyncInvoke whether async is to be used with sync invoke
 260      */
 261     public void setUseAsyncWithSyncInvoke(boolean useAsyncWithSyncInvoke) {
 262         this.useAsyncWithSyncInvoke = useAsyncWithSyncInvoke;
 263     }
 264 
 265     /**
 266      * Calculate a new EPR using an existing one and substituting SSL specific
 267      * host and port values.
 268      * @param epr Existing EPR that will be the starting point for the SSL
 269      *        version
 270      * @param sslHost New SSL host or null if the existing host should be used
 271      * @param sslPort New SSL port or -1 if the existing port should be used
 272      * @return
 273      */
 274     public static WSEndpointReference
 275     enableSslForEpr(@NotNull WSEndpointReference epr,
 276                     @Nullable String sslHost,
 277                     int sslPort) {
 278         if (!epr.isAnonymous()) {
 279             String address = epr.getAddress();
 280             URL url;
 281             try {
 282               url = new URL(address);
 283             } catch (Exception e) {
 284               throw new RuntimeException(e);
 285             }
 286             String protocol = url.getProtocol();
 287             if (!protocol.equalsIgnoreCase("https")) {
 288               protocol = "https";
 289               String host = url.getHost();
 290               if (sslHost != null) {
 291                 host = sslHost;
 292               }
 293               int port = url.getPort();
 294               if (sslPort > 0) {
 295                 port = sslPort;
 296               }
 297               try {
 298                 url = new URL(protocol, host, port, url.getFile());
 299               } catch (Exception e) {
 300                 throw new RuntimeException(e);
 301               }
 302               address = url.toExternalForm();
 303               return
 304                 new WSEndpointReference(address, epr.getVersion());
 305             }
 306         }
 307 
 308         return epr;
 309     }
 310 
 311 }