Class ScriptedResponseMappingStrategy
java.lang.Object
net.shibboleth.utilities.java.support.scripting.AbstractScriptEvaluator
net.shibboleth.idp.attribute.resolver.dc.http.impl.ScriptedResponseMappingStrategy
- All Implemented Interfaces:
HTTPResponseMappingStrategy
,MappingStrategy<Map<String,IdPAttribute>>
,org.apache.http.client.ResponseHandler<Map<String,IdPAttribute>>
public final class ScriptedResponseMappingStrategy extends AbstractScriptEvaluator implements HTTPResponseMappingStrategy
HTTPResponseMappingStrategy
that relies on a script to map the response to the
attribute set.
Well-suited to JSON output formats that can be parsed by the scripting engine.
-
Field Summary
Fields Modifier and Type Field Description private Set<Integer>
acceptStatuses
"Successful" statuses.private Set<String>
acceptTypes
Acceptable Content-Types.private org.slf4j.Logger
log
Class logger.private long
maxLength
Limit on content-length.static String
RESULTS_STRING
The id of the object where the results go.Fields inherited from class net.shibboleth.utilities.java.support.scripting.AbstractScriptEvaluator
DEFAULT_ENGINE
-
Constructor Summary
Constructors Modifier Constructor Description private
ScriptedResponseMappingStrategy(EvaluableScript theScript)
Constructor. -
Method Summary
Modifier and Type Method Description private void
checkContentLength(org.apache.http.HttpEntity entity)
Check the content length.private void
checkContentType(org.apache.http.HttpEntity entity)
Enforce Content-Type requirements.private void
checkStatus(org.apache.http.HttpResponse response)
Enforce any status code requirements.private void
checkValues(IdPAttribute attribute)
Ensure that all the values in the attribute are of the correct type.protected Object
finalizeContext(ScriptContext scriptContext, Object scriptResult)
Map<String,IdPAttribute>
handleResponse(org.apache.http.HttpResponse response)
(package private) static ScriptedResponseMappingStrategy
inlineScript(String scriptSource)
Factory to createScriptedResponseMappingStrategy
from inline data.(package private) static ScriptedResponseMappingStrategy
inlineScript(String engineName, String scriptSource)
Factory to createScriptedResponseMappingStrategy
from inline data.Map<String,IdPAttribute>
map(Map<String,IdPAttribute> results)
Maps the given results to a collection ofIdPAttribute
indexed by the attribute's ID.protected void
prepareContext(ScriptContext scriptContext, Object... input)
(package private) static ScriptedResponseMappingStrategy
resourceScript(String engineName, Resource resource)
Factory to createScriptedResponseMappingStrategy
from aResource
.(package private) static ScriptedResponseMappingStrategy
resourceScript(Resource resource)
Factory to createScriptedResponseMappingStrategy
from aResource
.void
setAcceptStatuses(Collection<Integer> statuses)
Set the HTTP status codes to treat as successful.void
setAcceptTypes(Collection<String> types)
Set the content-types to allow.void
setMaxLength(long len)
Set a limit on content-length.Methods inherited from class net.shibboleth.utilities.java.support.scripting.AbstractScriptEvaluator
evaluate, getCustomObject, getHideExceptions, getLogPrefix, getOutputType, getReturnOnError, setCustomObject, setHideExceptions, setLogPrefix, setOutputType, setReturnOnError
-
Field Details
-
RESULTS_STRING
The id of the object where the results go.- See Also:
- Constant Field Values
-
log
@Nonnull private final org.slf4j.Logger logClass logger. -
acceptStatuses
"Successful" statuses. -
acceptTypes
Acceptable Content-Types. -
maxLength
private long maxLengthLimit on content-length.
-
-
Constructor Details
-
ScriptedResponseMappingStrategy
Constructor.- Parameters:
theScript
- the script to run
-
-
Method Details
-
setAcceptStatuses
Set the HTTP status codes to treat as successful.- Parameters:
statuses
- successful codes
-
setAcceptTypes
Set the content-types to allow.- Parameters:
types
- types to allow
-
setMaxLength
public void setMaxLength(long len)Set a limit on content-length.Defaults to 0, allowing any. Setting a limit implies that an unknown length will be rejected.
- Parameters:
len
- limit on size
-
map
@Nonnull public Map<String,IdPAttribute> map(@Nonnull Map<String,IdPAttribute> results) throws ResolutionExceptionMaps the given results to a collection ofIdPAttribute
indexed by the attribute's ID.- Specified by:
map
in interfaceMappingStrategy<Map<String,IdPAttribute>>
- Parameters:
results
- to map- Returns:
- the mapped attributes or null if none exist
- Throws:
ResolutionException
- thrown if there is a problem reading data or mapping it
-
handleResponse
public Map<String,IdPAttribute> handleResponse(org.apache.http.HttpResponse response) throws IOException- Specified by:
handleResponse
in interfaceorg.apache.http.client.ResponseHandler<Map<String,IdPAttribute>>
- Throws:
IOException
-
prepareContext
- Specified by:
prepareContext
in classAbstractScriptEvaluator
-
finalizeContext
@Nullable protected Object finalizeContext(@Nonnull ScriptContext scriptContext, @Nullable Object scriptResult) throws ScriptException- Overrides:
finalizeContext
in classAbstractScriptEvaluator
- Throws:
ScriptException
-
checkStatus
Enforce any status code requirements.- Parameters:
response
- HTTP response- Throws:
IOException
- if the status is unacceptable
-
checkContentType
Enforce Content-Type requirements.- Parameters:
entity
- the entity body- Throws:
IOException
- if the type is unacceptable
-
checkContentLength
Check the content length.- Parameters:
entity
- the entity body- Throws:
IOException
- if the length is unacceptable
-
checkValues
Ensure that all the values in the attribute are of the correct type.- Parameters:
attribute
- the attribute to look at
-
resourceScript
@Nonnull static ScriptedResponseMappingStrategy resourceScript(@Nonnull @NotEmpty String engineName, @Nonnull Resource resource) throws ScriptException, IOExceptionFactory to createScriptedResponseMappingStrategy
from aResource
.- Parameters:
engineName
- the languageresource
- the resource to look at- Returns:
- the function
- Throws:
ScriptException
- if the compile failsIOException
- if the file doesn't exist.
-
resourceScript
@Nonnull static ScriptedResponseMappingStrategy resourceScript(@Nonnull Resource resource) throws ScriptException, IOExceptionFactory to createScriptedResponseMappingStrategy
from aResource
.- Parameters:
resource
- the resource to look at- Returns:
- the function
- Throws:
ScriptException
- if the compile failsIOException
- if the file doesn't exist.
-
inlineScript
@Nonnull static ScriptedResponseMappingStrategy inlineScript(@Nonnull @NotEmpty String engineName, @Nonnull @NotEmpty String scriptSource) throws ScriptExceptionFactory to createScriptedResponseMappingStrategy
from inline data.- Parameters:
scriptSource
- the script, as a stringengineName
- the language- Returns:
- the function
- Throws:
ScriptException
- if the compile fails
-
inlineScript
@Nonnull static ScriptedResponseMappingStrategy inlineScript(@Nonnull @NotEmpty String scriptSource) throws ScriptExceptionFactory to createScriptedResponseMappingStrategy
from inline data.- Parameters:
scriptSource
- the script, as a string- Returns:
- the function
- Throws:
ScriptException
- if the compile fails
-