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