diff --git a/containers/jetty-http/src/main/java/org/glassfish/jersey/jetty/JettyHttpContainer.java b/containers/jetty-http/src/main/java/org/glassfish/jersey/jetty/JettyHttpContainer.java index 6f998dc7aa..e50bdb7014 100644 --- a/containers/jetty-http/src/main/java/org/glassfish/jersey/jetty/JettyHttpContainer.java +++ b/containers/jetty-http/src/main/java/org/glassfish/jersey/jetty/JettyHttpContainer.java @@ -197,7 +197,7 @@ public void handle(final String target, final Request request, final HttpServlet private URI getRequestUri(final Request request, final URI baseUri) { try { final String serverAddress = getServerAddress(baseUri); - String uri = request.getRequestURI(); + String uri = request.getPathInfo(); final String queryString = request.getQueryString(); if (queryString != null) { diff --git a/containers/jetty-http/src/test/java/org/glassfish/jersey/jetty/AbstractJettyServerTester.java b/containers/jetty-http/src/test/java/org/glassfish/jersey/jetty/AbstractJettyServerTester.java index 821853ffa5..d35c0ca681 100644 --- a/containers/jetty-http/src/test/java/org/glassfish/jersey/jetty/AbstractJettyServerTester.java +++ b/containers/jetty-http/src/test/java/org/glassfish/jersey/jetty/AbstractJettyServerTester.java @@ -47,6 +47,7 @@ import javax.ws.rs.core.UriBuilder; +import org.eclipse.jetty.server.handler.ContextHandler; import org.glassfish.jersey.logging.LoggingFeature; import org.glassfish.jersey.internal.util.PropertiesHelper; import org.glassfish.jersey.server.ResourceConfig; @@ -54,6 +55,9 @@ import org.eclipse.jetty.server.Server; import org.junit.After; +import org.eclipse.jetty.server.Request; +import org.eclipse.jetty.server.Response; + /** * Abstract Jetty Server unit tester. * @@ -101,14 +105,30 @@ public UriBuilder getUri() { return UriBuilder.fromUri("http://localhost").port(getPort()).path(CONTEXT); } - public void startServer(Class... resources) { + public void startServer(String contextPath, Class... resources) { ResourceConfig config = new ResourceConfig(resources); config.register(new LoggingFeature(LOGGER, LoggingFeature.Verbosity.PAYLOAD_ANY)); final URI baseUri = getBaseUri(); - server = JettyHttpContainerFactory.createServer(baseUri, config); + if (contextPath == null) { + server = JettyHttpContainerFactory.createServer(baseUri, config); + } else { + server = JettyHttpContainerFactory.createServer(baseUri, config, false); + ContextHandler contextHandler = new ContextHandler(contextPath); + contextHandler.setHandler(server.getHandler()); + server.setHandler(contextHandler); + try { + server.start(); + } catch (final Exception e) { + throw new RuntimeException(e); + } + } LOGGER.log(Level.INFO, "Jetty-http server started on base uri: " + baseUri); } + public void startServer(Class... resources) { + startServer(null, resources); + } + public void startServer(ResourceConfig config) { final URI baseUri = getBaseUri(); server = JettyHttpContainerFactory.createServer(baseUri, config); diff --git a/containers/jetty-http/src/test/java/org/glassfish/jersey/jetty/ContextHandlerTest.java b/containers/jetty-http/src/test/java/org/glassfish/jersey/jetty/ContextHandlerTest.java new file mode 100644 index 0000000000..a0db6de3e8 --- /dev/null +++ b/containers/jetty-http/src/test/java/org/glassfish/jersey/jetty/ContextHandlerTest.java @@ -0,0 +1,74 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright (c) 2010-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. + */ + +package org.glassfish.jersey.jetty; + +import org.junit.Test; + +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.client.Client; +import javax.ws.rs.client.ClientBuilder; +import javax.ws.rs.core.Response; + +import static org.junit.Assert.assertEquals; + +public class ContextHandlerTest extends AbstractJettyServerTester { + + @Path("helloworld") + public static class HelloWorldResource { + public static final String CLICHED_MESSAGE = "Hello World!"; + + @GET + public String getHello() { + return CLICHED_MESSAGE; + } + } + + @Test + public void testBasic() throws Exception { + startServer("/uri-context" , HelloWorldResource.class); + Client client = ClientBuilder.newClient(); + Response response = client.target(getUri()).path("uri-context/helloworld").request().get(); + String entity = response.readEntity(String.class); + assertEquals(HelloWorldResource.CLICHED_MESSAGE, entity); + } + +}