src/share/jaxws_classes/com/sun/xml/internal/ws/api/EndpointAddress.java

Wed, 12 Jun 2013 14:47:09 +0100

author
mkos
date
Wed, 12 Jun 2013 14:47:09 +0100
changeset 384
8f2986ff0235
parent 368
0989ad8c0860
child 637
9c07ef4934dd
permissions
-rw-r--r--

8013021: Rebase 8005432 & 8003542 against the latest jdk8/jaxws
8003542: Improve processing of MTOM attachments
8005432: Update access to JAX-WS
Reviewed-by: mullan

ohair@286 1 /*
alanb@368 2 * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
ohair@286 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ohair@286 4 *
ohair@286 5 * This code is free software; you can redistribute it and/or modify it
ohair@286 6 * under the terms of the GNU General Public License version 2 only, as
ohair@286 7 * published by the Free Software Foundation. Oracle designates this
ohair@286 8 * particular file as subject to the "Classpath" exception as provided
ohair@286 9 * by Oracle in the LICENSE file that accompanied this code.
ohair@286 10 *
ohair@286 11 * This code is distributed in the hope that it will be useful, but WITHOUT
ohair@286 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ohair@286 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
ohair@286 14 * version 2 for more details (a copy is included in the LICENSE file that
ohair@286 15 * accompanied this code).
ohair@286 16 *
ohair@286 17 * You should have received a copy of the GNU General Public License version
ohair@286 18 * 2 along with this work; if not, write to the Free Software Foundation,
ohair@286 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ohair@286 20 *
ohair@286 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@286 22 * or visit www.oracle.com if you need additional information or have any
ohair@286 23 * questions.
ohair@286 24 */
ohair@286 25
ohair@286 26 package com.sun.xml.internal.ws.api;
ohair@286 27
ohair@286 28 import com.sun.istack.internal.Nullable;
ohair@286 29
ohair@286 30 import javax.xml.ws.WebServiceException;
ohair@286 31 import java.io.IOException;
ohair@286 32 import java.net.MalformedURLException;
ohair@286 33 import java.net.Proxy;
ohair@286 34 import java.net.ProxySelector;
ohair@286 35 import java.net.URI;
ohair@286 36 import java.net.URISyntaxException;
ohair@286 37 import java.net.URL;
ohair@286 38 import java.net.URLConnection;
ohair@286 39 import java.net.URLStreamHandler;
ohair@286 40 import java.util.Iterator;
ohair@286 41
ohair@286 42 /**
ohair@286 43 * Represents the endpoint address URI.
ohair@286 44 *
ohair@286 45 * <p>
ohair@286 46 * Conceptually this can be really thought of as an {@link URI},
ohair@286 47 * but it hides some of the details that improve the performance.
ohair@286 48 *
ohair@286 49 * <p>
ohair@286 50 * Being an {@link URI} allows this class to represent custom made-up URIs
ohair@286 51 * (like "jms" for example.) Whenever possible, this object
ohair@286 52 * also creates an {@link URL} (this is only possible when the address
ohair@286 53 * has a registered {@link URLStreamHandler}), so that if the clients
ohair@286 54 * of this code wants to use it, it can do so.
ohair@286 55 *
ohair@286 56 *
ohair@286 57 * <h3>How it improves the performance</h3>
ohair@286 58 * <ol>
ohair@286 59 * <li>
ohair@286 60 * Endpoint address is often eventually turned into an {@link URLConnection},
ohair@286 61 * and given that generally this value is read more often than being set,
ohair@286 62 * it makes sense to eagerly turn it into an {@link URL},
ohair@286 63 * thereby avoiding a repeated conversion.
ohair@286 64 *
ohair@286 65 * <li>
ohair@286 66 * JDK spends a lot of time choosing a list of {@link Proxy}
ohair@286 67 * to connect to an {@link URL}. Since the default proxy selector
ohair@286 68 * implementation always return the same proxy for the same URL,
ohair@286 69 * we can determine the proxy by ourselves to let JDK skip its
ohair@286 70 * proxy-discovery step.
ohair@286 71 *
ohair@286 72 * (That said, user-defined proxy selector can do a lot of interesting things
ohair@286 73 * --- like doing a round-robin, or pick one from a proxy farm randomly,
ohair@286 74 * and so it's dangerous to stick to one proxy. For this case,
ohair@286 75 * we still let JDK decide the proxy. This shouldn't be that much of an
ohair@286 76 * disappointment, since most people only mess with system properties,
ohair@286 77 * and never with {@link ProxySelector}. Also, avoiding optimization
ohair@286 78 * with non-standard proxy selector allows people to effectively disable
ohair@286 79 * this optimization, which may come in handy for a trouble-shooting.)
ohair@286 80 * </ol>
ohair@286 81 *
ohair@286 82 * @author Kohsuke Kawaguchi
ohair@286 83 */
ohair@286 84 public final class EndpointAddress {
ohair@286 85 @Nullable
ohair@286 86 private URL url;
ohair@286 87 private final URI uri;
ohair@286 88 private final String stringForm;
ohair@286 89 private volatile boolean dontUseProxyMethod;
ohair@286 90 /**
ohair@286 91 * Pre-selected proxy.
ohair@286 92 *
ohair@286 93 * If {@link #url} is null, this field is null.
ohair@286 94 * Otherwise, this field could still be null if the proxy couldn't be chosen
ohair@286 95 * upfront.
ohair@286 96 */
ohair@286 97 private Proxy proxy;
ohair@286 98
ohair@286 99 public EndpointAddress(URI uri) {
ohair@286 100 this.uri = uri;
ohair@286 101 this.stringForm = uri.toString();
ohair@286 102 try {
ohair@286 103 initURL();
ohair@286 104 proxy = chooseProxy();
ohair@286 105 } catch (MalformedURLException e) {
ohair@286 106 // ignore
ohair@286 107 }
ohair@286 108 }
ohair@286 109
ohair@286 110 /**
ohair@286 111 *
ohair@286 112 * @see #create(String)
ohair@286 113 */
ohair@286 114 public EndpointAddress(String url) throws URISyntaxException {
ohair@286 115 this.uri = new URI(url);
ohair@286 116 this.stringForm = url;
ohair@286 117 try {
ohair@286 118 initURL();
ohair@286 119 proxy = chooseProxy();
ohair@286 120 } catch (MalformedURLException e) {
ohair@286 121 // ignore
ohair@286 122 }
ohair@286 123 }
ohair@286 124
ohair@286 125
ohair@286 126 private void initURL() throws MalformedURLException {
ohair@286 127 String scheme = uri.getScheme();
ohair@286 128 //URI.toURL() only works when scheme is not null.
ohair@286 129 if (scheme == null) {
ohair@286 130 this.url = new URL(uri.toString());
ohair@286 131 return;
ohair@286 132 }
ohair@286 133 scheme =scheme.toLowerCase();
ohair@286 134 if ("http".equals(scheme) || "https".equals(scheme)) {
ohair@286 135 url = new URL(uri.toASCIIString());
ohair@286 136 } else {
ohair@286 137 this.url = uri.toURL();
ohair@286 138 }
ohair@286 139 }
ohair@286 140
ohair@286 141 /**
ohair@286 142 * Creates a new {@link EndpointAddress} with a reasonably
ohair@286 143 * generic error handling.
ohair@286 144 */
ohair@286 145 public static EndpointAddress create(String url) {
ohair@286 146 try {
ohair@286 147 return new EndpointAddress(url);
ohair@286 148 } catch(URISyntaxException e) {
ohair@286 149 throw new WebServiceException("Illegal endpoint address: "+url,e);
ohair@286 150 }
ohair@286 151 }
ohair@286 152
ohair@286 153 private Proxy chooseProxy() {
ohair@286 154 ProxySelector sel =
ohair@286 155 java.security.AccessController.doPrivileged(
ohair@286 156 new java.security.PrivilegedAction<ProxySelector>() {
alanb@368 157 @Override
ohair@286 158 public ProxySelector run() {
ohair@286 159 return ProxySelector.getDefault();
ohair@286 160 }
ohair@286 161 });
ohair@286 162
ohair@286 163 if(sel==null)
ohair@286 164 return Proxy.NO_PROXY;
ohair@286 165
ohair@286 166
ohair@286 167 if(!sel.getClass().getName().equals("sun.net.spi.DefaultProxySelector"))
ohair@286 168 // user-defined proxy. may return a different proxy for each invocation
ohair@286 169 return null;
ohair@286 170
ohair@286 171 Iterator<Proxy> it = sel.select(uri).iterator();
ohair@286 172 if(it.hasNext())
ohair@286 173 return it.next();
ohair@286 174
ohair@286 175 return Proxy.NO_PROXY;
ohair@286 176 }
ohair@286 177
ohair@286 178 /**
ohair@286 179 * Returns an URL of this endpoint adress.
ohair@286 180 *
ohair@286 181 * @return
ohair@286 182 * null if this endpoint address doesn't have a registered {@link URLStreamHandler}.
ohair@286 183 */
ohair@286 184 public URL getURL() {
ohair@286 185 return url;
ohair@286 186 }
ohair@286 187
ohair@286 188 /**
ohair@286 189 * Returns an URI of the endpoint address.
ohair@286 190 *
ohair@286 191 * @return
ohair@286 192 * always non-null.
ohair@286 193 */
ohair@286 194 public URI getURI() {
ohair@286 195 return uri;
ohair@286 196 }
ohair@286 197
ohair@286 198 /**
ohair@286 199 * Tries to open {@link URLConnection} for this endpoint.
ohair@286 200 *
ohair@286 201 * <p>
ohair@286 202 * This is possible only when an endpoint address has
ohair@286 203 * the corresponding {@link URLStreamHandler}.
ohair@286 204 *
ohair@286 205 * @throws IOException
ohair@286 206 * if {@link URL#openConnection()} reports an error.
ohair@286 207 * @throws AssertionError
ohair@286 208 * if this endpoint doesn't have an associated URL.
ohair@286 209 * if the code is written correctly this shall never happen.
ohair@286 210 */
ohair@286 211 public URLConnection openConnection() throws IOException {
ohair@286 212 if (url == null) {
ohair@286 213 throw new WebServiceException("URI="+uri+" doesn't have the corresponding URL");
ohair@286 214 }
ohair@286 215 if(proxy!=null && !dontUseProxyMethod) {
ohair@286 216 try {
ohair@286 217 return url.openConnection(proxy);
ohair@286 218 } catch(UnsupportedOperationException e) {
ohair@286 219 // Some OSGi and app server environments donot
ohair@286 220 // override URLStreamHandler.openConnection(URL, Proxy) as it
ohair@286 221 // is introduced in Java SE 5 API. Fallback to the other method.
ohair@286 222 dontUseProxyMethod = true;
ohair@286 223 }
ohair@286 224 }
ohair@286 225 return url.openConnection();
ohair@286 226 }
ohair@286 227
alanb@368 228 @Override
ohair@286 229 public String toString() {
ohair@286 230 return stringForm;
ohair@286 231 }
ohair@286 232 }

mercurial