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

Fix Jetty context path #3786

Open
wants to merge 1 commit 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
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,17 @@

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;

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.
*
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}

}