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

Add a ParamConverterProvider for array support #4684

Merged
merged 3 commits into from
Jan 22, 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
@@ -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 javax.ws.rs.core.MultivaluedHashMap;
import javax.ws.rs.core.MultivaluedMap;
import javax.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, 2019 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 All @@ -25,19 +25,18 @@
import java.util.Set;
import java.util.SortedSet;

import javax.inject.Singleton;
import javax.ws.rs.ProcessingException;
import javax.ws.rs.ext.ParamConverter;

import javax.inject.Singleton;

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 +119,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 +178,6 @@ private MultivaluedParameterExtractor<?> process(
}

}

return null;
}
}
Loading