Skip to content
This repository has been archived by the owner on May 28, 2018. It is now read-only.

Undertow container implementation #3722

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions containers/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
<module>jetty-servlet</module>
<module>netty-http</module>
<module>simple-http</module>
<module>undertow-http</module>
</modules>

<dependencies>
Expand Down
99 changes: 99 additions & 0 deletions containers/undertow-http/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--

DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.

Copyright (c) 2011-2017 Oracle and/or its affiliates. All rights reserved.

The contents of this file are subject to the terms of either the GNU
General Public License Version 2 only ("GPL") or the Common Development
and Distribution License("CDDL") (collectively, the "License"). You
may not use this file except in compliance with the License. You can
obtain a copy of the License at
https://oss.oracle.com/licenses/CDDL+GPL-1.1
or LICENSE.txt. See the License for the specific
language governing permissions and limitations under the License.

When distributing the software, include this License Header Notice in each
file and include the License file at LICENSE.txt.

GPL Classpath Exception:
Oracle designates this particular file as subject to the "Classpath"
exception as provided by Oracle in the GPL Version 2 section of the License
file that accompanied this code.

Modifications:
If applicable, add the following below the License Header, with the fields
enclosed by brackets [] replaced by your own identifying information:
"Portions Copyright [year] [name of copyright owner]"

Contributor(s):
If you wish your version of this file to be governed by only the CDDL or
only the GPL Version 2, indicate your decision by adding "[Contributor]
elects to include this software in this distribution under the [CDDL or GPL
Version 2] license." If you don't indicate a single choice of license, a
recipient has the option to distribute your version of this file under
either the CDDL, the GPL Version 2 or to extend the choice of license to
its licensees as provided above. However, if you add GPL Version 2 code
and therefore, elected the GPL Version 2 license, then the option applies
only if the new code is made subject to such option by the copyright
holder.

-->

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>project</artifactId>
<version>2.26</version>
</parent>

<artifactId>jersey-container-undertow-http</artifactId>
<packaging>jar</packaging>
<name>jersey-container-undertow-http</name>

<description>Undertow Http Container</description>

<dependencies>
<dependency>
<groupId>io.undertow</groupId>
<artifactId>undertow-core</artifactId>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>com.sun.istack</groupId>
<artifactId>maven-istack-commons-plugin</artifactId>
<inherited>true</inherited>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<inherited>true</inherited>
</plugin>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<inherited>true</inherited>
</plugin>
</plugins>

<resources>
<resource>
<directory>${basedir}/src/main/java</directory>
<includes>
<include>META-INF/**/*</include>
</includes>
</resource>
<resource>
<directory>${basedir}/src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package org.glassfish.jersey.undertow;

import io.undertow.server.HttpHandler;
import io.undertow.server.HttpServerExchange;
import io.undertow.util.HeaderValues;
import org.glassfish.jersey.internal.MapPropertiesDelegate;
import org.glassfish.jersey.server.ApplicationHandler;
import org.glassfish.jersey.server.ContainerRequest;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.server.internal.ContainerUtils;
import org.glassfish.jersey.server.spi.Container;

import javax.ws.rs.core.Application;
import java.net.URI;
import java.net.URISyntaxException;

/**
* Undertow {@code Container} implementation based on Undertow's {@link io.undertow.server.HttpHandler}.
*
* @author Jonathan Como (jonathan.como at gmail.com)
*/
public class UndertowHttpContainer implements HttpHandler, Container {
private volatile ApplicationHandler appHandler;

UndertowHttpContainer(final Application application) {
appHandler = new ApplicationHandler(application);

// No lifecycle hooks for Undertow's server, so we do this for
// completeness but not accuracy.
appHandler.onStartup(this);
}

@Override
public ApplicationHandler getApplicationHandler() {
return appHandler;
}

@Override
public ResourceConfig getConfiguration() {
return appHandler.getConfiguration();
}

@Override
public void reload() {
reload(getConfiguration());
}

@Override
public void reload(final ResourceConfig configuration) {
appHandler.onShutdown(this);

appHandler = new ApplicationHandler(configuration);
appHandler.onReload(this);
appHandler.onStartup(this);
}

@Override
public void handleRequest(final HttpServerExchange exchange) throws Exception {
// If we ware on the IO thread (where the raw HTTP request is processed),
// we want to dispatch to a worker. This is the preferred approach.
if (exchange.isInIoThread()) {
exchange.dispatch(this);
return;
}

exchange.startBlocking();
URI baseUri = getBaseUri(exchange);
ContainerRequest request = new ContainerRequest(baseUri, getRequestUri(exchange, baseUri),
exchange.getRequestMethod().toString(), new UndertowSecurityContext(exchange),
new MapPropertiesDelegate());

request.setEntityStream(exchange.getInputStream());
request.setWriter(new UndertowResponseWriter(exchange));
for (HeaderValues values : exchange.getRequestHeaders()) {
String name = values.getHeaderName().toString();
for (String value : values) {
request.header(name, value);
}
}

appHandler.handle(request);
}

// TODO: No easy way to get the mount point without using Undertow servlets library
private URI getBaseUri(final HttpServerExchange exchange) {
try {
return new URI(exchange.getRequestScheme(), null, exchange.getHostName(),
exchange.getHostPort(), "/", null, null);
} catch (URISyntaxException e) {
throw new IllegalArgumentException(e);
}
}

private URI getRequestUri(final HttpServerExchange exchange, final URI baseUri) {
String uri = getServerAddress(baseUri) + exchange.getRequestURI();
String query = exchange.getQueryString();
if (query != null && !query.isEmpty()) {
uri += "?" + ContainerUtils.encodeUnsafeCharacters(query);
}

try {
return new URI(uri);
} catch (URISyntaxException e) {
throw new IllegalArgumentException(e);
}
}

private String getServerAddress(final URI baseUri) {
String serverAddress = baseUri.toString();
if (serverAddress.charAt(serverAddress.length() - 1) == '/') {
return serverAddress.substring(0, serverAddress.length() - 1);
}

return serverAddress;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package org.glassfish.jersey.undertow;

import io.undertow.Undertow;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.server.spi.Container;
import org.glassfish.jersey.undertow.internal.LocalizationMessages;

import javax.net.ssl.SSLContext;
import java.net.URI;

/**
* Factory for creating and starting Undertow server handlers. This returns
* a handle to the started server as {@link Undertow} instances, which allows
* the server to be stopped by invoking the {@link Undertow#stop()} method.
* <p/>
* To start the server in HTTPS mode an {@link SSLContext} can be provided.
* This will be used to decrypt and encrypt information sent over the
* connected TCP socket channel.
*
* @author Jonathan Como (jonathan.como at gmail.com)
*/
public class UndertowHttpContainerFactory {
public static Undertow createServer(URI uri, ResourceConfig config) {
return createServer(uri, null, config, true);
}

public static Undertow createServer(URI uri, ResourceConfig config, boolean start) {
return createServer(uri, null, config, start);
}

public static Undertow createServer(URI uri, SSLContext sslContext, ResourceConfig config) {
return createServer(uri, sslContext, config, true);
}

public static Undertow createServer(URI uri, SSLContext sslContext, ResourceConfig config, boolean start) {
if (uri == null) {
throw new IllegalArgumentException(LocalizationMessages.URI_CANNOT_BE_NULL());
}

int defaultPort;
String scheme = uri.getScheme();

if (sslContext != null) {
defaultPort = Container.DEFAULT_HTTPS_PORT;
if (!"https".equals(scheme)) {
throw new IllegalArgumentException(LocalizationMessages.WRONG_SCHEME_WHEN_USING_HTTPS());
}
} else {
defaultPort = Container.DEFAULT_HTTP_PORT;
if (!"http".equals(scheme)) {
throw new IllegalArgumentException(LocalizationMessages.WRONG_SCHEME_WHEN_USING_HTTP());
}
}

int port = uri.getPort();
if (port == -1) {
port = defaultPort;
}

Undertow.Builder builder = Undertow.builder()
.setHandler(new UndertowHttpContainer(config));

if (sslContext != null) {
builder.addHttpsListener(port, uri.getHost(), sslContext);
} else {
builder.addHttpListener(port, uri.getHost());
}

Undertow server = builder.build();
if (start) {
server.start();
}

return server;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package org.glassfish.jersey.undertow;

import io.undertow.server.HttpHandler;
import org.glassfish.jersey.server.spi.ContainerProvider;

import javax.ws.rs.ProcessingException;
import javax.ws.rs.core.Application;

/**
* Container provider for containers based on Undertow Server {@link io.undertow.Undertow}.
*
* @author Jonathan Como (jonathan.como at gmail.com)
*/
public final class UndertowHttpContainerProvider implements ContainerProvider {

@Override
public <T> T createContainer(final Class<T> type, final Application application) throws ProcessingException {
if (HttpHandler.class == type || UndertowHttpContainer.class == type) {
return type.cast(new UndertowHttpContainer(application));
}

return null;
}
}
Loading