325N/A/*
325N/A * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
325N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
325N/A *
325N/A * This code is free software; you can redistribute it and/or modify it
325N/A * under the terms of the GNU General Public License version 2 only, as
325N/A * published by the Free Software Foundation. Oracle designates this
325N/A * particular file as subject to the "Classpath" exception as provided
325N/A * by Oracle in the LICENSE file that accompanied this code.
325N/A *
325N/A * This code is distributed in the hope that it will be useful, but WITHOUT
325N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
325N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
325N/A * version 2 for more details (a copy is included in the LICENSE file that
325N/A * accompanied this code).
325N/A *
325N/A * You should have received a copy of the GNU General Public License version
325N/A * 2 along with this work; if not, write to the Free Software Foundation,
325N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
325N/A *
325N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
325N/A * or visit www.oracle.com if you need additional information or have any
325N/A * questions.
325N/A */
325N/A
325N/Apackage com.sun.xml.internal.ws.transport.http.server;
325N/A
325N/Aimport com.sun.istack.internal.NotNull;
325N/Aimport com.sun.istack.internal.Nullable;
325N/Aimport com.sun.net.httpserver.HttpExchange;
325N/Aimport com.sun.net.httpserver.HttpHandler;
325N/Aimport com.sun.net.httpserver.HttpsExchange;
325N/Aimport com.sun.xml.internal.ws.resources.HttpserverMessages;
325N/Aimport com.sun.xml.internal.ws.transport.http.HttpAdapter;
325N/Aimport com.sun.xml.internal.ws.transport.http.WSHTTPConnection;
325N/A
325N/Aimport java.io.IOException;
325N/Aimport java.util.List;
325N/Aimport java.util.concurrent.Executor;
325N/Aimport java.util.logging.Logger;
325N/A
325N/A/**
325N/A * {@link HttpHandler} implementation that serves the actual request.
325N/A *
325N/A * @author Jitendra Kotamraju
325N/A * @author Kohsuke Kawaguhi
325N/A */
325N/Afinal class WSHttpHandler implements HttpHandler {
325N/A
325N/A private static final String GET_METHOD = "GET";
325N/A private static final String POST_METHOD = "POST";
325N/A private static final String HEAD_METHOD = "HEAD";
325N/A private static final String PUT_METHOD = "PUT";
325N/A private static final String DELETE_METHOD = "DELETE";
325N/A
325N/A private static final Logger logger =
325N/A Logger.getLogger(
325N/A com.sun.xml.internal.ws.util.Constants.LoggingDomain + ".server.http");
325N/A
325N/A private final HttpAdapter adapter;
325N/A private final Executor executor;
325N/A
325N/A public WSHttpHandler(@NotNull HttpAdapter adapter, @Nullable Executor executor) {
325N/A assert adapter!=null;
325N/A this.adapter = adapter;
325N/A this.executor = executor;
325N/A }
325N/A
325N/A /**
325N/A * Called by HttpServer when there is a matching request for the context
325N/A */
325N/A public void handle(HttpExchange msg) {
325N/A try {
325N/A logger.fine("Received HTTP request:"+msg.getRequestURI());
325N/A if (executor != null) {
325N/A // Use application's Executor to handle request. Application may
325N/A // have set an executor using Endpoint.setExecutor().
325N/A executor.execute(new HttpHandlerRunnable(msg));
325N/A } else {
325N/A handleExchange(msg);
325N/A }
325N/A } catch(Throwable e) {
325N/A // Dont't propagate the exception otherwise it kills the httpserver
325N/A e.printStackTrace();
325N/A }
325N/A }
325N/A
325N/A public void handleExchange(HttpExchange msg) throws IOException {
325N/A WSHTTPConnection con = new ServerConnectionImpl(adapter,msg);
325N/A try {
325N/A logger.fine("Received HTTP request:"+msg.getRequestURI());
325N/A String method = msg.getRequestMethod();
325N/A if(method.equals(GET_METHOD) || method.equals(POST_METHOD) || method.equals(HEAD_METHOD)
325N/A || method.equals(PUT_METHOD) || method.equals(DELETE_METHOD)) {
325N/A adapter.handle(con);
325N/A } else {
325N/A logger.warning(HttpserverMessages.UNEXPECTED_HTTP_METHOD(method));
325N/A }
325N/A } finally {
325N/A msg.close();
325N/A }
325N/A }
325N/A
325N/A /**
325N/A * Wrapping the processing of request in a Runnable so that it can be
325N/A * executed in Executor.
325N/A */
325N/A class HttpHandlerRunnable implements Runnable {
325N/A final HttpExchange msg;
325N/A
325N/A HttpHandlerRunnable(HttpExchange msg) {
325N/A this.msg = msg;
325N/A }
325N/A
325N/A public void run() {
325N/A try {
325N/A handleExchange(msg);
325N/A } catch (Throwable e) {
325N/A // Does application's executor handle this exception ?
325N/A e.printStackTrace();
325N/A }
325N/A }
325N/A }
325N/A
325N/A
325N/A}