Class AbstractWebProvider
- java.lang.Object
-
- org.teichert.databaseexplorer.messages.web.AbstractWebProvider
-
- Direct Known Subclasses:
PluginWebProvider
public abstract class AbstractWebProvider extends Object
The Class provides necessary methods to handle a Web-Communication.
It can handle for example OAuth2-communicationWebDetails
.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
AbstractWebProvider.MethodTypes
-
Constructor Summary
Constructors Constructor Description AbstractWebProvider()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description WebDetails
createWebDetails(Properties config)
Reads the given properties and creates a webDetails object from it.
The following information will be read: ACCESS_TOKEN REFRESH_TOKEN GRANT_TYPE CLIENT_ID CLIENT_SECRET SCOPE AUTHENTICATION_SERVER_URI ACCESS_TOKEN_URI USERNAME PASSWORD RESOURCE_SERVER_URL
The value for accessTokenRequest will be set totrue
if the properties does<E> E
executeProtectedMethod(WebDetails webDetails, AbstractWebProvider.MethodTypes methodType, Object data, org.apache.http.client.ResponseHandler<E> responseHandler)
Required fields in the webDetails are:
WebDetails.getResourceServerUri()
int
executeProtectedMethod(WebDetails webDetails, AbstractWebProvider.MethodTypes methodType, WebConstants.ENTITY_TYPES entityType, Map<String,String> mapOfBodyPart)
Autorize to the given endpoint with the accessToken in the details and executes the method from the endpoint
and add the given data as json in the request.
Required fields in the webDetails are:
WebDetails.getResourceServerUri()
WebDetails.getAccessToken()
<E> E
executeProtectedMethod(WebDetails webDetails, AbstractWebProvider.MethodTypes methodType, WebConstants.ENTITY_TYPES entityType, Map<String,String> mapOfBodyPart, org.apache.http.client.ResponseHandler<E> responseHandler)
Autorize to the given endpoint with the accessToken in the details and executes the method from the endpoint
and add the given data as json in the request.
Required fields in the webDetails are:
WebDetails.getResourceServerUri()
WebDetails.getAccessToken()
String
getAccessToken(String authServerUri, Map<String,String> mapOfBodyPart, Map<String,String> mapOfHeaderPart)
Ask the endpoint in the details for an accessToken and send it back.
The bodyPart-Entity is by default "UrlEncodedFormEntity".
if you want to use another Entity-form then you have to usegetAccessToken(String, WebConstants.ENTITY_TYPES, Map, Map)
String
getAccessToken(String authServerUri, WebConstants.ENTITY_TYPES entityType, Map<String,String> mapOfBodyPart, Map<String,String> mapOfHeaderPart)
Ask the endpoint in the details for an accessToken and send it back.String
getAccessToken(WebDetails webDetails)
Ask the endpoint in the details for an accessToken and send it back.
SeeAlso:getAccessToken(WebDetails)
}protected String
getAuthorizationHeaderForAccessToken(String accessToken)
Returns the http-header for the given accesstoken value.protected String
getBasicAuthorizationHeader(String username, String password)
Return the http-header for the given username and password.Properties
getClientConfigProps(String path)
Load the given Properties from the path.Map<String,String>
getProtectedResource(WebDetails webDetails, AbstractWebProvider.MethodTypes methodType, Object data)
Autorize to the given endpoint with the accessToken in the details and executes the method from the endpoint
and add the given data as json in the request.
Required fields in the webDetails are:
WebDetails.getResourceServerUri()
WebDetails.getAccessToken()
Map<String,String>
getProtectedResource(WebDetails webDetails, AbstractWebProvider.MethodTypes methodType, WebConstants.ENTITY_TYPES entityType, Map<String,String> mapOfBodyPart)
Autorize to the given endpoint with the accessToken in the details and executes the method from the endpoint
and add the given data as json in the request.
Required fields in the webDetails are:
WebDetails.getResourceServerUri()
WebDetails.getAccessToken()
protected Map<String,String>
handleJsonResponse(org.apache.http.HttpResponse response)
Handles the content of the response as JSON and return a parsed Map.protected Map<String,String>
handleResponse(org.apache.http.HttpResponse response)
Handles the given response depends on the content-type.
supported types are: JSON
URL_Encoded XML
Otherwise there will throw an exception!protected Map<String,String>
handleURLEncodedResponse(org.apache.http.HttpResponse response)
Handles the content of the response as URL_Encoded and return a parsed Map.protected Map<String,String>
handleXMLResponse(org.apache.http.HttpResponse response)
Handles the content of the response as XML and return a parsed Map.protected void
parseXMLDoc(Element element, Document doc, Map<String,String> oauthResponse)
Parse the doc and fills the oauthResponse (key=tagname, value=content).
-
-
-
Method Detail
-
createWebDetails
public WebDetails createWebDetails(Properties config)
Reads the given properties and creates a webDetails object from it.
The following information will be read:- ACCESS_TOKEN
- REFRESH_TOKEN
- GRANT_TYPE
- CLIENT_ID
- CLIENT_SECRET
- SCOPE
- AUTHENTICATION_SERVER_URI
- ACCESS_TOKEN_URI
- USERNAME
- PASSWORD
- RESOURCE_SERVER_URL
The value for accessTokenRequest will be set totrue
if the properties does- Parameters:
config
-- Returns:
-
getClientConfigProps
public Properties getClientConfigProps(String path)
Load the given Properties from the path.- Parameters:
path
-- Returns:
-
executeProtectedMethod
public <E> E executeProtectedMethod(WebDetails webDetails, AbstractWebProvider.MethodTypes methodType, Object data, org.apache.http.client.ResponseHandler<E> responseHandler) throws WebException
Required fields in the webDetails are:
WebDetails.getResourceServerUri()
- Parameters:
webDetails
- = holds the request/response informations (can not be null)methodType
- = the kind of the method for a http-request (can not be null)responseHandler
- = the handler will handle the response and returns the serialized object- Throws:
WebException
-
executeProtectedMethod
public <E> E executeProtectedMethod(WebDetails webDetails, AbstractWebProvider.MethodTypes methodType, WebConstants.ENTITY_TYPES entityType, Map<String,String> mapOfBodyPart, org.apache.http.client.ResponseHandler<E> responseHandler) throws WebException, UnsupportedEncodingException
Autorize to the given endpoint with the accessToken in the details and executes the method from the endpoint
and add the given data as json in the request.
Required fields in the webDetails are:
WebDetails.getResourceServerUri()
WebDetails.getAccessToken()
- Parameters:
webDetails
- = holds the request/response informations (can not be null)methodType
- = the kind of the method for a http-request (can not be null)entityType
- = the entity-type can be JSON or URL_ENCODED (default) andy specifies the kind of how the body-part is inserted in the request (can be null)mapOfBodyPart
- = a map which contains the input for the http-body-part (can be null)- Throws:
WebException
UnsupportedEncodingException
-
executeProtectedMethod
public int executeProtectedMethod(WebDetails webDetails, AbstractWebProvider.MethodTypes methodType, WebConstants.ENTITY_TYPES entityType, Map<String,String> mapOfBodyPart) throws WebException, UnsupportedEncodingException
Autorize to the given endpoint with the accessToken in the details and executes the method from the endpoint
and add the given data as json in the request.
Required fields in the webDetails are:
WebDetails.getResourceServerUri()
WebDetails.getAccessToken()
- Parameters:
webDetails
- = holds the request/response informations (can not be null)methodType
- = the kind of the method for a http-request (can not be null)entityType
- = the entity-type can be JSON or URL_ENCODED (default) andy specifies the kind of how the body-part is inserted in the request (can be null)mapOfBodyPart
- = a map which contains the input for the http-body-part (can be null)- Returns:
- Returns the httpCode (200 == successful, -1 == No Result Code)
- Throws:
WebException
UnsupportedEncodingException
-
getProtectedResource
public Map<String,String> getProtectedResource(WebDetails webDetails, AbstractWebProvider.MethodTypes methodType, WebConstants.ENTITY_TYPES entityType, Map<String,String> mapOfBodyPart) throws WebException, UnsupportedEncodingException
Autorize to the given endpoint with the accessToken in the details and executes the method from the endpoint
and add the given data as json in the request.
Required fields in the webDetails are:
WebDetails.getResourceServerUri()
WebDetails.getAccessToken()
- Parameters:
webDetails
- = holds the request/response informations (can not be null)methodType
- = the kind of the method for a http-request (can not be null)entityType
- = the entity-type can be JSON or URL_ENCODED (default) andy specifies the kind of how the body-part is inserted in the request (can be null)mapOfBodyPart
- = a map which contains the input for the http-body-part (can be null)- Returns:
- Throws:
WebException
UnsupportedEncodingException
-
getProtectedResource
public Map<String,String> getProtectedResource(WebDetails webDetails, AbstractWebProvider.MethodTypes methodType, Object data) throws WebException
Autorize to the given endpoint with the accessToken in the details and executes the method from the endpoint
and add the given data as json in the request.
Required fields in the webDetails are:
WebDetails.getResourceServerUri()
WebDetails.getAccessToken()
- Parameters:
webDetails
- = holds the request/response informations (can not be null)methodType
- = the kind of the method for a http-request (can not be null)data
- = json data (only needed if the methodType value is an instanceof HttpEntityEnclosingRequestBase, which can contain StringEntity)
this value can benull
. (can be null)- Returns:
- Throws:
WebException
-
getAccessToken
public String getAccessToken(WebDetails webDetails) throws WebException
Ask the endpoint in the details for an accessToken and send it back.
SeeAlso:getAccessToken(WebDetails)
}- Parameters:
webDetails
- (can not be null)- Returns:
- Throws:
WebException
-
getAccessToken
public String getAccessToken(String authServerUri, Map<String,String> mapOfBodyPart, Map<String,String> mapOfHeaderPart) throws WebException
Ask the endpoint in the details for an accessToken and send it back.
The bodyPart-Entity is by default "UrlEncodedFormEntity".
if you want to use another Entity-form then you have to usegetAccessToken(String, WebConstants.ENTITY_TYPES, Map, Map)
- Parameters:
authServerUri
- = the authorization URI of the server we want to request for the access-token (can not be null)mapOfBodyPart
- = a map which contains the input for the http-body-part (can not be null)mapOfHeaderPart
- = a map which contains the input for the http-header (can be null)- Returns:
- Throws:
WebException
-
getAccessToken
public String getAccessToken(String authServerUri, WebConstants.ENTITY_TYPES entityType, Map<String,String> mapOfBodyPart, Map<String,String> mapOfHeaderPart) throws WebException
Ask the endpoint in the details for an accessToken and send it back.- Parameters:
authServerUri
- = the authorization URI of the server we want to request for the access-token (can not be null)entityType
- = the entity-type can be JSON or URL_ENCODED (default) andy specifies the kind of how the body-part is inserted in the requestmapOfBodyPart
- = a map which contains the input for the http-body-part (can not be null)mapOfHeaderPart
- = a map which contains the input for the http-header (can be null)- Returns:
- Throws:
WebException
-
handleResponse
protected Map<String,String> handleResponse(org.apache.http.HttpResponse response) throws WebException
Handles the given response depends on the content-type.
supported types are:- JSON
- URL_Encoded
- XML
Otherwise there will throw an exception!- Parameters:
response
-- Returns:
- Throws:
WebException
- JSON
-
handleJsonResponse
protected Map<String,String> handleJsonResponse(org.apache.http.HttpResponse response) throws WebException
Handles the content of the response as JSON and return a parsed Map.- Parameters:
response
-- Returns:
- Throws:
WebException
-
handleURLEncodedResponse
protected Map<String,String> handleURLEncodedResponse(org.apache.http.HttpResponse response) throws WebException
Handles the content of the response as URL_Encoded and return a parsed Map.- Parameters:
response
-- Returns:
- Throws:
WebException
-
handleXMLResponse
protected Map<String,String> handleXMLResponse(org.apache.http.HttpResponse response) throws WebException
Handles the content of the response as XML and return a parsed Map.- Parameters:
response
-- Returns:
- Throws:
WebException
-
parseXMLDoc
protected void parseXMLDoc(Element element, Document doc, Map<String,String> oauthResponse)
Parse the doc and fills the oauthResponse (key=tagname, value=content).- Parameters:
element
-doc
-oauthResponse
-
-
getAuthorizationHeaderForAccessToken
protected String getAuthorizationHeaderForAccessToken(String accessToken)
Returns the http-header for the given accesstoken value.- Parameters:
accessToken
-- Returns:
-
-