pl.mbdev.openstage.push
Class HttpRequest

java.lang.Object
  extended by pl.mbdev.openstage.push.HttpRequest
Direct Known Subclasses:
PostRequest

public abstract class HttpRequest
extends java.lang.Object

Enables easier formulation and execution of HTTP requests.

 Copyright 2011 Mateusz Bysiek,
     mb@mbdev.pl, http://mbdev.pl/
 
 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.
 

Author:
Mateusz Bysiek

Field Summary
private  java.io.BufferedReader bufferedReader
          Buffer for the reader of the input stream.
private  java.net.HttpURLConnection connection
          HTTP connection.
private  java.io.InputStream in
          Input stream.
protected  java.io.OutputStream out
          Output stream.
private  RequestParameters parameters
          Parameters parameters of this request.
private  java.io.Reader reader
          Reader for the input stream.
private  java.io.OutputStreamWriter writer
          Writer for the output stream.
 
Constructor Summary
HttpRequest(RequestParameters parameters)
          Creates new generic HTTP request with given parameters.
 
Method Summary
protected  void connectTo(java.net.URL targetAddress, java.lang.String method)
           
 void disconnect()
          Disconnects from the remote server.
protected  void endInput()
          Closes the input stream.
protected  void endOutput()
           
protected  RequestParameters getParameters()
          Returns the request parameters.
protected abstract  java.lang.String getResponse(java.net.URL targetAddress)
          Provides default implementation for HTTP request execution.
protected abstract  boolean sendTo(java.net.URL targetAddress)
          Sends the push to a given URL.
protected  java.io.BufferedReader startInput()
          Starts input, opens input stream.
protected  java.io.OutputStreamWriter startOutput(java.lang.String encoding)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

parameters

private final RequestParameters parameters
Parameters parameters of this request.


connection

private java.net.HttpURLConnection connection
HTTP connection.


out

protected java.io.OutputStream out
Output stream.


writer

private java.io.OutputStreamWriter writer
Writer for the output stream.


in

private java.io.InputStream in
Input stream.


reader

private java.io.Reader reader
Reader for the input stream.


bufferedReader

private java.io.BufferedReader bufferedReader
Buffer for the reader of the input stream.

Constructor Detail

HttpRequest

public HttpRequest(RequestParameters parameters)
Creates new generic HTTP request with given parameters.

Parameters:
parameters - parameters of this request
Method Detail

getParameters

protected final RequestParameters getParameters()
Returns the request parameters.

Returns:
request parameters

connectTo

protected void connectTo(java.net.URL targetAddress,
                         java.lang.String method)
                  throws java.io.IOException
Parameters:
targetAddress - target address
method - HTTP method: GET, POST, ...
Throws:
java.io.IOException - when connection cannot be established

startOutput

protected java.io.OutputStreamWriter startOutput(java.lang.String encoding)
                                          throws java.io.UnsupportedEncodingException,
                                                 java.io.IOException
Parameters:
encoding - character encoding, for example 'UTF-8'
Returns:
content writer for this HTTP request
Throws:
java.io.UnsupportedEncodingException - when given encoding is not supported by the server.
java.io.IOException - when output streams cannot be opened

endOutput

protected void endOutput()
                  throws java.io.IOException
Throws:
java.io.IOException - when output streams cannot be closed

startInput

protected java.io.BufferedReader startInput()
                                     throws java.io.IOException
Starts input, opens input stream.

Returns:
buffered reader for the input
Throws:
java.io.IOException - when the input stream cannot be opened

endInput

protected void endInput()
                 throws java.io.IOException
Closes the input stream.

Throws:
java.io.IOException - when the input stream cannot be closed

disconnect

public void disconnect()
Disconnects from the remote server.


getResponse

protected abstract java.lang.String getResponse(java.net.URL targetAddress)
                                         throws java.io.IOException
Provides default implementation for HTTP request execution.

Parameters:
targetAddress - URL of the target to which this request will be sent
Returns:
response from the target address
Throws:
java.io.IOException - thrown when the given URL caused a connection error, there was a protocol exception or data transfer was interrupted

sendTo

protected abstract boolean sendTo(java.net.URL targetAddress)
                           throws java.io.IOException
Sends the push to a given URL.

Parameters:
targetAddress - URL of the target to which this request will be sent
Returns:
true if the request was successfully sent
Throws:
java.io.IOException - thrown when the given URL caused a connection error, there was a protocol exception or data transfer was interrupted