|
| 1 | +/* |
| 2 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
| 3 | + * |
| 4 | + * Copyright (c) 2010-2012 Oracle and/or its affiliates. All rights reserved. |
| 5 | + * |
| 6 | + * The contents of this file are subject to the terms of either the GNU |
| 7 | + * General Public License Version 2 only ("GPL") or the Common Development |
| 8 | + * and Distribution License("CDDL") (collectively, the "License"). You |
| 9 | + * may not use this file except in compliance with the License. You can |
| 10 | + * obtain a copy of the License at |
| 11 | + * http://glassfish.java.net/public/CDDL+GPL_1_1.html |
| 12 | + * or packager/legal/LICENSE.txt. See the License for the specific |
| 13 | + * language governing permissions and limitations under the License. |
| 14 | + * |
| 15 | + * When distributing the software, include this License Header Notice in each |
| 16 | + * file and include the License file at packager/legal/LICENSE.txt. |
| 17 | + * |
| 18 | + * GPL Classpath Exception: |
| 19 | + * Oracle designates this particular file as subject to the "Classpath" |
| 20 | + * exception as provided by Oracle in the GPL Version 2 section of the License |
| 21 | + * file that accompanied this code. |
| 22 | + * |
| 23 | + * Modifications: |
| 24 | + * If applicable, add the following below the License Header, with the fields |
| 25 | + * enclosed by brackets [] replaced by your own identifying information: |
| 26 | + * "Portions Copyright [year] [name of copyright owner]" |
| 27 | + * |
| 28 | + * Contributor(s): |
| 29 | + * If you wish your version of this file to be governed by only the CDDL or |
| 30 | + * only the GPL Version 2, indicate your decision by adding "[Contributor] |
| 31 | + * elects to include this software in this distribution under the [CDDL or GPL |
| 32 | + * Version 2] license." If you don't indicate a single choice of license, a |
| 33 | + * recipient has the option to distribute your version of this file under |
| 34 | + * either the CDDL, the GPL Version 2 or to extend the choice of license to |
| 35 | + * its licensees as provided above. However, if you add GPL Version 2 code |
| 36 | + * and therefore, elected the GPL Version 2 license, then the option applies |
| 37 | + * only if the new code is made subject to such option by the copyright |
| 38 | + * holder. |
| 39 | + */ |
| 40 | +package org.glassfish.jersey.grizzly2; |
| 41 | + |
| 42 | +import org.glassfish.grizzly.http.server.HttpHandler; |
| 43 | +import org.glassfish.grizzly.http.server.Request; |
| 44 | +import org.glassfish.grizzly.http.server.Response; |
| 45 | +import org.glassfish.grizzly.utils.Charsets; |
| 46 | +import org.glassfish.jersey.message.internal.Requests; |
| 47 | +import org.glassfish.jersey.server.Application; |
| 48 | +import org.glassfish.jersey.server.ContainerResponseWriter; |
| 49 | + |
| 50 | +import javax.ws.rs.core.HttpHeaders; |
| 51 | +import javax.ws.rs.core.Request.RequestBuilder; |
| 52 | +import javax.ws.rs.core.UriBuilder; |
| 53 | +import java.io.IOException; |
| 54 | +import java.io.OutputStream; |
| 55 | +import java.net.URI; |
| 56 | +import java.net.URISyntaxException; |
| 57 | +import java.util.List; |
| 58 | +import java.util.Map; |
| 59 | + |
| 60 | +/** |
| 61 | + * |
| 62 | + * Grizzly 2 Jersey HTTP Container Prototype |
| 63 | + * |
| 64 | + * @author Jakub Podlesak |
| 65 | + */ |
| 66 | +public final class GrizzlyHttpContainer extends HttpHandler { |
| 67 | + |
| 68 | + private final static class Writer implements ContainerResponseWriter { |
| 69 | + |
| 70 | + final Response grizzlyResponse; |
| 71 | + |
| 72 | + Writer(final Response response) { |
| 73 | + this.grizzlyResponse = response; |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public void finish() throws IOException { |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + public OutputStream writeStatusAndHeaders(final long contentLength, |
| 82 | + final javax.ws.rs.core.Response jaxrsResponse) throws IOException { |
| 83 | + |
| 84 | + grizzlyResponse.setStatus(jaxrsResponse.getStatus()); |
| 85 | + |
| 86 | + for (final Map.Entry<String, List<String>> e : jaxrsResponse.getHeaders().asMap().entrySet()) { |
| 87 | + for (final String value : e.getValue()) { |
| 88 | + grizzlyResponse.addHeader(e.getKey(), value); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + final String contentType = jaxrsResponse.getHeaders().getHeader(HttpHeaders.CONTENT_TYPE); |
| 93 | + if (contentLength > 0 && contentType != null) { |
| 94 | + grizzlyResponse.setContentType(contentType); |
| 95 | + } |
| 96 | + |
| 97 | + return grizzlyResponse.getOutputStream(); |
| 98 | + } |
| 99 | + } |
| 100 | + // |
| 101 | + private Application application; |
| 102 | + |
| 103 | + /** |
| 104 | + * Creates a new Grizzly container. |
| 105 | + * |
| 106 | + * @param application Jersey application to be deployed on Grizzly container. |
| 107 | + */ |
| 108 | + GrizzlyHttpContainer(final Application application) { |
| 109 | + this.application = application; |
| 110 | + } |
| 111 | + |
| 112 | + // HttpRequestProcessor |
| 113 | + @Override |
| 114 | + public void service(final Request request, final Response response) { |
| 115 | + application.apply(toJaxrsRequest(request), new Writer(response)); |
| 116 | + response.finish(); |
| 117 | + } |
| 118 | + |
| 119 | + private URI getBaseUri(final Request request) { |
| 120 | + try { |
| 121 | + return new URI(request.getScheme(), null, request.getServerName(), |
| 122 | + request.getServerPort(), getBasePath(request), null, null); |
| 123 | + } catch (final URISyntaxException ex) { |
| 124 | + throw new IllegalArgumentException(ex); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + private String getBasePath(final Request request) { |
| 129 | + final String contextPath = request.getContextPath(); |
| 130 | + |
| 131 | + if (contextPath == null || contextPath.length() == 0) { |
| 132 | + return "/"; |
| 133 | + } else if (contextPath.charAt(contextPath.length() - 1) != '/') { |
| 134 | + return contextPath + "/"; |
| 135 | + } else { |
| 136 | + return contextPath; |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + private javax.ws.rs.core.Request toJaxrsRequest(Request grizzlyRequest) { |
| 141 | + |
| 142 | + final URI baseUri = getBaseUri(grizzlyRequest); |
| 143 | + |
| 144 | + // TODO: this is terrible, there must be a way to obtain the original request URI! |
| 145 | + String originalURI = UriBuilder.fromPath( |
| 146 | + grizzlyRequest.getRequest().getRequestURIRef().getOriginalRequestURIBC().toString(Charsets.DEFAULT_CHARSET)).build().toString(); |
| 147 | + |
| 148 | + String queryString = grizzlyRequest.getQueryString(); |
| 149 | + if (queryString != null) { |
| 150 | + originalURI = originalURI + "?" + queryString; |
| 151 | + } |
| 152 | + |
| 153 | + final URI requestUri = baseUri.resolve(originalURI); |
| 154 | + |
| 155 | + final String method = grizzlyRequest.getMethod().getMethodString(); |
| 156 | + |
| 157 | + RequestBuilder rb = Requests.from(baseUri, requestUri, method).entity(grizzlyRequest.getInputStream()); |
| 158 | + |
| 159 | + for (String name : grizzlyRequest.getHeaderNames()) { |
| 160 | + for (String value : grizzlyRequest.getHeaders(name)) { |
| 161 | + rb.header(name, value); |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + return rb.build(); |
| 166 | + } |
| 167 | +} |
0 commit comments