Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MicroProfile Rest Client 2.0 support #4699

Merged
merged 16 commits into from
Feb 11, 2021
Merged
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 @@ -451,6 +451,18 @@ public final class ClientProperties {
*/
public static final Long DEFAULT_EXPECT_100_CONTINUE_THRESHOLD_SIZE = 65536L;

/**
* The property defines the desired format of query param when multiple
* values are sent for the same parameter.
*
* <p>
* The value MUST be an instance of
* {@link org.glassfish.jersey.uri.QueryParamStyle}.</p>
* <p>
* The default value is {@code null}.</p>
*/
public static final String QUERY_PARAM_STYLE = "jersey.config.client.uri.query.param.style";

private ClientProperties() {
// prevents instantiation
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import javax.ws.rs.core.UriBuilder;

import org.glassfish.jersey.internal.guava.Preconditions;
import org.glassfish.jersey.uri.JerseyQueryParamStyle;
import org.glassfish.jersey.uri.internal.JerseyUriBuilder;

/**
* Jersey implementation of {@link javax.ws.rs.client.WebTarget JAX-RS client target}
Expand Down Expand Up @@ -146,7 +148,13 @@ public JerseyWebTarget matrixParam(String name, Object... values) throws NullPoi
@Override
public JerseyWebTarget queryParam(String name, Object... values) throws NullPointerException {
checkNotClosed();
return new JerseyWebTarget(JerseyWebTarget.setQueryParam(getUriBuilder(), name, values), this);
UriBuilder uriBuilder = getUriBuilder();
if (uriBuilder instanceof JerseyUriBuilder) {
((JerseyUriBuilder) uriBuilder).setQueryParamStyle((JerseyQueryParamStyle) this.getConfiguration()
.getProperty(ClientProperties.QUERY_PARAM_STYLE)
);
}
return new JerseyWebTarget(JerseyWebTarget.setQueryParam(uriBuilder, name, values), this);
}

private static UriBuilder setQueryParam(UriBuilder uriBuilder, String name, Object[] values) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* 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.uri;

/**
* JerseyQueryParamStyle is used to specify the desired format of query param
* when multiple values are sent for the same parameter.
*/
public enum JerseyQueryParamStyle {

/**
* Multiple parameter instances, e.g.:
* <code>foo=v1&amp;foot=v2&amp;foo=v3</code>
*
* This is the default if no style is configured.
*/
MULTI_PAIRS,

/** A single parameter instance with multiple, comma-separated values, e.g.:
* <code>foo=v1,v2,v3</code>
*/
COMMA_SEPARATED,

/**
* Multiple parameter instances with square brackets for each parameter, e.g.:
* <code>foo[]=v1&amp;foo[]=v2&amp;foo[]=v3</code>
*/
ARRAY_PAIRS
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2010, 2018 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2010, 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
Expand Down Expand Up @@ -34,6 +34,7 @@
import org.glassfish.jersey.internal.guava.InetAddresses;
import org.glassfish.jersey.internal.util.ReflectionHelper;
import org.glassfish.jersey.internal.util.collection.MultivaluedStringMap;
import org.glassfish.jersey.uri.JerseyQueryParamStyle;
import org.glassfish.jersey.uri.UriComponent;
import org.glassfish.jersey.uri.UriTemplate;

Expand Down Expand Up @@ -68,6 +69,8 @@ public class JerseyUriBuilder extends UriBuilder {

private MultivaluedMap<String, String> queryParams;

private JerseyQueryParamStyle queryParamStyle;

private String fragment;

/**
Expand Down Expand Up @@ -536,6 +539,31 @@ public JerseyUriBuilder queryParam(String name, final Object... values) {
}

name = encode(name, UriComponent.Type.QUERY_PARAM);
if (null == queryParamStyle) {
clientQueryParamMultiPairs(name, values);
} else switch (queryParamStyle) {
case ARRAY_PAIRS:
clientQueryParamArrayPairs(name, values);
break;
case COMMA_SEPARATED:
clientQueryParamCommaSeparated(name, values);
break;
default:
clientQueryParamMultiPairs(name, values);
break;
}
return this;
}

/**
* Multiple parameter instances, e.g foo=v1&amp;foot=v2&amp;foo=v3 This is
* the default if no style is configured.
*
* @param name
* @param values
* @throws IllegalArgumentException
*/
private void clientQueryParamMultiPairs(String name, final Object... values) {
if (queryParams == null) {
for (final Object value : values) {
if (query.length() > 0) {
Expand All @@ -558,7 +586,91 @@ public JerseyUriBuilder queryParam(String name, final Object... values) {
queryParams.add(name, encode(value.toString(), UriComponent.Type.QUERY_PARAM));
}
}
return this;
}

/**
* A single parameter instance with multiple, comma-separated values, e.g
* key=value1,value2,value3.
*
* @param name
* @param values
* @throws IllegalArgumentException
*/
private void clientQueryParamCommaSeparated(String name, final Object... values) throws IllegalArgumentException {
StringBuilder sb = new StringBuilder();
if (queryParams == null) {
if (query.length() > 0) {
query.append('&');
}
query.append(name);
int valuesCount = values.length - 1;
for (final Object value : values) {
if (value == null) {
throw new IllegalArgumentException(LocalizationMessages.QUERY_PARAM_NULL());
}
sb.append(encode(value.toString(), UriComponent.Type.QUERY_PARAM));
if (valuesCount > 0) {
sb.append(",");
--valuesCount;
}
}
query.append('=').append(sb.toString());
} else {
int valuesCount = values.length - 1;
for (final Object value : values) {
if (value == null) {
throw new IllegalArgumentException(LocalizationMessages.QUERY_PARAM_NULL());
}
sb.append(encode(value.toString(), UriComponent.Type.QUERY_PARAM));
if (valuesCount > 0) {
sb.append(",");
--valuesCount;
}
}
queryParams.add(name, sb.toString());
}

}

/**
* Multiple parameter instances with square brackets for each parameter, e.g
* key[]=value1&key[]=value2&key[]=value3.
*
* @param name
* @param values
* @throws IllegalArgumentException
*/
private void clientQueryParamArrayPairs(String name, final Object... values) throws IllegalArgumentException {
if (queryParams == null) {
for (final Object value : values) {
if (query.length() > 0) {
query.append('&');
}
query.append(name).append("[]");

if (value == null) {
throw new IllegalArgumentException(LocalizationMessages.QUERY_PARAM_NULL());
}

query.append('=').append(encode(value.toString(), UriComponent.Type.QUERY_PARAM));
}
} else {
for (final Object value : values) {
if (value == null) {
throw new IllegalArgumentException(LocalizationMessages.QUERY_PARAM_NULL());
}

queryParams.add(name + "[]", encode(value.toString(), UriComponent.Type.QUERY_PARAM));
}
}
}

public JerseyQueryParamStyle getQueryParamStyle() {
return queryParamStyle;
}

public void setQueryParamStyle(JerseyQueryParamStyle queryParamStyle) {
this.queryParamStyle = queryParamStyle;
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion ext/microprofile/mp-config/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
<dependency>
<groupId>org.eclipse.microprofile.config</groupId>
<artifactId>microprofile-config-api</artifactId>
<version>${config.version}</version>
<version>${microprofile.config.version}</version>
</dependency>

<dependency>
Expand Down
18 changes: 14 additions & 4 deletions ext/microprofile/mp-rest-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@

-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>project</artifactId>
<groupId>org.glassfish.jersey.ext.microprofile</groupId>
Expand All @@ -32,7 +32,7 @@
<dependency>
<groupId>org.eclipse.microprofile.rest.client</groupId>
<artifactId>microprofile-rest-client-api</artifactId>
<version>1.4.1</version>
<version>2.0</version>
</dependency>
<dependency>
<groupId>org.eclipse.microprofile.config</groupId>
Expand Down Expand Up @@ -83,10 +83,20 @@
<groupId>org.glassfish</groupId>
<artifactId>jsonp-jaxrs</artifactId>
</dependency>
<dependency>
<groupId>org.reactivestreams</groupId>
<artifactId>reactive-streams</artifactId>
<version>1.0.3</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-sse</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugins>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
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, 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
Expand All @@ -17,6 +17,7 @@
package org.glassfish.jersey.microprofile.restclient;

import java.lang.annotation.Annotation;
import java.util.Collection;
import java.util.Map;

import javax.ws.rs.QueryParam;
Expand All @@ -43,8 +44,10 @@ public Map<String, Object[]> handleParameter(Map<String, Object[]> requestPart,
Object resolvedValue = interfaceModel.resolveParamValue(instance, parameter);
if (resolvedValue instanceof Object[]) {
requestPart.put(queryParamName, (Object[]) resolvedValue);
} else if (resolvedValue instanceof Collection) {
requestPart.put(queryParamName, ((Collection) resolvedValue).toArray());
} else {
requestPart.put(queryParamName, new Object[] {resolvedValue});
requestPart.put(queryParamName, new Object[]{resolvedValue});
}
return requestPart;
}
Expand Down
Loading