Class JsonHttpClientSupport
java.lang.Object
net.solarnetwork.service.support.BasicIdentifiable
net.solarnetwork.node.service.support.BaseIdentifiable
net.solarnetwork.node.service.support.HttpClientSupport
net.solarnetwork.node.service.support.JsonHttpClientSupport
- All Implemented Interfaces:
net.solarnetwork.service.Identifiable
- Direct Known Subclasses:
UserMetadataService
An abstract class to support HTTP based services that use JSON.
The configurable properties of this class are:
- compress
- Flag to compress the HTTP body content, defaults to false.
- objectMapper
- The
ObjectMapperto marshall/unmarshall objects to/from JSON with.
- Version:
- 1.3
- Author:
- matt
-
Field Summary
FieldsFields inherited from class net.solarnetwork.node.service.support.HttpClientSupport
ACCEPT_JSON, ACCEPT_TEXT, DEFAULT_CONNECTION_TIMEOUT, HTTP_METHOD_GET, HTTP_METHOD_POST, logFields inherited from interface net.solarnetwork.service.Identifiable
GROUP_UID_PROPERTY, UID_PROPERTY -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected final InputStreamPerform a JSON HTTP request.protected final InputStreamPerform a JSON HTTP request.protected <T> Collection<T>extractCollectionResponseData(InputStream in, Class<T> dataType) Parse a standardResponseHTTP response and return thedataarray as a collection of objects of the provided type.protected <T> Collection<T>extractFilterResultsCollectionResponseData(InputStream in, Class<T> dataType) Parse a standardResponseHTTP response and return thedata.resultsarray as objects of the provided type.protected <T> TextractResponseData(InputStream in, Class<T> dataType) Parse a standardResponseHTTP response and return thedataobject as the provided type.final com.fasterxml.jackson.databind.ObjectMapperGet the JSON mapper.final booleanGet the compress flag.protected final InputStreamPerform a JSON GET HTTP request.protected final InputStreamjsonGET(String url, Consumer<URLConnection> connectionCustomizer) Perform a JSON GET HTTP request.protected final InputStreamPerform a JSON POST HTTP request.protected final InputStreamjsonPOST(String url, Object data, Consumer<URLConnection> connectionCustomizer) Perform a JSON POST HTTP request.final voidsetCompress(boolean compress) Set the compress flag.final voidsetObjectMapper(com.fasterxml.jackson.databind.ObjectMapper objectMapper) Set the JSON mapper.protected voidTest for a successful standardResponseHTTP response.Methods inherited from class net.solarnetwork.node.service.support.HttpClientSupport
appendXWWWFormURLEncodedValue, getConnectionTimeout, getIdentityService, getInputStreamFromURLConnection, getSslService, getUnicodeReaderFromURLConnection, getURLConnection, getURLConnection, getURLConnection, postXWWWFormURLEncodedData, postXWWWFormURLEncodedDataForString, setConnectionTimeout, setIdentityService, setSslService, setupTokenAuthorization, xWWWFormURLEncodedMethods inherited from class net.solarnetwork.node.service.support.BaseIdentifiable
baseIdentifiableSettings, baseIdentifiableSettings, getExpressionServices, getPlaceholderService, populateExpressionDatumProperties, resolvePlaceholders, resolvePlaceholders, setExpressionServices, setPlaceholderServiceMethods inherited from class net.solarnetwork.service.support.BasicIdentifiable
basicIdentifiableMetadataSettings, basicIdentifiableSettings, basicIdentifiableSettings, basicIdentifiableSettings, doubleMetadataValue, getDisplayName, getGroupUid, getGroupUID, getMessageSource, getMetadata, getMetadataCount, getUid, getUID, integerMetadataValue, metadataValue, numberMetadataValue, saveMetadataValue, saveMetadataValue, setDisplayName, setGroupUid, setGroupUID, setMessageSource, setMetadata, setMetadataCount, setUid, setUID, smartMetadataValueMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface net.solarnetwork.service.Identifiable
getDisplayName, getGroupUid, getUid
-
Field Details
-
JSON_MIME_TYPE
The JSON MIME type.- See Also:
-
-
Constructor Details
-
JsonHttpClientSupport
public JsonHttpClientSupport()
-
-
Method Details
-
doJson
Perform a JSON HTTP request.- Parameters:
url- the URL to make the request tomethod- the HTTP method, e.g.HttpClientSupport.HTTP_METHOD_GETdata- the optional data to marshall to JSON and upload as the request content- Returns:
- the InputStream for the HTTP response
- Throws:
IOException- if any IO error occurs
-
doJson
protected final InputStream doJson(String url, String method, Object data, Consumer<URLConnection> connectionCustomizer) throws IOException Perform a JSON HTTP request.- Parameters:
url- the URL to make the request tomethod- the HTTP method, e.g.HttpClientSupport.HTTP_METHOD_GETdata- the optional data to marshall to JSON and upload as the request contentconnectionCustomizer- an optional consumer to customize the created URL connection, before it is opened- Returns:
- the InputStream for the HTTP response
- Throws:
IOException- if any IO error occurs- Since:
- 1.3
-
jsonGET
Perform a JSON GET HTTP request.- Parameters:
url- the URL to GET- Returns:
- the HTTP response InputStream
- Throws:
IOException- if any IO error occurs
-
jsonGET
protected final InputStream jsonGET(String url, Consumer<URLConnection> connectionCustomizer) throws IOException Perform a JSON GET HTTP request.- Parameters:
url- the URL to GETconnectionCustomizer- an optional consumer to customize the created URL connection, before it is opened- Returns:
- the HTTP response InputStream
- Throws:
IOException- if any IO error occurs- Since:
- 1.3
-
jsonPOST
Perform a JSON POST HTTP request.- Parameters:
url- the URL to POSTdata- the object to marshall into JSON- Returns:
- the HTTP response InputStream
- Throws:
IOException- if any IO error occurs
-
jsonPOST
protected final InputStream jsonPOST(String url, Object data, Consumer<URLConnection> connectionCustomizer) throws IOException Perform a JSON POST HTTP request.- Parameters:
url- the URL to POSTdata- the object to marshall into JSONconnectionCustomizer- an optional consumer to customize the created URL connection, before it is opened- Returns:
- the HTTP response InputStream
- Throws:
IOException- if any IO error occurs- Since:
- 1.3
-
extractResponseData
protected <T> T extractResponseData(InputStream in, Class<T> dataType) throws net.solarnetwork.service.RemoteServiceException, IOException Parse a standardResponseHTTP response and return thedataobject as the provided type.- Type Parameters:
T- the element type- Parameters:
in- the InputStream to read, which will be closed before returning from this methoddataType- the type of object to extract from the response- Returns:
- the extracted object, or null
- Throws:
net.solarnetwork.service.RemoteServiceException- if the response does not include the success flagIOException- if any IO error occurs
-
extractCollectionResponseData
protected <T> Collection<T> extractCollectionResponseData(InputStream in, Class<T> dataType) throws net.solarnetwork.service.RemoteServiceException, IOException Parse a standardResponseHTTP response and return thedataarray as a collection of objects of the provided type.- Type Parameters:
T- the collection element type- Parameters:
in- the InputStream to read, which will be closed before returning from this methoddataType- the type of object to extract from the response- Returns:
- the extracted object, or null
- Throws:
net.solarnetwork.service.RemoteServiceException- if the response does not include the success flagIOException- if any IO error occurs- Since:
- 1.1
-
extractFilterResultsCollectionResponseData
protected <T> Collection<T> extractFilterResultsCollectionResponseData(InputStream in, Class<T> dataType) throws net.solarnetwork.service.RemoteServiceException, IOException Parse a standardResponseHTTP response and return thedata.resultsarray as objects of the provided type.- Type Parameters:
T- the collection element type- Parameters:
in- the InputStream to read, which will be closed before returning from this methoddataType- the type of object to extract from the response- Returns:
- the extracted objects, or null
- Throws:
net.solarnetwork.service.RemoteServiceException- if the response does not include the success flagIOException- if any IO error occurs- Since:
- 1.2
-
verifyResponseSuccess
protected void verifyResponseSuccess(InputStream in) throws net.solarnetwork.service.RemoteServiceException, IOException Test for a successful standardResponseHTTP response.- Parameters:
in- the InputStream to read, which will be closed before returning from this method- Throws:
net.solarnetwork.service.RemoteServiceException- if the response does not include the success flagIOException- if any IO error occurs
-
getObjectMapper
public final com.fasterxml.jackson.databind.ObjectMapper getObjectMapper()Get the JSON mapper.- Returns:
- the mapper
-
setObjectMapper
public final void setObjectMapper(com.fasterxml.jackson.databind.ObjectMapper objectMapper) Set the JSON mapper.- Parameters:
objectMapper- the mapper
-
isCompress
public final boolean isCompress()Get the compress flag.- Returns:
- true to compress HTTP body content with gzip
-
setCompress
public final void setCompress(boolean compress) Set the compress flag.- Parameters:
compress- true to compress HTTP body content with gzip
-