Skip to content

Commit

Permalink
Merge pull request #4696 from senivam/3x_merged
Browse files Browse the repository at this point in the history
merge of actual master into 3.x
  • Loading branch information
senivam authored Jan 25, 2021
2 parents 628f1bb + 983cff9 commit 3053ef7
Show file tree
Hide file tree
Showing 6 changed files with 617 additions and 7 deletions.
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, 2021 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018 Payara Foundation and/or its affiliates.
*
* This program and the accompanying materials are made available under the
Expand All @@ -21,21 +21,24 @@
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.security.AccessController;
import java.text.ParseException;
import java.util.Date;
import java.util.Optional;

import jakarta.ws.rs.ProcessingException;
import jakarta.ws.rs.WebApplicationException;
import jakarta.ws.rs.ext.ParamConverter;
import jakarta.ws.rs.ext.ParamConverterProvider;

import jakarta.inject.Inject;
import jakarta.inject.Singleton;

import org.glassfish.jersey.internal.LocalizationMessages;
import org.glassfish.jersey.internal.util.ReflectionHelper;
import org.glassfish.jersey.message.internal.HttpDateFormat;
import org.glassfish.jersey.internal.LocalizationMessages;

/**
* Container of several different {@link ParamConverterProvider param converter providers}
Expand Down Expand Up @@ -247,6 +250,59 @@ public String toString(final T value) throws IllegalArgumentException {
}
}

/**
* Provider of {@link ParamConverter param converter} that produce the Optional instance
* by invoking {@link AggregatedProvider}.
*/
@Singleton
public static class OptionalProvider implements ParamConverterProvider {

// Delegates to this provider when the type of Optional is extracted.
private final AggregatedProvider aggregated;

@Inject
public OptionalProvider(AggregatedProvider aggregated) {
this.aggregated = aggregated;
}

@Override
public <T> ParamConverter<T> getConverter(Class<T> rawType, Type genericType, Annotation[] annotations) {
return (rawType != Optional.class) ? null : new ParamConverter<T>() {

@Override
public T fromString(String value) {
if (value == null) {
return (T) Optional.empty();
} else {
ParameterizedType parametrized = (ParameterizedType) genericType;
Type type = parametrized.getActualTypeArguments()[0];
T val = aggregated.getConverter((Class<T>) type, type, annotations).fromString(value.toString());
if (val != null) {
return (T) Optional.of(val);
} else {
/*
* In this case we don't send Optional.empty() because 'value' is not null.
* But we return null because the provider didn't find how to parse it.
*/
return null;
}
}
}

@Override
public String toString(T value) throws IllegalArgumentException {
/*
* Unfortunately 'orElse' cannot be stored in an Optional. As only one value can
* be stored, it makes no sense that 'value' is Optional. It can just be the value.
* We don't fail here but we don't process it.
*/
return null;
}
};
}

}

/**
* Aggregated {@link ParamConverterProvider param converter provider}.
*/
Expand All @@ -267,7 +323,8 @@ public AggregatedProvider() {
new TypeValueOf(),
new CharacterProvider(),
new TypeFromString(),
new StringConstructor()
new StringConstructor(),
new OptionalProvider(this)
};
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*
* Copyright (c) 2021 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.server.internal.inject;

import java.lang.reflect.Array;
import java.util.List;
import java.util.function.Function;

import jakarta.ws.rs.core.MultivaluedHashMap;
import jakarta.ws.rs.core.MultivaluedMap;
import jakarta.ws.rs.ext.ParamConverter;

/**
* Extract parameter value as an array.
*
*/
class ArrayExtractor implements MultivaluedParameterExtractor<Object> {

private final String parameterName;
private final String defaultValueString;
private final Function<String, Object> typeExtractor;
private final Class<?> type;

private ArrayExtractor(Class<?> type, Function<String, Object> typeExtractor,
String parameterName, String defaultValueString) {
this.type = type;
this.typeExtractor = typeExtractor;
this.parameterName = parameterName;
this.defaultValueString = defaultValueString;
}

@Override
public String getName() {
return parameterName;
}

@Override
public String getDefaultValueString() {
return defaultValueString;
}

@Override
public Object extract(MultivaluedMap<String, String> parameters) {
List<String> stringList = parameters.get(getName());
Object array = null;
if (stringList != null) {
array = Array.newInstance(type, stringList.size());
for (int i = 0; i < stringList.size(); i++) {
Array.set(array, i, typeExtractor.apply(stringList.get(i)));
}
} else if (defaultValueString != null) {
array = Array.newInstance(type, 1);
Array.set(array, 0, typeExtractor.apply(defaultValueString));
} else {
array = Array.newInstance(type, 0);
}
return array;
}

/**
* Get array extractor instance supporting.
*
* @param type the type class to manage runtime generic.
* @param converter the converter of the type.
* @param parameterName extracted parameter name.
* @param defaultValueString default parameter value.
* @return string array extractor instance.
*/
public static MultivaluedParameterExtractor<Object> getInstance(Class<?> type,
ParamConverter<?> converter, String parameterName, String defaultValueString) {
Function<String, Object> typeExtractor = value -> converter.fromString(value);
return new ArrayExtractor(type, typeExtractor, parameterName, defaultValueString);
}

/**
* Get array extractor instance supporting.
*
* @param type the type class to manage runtime generic.
* @param extractor the extractor.
* @param parameterName extracted parameter name.
* @param defaultValueString default parameter value.
* @return string array extractor instance.
*/
public static MultivaluedParameterExtractor<Object> getInstance(Class<?> type,
MultivaluedParameterExtractor<?> extractor,
String parameterName,
String defaultValueString) {
Function<String, Object> typeExtractor = value -> {
MultivaluedMap<String, String> pair = new MultivaluedHashMap<>();
pair.putSingle(parameterName, value);
return extractor.extract(pair);
};
return new ArrayExtractor(type, typeExtractor, parameterName, defaultValueString);
}
}
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, 2021 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018 Payara Foundation and/or its affiliates.
*
* This program and the accompanying materials are made available under the
Expand Down Expand Up @@ -32,12 +32,12 @@

import org.glassfish.jersey.internal.inject.ExtractorException;
import org.glassfish.jersey.internal.inject.ParamConverterFactory;
import org.glassfish.jersey.internal.inject.PrimitiveMapper;
import org.glassfish.jersey.internal.util.ReflectionHelper;
import org.glassfish.jersey.internal.util.collection.ClassTypePair;
import org.glassfish.jersey.internal.util.collection.LazyValue;
import org.glassfish.jersey.server.internal.LocalizationMessages;
import org.glassfish.jersey.model.Parameter;
import org.glassfish.jersey.internal.inject.PrimitiveMapper;
import org.glassfish.jersey.server.internal.LocalizationMessages;

/**
* Implementation of {@link MultivaluedParameterExtractorProvider}. For each
Expand Down Expand Up @@ -120,8 +120,30 @@ private MultivaluedParameterExtractor<?> process(
throw new ProcessingException(LocalizationMessages.ERROR_PARAMETER_TYPE_PROCESSING(rawType), e);
}
}
} else if (rawType.isArray()) {
if (rawType.getComponentType().isPrimitive()) {
MultivaluedParameterExtractor<?> primitiveExtractor =
createPrimitiveExtractor(rawType.getComponentType(), parameterName, defaultValue);
if (primitiveExtractor == null) {
return null;
}
return ArrayExtractor.getInstance(rawType.getComponentType(), primitiveExtractor, parameterName, defaultValue);
} else {
converter = paramConverterFactory.getConverter(rawType.getComponentType(),
rawType.getComponentType(),
annotations);
if (converter == null) {
return null;
}
return ArrayExtractor.getInstance(rawType.getComponentType(), converter, parameterName, defaultValue);
}
}
// Check primitive types.
return createPrimitiveExtractor(rawType, parameterName, defaultValue);
}

private MultivaluedParameterExtractor<?> createPrimitiveExtractor(Class<?> rawType, String parameterName,
String defaultValue){
// Check primitive types.
if (rawType == String.class) {
return new SingleStringValueExtractor(parameterName, defaultValue);
Expand Down Expand Up @@ -157,7 +179,6 @@ private MultivaluedParameterExtractor<?> process(
}

}

return null;
}
}
57 changes: 57 additions & 0 deletions docs/src/main/docbook/client.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1129,4 +1129,61 @@ Client client = ClientBuilder.newBuilder().sslContext(sslContext).build();</prog
}
</programlisting>
</section>
<section>
<title>Header Expect:100-continue support</title>
<para>
This section describes support of Expect:100-continue in Jersey client using Expect100Continue feature.
Jersey client supports given header for default JDK HTTP connector only.
</para>
<para>
<emphasis>Jersey client Expect100Continue feature</emphasis>
</para>
<para>
Since Jersey 2.32 it is possible to send Expect:100-continue header from Jersey client. Feature shall be
registered in client using (for example)
<programlisting language="java" linenumbering="numbered">
target(RESOURCE_PATH).register(Expect100ContinueFeature.basic());
</programlisting>
Note that registration can be done in several ways: with basic settings, and with custom settings:
<programlisting language="java" linenumbering="numbered">
target(RESOURCE_PATH).register(Expect100ContinueFeature.withCustomThreshold(100L));
</programlisting>
Basic registration means that default sending threshold will be used. Value of the default threshold is
<programlisting language="java" linenumbering="numbered">
DEFAULT_EXPECT_100_CONTINUE_THRESHOLD_SIZE = 65536L;
</programlisting>
Threshold is used to determine allowed size of request after which 100-continue header shall be sent before
sending request itself.
</para>
<para>
<emphasis>Environment properties configuration</emphasis>
</para>
<para>
Previous paragraph described programmatic way of configuration. However the Expect100Continue feature can
be configured using environment variables as well.
</para>
<para>
Since Jersey client can be influenced through environment variables, there are two variables which come
since Jersey 2.32:
<programlisting language="bash" linenumbering="unnumbered">
-Djersey.config.client.request.expect.100.continue.processing=true/false
-Djersey.config.client.request.expect.100.continue.threshold.size=12345
</programlisting>
</para>
<para>
First variable can be used to forbid the Expect (100-continue) header be sent at all even though it is
registered as described in the previous paragraph. If this property is not provided (or true) and the
Expect100Continue feature is registered, sending of the Expect header is enabled.
</para>
<para>
The second property defines (or modifies) threshold size. So, if the Expect100Continue feature is registered
using basic (default threshold size) parameters, value of the threshold can be modified using this property.
This is valid for custom threshold as well - when the Expect100Continue feature is registered using
withCustomThreshold method its value can be modified anyway by the environment property
<literal>jersey.config.client.request.expect.100.continue.threshold.size</literal>.
<important>
In other words this variable has precedence over any programmatically set value of the threshold.
</important>
</para>
</section>
</chapter>
Loading

0 comments on commit 3053ef7

Please sign in to comment.