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