Skip to content

Commit

Permalink
Query parameters were not included in netty URI (eclipse-ee4j#4393)
Browse files Browse the repository at this point in the history
* Query parameters were not included in netty URI

Signed-off-by: Jorge Bescos Gascon <[email protected]>
  • Loading branch information
jbescos authored Feb 17, 2020
1 parent f2ccfd5 commit 706c01c
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2016, 2019 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2016, 2020 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand Down Expand Up @@ -205,15 +205,16 @@ public void operationComplete(io.netty.util.concurrent.Future<? super Void> futu
ch.closeFuture().addListener(closeListener);

HttpRequest nettyRequest;
String pathWithQuery = buildPathWithQueryParameters(requestUri);

if (jerseyRequest.hasEntity()) {
nettyRequest = new DefaultHttpRequest(HttpVersion.HTTP_1_1,
HttpMethod.valueOf(jerseyRequest.getMethod()),
requestUri.getRawPath());
pathWithQuery);
} else {
nettyRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1,
HttpMethod.valueOf(jerseyRequest.getMethod()),
requestUri.getRawPath());
pathWithQuery);
}

// headers
Expand Down Expand Up @@ -282,6 +283,14 @@ public void run() {
return settableFuture;
}

private String buildPathWithQueryParameters(URI requestUri) {
if (requestUri.getRawQuery() != null) {
return String.format("%s?%s", requestUri.getRawPath(), requestUri.getRawQuery());
} else {
return requestUri.getRawPath();
}
}

@Override
public String getName() {
return "Netty 4.1.x";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2016, 2019 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2016, 2020 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Distribution License v. 1.0, which is available at
Expand All @@ -10,9 +10,13 @@

package org.glassfish.jersey.examples.helloworld.netty;

import javax.ws.rs.Consumes;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;

/**
*
Expand All @@ -28,4 +32,21 @@ public String getHello() {
return CLICHED_MESSAGE;
}

@GET
@Path("query1")
@Produces("text/plain")
public String getQueryParameter(@DefaultValue("error1") @QueryParam(value = "test1") String test1,
@DefaultValue("error2") @QueryParam(value = "test2") String test2) {
return test1 + test2;
}

@POST
@Path("query2")
@Consumes("text/plain")
@Produces("text/plain")
public String postQueryParameter(@DefaultValue("error1") @QueryParam(value = "test1") String test1,
@DefaultValue("error2") @QueryParam(value = "test2") String test2, String entity) {
return entity + test1 + test2;
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2016, 2018 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2016, 2020 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Distribution License v. 1.0, which is available at
Expand All @@ -18,6 +18,7 @@

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.InvocationCallback;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
Expand Down Expand Up @@ -224,4 +225,22 @@ public void testConfigurationUpdate() {
assertEquals(1, CustomLoggingFilter.preFilterCalled);
assertEquals(1, CustomLoggingFilter.postFilterCalled);
}

@Test
@RunSeparately
public void testQueryParameterGet() {
String result = target().path(App.ROOT_PATH + "/query1").queryParam("test1", "expected1")
.queryParam("test2", "expected2").request().get(String.class);
assertEquals("expected1expected2", result);
}

@Test
@RunSeparately
public void testQueryParameterPost() {
String result = target().path(App.ROOT_PATH + "/query2").queryParam("test1", "expected1")
.queryParam("test2", "expected2").request("text/plain").post(Entity.entity("entity", "text/plain"))
.readEntity(String.class);
assertEquals("entityexpected1expected2", result);
}

}

0 comments on commit 706c01c

Please sign in to comment.