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