|
1 /* |
|
2 * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. Oracle designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Oracle in the LICENSE file that accompanied this code. |
|
10 * |
|
11 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 * version 2 for more details (a copy is included in the LICENSE file that |
|
15 * accompanied this code). |
|
16 * |
|
17 * You should have received a copy of the GNU General Public License version |
|
18 * 2 along with this work; if not, write to the Free Software Foundation, |
|
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 * |
|
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 * or visit www.oracle.com if you need additional information or have any |
|
23 * questions. |
|
24 */ |
|
25 |
|
26 package com.sun.xml.internal.ws.transport.http.server; |
|
27 |
|
28 import com.sun.istack.internal.NotNull; |
|
29 import com.sun.istack.internal.Nullable; |
|
30 import com.sun.net.httpserver.HttpExchange; |
|
31 import com.sun.net.httpserver.HttpHandler; |
|
32 import com.sun.xml.internal.ws.resources.HttpserverMessages; |
|
33 import com.sun.xml.internal.ws.transport.http.HttpAdapter; |
|
34 import com.sun.xml.internal.ws.transport.http.WSHTTPConnection; |
|
35 |
|
36 import java.io.IOException; |
|
37 import java.util.concurrent.Executor; |
|
38 import java.util.logging.Level; |
|
39 import java.util.logging.Logger; |
|
40 |
|
41 /** |
|
42 * {@link HttpHandler} implementation that serves the actual request. |
|
43 * |
|
44 * @author Jitendra Kotamraju |
|
45 * @author Kohsuke Kawaguhi |
|
46 */ |
|
47 final class WSHttpHandler implements HttpHandler { |
|
48 |
|
49 private static final String GET_METHOD = "GET"; |
|
50 private static final String POST_METHOD = "POST"; |
|
51 private static final String HEAD_METHOD = "HEAD"; |
|
52 private static final String PUT_METHOD = "PUT"; |
|
53 private static final String DELETE_METHOD = "DELETE"; |
|
54 |
|
55 private static final Logger LOGGER = |
|
56 Logger.getLogger( |
|
57 com.sun.xml.internal.ws.util.Constants.LoggingDomain + ".server.http"); |
|
58 private static final boolean fineTraceEnabled = LOGGER.isLoggable(Level.FINE); |
|
59 |
|
60 private final HttpAdapter adapter; |
|
61 private final Executor executor; |
|
62 |
|
63 public WSHttpHandler(@NotNull HttpAdapter adapter, @Nullable Executor executor) { |
|
64 assert adapter!=null; |
|
65 this.adapter = adapter; |
|
66 this.executor = executor; |
|
67 } |
|
68 |
|
69 /** |
|
70 * Called by HttpServer when there is a matching request for the context |
|
71 */ |
|
72 public void handle(HttpExchange msg) { |
|
73 try { |
|
74 if (fineTraceEnabled) { |
|
75 LOGGER.fine("Received HTTP request:"+msg.getRequestURI()); |
|
76 } |
|
77 if (executor != null) { |
|
78 // Use application's Executor to handle request. Application may |
|
79 // have set an executor using Endpoint.setExecutor(). |
|
80 executor.execute(new HttpHandlerRunnable(msg)); |
|
81 } else { |
|
82 handleExchange(msg); |
|
83 } |
|
84 } catch(Throwable e) { |
|
85 // Dont't propagate the exception otherwise it kills the httpserver |
|
86 } |
|
87 } |
|
88 |
|
89 private void handleExchange(HttpExchange msg) throws IOException { |
|
90 WSHTTPConnection con = new ServerConnectionImpl(adapter,msg); |
|
91 try { |
|
92 if (fineTraceEnabled) { |
|
93 LOGGER.fine("Received HTTP request:"+msg.getRequestURI()); |
|
94 } |
|
95 String method = msg.getRequestMethod(); |
|
96 if(method.equals(GET_METHOD) || method.equals(POST_METHOD) || method.equals(HEAD_METHOD) |
|
97 || method.equals(PUT_METHOD) || method.equals(DELETE_METHOD)) { |
|
98 adapter.handle(con); |
|
99 } else { |
|
100 if (LOGGER.isLoggable(Level.WARNING)) { |
|
101 LOGGER.warning(HttpserverMessages.UNEXPECTED_HTTP_METHOD(method)); |
|
102 } |
|
103 } |
|
104 } finally { |
|
105 msg.close(); |
|
106 } |
|
107 } |
|
108 |
|
109 /** |
|
110 * Wrapping the processing of request in a Runnable so that it can be |
|
111 * executed in Executor. |
|
112 */ |
|
113 class HttpHandlerRunnable implements Runnable { |
|
114 final HttpExchange msg; |
|
115 |
|
116 HttpHandlerRunnable(HttpExchange msg) { |
|
117 this.msg = msg; |
|
118 } |
|
119 |
|
120 public void run() { |
|
121 try { |
|
122 handleExchange(msg); |
|
123 } catch (Throwable e) { |
|
124 // Does application's executor handle this exception ? |
|
125 e.printStackTrace(); |
|
126 } |
|
127 } |
|
128 } |
|
129 |
|
130 |
|
131 } |