public class GrouperHttpClient extends Object
HTTP call. Use this for all HTTP calls as a client
import edu.internet2.middleware.grouper.util.*; GrouperHttpClient grouperHttpCall = new GrouperHttpClient(); grouperHttpCall.assignUrl(url); grouperHttpCall.assignGrouperHttpMethod("POST"); grouperHttpCall.addHeader("Content-Type", "application/json"); grouperHttpCall.addHeader("Authorization", "Bearer " + bearerToken); grouperHttpCall.assignBody(body); grouperHttpCall.executeRequest(); if (grouperHttpCall.getResponseCode() != 200) { throw new RuntimeException("Error connecting to '" + url + "': " + grouperHttpCall.getResponseCode()); } String json = grouperHttpCall.getResponseBody();
Constructor and Description |
---|
GrouperHttpClient() |
Modifier and Type | Method and Description |
---|---|
GrouperHttpClient |
addBodyParameter(String key,
String value)
Add a parameter to the BODY for a POST body form.
|
GrouperHttpClient |
addFileToSend(String filename,
File file) |
GrouperHttpClient |
addHeader(String key,
String value)
Adds a parameter to the header.
|
GrouperHttpClient |
addUrlParameter(String key,
String value)
Add a parameter to the URL.
|
GrouperHttpClient |
assignBody(String _body)
Sets the body to send.
|
GrouperHttpClient |
assignBodyBytes(byte[] _body)
Sets the body to send.
|
GrouperHttpClient |
assignGrouperHttpMethod(GrouperHttpMethod grouperHttpMethod) |
GrouperHttpClient |
assignGrouperHttpMethod(String grouperHttpMethodType) |
GrouperHttpClient |
assignHttpMultipartMode(org.apache.http.entity.mime.HttpMultipartMode _httpMultipartMode)
Use to override HttpMultipartMode.BROWSER_COMPATIBLE which is the default.
|
GrouperHttpClient |
assignHttpResponseBodyCallback(GrouperHttpResponseBodyCallback httpResponseBodyCallback) |
GrouperHttpClient |
assignPassword(String _password)
Sets the password for basic auth.
|
GrouperHttpClient |
assignResponseAsFile(boolean _responseAsFile)
Whether you want the response as a file as opposed to a string in memory.
|
GrouperHttpClient |
assignResponseCode(int _responseCode)
The response code of the call.
|
GrouperHttpClient |
assignResponseFile(File _responseFile)
If getting the reponse body as a file, this is the file.
|
GrouperHttpClient |
assignResponseFileName(String _responseFileName)
The filename to use for the response, if the response is retrieved as a file.
|
GrouperHttpClient |
assignResponseHeaders(Map<String,String> _responseHeaders)
Any headers sent back in the resopnse.
|
GrouperHttpClient |
assignTrust(boolean _trust)
Trust regardless of cert; ONLY use when you KNOW the endpoint.
|
GrouperHttpClient |
assignTrustStore(File _trustStore)
Truststore (.jks) to add dynamically to list of truststores.
|
GrouperHttpClient |
assignTrustStorePassword(String _trustStoreKey)
Password for truststore.
|
GrouperHttpClient |
assignUrl(String _url)
Sets the url being called.
|
GrouperHttpClient |
assignUser(String _user)
Sets the user for basic auth.
|
static String |
basicAuthenticationString(String login,
String password)
Create a basic authentication string.
|
void |
executeRequest()
Execute a post with the given parameters, set teh code and the response into the call.
|
String |
getResponseBody()
Get the response body of the call.
|
int |
getResponseCode()
Get the response code.
|
File |
getResponseFile()
If getting the reponse body as a file, this is the file.
|
String |
getResponseFileName()
The filename to use for the response, if the response is retrieved as a file.
|
Map<String,String> |
getResponseHeaders()
Any headers sent back in the resopnse.
|
static GrouperHttpClientLog |
logCurrent()
get the current log
log start
|
static String |
logEnd()
stop a debug log in a finally block
|
static void |
logStart(GrouperHttpClientLog grouperHttpCallLog)
start a static debug log
log start
|
static String |
responseBodyAsString(org.apache.http.client.methods.CloseableHttpResponse closeableHttpResponse)
Get the body of the response as a string.
|
static String |
responseBodyAsString(org.apache.commons.httpclient.HttpMethod httpMethod)
Get the body of the response as a string.
|
public GrouperHttpClient assignGrouperHttpMethod(GrouperHttpMethod grouperHttpMethod)
public GrouperHttpClient assignGrouperHttpMethod(String grouperHttpMethodType)
public GrouperHttpClient assignUrl(String _url)
_url
- the new url being calledpublic GrouperHttpClient assignUser(String _user)
_user
- the new user for basic authpublic GrouperHttpClient assignPassword(String _password)
_password
- the new password for basic authpublic GrouperHttpClient addBodyParameter(String key, String value)
key
- is the name.value
- is the value.public GrouperHttpClient addUrlParameter(String key, String value)
key
- is the name.value
- is the value.public GrouperHttpClient addHeader(String key, String value)
key
- is the name.value
- is the value.public GrouperHttpClient assignBodyBytes(byte[] _body)
_body
- the body to sendpublic GrouperHttpClient assignBody(String _body)
_body
- the body to sendpublic String getResponseBody()
public int getResponseCode()
public GrouperHttpClient assignResponseCode(int _responseCode)
_responseCode
- the responseCode to setpublic GrouperHttpClient assignTrustStore(File _trustStore)
_trustStore
- the trustStore to set.public GrouperHttpClient assignTrustStorePassword(String _trustStoreKey)
_trustStoreKey
- the trustStoreKey to set.public GrouperHttpClient assignHttpResponseBodyCallback(GrouperHttpResponseBodyCallback httpResponseBodyCallback)
public GrouperHttpClient addFileToSend(String filename, File file)
filename
- The name of the file to send.file
- The file to send.public Map<String,String> getResponseHeaders()
public GrouperHttpClient assignResponseHeaders(Map<String,String> _responseHeaders)
_responseHeaders
- the responseHeaders to setpublic GrouperHttpClient assignHttpMultipartMode(org.apache.http.entity.mime.HttpMultipartMode _httpMultipartMode)
_httpMultipartMode
- the httpMultipartMode to setpublic GrouperHttpClient assignResponseAsFile(boolean _responseAsFile)
_responseAsFile
- the responseAsFile to set,public File getResponseFile()
public GrouperHttpClient assignResponseFile(File _responseFile)
_responseFile
- the responseFile to set.public String getResponseFileName()
public GrouperHttpClient assignResponseFileName(String _responseFileName)
_responseFileName
- the responseFileName to setpublic GrouperHttpClient assignTrust(boolean _trust)
_trust
- the trust to setpublic static String responseBodyAsString(org.apache.commons.httpclient.HttpMethod httpMethod)
httpMethod
- is the method.public static String responseBodyAsString(org.apache.http.client.methods.CloseableHttpResponse closeableHttpResponse)
closeableHttpResponse
- is the response.public static String basicAuthenticationString(String login, String password)
login
- is the login.password
- is the password.public void executeRequest()
Execute a post with the given parameters, set teh code and the response into the call.
grouperHttpCall
- is the configuration object.public static GrouperHttpClientLog logCurrent()
public static String logEnd()
public static void logStart(GrouperHttpClientLog grouperHttpCallLog)
Copyright © 2016 Internet2. All rights reserved.