src/share/jaxws_classes/com/sun/xml/internal/ws/transport/http/WSHTTPConnection.java

Thu, 31 Aug 2017 15:18:52 +0800

author
aoqi
date
Thu, 31 Aug 2017 15:18:52 +0800
changeset 637
9c07ef4934dd
parent 368
0989ad8c0860
parent 0
373ffda63c9a
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation. Oracle designates this
aoqi@0 8 * particular file as subject to the "Classpath" exception as provided
aoqi@0 9 * by Oracle in the LICENSE file that accompanied this code.
aoqi@0 10 *
aoqi@0 11 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 14 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 15 * accompanied this code).
aoqi@0 16 *
aoqi@0 17 * You should have received a copy of the GNU General Public License version
aoqi@0 18 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 20 *
aoqi@0 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 22 * or visit www.oracle.com if you need additional information or have any
aoqi@0 23 * questions.
aoqi@0 24 */
aoqi@0 25
aoqi@0 26 package com.sun.xml.internal.ws.transport.http;
aoqi@0 27
aoqi@0 28 import com.oracle.webservices.internal.api.message.BasePropertySet;
aoqi@0 29 import com.oracle.webservices.internal.api.message.PropertySet;
aoqi@0 30 import com.sun.istack.internal.NotNull;
aoqi@0 31 import com.sun.istack.internal.Nullable;
aoqi@0 32 import com.sun.xml.internal.ws.api.message.Packet;
aoqi@0 33 import com.sun.xml.internal.ws.api.server.WebServiceContextDelegate;
aoqi@0 34
aoqi@0 35 import javax.xml.ws.WebServiceContext;
aoqi@0 36 import java.io.IOException;
aoqi@0 37 import java.io.InputStream;
aoqi@0 38 import java.io.OutputStream;
aoqi@0 39 import java.net.HttpURLConnection;
aoqi@0 40 import java.security.Principal;
aoqi@0 41 import java.util.Collections;
aoqi@0 42 import java.util.List;
aoqi@0 43 import java.util.Map;
aoqi@0 44 import java.util.Set;
aoqi@0 45
aoqi@0 46
aoqi@0 47 /**
aoqi@0 48 * The view of an HTTP exchange from the point of view of JAX-WS.
aoqi@0 49 *
aoqi@0 50 * <p>
aoqi@0 51 * Different HTTP server layer uses different implementations of this class
aoqi@0 52 * so that JAX-WS can be shielded from individuality of such layers.
aoqi@0 53 * This is an interface implemented as an abstract class, so that
aoqi@0 54 * future versions of the JAX-WS RI can add new methods.
aoqi@0 55 *
aoqi@0 56 * <p>
aoqi@0 57 * This class extends {@link PropertySet} so that a transport can
aoqi@0 58 * expose its properties to the application and pipes. (This object
aoqi@0 59 * will be added to {@link Packet#addSatellite(PropertySet)}.)
aoqi@0 60 *
aoqi@0 61 * @author Jitendra Kotamraju
aoqi@0 62 */
aoqi@0 63 public abstract class WSHTTPConnection extends BasePropertySet {
aoqi@0 64
aoqi@0 65 public static final int OK=200;
aoqi@0 66 public static final int ONEWAY=202;
aoqi@0 67 public static final int UNSUPPORTED_MEDIA=415;
aoqi@0 68 public static final int MALFORMED_XML=400;
aoqi@0 69 public static final int INTERNAL_ERR=500;
aoqi@0 70
aoqi@0 71 /**
aoqi@0 72 * Overwrites all the HTTP response headers written thus far.
aoqi@0 73 *
aoqi@0 74 * <p>
aoqi@0 75 * The implementation should copy the contents of the {@link Map},
aoqi@0 76 * rather than retaining a reference. The {@link Map} passed as a
aoqi@0 77 * parameter may change after this method is invoked.
aoqi@0 78 *
aoqi@0 79 * <p>
aoqi@0 80 * This method may be called repeatedly, although in normal use
aoqi@0 81 * case that's rare (so the implementation is encourage to take
aoqi@0 82 * advantage of this usage pattern to improve performance, if possible.)
aoqi@0 83 *
aoqi@0 84 * <p>
aoqi@0 85 * Initially, no header is set.
aoqi@0 86 *
aoqi@0 87 * <p>
aoqi@0 88 * This parameter is usually exposed to {@link WebServiceContext}
aoqi@0 89 * as {@link Packet#OUTBOUND_TRANSPORT_HEADERS}, and thus it
aoqi@0 90 * should ignore <tt>Content-Type</tt> and <tt>Content-Length</tt> headers.
aoqi@0 91 *
aoqi@0 92 * @param headers
aoqi@0 93 * See {@link HttpURLConnection#getHeaderFields()} for the format.
aoqi@0 94 * This parameter may not be null, but since the user application
aoqi@0 95 * code may invoke this method, a graceful error checking with
aoqi@0 96 * an helpful error message should be provided if it's actually null.
aoqi@0 97 * @see #setContentTypeResponseHeader(String)
aoqi@0 98 */
aoqi@0 99 public abstract void setResponseHeaders(@NotNull Map<String,List<String>> headers);
aoqi@0 100
aoqi@0 101 public void setResponseHeader(String key, String value) {
aoqi@0 102 setResponseHeader(key, Collections.singletonList(value));
aoqi@0 103 }
aoqi@0 104
aoqi@0 105 public abstract void setResponseHeader(String key, List<String> value);
aoqi@0 106
aoqi@0 107 /**
aoqi@0 108 * Sets the <tt>"Content-Type"</tt> header.
aoqi@0 109 *
aoqi@0 110 * <p>
aoqi@0 111 * If the Content-Type header has already been set, this method will overwrite
aoqi@0 112 * the previously set value. If not, this method adds it.
aoqi@0 113 *
aoqi@0 114 * <p>
aoqi@0 115 * Note that this method and {@link #setResponseHeaders(java.util.Map)}
aoqi@0 116 * may be invoked in any arbitrary order.
aoqi@0 117 *
aoqi@0 118 * @param value
aoqi@0 119 * strings like <tt>"application/xml; charset=UTF-8"</tt> or
aoqi@0 120 * <tt>"image/jpeg"</tt>.
aoqi@0 121 */
aoqi@0 122 public abstract void setContentTypeResponseHeader(@NotNull String value);
aoqi@0 123
aoqi@0 124 /**
aoqi@0 125 * Sets the HTTP response code like {@link #OK}.
aoqi@0 126 *
aoqi@0 127 * <p>
aoqi@0 128 * While JAX-WS processes a {@link WSHTTPConnection}, it
aoqi@0 129 * will at least call this method once to set a valid HTTP response code.
aoqi@0 130 * Note that this method may be invoked multiple times (from user code),
aoqi@0 131 * so do not consider the value to be final until {@link #getOutput()}
aoqi@0 132 * is invoked.
aoqi@0 133 */
aoqi@0 134
aoqi@0 135 public abstract void setStatus(int status);
aoqi@0 136
aoqi@0 137 /**
aoqi@0 138 * Gets the last value set by {@link #setStatus(int)}.
aoqi@0 139 *
aoqi@0 140 * @return
aoqi@0 141 * if {@link #setStatus(int)} has not been invoked yet,
aoqi@0 142 * return 0.
aoqi@0 143 */
aoqi@0 144 // I know this is ugly method!
aoqi@0 145 public abstract int getStatus();
aoqi@0 146
aoqi@0 147 /**
aoqi@0 148 * Transport's underlying input stream.
aoqi@0 149 *
aoqi@0 150 * <p>
aoqi@0 151 * This method will be invoked at most once by the JAX-WS RI to
aoqi@0 152 * read the request body. If there's no request body, this method
aoqi@0 153 * should return an empty {@link InputStream}.
aoqi@0 154 *
aoqi@0 155 * @return
aoqi@0 156 * the stream from which the request body will be read.
aoqi@0 157 */
aoqi@0 158 public abstract @NotNull InputStream getInput() throws IOException;
aoqi@0 159
aoqi@0 160 /**
aoqi@0 161 * Transport's underlying output stream
aoqi@0 162 *
aoqi@0 163 * <p>
aoqi@0 164 * This method will be invoked exactly once by the JAX-WS RI
aoqi@0 165 * to start writing the response body (unless the processing aborts abnormally.)
aoqi@0 166 * Even if there's no response body to write, this method will
aoqi@0 167 * still be invoked only to be closed immediately.
aoqi@0 168 *
aoqi@0 169 * <p>
aoqi@0 170 * Once this method is called, the status code and response
aoqi@0 171 * headers will never change (IOW {@link #setStatus(int)},
aoqi@0 172 * {@link #setResponseHeaders}, and {@link #setContentTypeResponseHeader(String)}
aoqi@0 173 * will never be invoked.
aoqi@0 174 */
aoqi@0 175 public abstract @NotNull OutputStream getOutput() throws IOException;
aoqi@0 176
aoqi@0 177 /**
aoqi@0 178 * Returns the {@link WebServiceContextDelegate} for this connection.
aoqi@0 179 */
aoqi@0 180 public abstract @NotNull WebServiceContextDelegate getWebServiceContextDelegate();
aoqi@0 181
aoqi@0 182 /**
aoqi@0 183 * HTTP request method, such as "GET" or "POST".
aoqi@0 184 */
aoqi@0 185 public abstract @NotNull String getRequestMethod();
aoqi@0 186
aoqi@0 187 /**
aoqi@0 188 * HTTP request headers.
aoqi@0 189 *
aoqi@0 190 * @deprecated
aoqi@0 191 * This is a potentially expensive operation.
aoqi@0 192 * Programs that want to access HTTP headers should consider using
aoqi@0 193 * other methods such as {@link #getRequestHeader(String)}.
aoqi@0 194 *
aoqi@0 195 * @return
aoqi@0 196 * can be empty but never null.
aoqi@0 197 */
aoqi@0 198 public abstract @NotNull Map<String,List<String>> getRequestHeaders();
aoqi@0 199
aoqi@0 200 /**
aoqi@0 201 * HTTP request header names.
aoqi@0 202 *
aoqi@0 203 * @deprecated
aoqi@0 204 * This is a potentially expensive operation.
aoqi@0 205 * Programs that want to access HTTP headers should consider using
aoqi@0 206 * other methods such as {@link #getRequestHeader(String)}.
aoqi@0 207 *
aoqi@0 208 * @return
aoqi@0 209 * can be empty but never null.
aoqi@0 210 */
aoqi@0 211 public abstract @NotNull Set<String> getRequestHeaderNames();
aoqi@0 212
aoqi@0 213 /**
aoqi@0 214 * @return
aoqi@0 215 * HTTP response headers.
aoqi@0 216 */
aoqi@0 217 public abstract Map<String,List<String>> getResponseHeaders();
aoqi@0 218
aoqi@0 219 /**
aoqi@0 220 * Gets an HTTP request header.
aoqi@0 221 *
aoqi@0 222 * <p>
aoqi@0 223 * if multiple headers are present, this method returns one of them.
aoqi@0 224 * (The implementation is free to choose which one it returns.)
aoqi@0 225 *
aoqi@0 226 * @return
aoqi@0 227 * null if no header exists.
aoqi@0 228 */
aoqi@0 229 public abstract @Nullable String getRequestHeader(@NotNull String headerName);
aoqi@0 230
aoqi@0 231 /**
aoqi@0 232 * Gets an HTTP request header.
aoqi@0 233 *
aoqi@0 234 * @return
aoqi@0 235 * null if no header exists.
aoqi@0 236 */
aoqi@0 237 public abstract @Nullable List<String> getRequestHeaderValues(@NotNull String headerName);
aoqi@0 238
aoqi@0 239 /**
aoqi@0 240 * HTTP Query string, such as "foo=bar", or null if none exists.
aoqi@0 241 */
aoqi@0 242 public abstract @Nullable String getQueryString();
aoqi@0 243
aoqi@0 244 /**
aoqi@0 245 * Extra portion of the request URI after the end of the expected address of the service
aoqi@0 246 * but before the query string
aoqi@0 247 */
aoqi@0 248 public abstract @Nullable String getPathInfo();
aoqi@0 249
aoqi@0 250 /**
aoqi@0 251 * Requested path. A string like "/foo/bar/baz"
aoqi@0 252 */
aoqi@0 253 public abstract @NotNull String getRequestURI();
aoqi@0 254
aoqi@0 255 /**
aoqi@0 256 * Requested scheme, e.g. "http" or "https"
aoqi@0 257 */
aoqi@0 258 public abstract @NotNull String getRequestScheme();
aoqi@0 259
aoqi@0 260 /**
aoqi@0 261 * Server name
aoqi@0 262 */
aoqi@0 263 public abstract @NotNull String getServerName();
aoqi@0 264
aoqi@0 265 /**
aoqi@0 266 * Server port
aoqi@0 267 */
aoqi@0 268 public abstract int getServerPort();
aoqi@0 269
aoqi@0 270 /**
aoqi@0 271 * Portion of the request URI that groups related service addresses. The value, if non-empty, will
aoqi@0 272 * always begin with '/', but will never end with '/'. Environments that do not support
aoqi@0 273 * context paths must return an empty string.
aoqi@0 274 */
aoqi@0 275 public @NotNull String getContextPath() {
aoqi@0 276 return "";
aoqi@0 277 }
aoqi@0 278
aoqi@0 279 /**
aoqi@0 280 * Environment specific context , if available
aoqi@0 281 */
aoqi@0 282 public Object getContext() {
aoqi@0 283 return null;
aoqi@0 284 }
aoqi@0 285
aoqi@0 286 /**
aoqi@0 287 * Gets the absolute URL up to the context path.
aoqi@0 288 * @return
aoqi@0 289 * String like "http://myhost/myapp"
aoqi@0 290 * @since 2.1.2
aoqi@0 291 */
aoqi@0 292 public @NotNull String getBaseAddress() {
aoqi@0 293 throw new UnsupportedOperationException();
aoqi@0 294 }
aoqi@0 295
aoqi@0 296 /**
aoqi@0 297 * Whether connection is HTTPS or not
aoqi@0 298 *
aoqi@0 299 * @return if the received request is on HTTPS, return true
aoqi@0 300 * else false
aoqi@0 301 */
aoqi@0 302 public abstract boolean isSecure();
aoqi@0 303
aoqi@0 304 /**
aoqi@0 305 * User principal associated with the request
aoqi@0 306 *
aoqi@0 307 * @return user principal
aoqi@0 308 */
aoqi@0 309 public Principal getUserPrincipal() {
aoqi@0 310 return null;
aoqi@0 311 }
aoqi@0 312
aoqi@0 313 /**
aoqi@0 314 * Whether user associated with the request holds the given role
aoqi@0 315 *
aoqi@0 316 * @param role Role to check
aoqi@0 317 * @return if the caller holds the role
aoqi@0 318 */
aoqi@0 319 public boolean isUserInRole(String role) {
aoqi@0 320 return false;
aoqi@0 321 }
aoqi@0 322
aoqi@0 323 /**
aoqi@0 324 * Gets request metadata attribute
aoqi@0 325 * @param key Request metadata key
aoqi@0 326 * @return Value of metadata attribute or null, if no value present
aoqi@0 327 */
aoqi@0 328 public Object getRequestAttribute(String key) {
aoqi@0 329 return null;
aoqi@0 330 }
aoqi@0 331
aoqi@0 332 private volatile boolean closed;
aoqi@0 333
aoqi@0 334 /**
aoqi@0 335 * Close the connection
aoqi@0 336 */
aoqi@0 337 public void close() {
aoqi@0 338 this.closed = true;
aoqi@0 339 }
aoqi@0 340
aoqi@0 341 /**
aoqi@0 342 * Retuns whether connection is closed or not.
aoqi@0 343 */
aoqi@0 344 public boolean isClosed() {
aoqi@0 345 return closed;
aoqi@0 346 }
aoqi@0 347
aoqi@0 348 /**
aoqi@0 349 * Subclasses are expected to override
aoqi@0 350 *
aoqi@0 351 * @return a {@link String} containing the protocol name and version number
aoqi@0 352 */
aoqi@0 353 public String getProtocol() {
aoqi@0 354 return "HTTP/1.1";
aoqi@0 355 }
aoqi@0 356
aoqi@0 357 /**
aoqi@0 358 * Subclasses are expected to override
aoqi@0 359 *
aoqi@0 360 * @since JAX-WS RI 2.2.2
aoqi@0 361 * @return value of given cookie
aoqi@0 362 */
aoqi@0 363 public String getCookie(String name) {
aoqi@0 364 return null;
aoqi@0 365 }
aoqi@0 366
aoqi@0 367 /**
aoqi@0 368 * Subclasses are expected to override
aoqi@0 369 *
aoqi@0 370 *
aoqi@0 371 * @since JAX-WS RI 2.2.2
aoqi@0 372 */
aoqi@0 373 public void setCookie(String name, String value) {
aoqi@0 374 }
aoqi@0 375
aoqi@0 376 /**
aoqi@0 377 * Subclasses are expected to override
aoqi@0 378 */
aoqi@0 379 public void setContentLengthResponseHeader(int value) {
aoqi@0 380 }
aoqi@0 381
aoqi@0 382 }

mercurial