-
Notifications
You must be signed in to change notification settings - Fork 357
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Separate Payara's inserters from PR 3844
Signed-off-by: Jan Supol <jan.supol@oracle.com>
- Loading branch information
1 parent
895c293
commit f2d3796
Showing
25 changed files
with
1,015 additions
and
24 deletions.
There are no files selected for viewing
41 changes: 41 additions & 0 deletions
41
core-client/src/main/java/org/glassfish/jersey/client/ClientBootstrapBag.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,41 @@ | ||
/* | ||
* Copyright (c) 2018 Payara Foundation 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.client; | ||
|
||
|
||
import org.glassfish.jersey.internal.BootstrapBag; | ||
import org.glassfish.jersey.client.inject.ParameterInserterProvider; | ||
|
||
/** | ||
* {@inheritDoc} | ||
* <p> | ||
* This bootstrap bag is specialized for client part of Jersey. | ||
* | ||
* @author Gaurav Gupta (gaurav.gupta@payara.fish) | ||
*/ | ||
public class ClientBootstrapBag extends BootstrapBag { | ||
|
||
private ParameterInserterProvider parameterInserterProvider; | ||
|
||
public ParameterInserterProvider getParameterInserterProvider() { | ||
requireNonNull(parameterInserterProvider, ParameterInserterProvider.class); | ||
return parameterInserterProvider; | ||
} | ||
|
||
public void setParameterInserterProvider(ParameterInserterProvider provider) { | ||
this.parameterInserterProvider = provider; | ||
} | ||
} |
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
55 changes: 55 additions & 0 deletions
55
core-client/src/main/java/org/glassfish/jersey/client/inject/ParameterInserter.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,55 @@ | ||
/* | ||
* Copyright (c) 2010, 2017 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 | ||
* 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.client.inject; | ||
|
||
/** | ||
* Provider that converts the an object of a custom Java type | ||
* values to String / Collection<String>> type | ||
* | ||
* @param <T> custom Java type | ||
* @param <R> String / Collection<String>> type | ||
* | ||
* @author Paul Sandoz | ||
* @author Marek Potociar (marek.potociar at oracle.com) | ||
* @author Gaurav Gupta (gaurav.gupta@payara.fish) | ||
*/ | ||
public interface ParameterInserter<T, R> { | ||
|
||
/** | ||
* Name of the parameter to be inserted | ||
* | ||
* @return name of the inserted parameter. | ||
*/ | ||
String getName(); | ||
|
||
/** | ||
* Default value (string) that will be used in case input value is not available. | ||
* | ||
* @return default (back-up) value. | ||
*/ | ||
String getDefaultValueString(); | ||
|
||
/** | ||
* Insert the value using ParamConverter#toString (and using | ||
* the configured {@link #getDefaultValueString() default value}) | ||
* | ||
* @param parameters custom Java type instance value. | ||
* @return converted value. | ||
*/ | ||
R insert(T parameters); | ||
} |
41 changes: 41 additions & 0 deletions
41
core-client/src/main/java/org/glassfish/jersey/client/inject/ParameterInserterProvider.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,41 @@ | ||
/* | ||
* Copyright (c) 2010, 2017 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 | ||
* 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.client.inject; | ||
|
||
import org.glassfish.jersey.model.Parameter; | ||
|
||
/** | ||
* Provider of parameter inserter. | ||
* | ||
* @author Paul Sandoz | ||
* @author Marek Potociar (marek.potociar at oracle.com) | ||
* @author Gaurav Gupta (gaurav.gupta@payara.fish) | ||
*/ | ||
public interface ParameterInserterProvider { | ||
|
||
/** | ||
* Get the inserter configured to insert value of given {@link Parameter parameter}. | ||
* <p /> | ||
* If the default value has been set on the parameter, it will be configured | ||
* in the inserter. | ||
* | ||
* @param parameter client model parameter. | ||
* @return inserter for the method parameter. | ||
*/ | ||
ParameterInserter<?, ?> get(Parameter parameter); | ||
} |
143 changes: 143 additions & 0 deletions
143
...src/main/java/org/glassfish/jersey/client/internal/inject/AbstractParamValueInserter.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,143 @@ | ||
/* | ||
* Copyright (c) 2010, 2017 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 | ||
* 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.client.internal.inject; | ||
|
||
import javax.ws.rs.WebApplicationException; | ||
import javax.ws.rs.ext.ParamConverter; | ||
|
||
import org.glassfish.jersey.internal.inject.InserterException; | ||
import org.glassfish.jersey.internal.util.collection.UnsafeValue; | ||
import org.glassfish.jersey.internal.util.collection.Values; | ||
|
||
/** | ||
* Abstract base class for implementing parameter value inserter | ||
* logic supplied using {@link ParamConverter parameter converters}. | ||
* | ||
* @author Paul Sandoz | ||
* @author Marek Potociar (marek.potociar at oracle.com) | ||
* @author Gaurav Gupta (gaurav.gupta@payara.fish) | ||
*/ | ||
abstract class AbstractParamValueInserter<T> { | ||
|
||
private final ParamConverter<T> paramConverter; | ||
private final String parameterName; | ||
private final String defaultValue; | ||
private final UnsafeValue<String, RuntimeException> convertedDefaultValue; | ||
|
||
/** | ||
* Constructor that initializes parameter inserter. | ||
* | ||
* @param converter parameter converter. | ||
* @param parameterName name of the parameter. | ||
* @param defaultValueString default parameter value string. | ||
*/ | ||
protected AbstractParamValueInserter(ParamConverter<T> converter, String parameterName, final String defaultValue) { | ||
this.paramConverter = converter; | ||
this.parameterName = parameterName; | ||
this.defaultValue = defaultValue; | ||
|
||
if (defaultValue != null) { | ||
this.convertedDefaultValue = Values.lazy(new UnsafeValue<String, RuntimeException>() { | ||
@Override | ||
public String get() throws RuntimeException { | ||
return defaultValue; | ||
} | ||
}); | ||
|
||
if (!converter.getClass().isAnnotationPresent(ParamConverter.Lazy.class)) { | ||
// ignore return value - executed just for validation reasons | ||
convertedDefaultValue.get(); | ||
} | ||
} else { | ||
convertedDefaultValue = null; | ||
} | ||
} | ||
|
||
/** | ||
* Get the name of the parameter this inserter belongs to. | ||
* | ||
* @return parameter name. | ||
*/ | ||
public String getName() { | ||
return parameterName; | ||
} | ||
|
||
/** | ||
* Get the default value of the parameter. | ||
* | ||
* @return default parameter value. | ||
*/ | ||
public String getDefaultValueString() { | ||
return defaultValue; | ||
} | ||
|
||
/** | ||
* Insert parameter value to string using the configured {@link ParamConverter parameter converter}. | ||
* | ||
* A {@link WebApplicationException} / {@link IllegalArgumentException} thrown | ||
* from the converter is propagated unchanged. Any other exception throws by | ||
* the converter is wrapped in a new {@link InserterException} before rethrowing. | ||
* | ||
* @param value parameter value to be converted/inserted. | ||
* @return inserted value of a given Java type. | ||
* @throws WebApplicationException in case the underlying parameter converter throws | ||
* a {@code WebApplicationException}. The exception is rethrown without a change. | ||
* @throws InserterException wrapping any other exception thrown by the parameter converter. | ||
*/ | ||
protected final String toString(T value) { | ||
String result = convert(value); | ||
if (result == null) { | ||
return defaultValue(); | ||
} | ||
return result; | ||
} | ||
|
||
private String convert(T value) { | ||
try { | ||
return paramConverter.toString(value); | ||
} catch (WebApplicationException | IllegalArgumentException ex) { | ||
throw ex; | ||
} catch (Exception ex) { | ||
throw new InserterException(ex); | ||
} | ||
} | ||
|
||
/** | ||
* Check if there is a default value registered for the parameter. | ||
* | ||
* @return {@code true} if there is a default parameter value registered, {@code false} otherwise. | ||
*/ | ||
protected final boolean isDefaultValueRegistered() { | ||
return defaultValue != null; | ||
} | ||
|
||
/** | ||
* Get converted default value. | ||
* | ||
* The conversion happens lazily during first call of the method. | ||
* | ||
* @return converted default value. | ||
*/ | ||
protected final String defaultValue() { | ||
if (!isDefaultValueRegistered()) { | ||
return null; | ||
} | ||
|
||
return convertedDefaultValue.get(); | ||
} | ||
} |
Oops, something went wrong.