com.atlassian.sal.core.net
Class HttpClientRequest

java.lang.Object
  extended by com.atlassian.sal.core.net.HttpClientRequest
All Implemented Interfaces:
Request<HttpClientRequest,HttpClientResponse>

public class HttpClientRequest
extends Object
implements Request<HttpClientRequest,HttpClientResponse>

HttpClient implementation of Request interface


Nested Class Summary
 
Nested classes/interfaces inherited from interface com.atlassian.sal.api.net.Request
Request.MethodType
 
Field Summary
static int MAX_REDIRECTS
          The default number of redirects that will be followed.
 
Constructor Summary
HttpClientRequest(org.apache.commons.httpclient.HttpClient httpClient, Request.MethodType methodType, String url, CertificateFactory certificateFactory, UserManager userManager)
           
 
Method Summary
 HttpClientRequest addAuthentication(Authenticator authenticator)
          Adds generic Authenticator to the request.
 HttpClientRequest addBasicAuthentication(String username, String password)
          Adds basic authentication to the request.
 HttpClientRequest addHeader(String headerName, String headerValue)
          Adds the specified header to the request, not overwriting any previous value.
 HttpClientRequest addHeaders(String... params)
           
 HttpClientRequest addRequestParameters(String... params)
          Sets parameters of the request.
 HttpClientRequest addSeraphAuthentication(String username, String password)
          Adds seraph authentication to the request.
 HttpClientRequest addTrustedTokenAuthentication()
          Adds TrustedTokenAuthentication to the request.
 HttpClientRequest addTrustedTokenAuthentication(String username)
          Adds TrustedTokenAuthentication to the request.
protected  void configureProtocol(String url)
          Configures the protocol for the underlying HttpClient.
protected  void configureProxy()
          Configures the proxy for the underlying HttpClient.
 String execute()
          Executes a request and if response is successful, returns response as a string.
 void execute(ResponseHandler<HttpClientResponse> responseHandler)
          Executes the request.
<E> E
executeAndReturn(ReturningResponseHandler<HttpClientResponse,E> httpClientResponseResponseHandler)
          Executes the request and returns a result value.
protected  com.google.common.collect.ImmutableList<HttpClientAuthenticator> getAuthenticators()
           
 Map<String,List<String>> getHeaders()
           
 int getMaxRedirects()
          Get the maximum number of redirects allowed by this instance.
 Request.MethodType getMethodType()
           
protected  org.apache.commons.httpclient.HttpMethod makeMethod()
           
 HttpClientRequest setConnectionTimeout(int connectionTimeout)
          Setting connection timeout in milliseconds.
 HttpClientRequest setEntity(Object entity)
          Set an entity as the request body
 HttpClientRequest setFiles(List<RequestFilePart> files)
          Sets file parts of the request.
 HttpClientRequest setFollowRedirects(boolean follow)
          Sets whether the request should transparently follow a redirect from the server.
 HttpClientRequest setHeader(String headerName, String headerValue)
          Sets the specified header to the request, overwriting any previous value.
 HttpClientRequest setRequestBody(String requestBody)
          Sets the body of the request.
 HttpClientRequest setRequestContentType(String requestContentType)
          Sets Content-Type of the body of the request.
 HttpClientRequest setSoTimeout(int soTimeout)
          Setting socket timeout in milliseconds.
 HttpClientRequest setUrl(String url)
           
static void shutdownStream(InputStream input)
          Unconditionally close an InputStream.
 String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

MAX_REDIRECTS

public static final int MAX_REDIRECTS
The default number of redirects that will be followed. Can be overridden using the system property "http.protocol.max-redirects=10" etc.

See Also:
Raised the default in 2.10.1 to 20 as currently JIRA home page does 4 redirects. NB prior to v2.10.1 one "redirect" was used up handling a successful request., Constant Field Values
Constructor Detail

HttpClientRequest

public HttpClientRequest(org.apache.commons.httpclient.HttpClient httpClient,
                         Request.MethodType methodType,
                         String url,
                         CertificateFactory certificateFactory,
                         UserManager userManager)
Method Detail

setUrl

public HttpClientRequest setUrl(String url)
Specified by:
setUrl in interface Request<HttpClientRequest,HttpClientResponse>
Parameters:
url - the url to request
Returns:
a reference to this object.

addAuthentication

public HttpClientRequest addAuthentication(Authenticator authenticator)
Description copied from interface: Request
Adds generic Authenticator to the request.

Specified by:
addAuthentication in interface Request<HttpClientRequest,HttpClientResponse>
Parameters:
authenticator - the authenticator to use for authentication
Returns:
a reference to this object.

addTrustedTokenAuthentication

public HttpClientRequest addTrustedTokenAuthentication()
Description copied from interface: Request
Adds TrustedTokenAuthentication to the request. Trusted token authenticator uses current user to make a trusted application call.

Specified by:
addTrustedTokenAuthentication in interface Request<HttpClientRequest,HttpClientResponse>
Returns:
a reference to this object.

addTrustedTokenAuthentication

public HttpClientRequest addTrustedTokenAuthentication(String username)
Description copied from interface: Request
Adds TrustedTokenAuthentication to the request. Trusted token authenticator uses the passed user to make a trusted application call.

Specified by:
addTrustedTokenAuthentication in interface Request<HttpClientRequest,HttpClientResponse>
Parameters:
username - The user to make the request with
Returns:
this

addBasicAuthentication

public HttpClientRequest addBasicAuthentication(String username,
                                                String password)
Description copied from interface: Request
Adds basic authentication to the request.

Specified by:
addBasicAuthentication in interface Request<HttpClientRequest,HttpClientResponse>
Parameters:
username - The user name
password - The password
Returns:
a reference to this object.

addSeraphAuthentication

public HttpClientRequest addSeraphAuthentication(String username,
                                                 String password)
Description copied from interface: Request
Adds seraph authentication to the request.

Specified by:
addSeraphAuthentication in interface Request<HttpClientRequest,HttpClientResponse>
Parameters:
username - The user name
password - The password
Returns:
a reference to this object.

getAuthenticators

protected com.google.common.collect.ImmutableList<HttpClientAuthenticator> getAuthenticators()

setConnectionTimeout

public HttpClientRequest setConnectionTimeout(int connectionTimeout)
Description copied from interface: Request
Setting connection timeout in milliseconds.

Specified by:
setConnectionTimeout in interface Request<HttpClientRequest,HttpClientResponse>
Parameters:
connectionTimeout - The timeout in milliseconds
Returns:
a reference to this object.

setSoTimeout

public HttpClientRequest setSoTimeout(int soTimeout)
Description copied from interface: Request
Setting socket timeout in milliseconds.

Specified by:
setSoTimeout in interface Request<HttpClientRequest,HttpClientResponse>
Parameters:
soTimeout - the timeout in milliseconds
Returns:
a reference to this object.

setRequestBody

public HttpClientRequest setRequestBody(String requestBody)
Description copied from interface: Request
Sets the body of the request. In default implementation only requests of type Request.MethodType.POST and Request.MethodType.PUT can have a request body.

Specified by:
setRequestBody in interface Request<HttpClientRequest,HttpClientResponse>
Parameters:
requestBody - the body of the request
Returns:
a reference to this object.

setFiles

public HttpClientRequest setFiles(List<RequestFilePart> files)
Description copied from interface: Request
Sets file parts of the request. File parts can only be added if the request body is empty. Only requests of type Request.MethodType.POST and Request.MethodType.PUT can have file parts.

Specified by:
setFiles in interface Request<HttpClientRequest,HttpClientResponse>
Parameters:
files - the file parts, cannot be null.
Returns:
a reference to this object.

setEntity

public HttpClientRequest setEntity(Object entity)
Description copied from interface: Request
Set an entity as the request body

Specified by:
setEntity in interface Request<HttpClientRequest,HttpClientResponse>
Parameters:
entity - the request entity to be marshalled, not null
Returns:
this Request object

setRequestContentType

public HttpClientRequest setRequestContentType(String requestContentType)
Description copied from interface: Request
Sets Content-Type of the body of the request. In default implementation only requests of type Request.MethodType.POST and Request.MethodType.PUT can have a request body.

Specified by:
setRequestContentType in interface Request<HttpClientRequest,HttpClientResponse>
Parameters:
requestContentType - the contentType of the request
Returns:
a reference to this object.

addRequestParameters

public HttpClientRequest addRequestParameters(String... params)
Description copied from interface: Request
Sets parameters of the request. In default implementation only requests of type Request.MethodType.POST can have parameters. For other requests include parameters in url. This method accepts an even number of arguments, in form of name, value, name, value, name, value, etc

Specified by:
addRequestParameters in interface Request<HttpClientRequest,HttpClientResponse>
Parameters:
params - parameters of the request in as name, value pairs
Returns:
a reference to this object.

addHeader

public HttpClientRequest addHeader(String headerName,
                                   String headerValue)
Description copied from interface: Request
Adds the specified header to the request, not overwriting any previous value. Support for this operation is optional.

Specified by:
addHeader in interface Request<HttpClientRequest,HttpClientResponse>
Parameters:
headerName - the header's name
headerValue - the header's value
Returns:
a reference to this object
See Also:
RequestFactory.supportsHeader()

setHeader

public HttpClientRequest setHeader(String headerName,
                                   String headerValue)
Description copied from interface: Request
Sets the specified header to the request, overwriting any previous value. Support for this operation is optional.

Specified by:
setHeader in interface Request<HttpClientRequest,HttpClientResponse>
Parameters:
headerName - the header's name
headerValue - the header's value
Returns:
a reference to this object
See Also:
RequestFactory.supportsHeader()

setFollowRedirects

public HttpClientRequest setFollowRedirects(boolean follow)
Description copied from interface: Request
Sets whether the request should transparently follow a redirect from the server. The default behavior is that when a response is received with a status code in the 300s, a new request is made using the location header of the response without notifying the client. Set this to false to turn this behavior off.

Specified by:
setFollowRedirects in interface Request<HttpClientRequest,HttpClientResponse>
Parameters:
follow - set this to false to have the request not transparently follow redirects.
Returns:
a reference to this object.

addHeaders

public HttpClientRequest addHeaders(String... params)

executeAndReturn

public <E> E executeAndReturn(ReturningResponseHandler<HttpClientResponse,E> httpClientResponseResponseHandler)
                   throws ResponseException
Description copied from interface: Request
Executes the request and returns a result value.

Specified by:
executeAndReturn in interface Request<HttpClientRequest,HttpClientResponse>
Parameters:
httpClientResponseResponseHandler - Callback handler of the response.
Throws:
ResponseProtocolException - If the server returned a malformed response
ResponseTimeoutException - If a connection timeout or read timeout occurred
ResponseTransportException - If an I/O error occurred in request transport
ResponseException - For all errors not otherwise specified

execute

public void execute(ResponseHandler<HttpClientResponse> responseHandler)
             throws ResponseException
Description copied from interface: Request
Executes the request.

Specified by:
execute in interface Request<HttpClientRequest,HttpClientResponse>
Parameters:
responseHandler - Callback handler of the response.
Throws:
ResponseProtocolException - If the server returned a malformed response
ResponseTimeoutException - If a connection timeout or read timeout occurred
ResponseTransportException - If an I/O error occurred in request transport
ResponseException - For all errors not otherwise specified

shutdownStream

public static void shutdownStream(InputStream input)
Unconditionally close an InputStream. Equivalent to InputStream.close(), except any exceptions will be ignored.

Parameters:
input - A (possibly null) InputStream

execute

public String execute()
               throws ResponseException
Description copied from interface: Request
Executes a request and if response is successful, returns response as a string. @see Response.getResponseBodyAsString()

Specified by:
execute in interface Request<HttpClientRequest,HttpClientResponse>
Returns:
response as String
Throws:
ResponseStatusException - If the server returned a response that contained an error code
ResponseProtocolException - If the server returned a malformed response
ResponseTimeoutException - If a connection timeout or read timeout occurred
ResponseTransportException - If an I/O error occurred in request transport
ResponseException - For all errors not otherwise specified

makeMethod

protected org.apache.commons.httpclient.HttpMethod makeMethod()

configureProxy

protected void configureProxy()
Configures the proxy for the underlying HttpClient.


configureProtocol

protected void configureProtocol(String url)
Configures the protocol for the underlying HttpClient.


getMaxRedirects

public int getMaxRedirects()
Get the maximum number of redirects allowed by this instance.


getHeaders

public Map<String,List<String>> getHeaders()
Specified by:
getHeaders in interface Request<HttpClientRequest,HttpClientResponse>
Returns:
an immutable Map of headers added to the request so far

getMethodType

public Request.MethodType getMethodType()

toString

public String toString()
Overrides:
toString in class Object


Copyright © 2015 Atlassian. All rights reserved.