Package com.hybris.ymkt.common.odata
Class ODataService
- java.lang.Object
-
- com.hybris.ymkt.common.odata.ODataService
-
public class ODataService extends java.lang.ObjectThis bean shall be instantiated for each OData service as separate $metadata are used by OData services.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected static classODataService.TokenCache
-
Field Summary
Fields Modifier and Type Field Description protected org.apache.olingo.odata2.core.edm.provider.EdmxProvideredmxProviderprotected HttpURLConnectionServicehttpURLConnectionServiceprotected ODataConvertEdmServiceoDataConvertEdmServiceprotected java.lang.Stringpasswordprotected java.net.Proxyproxyprotected java.lang.StringrootUrlprotected java.lang.StringsapClientprotected ODataService.TokenCachetokenprotected java.lang.Stringuserprotected static java.lang.StringX_CSRF_TOKEN
-
Constructor Summary
Constructors Constructor Description ODataService()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description byte[]bufferStream(java.io.InputStream stream)ReadInputStreamand return a byte array.protected voidcheckStatus(HttpURLConnectionResponse response)protected com.google.gson.JsonObjectconvertMapToJSON(org.apache.olingo.odata2.api.edm.EdmEntityType entityType, java.util.Map<?,?> data)byte[]convertMapToJSONPayload(java.lang.String entitySetName, java.util.Map<java.lang.String,java.lang.Object> data)Convert an entity map into a JSON payload reading to be transfered.java.lang.StringconvertMapToJSONString(org.apache.olingo.odata2.api.edm.EdmEntityType entityType, java.util.Map<java.lang.String,java.lang.Object> data)Convert an entity map into a JSON String.java.util.Map<java.lang.String,java.lang.String>convertMapToURIParameters(java.lang.String functionImportName, java.util.Map<java.lang.String,java.lang.Object> data)Build properly formated String value arguments for OData Function Import.protected com.google.gson.JsonElementconvertObjectToJsonElement(java.lang.Object value, org.apache.olingo.odata2.api.edm.EdmElement element)protected com.google.gson.JsonElementconvertObjectToJsonElement(java.lang.Object value, org.apache.olingo.odata2.api.edm.EdmNavigationProperty navigation)protected java.lang.StringconvertObjectToString(java.lang.Object value, org.apache.olingo.odata2.api.edm.EdmLiteralKind literalKind, org.apache.olingo.odata2.api.edm.EdmElement element)java.lang.StringcreateResourcePath(java.lang.String entitySetName, java.util.Map<java.lang.String,java.lang.Object> data)Create $batch part URL accessing entity by keys for Addressing Entries.java.net.URLcreateURL(java.lang.String resourcePath)Create an absolute URL accessing the OData service at the resource path provided.java.net.URLcreateURL(java.lang.String resourcePath, java.lang.String... queryOptions)Create an absolute URL accessing the OData service at the resourcePath provided using OData query options.java.net.URLcreateURL(java.lang.String resourcePath, java.util.Map<java.lang.String,java.lang.String> queryOptions)Create an absolute URL accessing the OData service at the resourcePath provided using OData query options.protected byte[]decompressIfGZIP(byte[] payload)protected HttpURLConnectionResponseexecuteWithBasicAuth(HttpURLConnectionRequest request)Execute the request with Authorization : "Basic BASE64user:pass".
Using this method will create a new http session (unnecessary cost) on the yMKT side.
It is preferable to useexecuteWithRetry(HttpURLConnectionRequest)for most execution scenarios.HttpURLConnectionResponseexecuteWithRetry(HttpURLConnectionRequest request)Execute the request with "cookie" & "x-csrf-token".
If the existing session and/or token expired, those are refreshed and the request is re-tried.ODataFilterBuilderfilter(java.lang.String entitySetName)Create anODataFilterBuilderstarting on the entitySetName provided.protected java.lang.StringBuildergetCustomQueryOptions()org.apache.olingo.odata2.api.edm.EdmgetEdm()Provide buffered olingo'sEdmof the OData service.org.apache.olingo.odata2.api.edm.EdmEntitySetgetEntitySet(java.lang.String entitySetName)Simple wrapping with improved error handling of :
this.getEdm().getDefaultEntityContainer().getEntitySet(entitySetName);protected ODataService.TokenCachegetTokenCache()protected ODataService.TokenCacherefreshToken(ODataService.TokenCache token)voidsetHttpURLConnectionService(HttpURLConnectionService httpURLConnectionService)voidsetODataConvertEdmService(ODataConvertEdmService oDataConvertEdmService)voidsetPassword(java.lang.String password)voidsetProxy(java.lang.String proxy)voidsetRootUrl(java.lang.String rootUrl)voidsetSapClient(java.lang.String sapClient)voidsetUser(java.lang.String user)
-
-
-
Field Detail
-
X_CSRF_TOKEN
protected static final java.lang.String X_CSRF_TOKEN
- See Also:
- Constant Field Values
-
edmxProvider
protected volatile org.apache.olingo.odata2.core.edm.provider.EdmxProvider edmxProvider
-
httpURLConnectionService
protected HttpURLConnectionService httpURLConnectionService
-
oDataConvertEdmService
protected ODataConvertEdmService oDataConvertEdmService
-
password
protected java.lang.String password
-
proxy
protected java.net.Proxy proxy
-
rootUrl
protected java.lang.String rootUrl
-
sapClient
protected java.lang.String sapClient
-
token
protected volatile ODataService.TokenCache token
-
user
protected java.lang.String user
-
-
Method Detail
-
bufferStream
@Nonnull public byte[] bufferStream(java.io.InputStream stream) throws java.io.IOExceptionReadInputStreamand return a byte array.- Parameters:
stream-InputStreamto read.- Returns:
- All bytes that could be read from the
InputStream - Throws:
java.io.IOException- fromInputStream.read(byte[])- See Also:
HttpURLConnectionService.bufferStream(InputStream)
-
checkStatus
protected void checkStatus(HttpURLConnectionResponse response) throws java.io.IOException
- Throws:
java.io.IOException
-
convertMapToJSON
@Nonnull protected com.google.gson.JsonObject convertMapToJSON(org.apache.olingo.odata2.api.edm.EdmEntityType entityType, java.util.Map<?,?> data) throws java.io.IOException, org.apache.olingo.odata2.api.edm.EdmException- Throws:
java.io.IOExceptionorg.apache.olingo.odata2.api.edm.EdmException
-
convertMapToJSONPayload
@Nonnull public byte[] convertMapToJSONPayload(java.lang.String entitySetName, java.util.Map<java.lang.String,java.lang.Object> data) throws java.io.IOExceptionConvert an entity map into a JSON payload reading to be transfered.- Parameters:
entitySetName- <EntitySet Name> value found in the DefaultEntityContainer of the OData service.data-Mapcontaining the <Property/NavigationProperty Name> and value to be converted.- Returns:
- byte array of the JSON payload.
- Throws:
java.io.IOException- if an I/O error occurs.- See Also:
EdmLiteralKind.JSON
-
convertMapToJSONString
@Nonnull public java.lang.String convertMapToJSONString(org.apache.olingo.odata2.api.edm.EdmEntityType entityType, java.util.Map<java.lang.String,java.lang.Object> data) throws java.io.IOExceptionConvert an entity map into a JSON String.- Parameters:
entityType-EdmEntityTypeof the OData entity to be converted.data-Mapcontaining the <Parameter> Name and value to be converted.- Returns:
- String of the JSON payload.
- Throws:
java.io.IOException- if an I/O error occurs.- See Also:
EdmLiteralKind.JSON
-
convertMapToURIParameters
public java.util.Map<java.lang.String,java.lang.String> convertMapToURIParameters(java.lang.String functionImportName, java.util.Map<java.lang.String,java.lang.Object> data) throws java.io.IOExceptionBuild properly formated String value arguments for OData Function Import.- Parameters:
functionImportName- Attribute Name value found in the <FunctionImport> of the OData service.data-Mapcontaining the <Parameter> Name and value to be converted.- Returns:
Mapcontaining the <Parameter> Name and converted value.- Throws:
java.io.IOException- if an I/O error occurs.- See Also:
EdmLiteralKind.URI
-
convertObjectToJsonElement
@Nonnull protected com.google.gson.JsonElement convertObjectToJsonElement(java.lang.Object value, org.apache.olingo.odata2.api.edm.EdmElement element) throws org.apache.olingo.odata2.api.edm.EdmException- Throws:
org.apache.olingo.odata2.api.edm.EdmException
-
convertObjectToJsonElement
protected com.google.gson.JsonElement convertObjectToJsonElement(java.lang.Object value, org.apache.olingo.odata2.api.edm.EdmNavigationProperty navigation) throws org.apache.olingo.odata2.api.edm.EdmException, java.io.IOException- Throws:
org.apache.olingo.odata2.api.edm.EdmExceptionjava.io.IOException
-
convertObjectToString
@Nullable protected java.lang.String convertObjectToString(java.lang.Object value, org.apache.olingo.odata2.api.edm.EdmLiteralKind literalKind, org.apache.olingo.odata2.api.edm.EdmElement element) throws org.apache.olingo.odata2.api.edm.EdmException- Throws:
org.apache.olingo.odata2.api.edm.EdmException
-
createResourcePath
public java.lang.String createResourcePath(java.lang.String entitySetName, java.util.Map<java.lang.String,java.lang.Object> data) throws java.io.IOExceptionCreate $batch part URL accessing entity by keys for Addressing Entries.- Parameters:
entitySetName- <EntitySet Name> value found in the DefaultEntityContainer of the OData service.data-Mapcontaining the <Property/NavigationProperty Name> and value to be converted.- Returns:
- resource path
- Throws:
java.io.IOException- if an I/O error occurs.
-
createURL
@Nonnull public java.net.URL createURL(java.lang.String resourcePath)
Create an absolute URL accessing the OData service at the resource path provided.- Parameters:
resourcePath- Resource path of the OData service.- Returns:
URL
-
createURL
@Nonnull public java.net.URL createURL(java.lang.String resourcePath, java.util.Map<java.lang.String,java.lang.String> queryOptions)Create an absolute URL accessing the OData service at the resourcePath provided using OData query options.- Parameters:
resourcePath- Resource path of the OData service.queryOptions- OData Query String Options.- Returns:
URL
-
createURL
@Nonnull public java.net.URL createURL(java.lang.String resourcePath, java.lang.String... queryOptions)Create an absolute URL accessing the OData service at the resourcePath provided using OData query options.- Parameters:
resourcePath- Resource path of the OData service.queryOptions- OData Query String Options.- Returns:
URL
-
decompressIfGZIP
@Nonnull protected byte[] decompressIfGZIP(byte[] payload) throws java.io.IOException- Throws:
java.io.IOException
-
executeWithBasicAuth
@Nonnull protected HttpURLConnectionResponse executeWithBasicAuth(HttpURLConnectionRequest request) throws java.io.IOException
Execute the request with Authorization : "Basic BASE64user:pass".
Using this method will create a new http session (unnecessary cost) on the yMKT side.
It is preferable to useexecuteWithRetry(HttpURLConnectionRequest)for most execution scenarios.- Parameters:
request-HttpURLConnectionRequestto execute.- Returns:
HttpURLConnectionResponseexecuted.- Throws:
java.io.IOException- If the HTTP request was not successful.- See Also:
URLConnection.setRequestProperty(java.lang.String, java.lang.String)
-
executeWithRetry
@Nonnull public HttpURLConnectionResponse executeWithRetry(HttpURLConnectionRequest request) throws java.io.IOException
Execute the request with "cookie" & "x-csrf-token".
If the existing session and/or token expired, those are refreshed and the request is re-tried.- Parameters:
request-HttpURLConnectionRequestto execute.- Returns:
HttpURLConnectionResponseexecuted.- Throws:
java.io.IOException- If the HTTP request was not successful.
-
filter
public ODataFilterBuilder filter(java.lang.String entitySetName) throws java.io.IOException
Create anODataFilterBuilderstarting on the entitySetName provided.- Parameters:
entitySetName- <EntitySet Name> value found in the DefaultEntityContainer of the OData service.- Returns:
ODataFilterBuilder- Throws:
java.io.IOException- if an I/O error occurs.
-
getCustomQueryOptions
@Nonnull protected java.lang.StringBuilder getCustomQueryOptions()
-
getEdm
@Nonnull public org.apache.olingo.odata2.api.edm.Edm getEdm() throws java.io.IOExceptionProvide buffered olingo'sEdmof the OData service.- Returns:
- And instance of
Edm$metadatafor the service end point set bysetRootUrl(String).
This resource is requested once for the entire lifecycle of the bean. This resource is also synchronized.
Each Thread has its own instance of Edm. - Throws:
java.io.IOException- If Edm could not be returned.
-
getEntitySet
public org.apache.olingo.odata2.api.edm.EdmEntitySet getEntitySet(java.lang.String entitySetName) throws java.io.IOExceptionSimple wrapping with improved error handling of :
this.getEdm().getDefaultEntityContainer().getEntitySet(entitySetName);- Parameters:
entitySetName- <EntitySet Name> value found in the DefaultEntityContainer of the OData service.- Returns:
EdmEntitySet- Throws:
java.io.IOException- In case of communication errors.
-
getTokenCache
@Nonnull protected ODataService.TokenCache getTokenCache() throws java.io.IOException
- Throws:
java.io.IOException
-
refreshToken
@Nonnull protected ODataService.TokenCache refreshToken(ODataService.TokenCache token) throws java.io.IOException
- Throws:
java.io.IOException
-
setHttpURLConnectionService
public void setHttpURLConnectionService(HttpURLConnectionService httpURLConnectionService)
-
setODataConvertEdmService
public void setODataConvertEdmService(ODataConvertEdmService oDataConvertEdmService)
-
setPassword
public void setPassword(java.lang.String password)
- Parameters:
password- Password used to login withsetUser(String)at thesetRootUrl(String)address.
-
setProxy
public void setProxy(java.lang.String proxy) throws java.net.MalformedURLException- Throws:
java.net.MalformedURLException
-
setRootUrl
public void setRootUrl(java.lang.String rootUrl)
- Parameters:
rootUrl- OData service root URL.
Sample :https://localhost:50100/sap/opu/odata/sap/PROD_RECO_RUNTIME_SRV/
localhostshould be replaced with the proper SAP Marketing server host name.
-
setSapClient
public void setSapClient(java.lang.String sapClient)
- Parameters:
sapClient- the sapClient to set
-
setUser
public void setUser(java.lang.String user)
- Parameters:
user- Username used to login at thesetRootUrl(String)address.
-
-