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

changeset 286
f50545b5e2f1
child 368
0989ad8c0860
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/ws/transport/http/server/ServerConnectionImpl.java	Tue Mar 06 16:09:35 2012 -0800
     1.3 @@ -0,0 +1,349 @@
     1.4 +/*
     1.5 + * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +package com.sun.xml.internal.ws.transport.http.server;
    1.30 +
    1.31 +import com.sun.istack.internal.NotNull;
    1.32 +import com.sun.net.httpserver.Headers;
    1.33 +import com.sun.net.httpserver.HttpExchange;
    1.34 +import com.sun.net.httpserver.HttpsExchange;
    1.35 +import com.sun.xml.internal.ws.api.message.Packet;
    1.36 +import com.sun.xml.internal.ws.api.server.WSEndpoint;
    1.37 +import com.sun.xml.internal.ws.api.server.WebServiceContextDelegate;
    1.38 +import com.sun.xml.internal.ws.api.server.PortAddressResolver;
    1.39 +import com.sun.xml.internal.ws.transport.http.HttpAdapter;
    1.40 +import com.sun.xml.internal.ws.transport.http.WSHTTPConnection;
    1.41 +import com.sun.xml.internal.ws.developer.JAXWSProperties;
    1.42 +import com.sun.xml.internal.ws.resources.WsservletMessages;
    1.43 +import com.sun.xml.internal.ws.util.ReadAllStream;
    1.44 +
    1.45 +import javax.xml.ws.handler.MessageContext;
    1.46 +import javax.xml.ws.WebServiceException;
    1.47 +import java.io.FilterInputStream;
    1.48 +import java.io.FilterOutputStream;
    1.49 +import java.io.IOException;
    1.50 +import java.io.InputStream;
    1.51 +import java.io.OutputStream;
    1.52 +import java.net.URI;
    1.53 +import java.security.Principal;
    1.54 +import java.util.ArrayList;
    1.55 +import java.util.List;
    1.56 +import java.util.Map;
    1.57 +import java.util.Set;
    1.58 +
    1.59 +
    1.60 +/**
    1.61 + * {@link WSHTTPConnection} used with Java SE endpoints. It provides connection
    1.62 + * implementation using {@link HttpExchange} object.
    1.63 + *
    1.64 + * @author Jitendra Kotamraju
    1.65 + */
    1.66 +final class ServerConnectionImpl extends WSHTTPConnection implements WebServiceContextDelegate {
    1.67 +
    1.68 +    private final HttpExchange httpExchange;
    1.69 +    private int status;
    1.70 +    private final HttpAdapter adapter;
    1.71 +    private LWHSInputStream in;
    1.72 +    private OutputStream out;
    1.73 +
    1.74 +
    1.75 +    public ServerConnectionImpl(@NotNull HttpAdapter adapter, @NotNull HttpExchange httpExchange) {
    1.76 +        this.adapter = adapter;
    1.77 +        this.httpExchange = httpExchange;
    1.78 +    }
    1.79 +
    1.80 +    @Override
    1.81 +    @Property(value = {MessageContext.HTTP_REQUEST_HEADERS, Packet.INBOUND_TRANSPORT_HEADERS})
    1.82 +    public @NotNull Map<String,List<String>> getRequestHeaders() {
    1.83 +        return httpExchange.getRequestHeaders();
    1.84 +    }
    1.85 +
    1.86 +    @Override
    1.87 +    public String getRequestHeader(String headerName) {
    1.88 +        return httpExchange.getRequestHeaders().getFirst(headerName);
    1.89 +    }
    1.90 +
    1.91 +    @Override
    1.92 +    public void setResponseHeaders(Map<String,List<String>> headers) {
    1.93 +        Headers r = httpExchange.getResponseHeaders();
    1.94 +        r.clear();
    1.95 +        for(Map.Entry <String, List<String>> entry : headers.entrySet()) {
    1.96 +            String name = entry.getKey();
    1.97 +            List<String> values = entry.getValue();
    1.98 +            // ignore headers that interfere with our correct operations
    1.99 +            if (!"Content-Length".equalsIgnoreCase(name) && !"Content-Type".equalsIgnoreCase(name)) {
   1.100 +                r.put(name,new ArrayList<String>(values));
   1.101 +            }
   1.102 +        }
   1.103 +    }
   1.104 +
   1.105 +    @Override
   1.106 +        public void setResponseHeader(String key, List<String> value) {
   1.107 +                httpExchange.getResponseHeaders().put(key, value);
   1.108 +        }
   1.109 +
   1.110 +        @Override
   1.111 +        public Set<String> getRequestHeaderNames() {
   1.112 +        return httpExchange.getRequestHeaders().keySet();
   1.113 +        }
   1.114 +
   1.115 +        @Override
   1.116 +        public List<String> getRequestHeaderValues(String headerName) {
   1.117 +                return httpExchange.getRequestHeaders().get(headerName);
   1.118 +        }
   1.119 +
   1.120 +    @Override
   1.121 +    @Property({MessageContext.HTTP_RESPONSE_HEADERS,Packet.OUTBOUND_TRANSPORT_HEADERS})
   1.122 +    public Map<String,List<String>> getResponseHeaders() {
   1.123 +        return httpExchange.getResponseHeaders();
   1.124 +    }
   1.125 +
   1.126 +    @Override
   1.127 +    public void setContentTypeResponseHeader(@NotNull String value) {
   1.128 +        httpExchange.getResponseHeaders().set("Content-Type",value);
   1.129 +    }
   1.130 +
   1.131 +    @Override
   1.132 +    public void setStatus(int status) {
   1.133 +        this.status = status;
   1.134 +    }
   1.135 +
   1.136 +    @Override
   1.137 +    @Property(MessageContext.HTTP_RESPONSE_CODE)
   1.138 +    public int getStatus() {
   1.139 +        return status;
   1.140 +    }
   1.141 +
   1.142 +    public @NotNull InputStream getInput() {
   1.143 +        if (in == null) {
   1.144 +            in = new LWHSInputStream(httpExchange.getRequestBody());
   1.145 +        }
   1.146 +        return in;
   1.147 +    }
   1.148 +
   1.149 +    // Light weight http server's InputStream.close() throws exception if
   1.150 +    // all the bytes are not read. Work around until it is fixed.
   1.151 +    private static class LWHSInputStream extends FilterInputStream {
   1.152 +        // Workaround for "SJSXP XMLStreamReader.next() closes stream".
   1.153 +        boolean closed;
   1.154 +        boolean readAll;
   1.155 +
   1.156 +        LWHSInputStream(InputStream in) {
   1.157 +            super(in);
   1.158 +        }
   1.159 +
   1.160 +        void readAll() throws IOException {
   1.161 +            if (!closed && !readAll) {
   1.162 +                ReadAllStream all = new ReadAllStream();
   1.163 +                all.readAll(in, 4000000);
   1.164 +                in.close();
   1.165 +                in = all;
   1.166 +                readAll = true;
   1.167 +            }
   1.168 +        }
   1.169 +
   1.170 +        @Override
   1.171 +        public void close() throws IOException {
   1.172 +            if (!closed) {
   1.173 +                readAll();
   1.174 +                super.close();
   1.175 +                closed = true;
   1.176 +            }
   1.177 +        }
   1.178 +
   1.179 +    }
   1.180 +
   1.181 +
   1.182 +    public @NotNull OutputStream getOutput() throws IOException {
   1.183 +        if (out == null) {
   1.184 +            String lenHeader = httpExchange.getResponseHeaders().getFirst("Content-Length");
   1.185 +            int length = (lenHeader != null) ? Integer.parseInt(lenHeader) : 0;
   1.186 +            httpExchange.sendResponseHeaders(getStatus(), length);
   1.187 +
   1.188 +            // Light weight http server's OutputStream.close() throws exception if
   1.189 +            // all the bytes are not read on the client side(StreamMessage on the client
   1.190 +            // side doesn't read all bytes.
   1.191 +            out =  new FilterOutputStream(httpExchange.getResponseBody()) {
   1.192 +                boolean closed;
   1.193 +                @Override
   1.194 +                public void close() throws IOException {
   1.195 +                    if (!closed) {
   1.196 +                        closed = true;
   1.197 +                        // lwhs closes input stream, when you close the output stream
   1.198 +                        // This causes problems for streaming in one-way cases
   1.199 +                        in.readAll();
   1.200 +                        try {
   1.201 +                            super.close();
   1.202 +                        } catch(IOException ioe) {
   1.203 +                            // Ignoring purposefully.
   1.204 +                        }
   1.205 +                    }
   1.206 +                }
   1.207 +
   1.208 +                // Otherwise, FilterOutpuStream writes byte by byte
   1.209 +                @Override
   1.210 +                public void write(byte[] buf, int start, int len) throws IOException {
   1.211 +                    out.write(buf, start, len);
   1.212 +                }
   1.213 +            };
   1.214 +        }
   1.215 +        return out;
   1.216 +    }
   1.217 +
   1.218 +    public @NotNull WebServiceContextDelegate getWebServiceContextDelegate() {
   1.219 +        return this;
   1.220 +    }
   1.221 +
   1.222 +    public Principal getUserPrincipal(Packet request) {
   1.223 +        return httpExchange.getPrincipal();
   1.224 +    }
   1.225 +
   1.226 +    public boolean isUserInRole(Packet request, String role) {
   1.227 +        return false;
   1.228 +    }
   1.229 +
   1.230 +    public @NotNull String getEPRAddress(Packet request, WSEndpoint endpoint) {
   1.231 +        //return WSHttpHandler.getRequestAddress(httpExchange);
   1.232 +
   1.233 +        PortAddressResolver resolver = adapter.owner.createPortAddressResolver(getBaseAddress());
   1.234 +        String address = resolver.getAddressFor(endpoint.getServiceName(), endpoint.getPortName().getLocalPart());
   1.235 +        if(address==null)
   1.236 +            throw new WebServiceException(WsservletMessages.SERVLET_NO_ADDRESS_AVAILABLE(endpoint.getPortName()));
   1.237 +        return address;
   1.238 +
   1.239 +    }
   1.240 +
   1.241 +    public String getWSDLAddress(@NotNull Packet request, @NotNull WSEndpoint endpoint) {
   1.242 +        String eprAddress = getEPRAddress(request,endpoint);
   1.243 +        if(adapter.getEndpoint().getPort() != null)
   1.244 +            return eprAddress+"?wsdl";
   1.245 +        else
   1.246 +            return null;
   1.247 +    }
   1.248 +
   1.249 +    @Override
   1.250 +    public boolean isSecure() {
   1.251 +        return (httpExchange instanceof HttpsExchange);
   1.252 +    }
   1.253 +
   1.254 +    @Override
   1.255 +    @Property(MessageContext.HTTP_REQUEST_METHOD)
   1.256 +    public @NotNull String getRequestMethod() {
   1.257 +        return httpExchange.getRequestMethod();
   1.258 +    }
   1.259 +
   1.260 +    @Override
   1.261 +    @Property(MessageContext.QUERY_STRING)
   1.262 +    public String getQueryString() {
   1.263 +        URI requestUri = httpExchange.getRequestURI();
   1.264 +        String query = requestUri.getQuery();
   1.265 +        if (query != null)
   1.266 +            return query;
   1.267 +        return null;
   1.268 +    }
   1.269 +
   1.270 +    @Override
   1.271 +    @Property(MessageContext.PATH_INFO)
   1.272 +    public String getPathInfo() {
   1.273 +        URI requestUri = httpExchange.getRequestURI();
   1.274 +        String reqPath = requestUri.getPath();
   1.275 +        String ctxtPath = httpExchange.getHttpContext().getPath();
   1.276 +        if (reqPath.length() > ctxtPath.length()) {
   1.277 +            return reqPath.substring(ctxtPath.length());
   1.278 +        }
   1.279 +        return null;
   1.280 +    }
   1.281 +
   1.282 +    @Property(JAXWSProperties.HTTP_EXCHANGE)
   1.283 +    public HttpExchange getExchange() {
   1.284 +        return httpExchange;
   1.285 +    }
   1.286 +
   1.287 +    @Override @NotNull
   1.288 +    public String getBaseAddress() {
   1.289 +        /*
   1.290 +         * Computes the Endpoint's address from the request. Use "Host" header
   1.291 +         * so that it has correct address(IP address or someother hostname)
   1.292 +         * through which the application reached the endpoint.
   1.293 +         *
   1.294 +         */
   1.295 +        StringBuilder strBuf = new StringBuilder();
   1.296 +        strBuf.append((httpExchange instanceof HttpsExchange) ? "https" : "http");
   1.297 +        strBuf.append("://");
   1.298 +
   1.299 +        String hostHeader = httpExchange.getRequestHeaders().getFirst("Host");
   1.300 +        if (hostHeader != null) {
   1.301 +            strBuf.append(hostHeader);   // Uses Host header
   1.302 +        } else {
   1.303 +            strBuf.append(httpExchange.getLocalAddress().getHostName());
   1.304 +            strBuf.append(":");
   1.305 +            strBuf.append(httpExchange.getLocalAddress().getPort());
   1.306 +        }
   1.307 +        //Do not include URL pattern here
   1.308 +        //strBuf.append(httpExchange.getRequestURI().getPath());
   1.309 +
   1.310 +        return strBuf.toString();
   1.311 +    }
   1.312 +
   1.313 +    @Override
   1.314 +    public String getProtocol() {
   1.315 +        return httpExchange.getProtocol();
   1.316 +    }
   1.317 +
   1.318 +    @Override
   1.319 +    public void setContentLengthResponseHeader(int value) {
   1.320 +        httpExchange.getResponseHeaders().set("Content-Length", ""+value);
   1.321 +    }
   1.322 +
   1.323 +        @Override
   1.324 +        public String getRequestURI() {
   1.325 +                return httpExchange.getRequestURI().toString();
   1.326 +        }
   1.327 +
   1.328 +        @Override
   1.329 +        public String getRequestScheme() {
   1.330 +                return (httpExchange instanceof HttpsExchange) ? "https" : "http";
   1.331 +        }
   1.332 +
   1.333 +        @Override
   1.334 +        public String getServerName() {
   1.335 +                return httpExchange.getLocalAddress().getHostName();
   1.336 +        }
   1.337 +
   1.338 +        @Override
   1.339 +        public int getServerPort() {
   1.340 +                return httpExchange.getLocalAddress().getPort();
   1.341 +        }
   1.342 +
   1.343 +    protected PropertyMap getPropertyMap() {
   1.344 +        return model;
   1.345 +    }
   1.346 +
   1.347 +    private static final PropertyMap model;
   1.348 +
   1.349 +    static {
   1.350 +        model = parse(ServerConnectionImpl.class);
   1.351 +    }
   1.352 +}

mercurial