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

Tue, 06 Mar 2012 16:09:35 -0800

author
ohair
date
Tue, 06 Mar 2012 16:09:35 -0800
changeset 286
f50545b5e2f1
child 368
0989ad8c0860
permissions
-rw-r--r--

7150322: Stop using drop source bundles in jaxws
Reviewed-by: darcy, ohrstrom

ohair@286 1 /*
ohair@286 2 * Copyright (c) 1997, 2010, 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.transport.http.server;
ohair@286 27
ohair@286 28 import com.sun.istack.internal.NotNull;
ohair@286 29 import com.sun.net.httpserver.Headers;
ohair@286 30 import com.sun.net.httpserver.HttpExchange;
ohair@286 31 import com.sun.net.httpserver.HttpsExchange;
ohair@286 32 import com.sun.xml.internal.ws.api.message.Packet;
ohair@286 33 import com.sun.xml.internal.ws.api.server.WSEndpoint;
ohair@286 34 import com.sun.xml.internal.ws.api.server.WebServiceContextDelegate;
ohair@286 35 import com.sun.xml.internal.ws.api.server.PortAddressResolver;
ohair@286 36 import com.sun.xml.internal.ws.transport.http.HttpAdapter;
ohair@286 37 import com.sun.xml.internal.ws.transport.http.WSHTTPConnection;
ohair@286 38 import com.sun.xml.internal.ws.developer.JAXWSProperties;
ohair@286 39 import com.sun.xml.internal.ws.resources.WsservletMessages;
ohair@286 40 import com.sun.xml.internal.ws.util.ReadAllStream;
ohair@286 41
ohair@286 42 import javax.xml.ws.handler.MessageContext;
ohair@286 43 import javax.xml.ws.WebServiceException;
ohair@286 44 import java.io.FilterInputStream;
ohair@286 45 import java.io.FilterOutputStream;
ohair@286 46 import java.io.IOException;
ohair@286 47 import java.io.InputStream;
ohair@286 48 import java.io.OutputStream;
ohair@286 49 import java.net.URI;
ohair@286 50 import java.security.Principal;
ohair@286 51 import java.util.ArrayList;
ohair@286 52 import java.util.List;
ohair@286 53 import java.util.Map;
ohair@286 54 import java.util.Set;
ohair@286 55
ohair@286 56
ohair@286 57 /**
ohair@286 58 * {@link WSHTTPConnection} used with Java SE endpoints. It provides connection
ohair@286 59 * implementation using {@link HttpExchange} object.
ohair@286 60 *
ohair@286 61 * @author Jitendra Kotamraju
ohair@286 62 */
ohair@286 63 final class ServerConnectionImpl extends WSHTTPConnection implements WebServiceContextDelegate {
ohair@286 64
ohair@286 65 private final HttpExchange httpExchange;
ohair@286 66 private int status;
ohair@286 67 private final HttpAdapter adapter;
ohair@286 68 private LWHSInputStream in;
ohair@286 69 private OutputStream out;
ohair@286 70
ohair@286 71
ohair@286 72 public ServerConnectionImpl(@NotNull HttpAdapter adapter, @NotNull HttpExchange httpExchange) {
ohair@286 73 this.adapter = adapter;
ohair@286 74 this.httpExchange = httpExchange;
ohair@286 75 }
ohair@286 76
ohair@286 77 @Override
ohair@286 78 @Property(value = {MessageContext.HTTP_REQUEST_HEADERS, Packet.INBOUND_TRANSPORT_HEADERS})
ohair@286 79 public @NotNull Map<String,List<String>> getRequestHeaders() {
ohair@286 80 return httpExchange.getRequestHeaders();
ohair@286 81 }
ohair@286 82
ohair@286 83 @Override
ohair@286 84 public String getRequestHeader(String headerName) {
ohair@286 85 return httpExchange.getRequestHeaders().getFirst(headerName);
ohair@286 86 }
ohair@286 87
ohair@286 88 @Override
ohair@286 89 public void setResponseHeaders(Map<String,List<String>> headers) {
ohair@286 90 Headers r = httpExchange.getResponseHeaders();
ohair@286 91 r.clear();
ohair@286 92 for(Map.Entry <String, List<String>> entry : headers.entrySet()) {
ohair@286 93 String name = entry.getKey();
ohair@286 94 List<String> values = entry.getValue();
ohair@286 95 // ignore headers that interfere with our correct operations
ohair@286 96 if (!"Content-Length".equalsIgnoreCase(name) && !"Content-Type".equalsIgnoreCase(name)) {
ohair@286 97 r.put(name,new ArrayList<String>(values));
ohair@286 98 }
ohair@286 99 }
ohair@286 100 }
ohair@286 101
ohair@286 102 @Override
ohair@286 103 public void setResponseHeader(String key, List<String> value) {
ohair@286 104 httpExchange.getResponseHeaders().put(key, value);
ohair@286 105 }
ohair@286 106
ohair@286 107 @Override
ohair@286 108 public Set<String> getRequestHeaderNames() {
ohair@286 109 return httpExchange.getRequestHeaders().keySet();
ohair@286 110 }
ohair@286 111
ohair@286 112 @Override
ohair@286 113 public List<String> getRequestHeaderValues(String headerName) {
ohair@286 114 return httpExchange.getRequestHeaders().get(headerName);
ohair@286 115 }
ohair@286 116
ohair@286 117 @Override
ohair@286 118 @Property({MessageContext.HTTP_RESPONSE_HEADERS,Packet.OUTBOUND_TRANSPORT_HEADERS})
ohair@286 119 public Map<String,List<String>> getResponseHeaders() {
ohair@286 120 return httpExchange.getResponseHeaders();
ohair@286 121 }
ohair@286 122
ohair@286 123 @Override
ohair@286 124 public void setContentTypeResponseHeader(@NotNull String value) {
ohair@286 125 httpExchange.getResponseHeaders().set("Content-Type",value);
ohair@286 126 }
ohair@286 127
ohair@286 128 @Override
ohair@286 129 public void setStatus(int status) {
ohair@286 130 this.status = status;
ohair@286 131 }
ohair@286 132
ohair@286 133 @Override
ohair@286 134 @Property(MessageContext.HTTP_RESPONSE_CODE)
ohair@286 135 public int getStatus() {
ohair@286 136 return status;
ohair@286 137 }
ohair@286 138
ohair@286 139 public @NotNull InputStream getInput() {
ohair@286 140 if (in == null) {
ohair@286 141 in = new LWHSInputStream(httpExchange.getRequestBody());
ohair@286 142 }
ohair@286 143 return in;
ohair@286 144 }
ohair@286 145
ohair@286 146 // Light weight http server's InputStream.close() throws exception if
ohair@286 147 // all the bytes are not read. Work around until it is fixed.
ohair@286 148 private static class LWHSInputStream extends FilterInputStream {
ohair@286 149 // Workaround for "SJSXP XMLStreamReader.next() closes stream".
ohair@286 150 boolean closed;
ohair@286 151 boolean readAll;
ohair@286 152
ohair@286 153 LWHSInputStream(InputStream in) {
ohair@286 154 super(in);
ohair@286 155 }
ohair@286 156
ohair@286 157 void readAll() throws IOException {
ohair@286 158 if (!closed && !readAll) {
ohair@286 159 ReadAllStream all = new ReadAllStream();
ohair@286 160 all.readAll(in, 4000000);
ohair@286 161 in.close();
ohair@286 162 in = all;
ohair@286 163 readAll = true;
ohair@286 164 }
ohair@286 165 }
ohair@286 166
ohair@286 167 @Override
ohair@286 168 public void close() throws IOException {
ohair@286 169 if (!closed) {
ohair@286 170 readAll();
ohair@286 171 super.close();
ohair@286 172 closed = true;
ohair@286 173 }
ohair@286 174 }
ohair@286 175
ohair@286 176 }
ohair@286 177
ohair@286 178
ohair@286 179 public @NotNull OutputStream getOutput() throws IOException {
ohair@286 180 if (out == null) {
ohair@286 181 String lenHeader = httpExchange.getResponseHeaders().getFirst("Content-Length");
ohair@286 182 int length = (lenHeader != null) ? Integer.parseInt(lenHeader) : 0;
ohair@286 183 httpExchange.sendResponseHeaders(getStatus(), length);
ohair@286 184
ohair@286 185 // Light weight http server's OutputStream.close() throws exception if
ohair@286 186 // all the bytes are not read on the client side(StreamMessage on the client
ohair@286 187 // side doesn't read all bytes.
ohair@286 188 out = new FilterOutputStream(httpExchange.getResponseBody()) {
ohair@286 189 boolean closed;
ohair@286 190 @Override
ohair@286 191 public void close() throws IOException {
ohair@286 192 if (!closed) {
ohair@286 193 closed = true;
ohair@286 194 // lwhs closes input stream, when you close the output stream
ohair@286 195 // This causes problems for streaming in one-way cases
ohair@286 196 in.readAll();
ohair@286 197 try {
ohair@286 198 super.close();
ohair@286 199 } catch(IOException ioe) {
ohair@286 200 // Ignoring purposefully.
ohair@286 201 }
ohair@286 202 }
ohair@286 203 }
ohair@286 204
ohair@286 205 // Otherwise, FilterOutpuStream writes byte by byte
ohair@286 206 @Override
ohair@286 207 public void write(byte[] buf, int start, int len) throws IOException {
ohair@286 208 out.write(buf, start, len);
ohair@286 209 }
ohair@286 210 };
ohair@286 211 }
ohair@286 212 return out;
ohair@286 213 }
ohair@286 214
ohair@286 215 public @NotNull WebServiceContextDelegate getWebServiceContextDelegate() {
ohair@286 216 return this;
ohair@286 217 }
ohair@286 218
ohair@286 219 public Principal getUserPrincipal(Packet request) {
ohair@286 220 return httpExchange.getPrincipal();
ohair@286 221 }
ohair@286 222
ohair@286 223 public boolean isUserInRole(Packet request, String role) {
ohair@286 224 return false;
ohair@286 225 }
ohair@286 226
ohair@286 227 public @NotNull String getEPRAddress(Packet request, WSEndpoint endpoint) {
ohair@286 228 //return WSHttpHandler.getRequestAddress(httpExchange);
ohair@286 229
ohair@286 230 PortAddressResolver resolver = adapter.owner.createPortAddressResolver(getBaseAddress());
ohair@286 231 String address = resolver.getAddressFor(endpoint.getServiceName(), endpoint.getPortName().getLocalPart());
ohair@286 232 if(address==null)
ohair@286 233 throw new WebServiceException(WsservletMessages.SERVLET_NO_ADDRESS_AVAILABLE(endpoint.getPortName()));
ohair@286 234 return address;
ohair@286 235
ohair@286 236 }
ohair@286 237
ohair@286 238 public String getWSDLAddress(@NotNull Packet request, @NotNull WSEndpoint endpoint) {
ohair@286 239 String eprAddress = getEPRAddress(request,endpoint);
ohair@286 240 if(adapter.getEndpoint().getPort() != null)
ohair@286 241 return eprAddress+"?wsdl";
ohair@286 242 else
ohair@286 243 return null;
ohair@286 244 }
ohair@286 245
ohair@286 246 @Override
ohair@286 247 public boolean isSecure() {
ohair@286 248 return (httpExchange instanceof HttpsExchange);
ohair@286 249 }
ohair@286 250
ohair@286 251 @Override
ohair@286 252 @Property(MessageContext.HTTP_REQUEST_METHOD)
ohair@286 253 public @NotNull String getRequestMethod() {
ohair@286 254 return httpExchange.getRequestMethod();
ohair@286 255 }
ohair@286 256
ohair@286 257 @Override
ohair@286 258 @Property(MessageContext.QUERY_STRING)
ohair@286 259 public String getQueryString() {
ohair@286 260 URI requestUri = httpExchange.getRequestURI();
ohair@286 261 String query = requestUri.getQuery();
ohair@286 262 if (query != null)
ohair@286 263 return query;
ohair@286 264 return null;
ohair@286 265 }
ohair@286 266
ohair@286 267 @Override
ohair@286 268 @Property(MessageContext.PATH_INFO)
ohair@286 269 public String getPathInfo() {
ohair@286 270 URI requestUri = httpExchange.getRequestURI();
ohair@286 271 String reqPath = requestUri.getPath();
ohair@286 272 String ctxtPath = httpExchange.getHttpContext().getPath();
ohair@286 273 if (reqPath.length() > ctxtPath.length()) {
ohair@286 274 return reqPath.substring(ctxtPath.length());
ohair@286 275 }
ohair@286 276 return null;
ohair@286 277 }
ohair@286 278
ohair@286 279 @Property(JAXWSProperties.HTTP_EXCHANGE)
ohair@286 280 public HttpExchange getExchange() {
ohair@286 281 return httpExchange;
ohair@286 282 }
ohair@286 283
ohair@286 284 @Override @NotNull
ohair@286 285 public String getBaseAddress() {
ohair@286 286 /*
ohair@286 287 * Computes the Endpoint's address from the request. Use "Host" header
ohair@286 288 * so that it has correct address(IP address or someother hostname)
ohair@286 289 * through which the application reached the endpoint.
ohair@286 290 *
ohair@286 291 */
ohair@286 292 StringBuilder strBuf = new StringBuilder();
ohair@286 293 strBuf.append((httpExchange instanceof HttpsExchange) ? "https" : "http");
ohair@286 294 strBuf.append("://");
ohair@286 295
ohair@286 296 String hostHeader = httpExchange.getRequestHeaders().getFirst("Host");
ohair@286 297 if (hostHeader != null) {
ohair@286 298 strBuf.append(hostHeader); // Uses Host header
ohair@286 299 } else {
ohair@286 300 strBuf.append(httpExchange.getLocalAddress().getHostName());
ohair@286 301 strBuf.append(":");
ohair@286 302 strBuf.append(httpExchange.getLocalAddress().getPort());
ohair@286 303 }
ohair@286 304 //Do not include URL pattern here
ohair@286 305 //strBuf.append(httpExchange.getRequestURI().getPath());
ohair@286 306
ohair@286 307 return strBuf.toString();
ohair@286 308 }
ohair@286 309
ohair@286 310 @Override
ohair@286 311 public String getProtocol() {
ohair@286 312 return httpExchange.getProtocol();
ohair@286 313 }
ohair@286 314
ohair@286 315 @Override
ohair@286 316 public void setContentLengthResponseHeader(int value) {
ohair@286 317 httpExchange.getResponseHeaders().set("Content-Length", ""+value);
ohair@286 318 }
ohair@286 319
ohair@286 320 @Override
ohair@286 321 public String getRequestURI() {
ohair@286 322 return httpExchange.getRequestURI().toString();
ohair@286 323 }
ohair@286 324
ohair@286 325 @Override
ohair@286 326 public String getRequestScheme() {
ohair@286 327 return (httpExchange instanceof HttpsExchange) ? "https" : "http";
ohair@286 328 }
ohair@286 329
ohair@286 330 @Override
ohair@286 331 public String getServerName() {
ohair@286 332 return httpExchange.getLocalAddress().getHostName();
ohair@286 333 }
ohair@286 334
ohair@286 335 @Override
ohair@286 336 public int getServerPort() {
ohair@286 337 return httpExchange.getLocalAddress().getPort();
ohair@286 338 }
ohair@286 339
ohair@286 340 protected PropertyMap getPropertyMap() {
ohair@286 341 return model;
ohair@286 342 }
ohair@286 343
ohair@286 344 private static final PropertyMap model;
ohair@286 345
ohair@286 346 static {
ohair@286 347 model = parse(ServerConnectionImpl.class);
ohair@286 348 }
ohair@286 349 }

mercurial