Skip to content

Commit

Permalink
Merge pull request #4977 from senivam/30_merged
Browse files Browse the repository at this point in the history
 update 3.x branch with actual master
  • Loading branch information
jansupol authored Feb 7, 2022
2 parents 05f2f0f + fd25def commit 9c9137b
Show file tree
Hide file tree
Showing 50 changed files with 756 additions and 154 deletions.
2 changes: 1 addition & 1 deletion NOTICE.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ KineticJS, v4.7.1
* Copyright: Eric Rowell

org.objectweb.asm Version 9.0
* License: Modified BSD (http://asm.objectweb.org/license.html)
* License: Modified BSD (https://asm.ow2.io/license.html)
* Copyright (c) 2000-2011 INRIA, France Telecom. All rights reserved.

org.osgi.core version 6.0.0
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2019, 2022 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 @@ -63,12 +63,15 @@ void close(ClientRequest clientRequest, HttpUriRequest request, CloseableHttpRes
* Strategy that aborts Apache HttpRequests for the case of Chunked Stream, closes the stream, and response next.
*/
class GracefulClosingStrategy implements ApacheConnectionClosingStrategy {
private static final String UNIX_PROTOCOL = "unix";

static final GracefulClosingStrategy INSTANCE = new GracefulClosingStrategy();

@Override
public void close(ClientRequest clientRequest, HttpUriRequest request, CloseableHttpResponse response, InputStream stream)
throws IOException {
if (response.getEntity() != null && response.getEntity().isChunked()) {
if (response.getEntity() != null && response.getEntity().isChunked()
&& !request.getURI().getScheme().equals(UNIX_PROTOCOL)) {
request.abort();
}
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2010, 2020 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2010, 2022 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 @@ -509,7 +509,7 @@ public ClientResponse apply(final ClientRequest clientRequest) throws Processing
final HttpEntity entity = response.getEntity();

if (entity != null) {
if (headers.get(HttpHeaders.CONTENT_LENGTH) == null) {
if (headers.get(HttpHeaders.CONTENT_LENGTH) == null && entity.getContentLength() >= 0) {
headers.add(HttpHeaders.CONTENT_LENGTH, String.valueOf(entity.getContentLength()));
}

Expand Down
12 changes: 11 additions & 1 deletion connectors/grizzly-connector/pom.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0"?>
<!--
Copyright (c) 2011, 2021 Oracle and/or its affiliates. All rights reserved.
Copyright (c) 2011, 2022 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 @@ -40,6 +40,16 @@
<dependency>
<groupId>org.glassfish.grizzly</groupId>
<artifactId>grizzly-http-client</artifactId>
<exclusions>
<exclusion>
<groupId>org.glassfish.grizzly</groupId>
<artifactId>grizzly-websockets</artifactId>
</exclusion>
<exclusion>
<groupId>org.glassfish.grizzly</groupId>
<artifactId>connection-pool</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.glassfish.grizzly</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2016, 2021 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2016, 2022 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 All @@ -24,6 +24,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
import java.util.concurrent.ExecutorService;
Expand Down Expand Up @@ -102,7 +103,7 @@ class NettyConnector implements Connector {
private final Integer maxPoolSizeTotal; //either from Jersey config, or default
private final Integer maxPoolIdle; // either from Jersey config, or default

private static final String INACTIVE_POOLED_CONNECTION_HANDLER = "inactive_pooled_connection_handler";
static final String INACTIVE_POOLED_CONNECTION_HANDLER = "inactive_pooled_connection_handler";
private static final String PRUNE_INACTIVE_POOL = "prune_inactive_pool";
private static final String READ_TIMEOUT_HANDLER = "read_timeout_handler";
private static final String REQUEST_HANDLER = "request_handler";
Expand Down Expand Up @@ -190,10 +191,18 @@ protected CompletableFuture<ClientResponse> execute(final ClientRequest jerseyRe
synchronized (conns) {
while (chan == null && !conns.isEmpty()) {
chan = conns.remove(conns.size() - 1);
chan.pipeline().remove(INACTIVE_POOLED_CONNECTION_HANDLER);
chan.pipeline().remove(PRUNE_INACTIVE_POOL);
try {
chan.pipeline().remove(INACTIVE_POOLED_CONNECTION_HANDLER);
chan.pipeline().remove(PRUNE_INACTIVE_POOL);
} catch (NoSuchElementException e) {
/*
* Eat it.
* It could happen that the channel was closed, pipeline cleared and
* then it will fail to remove the names with this exception.
*/
}
if (!chan.isOpen()) {
chan = null;
chan = null;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2020 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 2022 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 @@ -288,16 +288,8 @@ protected void service(final HttpServletRequest request, final HttpServletRespon
* We need to work around this and not use getPathInfo
* for the decodedPath.
*/
final String decodedBasePath = request.getContextPath() + servletPath + "/";

final String encodedBasePath = UriComponent.encode(decodedBasePath,
UriComponent.Type.PATH);

if (!decodedBasePath.equals(encodedBasePath)) {
setResponseForInvalidUri(response, new ProcessingException("The servlet context path and/or the "
+ "servlet path contain characters that are percent encoded"));
return;
}
final String encodedBasePath = UriComponent.contextualEncode(
request.getContextPath() + servletPath, UriComponent.Type.PATH) + "/";

final URI baseUri;
final URI requestUri;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
/*
* Copyright (c) 2022 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
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/

package org.glassfish.jersey.servlet.internal;

import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.glassfish.jersey.internal.util.collection.Value;
import org.glassfish.jersey.internal.util.collection.Values;
import org.glassfish.jersey.servlet.ServletContainer;
import org.junit.Assert;
import org.junit.Test;

import java.io.IOException;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.net.URI;

/**
* Context encoding test. See Jersey-4949.
*/
public class ContextPathEncodingTest {
private static final String PATH = "A%20B";
private static final String CONTEXT = "c%20ntext";

@Test
public void contextEncodingTest() throws ServletException, IOException {
// In jetty maven plugin, context is set by
//<configuration>
// <scan>10</scan>
// <webApp>
// <contextPath>/c ntext</contextPath>
// </webApp>
//</configuration>

//Servlet path is not encoded, context is encoded
final ServletRequestValues servletRequestValues = new ServletRequestValues(
"/" + CONTEXT,
"",
"/" + CONTEXT + "/" + PATH
);
final EncodingTestServletContainer testServletContainer = new EncodingTestServletContainer(
"/" + CONTEXT + "/",
"/" + CONTEXT + "/" + PATH
);
EncodingTestData testData = new EncodingTestData(servletRequestValues, testServletContainer);

testData.test();
}

@Test
public void servletPathEncodingTest() throws ServletException, IOException {
//Servlet path is not encoded, context is encoded
final ServletRequestValues servletRequestValues = new ServletRequestValues(
"/",
"A B",
"/" + PATH + "/" + PATH
);
final EncodingTestServletContainer testServletContainer = new EncodingTestServletContainer(
"/" + PATH + "/",
"/" + PATH + "/" + PATH
);
EncodingTestData testData = new EncodingTestData(servletRequestValues, testServletContainer);

testData.test();
}

static class EncodingTestData {
final ServletRequestValues servletRequestValues;
final EncodingTestServletContainer encodingTestServletContainer;
final HttpServletRequest httpServletRequest;

EncodingTestData(ServletRequestValues servletRequestValues, EncodingTestServletContainer encodingTestServletContainer) {
this.servletRequestValues = servletRequestValues;
this.encodingTestServletContainer = encodingTestServletContainer;
this.httpServletRequest = (HttpServletRequest) Proxy.newProxyInstance(getClass().getClassLoader(),
new Class[]{HttpServletRequest.class}, new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
return servletRequestValues.handle(method.getName());
}
});
}

public void test() throws ServletException, IOException {
encodingTestServletContainer.service(httpServletRequest, (HttpServletResponse) null);
}

}

static class ServletRequestValues {
final String servletPath;
final String requestUri;
final String contextPath;

ServletRequestValues(String contextPath, String servletPath, String requestUri) {
this.servletPath = servletPath;
this.requestUri = requestUri;
this.contextPath = contextPath;
}

Object handle(String name) {
switch (name) {
case "getServletPath":
return servletPath;
case "getRequestURI":
return requestUri;
case "getRequestURL":
return new StringBuffer(requestUri);
case "getContextPath":
return contextPath;
default:
return null;
}
}
}

static class EncodingTestServletContainer extends ServletContainer {
final String baseUri;
final String requestUri;

EncodingTestServletContainer(String baseUri, String requestUri) {
this.baseUri = baseUri;
this.requestUri = requestUri;
}

@Override
public Value<Integer> service(URI baseUri, URI requestUri, HttpServletRequest request, HttpServletResponse response) {
Assert.assertEquals(this.baseUri, baseUri.toASCIIString());
Assert.assertEquals(this.requestUri, requestUri.toASCIIString());
return Values.of(0);
}

//Update visibility
public void service(final HttpServletRequest request, final HttpServletResponse response)
throws ServletException, IOException {
super.service(request, response);
}
};
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2021, 2022 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 All @@ -24,6 +24,7 @@

import jakarta.ws.rs.RuntimeType;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -62,7 +63,7 @@ protected List<Class<T>> loadImplementations(Map<String, Object> applicationProp
* @param features list of features to be registered
* @param bootstrapBag place where features are being registered
*/
protected void registerFeatures(List<Class<T>> features,
protected void registerFeatures(Collection<Class<T>> features,
BootstrapBag bootstrapBag) {
final List<AutoDiscoverable> autoDiscoverables = new ArrayList<>();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2021, 2022 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 All @@ -22,8 +22,10 @@

import jakarta.ws.rs.RuntimeType;
import jakarta.ws.rs.container.DynamicFeature;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
* Registers JAX-RS {@link DynamicFeature} which are listed as SPIs for registration.
Expand Down Expand Up @@ -51,7 +53,8 @@ public DynamicFeatureConfigurator() {
public void init(InjectionManager injectionManager, BootstrapBag bootstrapBag) {
final Map<String, Object> properties = bootstrapBag.getConfiguration().getProperties();
if (PropertiesHelper.isJaxRsServiceLoadingEnabled(properties)) {
final List<Class<DynamicFeature>> dynamicFeatures = loadImplementations(properties);
final Set<Class<DynamicFeature>> dynamicFeatures = new HashSet<>();
dynamicFeatures.addAll(loadImplementations(properties));
dynamicFeatures.addAll(loadImplementations(properties, DynamicFeature.class.getClassLoader()));

registerFeatures(dynamicFeatures, bootstrapBag);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2021, 2022 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 All @@ -22,8 +22,9 @@

import jakarta.ws.rs.RuntimeType;
import jakarta.ws.rs.core.Feature;
import java.util.List;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
* Registers JAX-RS {@link Feature} which are listed as SPIs for registration.
Expand All @@ -44,7 +45,8 @@ public FeatureConfigurator(RuntimeType runtimeType) {
public void init(InjectionManager injectionManager, BootstrapBag bootstrapBag) {
final Map<String, Object> properties = bootstrapBag.getConfiguration().getProperties();
if (PropertiesHelper.isJaxRsServiceLoadingEnabled(properties)) {
final List<Class<Feature>> features = loadImplementations(properties);
final Set<Class<Feature>> features = new HashSet<>();
features.addAll(loadImplementations(properties));
features.addAll(loadImplementations(properties, Feature.class.getClassLoader()));

registerFeatures(features, bootstrapBag);
Expand Down
Loading

0 comments on commit 9c9137b

Please sign in to comment.