-
Notifications
You must be signed in to change notification settings - Fork 355
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4696 from senivam/3x_merged
merge of actual master into 3.x
- Loading branch information
Showing
6 changed files
with
617 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
core-server/src/main/java/org/glassfish/jersey/server/internal/inject/ArrayExtractor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.