Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
jerry-http - Common Java Functionality Copyright (c) 2012-2014, Sandeep Gupta http://sangupta.com/projects/jerry-http Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 
 
 package com.sangupta.jerry.http;
 
 import java.io.File;
 import java.net.URI;
 import java.util.Date;
 
 
A wrapper that provides a builder based way to constructing a web request, executing it via the default HttpExecutor and then returning the raw WebRawResponse.

Author(s):
sangupta
Since:
0.3
 
 public class WebRequest {

The date format to use
 
 	public static final String DATE_FORMAT = "EEE, dd MMM yyyy HH:mm:ss zzz";
    
The date local for US timezone
 
 	public static final Locale DATE_LOCALE = .;
    
The GMT timezone
 
 	public static final TimeZone TIME_ZONE = TimeZone.getTimeZone("GMT");

Create a HTTP GET based WebRequest for the given java.net.URI

Parameters:
uri the java.net.URI for which to create web request
Returns:
the WebRequest object thus created
    public static WebRequest get(final URI uri) {
        return new WebRequest(new HttpGet(uri));
    }

    
Create a HTTP GET based WebRequest for the given string uri

Parameters:
uri the string uri for which to create web request
Returns:
the WebRequest object thus created
    public static WebRequest get(final String uri) {
        return new WebRequest(new HttpGet(uri));
    }

    
Create a HTTP HEAD based WebRequest for the given java.net.URI

Parameters:
uri the java.net.URI for which to create web request
Returns:
the WebRequest object thus created
    public static WebRequest head(final URI uri) {
        return new WebRequest(new HttpHead(uri));
    }

    
Create a HTTP HEAD based WebRequest for the given string uri.

Parameters:
uri the string uri for which to create web request
Returns:
the WebRequest object thus created
    public static WebRequest head(final String uri) {
        return new WebRequest(new HttpHead(uri));
    }

    
Create a HTTP POST based WebRequest for the given java.net.URI

Parameters:
uri the java.net.URI for which to create web request
Returns:
the WebRequest object thus created
    public static WebRequest post(final URI uri) {
        return new WebRequest(new HttpPost(uri));
    }

    
Create a HTTP POST based WebRequest for the given string uri.

Parameters:
uri the string uri for which to create web request
Returns:
the WebRequest object thus created
    public static WebRequest post(final String uri) {
        return new WebRequest(new HttpPost(uri));
    }

    
Create a HTTP PUT based WebRequest for the given java.net.URI

Parameters:
uri the java.net.URI for which to create web request
Returns:
the WebRequest object thus created
    public static WebRequest put(final URI uri) {
        return new WebRequest(new HttpPut(uri));
    }

    
Create a HTTP PUT based WebRequest for the given string URI.

Parameters:
uri the string uri for which to create web request
Returns:
the WebRequest object thus created
    public static WebRequest put(final String uri) {
        return new WebRequest(new HttpPut(uri));
    }

    
Create a HTTP TRACE based WebRequest for the given java.net.URI

Parameters:
uri the java.net.URI for which to create web request
Returns:
the WebRequest object thus created
    public static WebRequest trace(final URI uri) {
        return new WebRequest(new HttpTrace(uri));
    }

    
Create a HTTP TRACE based WebRequest for the given string URI

Parameters:
uri the string URI for which to create web request
Returns:
the WebRequest object thus created
    public static WebRequest trace(final String uri) {
        return new WebRequest(new HttpTrace(uri));
    }

    
Create a HTTP DELETE based WebRequest for the given java.net.URI

Parameters:
uri the java.net.URI for which to create web request
Returns:
the WebRequest object thus created
    public static WebRequest delete(final URI uri) {
        return new WebRequest(new HttpDelete(uri));
    }

    
Create a HTTP DELETE based WebRequest for the given string URI

Parameters:
uri the string URI for which to create web request
Returns:
the WebRequest object thus created
    public static WebRequest delete(final String uri) {
        return new WebRequest(new HttpDelete(uri));
    }

    
Create a HTTP OPTIONS based WebRequest for the given java.net.URI

Parameters:
uri the java.net.URI for which to create web request
Returns:
the WebRequest object thus created
    public static WebRequest options(final URI uri) {
        return new WebRequest(new HttpOptions(uri));
    }

    
Create a HTTP OPTIONS based WebRequest for the given string URI.

Parameters:
uri the string URI for which to create web request
Returns:
the WebRequest object thus created
    public static WebRequest options(final String uri) {
        return new WebRequest(new HttpOptions(uri));
    }
    
    // Instance methods start here
    
    
    private final HttpRequestBase request;

    
    private final HttpParams localParams;

    
The associated java.text.DateFormat formatter
    private SimpleDateFormat dateFormatter;

    
Create the WebRequest object using the given org.apache.http.client.methods.HttpRequestBase object.

Parameters:
request the base http request
    WebRequest(final HttpRequestBase request) {
        super();
        this. = request;
        this. = request.getParams();
    }

    
Get the underlying org.apache.http.client.methods.HttpRequestBase object.

Returns:
the base http request
        return this.;
    }
    
    
Display the debug information for this request

Returns:
this very WebRequest
    public String trace() {
    	StringBuilder builder = new StringBuilder();
    	
    	builder.append(this..getRequestLine());
    	
    	Header[] headers = this..getAllHeaders();
    	if(headers != null) {
    		for(Header header : headers) {
    			builder.append(header.toString());
    		}
    	}
    	
    	if (this. instanceof HttpEntityEnclosingRequest) {
    		HttpEntity entity = hecr.getEntity();
    		if(entity != null) {
    			if(entity.getContentType() != null) {
    				builder.append("Content-Type: ");
    				builder.append(entity.getContentType().toString());
    			}
    			
    			if(entity.getContentEncoding() != null) {
    				builder.append(entity.getContentEncoding().toString());
    			}
    			
    			builder.append("Content-Length: ");
    			builder.append(entity.getContentLength());
    		}
    	}
    	
    	return builder.toString();
    }
    
    
Return the HTTP VERB associated with this request.

Returns:
string representation of HTTP verb such as GET, POST in uppercase
    public String getVerb() {
    	return this..getMethod().toUpperCase();
    }
    
    
Return the URI associated with this request.

Returns:
the java.net.URI for this request.
    public URI getURI() {
    	return this..getURI();
    }
    
    
Return the type of request that we encapsulate.

Returns:
the WebRequestMethod identifying request type.
    	if(this. instanceof HttpGet) {
    		return .;
    	}
    	
    	if(this. instanceof HttpPost) {
    		return .;
    	}
    	
    	if(this. instanceof HttpPut) {
    		return .;
    	}
    	
    	if(this. instanceof HttpDelete) {
    		return .;
    	}
    	
    	if(this. instanceof HttpHead) {
    		return .;
    	}
    	
    	if(this. instanceof HttpTrace) {
    		return .;
    	}
    	
    	if(this. instanceof HttpOptions) {
    		return .;
    	}
    	
    	throw new IllegalStateException("Unknown request type");
    }

    
Execute this web request now.

Returns:
the obtained WebRawResponse.
Throws:
org.apache.http.client.ClientProtocolException if client protocol fails
java.io.IOException if something else fails
        return ..execute(this);
    }

    
Abort this request now.

    public void abort() throws UnsupportedOperationException {
        this..abort();
    }
    //// HTTP header operations

    
Add the given header to the request

Parameters:
header the org.apache.http.Header object to use
Returns:
this very WebRequest
    public WebRequest addHeader(final Header header) {
        this..addHeader(header);
        return this;
    }

    
Add a new header with the given name and value

Parameters:
name the header name
value the header value
Returns:
this very WebRequest
    public WebRequest addHeader(final String namefinal String value) {
        this..addHeader(namevalue);
        return this;
    }

    
Remove the given header

Parameters:
header the header name to remove
Returns:
this very WebRequest
    public WebRequest removeHeader(final Header header) {
        this..removeHeader(header);
        return this;
    }

    
Remove all headers with the given name.

Parameters:
name the header name to remove
Returns:
this very WebRequest
    public WebRequest removeHeaders(final String name) {
        this..removeHeaders(name);
        return this;
    }

    
Set the request headers to the given list of headers.

Parameters:
headers headers to set
Returns:
this very WebRequest
    public WebRequest setHeaders(final Header[] headers) {
        this..setHeaders(headers);
        return this;
    }

    
Set cache-control as the given one.

Parameters:
cacheControl the value to set
Returns:
this very WebRequest
    public WebRequest setCacheControl(String cacheControl) {
        this..setHeader(.cacheControl);
        return this;
    }

    
The associated java.text.DateFormat instance. If nothing is set, returns java.text.SimpleDateFormat for DATE_FORMAT in DATE_LOCALE and TIME_ZONE.

Returns:
the date format
    private DateFormat getDateFormat() {
        if (this. == null) {
            this. = new SimpleDateFormat();
            this..setTimeZone();
        }
        return this.;
    }

    
Set the request date header to the given date value.

Parameters:
date the java.util.Date value for header
Returns:
this very WebRequest
    public WebRequest setDate(final Date date) {
        this..setHeader(.getDateFormat().format(date));
        return this;
    }

    
Set the If-Modified-Since header to the given date value.

Parameters:
date the java.util.Date value for header
Returns:
this very WebRequest
    public WebRequest setIfModifiedSince(final Date date) {
        return this;
    }

    
Set the If-Unmodified-Since header to the given date value

Parameters:
date the java.util.Date value for header
Returns:
this very WebRequest
    public WebRequest setIfUnmodifiedSince(final Date date) {
        return this;
    }
    //// HTTP config parameter operations

    
Set the local param for this request to the value.

Parameters:
param the param name
object the value of the param
Returns:
this very WebRequest
    public WebRequest config(final String paramfinal Object object) {
        this..setParameter(paramobject);
        return this;
    }

    
Remove the local config param from this request with the given name

Parameters:
param the param name to remove
Returns:
this very WebRequest
    public WebRequest removeConfig(final String param) {
        this..removeParameter(param);
        return this;
    }
    //// HTTP protocol parameter operations

    
Set the request HTTP Protocol version

Parameters:
version org.apache.http.HttpVersion to use
Returns:
this very WebRequest
    public WebRequest version(final HttpVersion version) {
        return config(.version);
    }

    
Set the java.nio.charset.Charset for the given request

Parameters:
charset the java.nio.charset.Charset to use
Returns:
this very WebRequest
    public WebRequest elementCharset(final String charset) {
        return config(.charset);
    }

    
Set the request to use expect continue.

Returns:
this very WebRequest
    public WebRequest useExpectContinue() {
        return config(.true);
    }

    
Set the request User-Agent string to the given one.

Parameters:
agent the user-agent string
Returns:
this very WebRequest
    public WebRequest userAgent(final String agent) {
        return config(.agent);
    }
    //// HTTP connection parameter operations
    
    
Follow redirects for the request

Returns:
this very WebRequest
    public WebRequest followRedirects() {
    	HttpParams params = this..getParams();
    	return this;
    }

    
Do not follow any redirects

Returns:
this very WebRequest
    public WebRequest noRedirects() {
    	HttpParams params = this..getParams();
    	return this;
    }
    
    
Specify the socket time out to the given value.

Parameters:
timeout the timeout value
Returns:
this very WebRequest
    public WebRequest socketTimeout(int timeout) {
        return config(.timeout);
    }
    
    
Specify the connection time out to the given value.

Parameters:
timeout the timeout value
Returns:
this very WebRequest
    public WebRequest connectTimeout(int timeout) {
        return config(.timeout);
    }

    
Specifies if stale connection check needs to be performed before making a connection.

Parameters:
perform trueif stale connection checks needs to be performed, false otherwise
Returns:
this very WebRequest
    public WebRequest staleConnectionCheck(boolean perform) {
        return config(.perform);
    }
    //// HTTP connection route operations

    
Set the proxy via given http host.

Parameters:
proxy the org.apache.http.HttpHost to use for proxy
Returns:
this very WebRequest
    public WebRequest viaProxy(final HttpHost proxy) {
        return config(.proxy);
    }
    //// HTTP entity operations

    
Set the body from given org.apache.http.HttpEntity.

Parameters:
entity the org.apache.http.HttpEntity to set body from
Returns:
this very WebRequest
    public WebRequest body(final HttpEntity entity) {
        if (this. instanceof HttpEntityEnclosingRequest) {
            ((HttpEntityEnclosingRequestthis.).setEntity(entity);
        } else {
            throw new IllegalStateException(this..getMethod() + " request cannot enclose an entity");
        }
        
        return this;
    }

    
Set the body using FORM variables in given java.nio.charset.Charset

Parameters:
formParams the form params
charset the java.nio.charset.Charset for params
Returns:
this very WebRequest
    public WebRequest bodyForm(final Iterable <? extends NameValuePairformParamsfinal Charset charset) {
        return body(new UrlEncodedFormEntity(formParamscharset));
    }

    
Set the body using FORM variables

Parameters:
formParams the form params
Returns:
this very WebRequest
    public WebRequest bodyForm(final Iterable <? extends NameValuePairformParams) {
        return bodyForm(formParams.);
    }

    
Set the body using FORM variables

Parameters:
formParams the form parameters
Returns:
this very WebRequest
    public WebRequest bodyForm(final NameValuePair... formParams) {
        return bodyForm(Arrays.asList(formParams), .);
    }

    
Set the body from the string for given content type.

Parameters:
string the string to set body from
contentType the org.apache.http.entity.ContentType of the string
Returns:
this very WebRequest
    public WebRequest bodyString(final String stringfinal ContentType contentType) {
        return body(new StringEntity(stringcontentType));
    }
    
    
Set the body from the string for given java.nio.charset.Charset.

Parameters:
string the string to set body from
charset the java.nio.charset.Charset encoding for the string
Returns:
this very WebRequest
    public WebRequest bodyString(final String stringfinal Charset charset) {
    	return body(new StringEntity(stringcharset));
    }
    
    
Set the body from the string for given mime-type.

Parameters:
string the string to set body from
mimeType the MIME type of the string
Returns:
this very WebRequest
    public WebRequest bodyString(final String stringfinal String mimeType) {
    	ContentType contentType = ContentType.create(mimeType);
    	return body(new StringEntity(stringcontentType));
    }

    
Set the body from the string for given MIME type and java.nio.charset.Charset

Parameters:
string the string to set body from
mimeType the MIME type of the string
charset the java.nio.charset.Charset encoding for string
Returns:
this very WebRequest
    public WebRequest bodyString(final String stringfinal String mimeTypefinal String charset) {
    	ContentType contentType = ContentType.create(mimeTypecharset);
    	return body(new StringEntity(stringcontentType));
    }
    
    
Set the body from given file for the given content type.

Parameters:
file the java.io.File to read from
contentType the org.apache.http.entity.ContentType of the file
Returns:
this very WebRequest
    public WebRequest bodyFile(final File filefinal ContentType contentType) {
        return body(new FileEntity(filecontentType));
    }

    
Set the body from given byte array.

Parameters:
bytes the byte array
Returns:
this very WebRequest
    public WebRequest bodyByteArray(final byte[] bytes) {
        return body(new ByteArrayEntity(bytes));
    }

    
Set the body from given byte array.

Parameters:
bytes the byte array
offset the offset to read from
length the number of bytes to read
Returns:
this very WebRequest
    public WebRequest bodyByteArray(final byte[] bytesint offsetint length) {
        return body(new ByteArrayEntity(bytesoffsetlength));
    }

    
Set the body stream from given input stream.

Parameters:
instream the java.io.InputStream to read from
Returns:
this very WebRequest
    public WebRequest bodyStream(final InputStream instream) {
        return body(new InputStreamEntity(instream, -1));
    }

    
Set the body stream from given input stream of given content type.

Parameters:
instream the java.io.InputStream from which to read
contentType the org.apache.http.entity.ContentType of the stream
Returns:
this very WebRequest
    public WebRequest bodyStream(final InputStream instreamfinal ContentType contentType) {
        return body(new InputStreamEntity(instream, -1, contentType));
    }

Change the cookie policy to given cookie policy name

Parameters:
cookiePolicy the cookie policy name
Returns:
this very WebRequest
	public WebRequest cookiePolicy(String cookiePolicy) {
		return this;
	}

    
Convert this request to java.lang.String format. This is basically a representation of the request line that will be sent over the wire.
    @Override
    public String toString() {
        return this..getRequestLine().toString();
    }
New to GrepCode? Check out our FAQ X