001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036:
037: package com.sun.xml.ws.transport.http.server;
038:
039: import com.sun.istack.NotNull;
040: import com.sun.istack.Nullable;
041: import com.sun.net.httpserver.HttpExchange;
042: import com.sun.net.httpserver.HttpHandler;
043: import com.sun.net.httpserver.HttpsExchange;
044: import com.sun.xml.ws.resources.HttpserverMessages;
045: import com.sun.xml.ws.transport.http.HttpAdapter;
046: import com.sun.xml.ws.transport.http.WSHTTPConnection;
047:
048: import java.io.IOException;
049: import java.util.List;
050: import java.util.concurrent.Executor;
051: import java.util.logging.Logger;
052:
053: /**
054: * {@link HttpHandler} implementation that serves the actual request.
055: *
056: * @author Jitendra Kotamraju
057: * @author Kohsuke Kawaguhi
058: */
059: final class WSHttpHandler implements HttpHandler {
060:
061: private static final String GET_METHOD = "GET";
062: private static final String POST_METHOD = "POST";
063: private static final String HEAD_METHOD = "HEAD";
064: private static final String PUT_METHOD = "PUT";
065: private static final String DELETE_METHOD = "DELETE";
066:
067: private static final Logger logger = Logger
068: .getLogger(com.sun.xml.ws.util.Constants.LoggingDomain
069: + ".server.http");
070:
071: private final HttpAdapter adapter;
072: private final Executor executor;
073:
074: public WSHttpHandler(@NotNull
075: HttpAdapter adapter, @Nullable
076: Executor executor) {
077: assert adapter != null;
078: this .adapter = adapter;
079: this .executor = executor;
080: }
081:
082: /**
083: * Called by HttpServer when there is a matching request for the context
084: */
085: public void handle(HttpExchange msg) {
086: try {
087: logger.fine("Received HTTP request:" + msg.getRequestURI());
088: if (executor != null) {
089: // Use application's Executor to handle request. Application may
090: // have set an executor using Endpoint.setExecutor().
091: executor.execute(new HttpHandlerRunnable(msg));
092: } else {
093: handleExchange(msg);
094: }
095: } catch (Throwable e) {
096: // Dont't propagate the exception otherwise it kills the httpserver
097: e.printStackTrace();
098: }
099: }
100:
101: public void handleExchange(HttpExchange msg) throws IOException {
102: WSHTTPConnection con = new ServerConnectionImpl(adapter, msg);
103: try {
104: logger.fine("Received HTTP request:" + msg.getRequestURI());
105: String method = msg.getRequestMethod();
106: if (method.equals(GET_METHOD) || method.equals(POST_METHOD)
107: || method.equals(HEAD_METHOD)
108: || method.equals(PUT_METHOD)
109: || method.equals(DELETE_METHOD)) {
110: adapter.handle(con);
111: } else {
112: logger.warning(HttpserverMessages
113: .UNEXPECTED_HTTP_METHOD(method));
114: }
115: } finally {
116: msg.close();
117: }
118: }
119:
120: /**
121: * Wrapping the processing of request in a Runnable so that it can be
122: * executed in Executor.
123: */
124: class HttpHandlerRunnable implements Runnable {
125: final HttpExchange msg;
126:
127: HttpHandlerRunnable(HttpExchange msg) {
128: this .msg = msg;
129: }
130:
131: public void run() {
132: try {
133: handleExchange(msg);
134: } catch (Throwable e) {
135: // Does application's executor handle this exception ?
136: e.printStackTrace();
137: }
138: }
139: }
140:
141: /**
142: * Computes the Endpoint's address from the request. Use "Host" header
143: * so that it has correct address(IP address or someother hostname) through
144: * which the application reached the endpoint.
145: *
146: * @return
147: * a string like "http://foo.bar:1234/abc/def"
148: */
149: static @NotNull
150: String getRequestAddress(HttpExchange msg) {
151: StringBuilder strBuf = new StringBuilder();
152: strBuf
153: .append((msg instanceof HttpsExchange) ? "https"
154: : "http");
155: strBuf.append("://");
156:
157: List<String> hostHeader = msg.getRequestHeaders().get("Host");
158: if (hostHeader != null) {
159: strBuf.append(hostHeader.get(0)); // Uses Host header
160: } else {
161: strBuf.append(msg.getLocalAddress().getHostName());
162: strBuf.append(":");
163: strBuf.append(msg.getLocalAddress().getPort());
164: }
165: strBuf.append(msg.getRequestURI().getPath());
166:
167: return strBuf.toString();
168: }
169: }
|