src/share/jaxws_classes/com/sun/xml/internal/ws/transport/http/server/WSHttpHandler.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/WSHttpHandler.java	Tue Mar 06 16:09:35 2012 -0800
     1.3 @@ -0,0 +1,131 @@
     1.4 +/*
     1.5 + * Copyright (c) 1997, 2011, 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.istack.internal.Nullable;
    1.33 +import com.sun.net.httpserver.HttpExchange;
    1.34 +import com.sun.net.httpserver.HttpHandler;
    1.35 +import com.sun.xml.internal.ws.resources.HttpserverMessages;
    1.36 +import com.sun.xml.internal.ws.transport.http.HttpAdapter;
    1.37 +import com.sun.xml.internal.ws.transport.http.WSHTTPConnection;
    1.38 +
    1.39 +import java.io.IOException;
    1.40 +import java.util.concurrent.Executor;
    1.41 +import java.util.logging.Level;
    1.42 +import java.util.logging.Logger;
    1.43 +
    1.44 +/**
    1.45 + * {@link HttpHandler} implementation that serves the actual request.
    1.46 + *
    1.47 + * @author Jitendra Kotamraju
    1.48 + * @author Kohsuke Kawaguhi
    1.49 + */
    1.50 +final class WSHttpHandler implements HttpHandler {
    1.51 +
    1.52 +    private static final String GET_METHOD = "GET";
    1.53 +    private static final String POST_METHOD = "POST";
    1.54 +    private static final String HEAD_METHOD = "HEAD";
    1.55 +    private static final String PUT_METHOD = "PUT";
    1.56 +    private static final String DELETE_METHOD = "DELETE";
    1.57 +
    1.58 +    private static final Logger LOGGER =
    1.59 +        Logger.getLogger(
    1.60 +            com.sun.xml.internal.ws.util.Constants.LoggingDomain + ".server.http");
    1.61 +    private static final boolean fineTraceEnabled = LOGGER.isLoggable(Level.FINE);
    1.62 +
    1.63 +    private final HttpAdapter adapter;
    1.64 +    private final Executor executor;
    1.65 +
    1.66 +    public WSHttpHandler(@NotNull HttpAdapter adapter, @Nullable Executor executor) {
    1.67 +        assert adapter!=null;
    1.68 +        this.adapter = adapter;
    1.69 +        this.executor = executor;
    1.70 +    }
    1.71 +
    1.72 +    /**
    1.73 +     * Called by HttpServer when there is a matching request for the context
    1.74 +     */
    1.75 +    public void handle(HttpExchange msg) {
    1.76 +        try {
    1.77 +            if (fineTraceEnabled) {
    1.78 +                LOGGER.fine("Received HTTP request:"+msg.getRequestURI());
    1.79 +            }
    1.80 +            if (executor != null) {
    1.81 +                // Use application's Executor to handle request. Application may
    1.82 +                // have set an executor using Endpoint.setExecutor().
    1.83 +                executor.execute(new HttpHandlerRunnable(msg));
    1.84 +            } else {
    1.85 +                handleExchange(msg);
    1.86 +            }
    1.87 +        } catch(Throwable e) {
    1.88 +            // Dont't propagate the exception otherwise it kills the httpserver
    1.89 +        }
    1.90 +    }
    1.91 +
    1.92 +    private void handleExchange(HttpExchange msg) throws IOException {
    1.93 +        WSHTTPConnection con = new ServerConnectionImpl(adapter,msg);
    1.94 +        try {
    1.95 +            if (fineTraceEnabled) {
    1.96 +                LOGGER.fine("Received HTTP request:"+msg.getRequestURI());
    1.97 +            }
    1.98 +            String method = msg.getRequestMethod();
    1.99 +            if(method.equals(GET_METHOD) || method.equals(POST_METHOD) || method.equals(HEAD_METHOD)
   1.100 +            || method.equals(PUT_METHOD) || method.equals(DELETE_METHOD)) {
   1.101 +                adapter.handle(con);
   1.102 +            } else {
   1.103 +                if (LOGGER.isLoggable(Level.WARNING)) {
   1.104 +                    LOGGER.warning(HttpserverMessages.UNEXPECTED_HTTP_METHOD(method));
   1.105 +                }
   1.106 +            }
   1.107 +        } finally {
   1.108 +            msg.close();
   1.109 +        }
   1.110 +    }
   1.111 +
   1.112 +    /**
   1.113 +     * Wrapping the processing of request in a Runnable so that it can be
   1.114 +     * executed in Executor.
   1.115 +     */
   1.116 +    class HttpHandlerRunnable implements Runnable {
   1.117 +        final HttpExchange msg;
   1.118 +
   1.119 +        HttpHandlerRunnable(HttpExchange msg) {
   1.120 +            this.msg = msg;
   1.121 +        }
   1.122 +
   1.123 +        public void run() {
   1.124 +            try {
   1.125 +                handleExchange(msg);
   1.126 +            } catch (Throwable e) {
   1.127 +                // Does application's executor handle this exception ?
   1.128 +                e.printStackTrace();
   1.129 +            }
   1.130 +        }
   1.131 +    }
   1.132 +
   1.133 +
   1.134 +}

mercurial