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 Wed Apr 27 01:27:09 2016 +0800 1.3 @@ -0,0 +1,130 @@ 1.4 +/* 1.5 + * Copyright (c) 1997, 2013, 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.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 + */ 1.49 +final class PortableHttpHandler extends HttpHandler { 1.50 + 1.51 + private static final String GET_METHOD = "GET"; 1.52 + private static final String POST_METHOD = "POST"; 1.53 + private static final String HEAD_METHOD = "HEAD"; 1.54 + private static final String PUT_METHOD = "PUT"; 1.55 + private static final String DELETE_METHOD = "DELETE"; 1.56 + 1.57 + private static final Logger logger = 1.58 + Logger.getLogger( 1.59 + com.sun.xml.internal.ws.util.Constants.LoggingDomain + ".server.http"); 1.60 + 1.61 + private final HttpAdapter adapter; 1.62 + private final Executor executor; 1.63 + 1.64 + public PortableHttpHandler(@NotNull HttpAdapter adapter, @Nullable Executor executor) { 1.65 + assert adapter!=null; 1.66 + this.adapter = adapter; 1.67 + this.executor = executor; 1.68 + } 1.69 + 1.70 + /** 1.71 + * Called by HttpServer when there is a matching request for the context 1.72 + */ 1.73 + @Override 1.74 + public void handle(HttpExchange msg) { 1.75 + try { 1.76 + if (logger.isLoggable(Level.FINE)) { 1.77 + logger.log(Level.FINE, "Received HTTP request:{0}", msg.getRequestURI()); 1.78 + } 1.79 + if (executor != null) { 1.80 + // Use application's Executor to handle request. Application may 1.81 + // have set an executor using Endpoint.setExecutor(). 1.82 + executor.execute(new HttpHandlerRunnable(msg)); 1.83 + } else { 1.84 + handleExchange(msg); 1.85 + } 1.86 + } catch (Throwable e) { 1.87 + // Dont't propagate the exception otherwise it kills the httpserver 1.88 + logger.log(Level.SEVERE, null, e); 1.89 + } 1.90 + } 1.91 + 1.92 + public void handleExchange(HttpExchange msg) throws IOException { 1.93 + WSHTTPConnection con = new PortableConnectionImpl(adapter,msg); 1.94 + try { 1.95 + if (logger.isLoggable(Level.FINE)) { 1.96 + logger.log(Level.FINE, "Received HTTP request:{0}", 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 + logger.warning(HttpserverMessages.UNEXPECTED_HTTP_METHOD(method)); 1.104 + } 1.105 + } finally { 1.106 + msg.close(); 1.107 + } 1.108 + } 1.109 + 1.110 + /** 1.111 + * Wrapping the processing of request in a Runnable so that it can be 1.112 + * executed in Executor. 1.113 + */ 1.114 + class HttpHandlerRunnable implements Runnable { 1.115 + final HttpExchange msg; 1.116 + 1.117 + HttpHandlerRunnable(HttpExchange msg) { 1.118 + this.msg = msg; 1.119 + } 1.120 + 1.121 + @Override 1.122 + @SuppressWarnings("CallToThreadDumpStack") 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 +}