net.oauth
Class OAuthMessage

java.lang.Object
  extended by net.oauth.OAuthMessage
Direct Known Subclasses:
OAuthResponseMessage

public class OAuthMessage
extends java.lang.Object

A request or response message used in the OAuth protocol.

The parameters in this class are not percent-encoded. Methods like OAuthClient.invoke and OAuthResponseMessage.completeParameters are responsible for percent-encoding parameters before transmission and decoding them after reception.

Author:
John Kristian

Field Summary
static java.lang.String AUTH_SCHEME
           
static java.lang.String DELETE
           
static java.lang.String GET
           
 java.lang.String method
           
static java.lang.String POST
           
static java.lang.String PUT
           
 java.lang.String URL
           
 
Constructor Summary
OAuthMessage(java.lang.String method, java.lang.String URL, java.util.Collection<? extends java.util.Map.Entry> parameters)
           
OAuthMessage(java.lang.String method, java.lang.String URL, java.util.Collection<? extends java.util.Map.Entry> parameters, java.io.InputStream bodyAsStream)
           
 
Method Summary
 void addParameter(java.util.Map.Entry<java.lang.String,java.lang.String> parameter)
           
 void addParameter(java.lang.String key, java.lang.String value)
           
 void addParameters(java.util.Collection<? extends java.util.Map.Entry<java.lang.String,java.lang.String>> parameters)
           
 void addRequiredParameters(OAuthAccessor accessor)
          Add some of the parameters needed to request access to a protected resource, if they aren't already in the message.
protected  void completeParameters()
          Finish adding parameters; for example read an HTTP response body and parse parameters from it.
static java.util.List<OAuth.Parameter> decodeAuthorization(java.lang.String authorization)
          Parse the parameters from an OAuth Authorization or WWW-Authenticate header.
protected  void dump(java.util.Map<java.lang.String,java.lang.Object> into)
           
 java.lang.String getAuthorizationHeader(java.lang.String realm)
          Construct a WWW-Authenticate or Authentication header value, containing the given realm plus all the parameters whose names begin with "oauth_".
 java.io.InputStream getBodyAsStream()
          Get a stream from which to read the body of the HTTP request or response.
 java.lang.String getBodyEncoding()
          The character encoding of the body of this message.
 java.lang.String getBodyType()
          The MIME type of the body of this message.
 java.lang.String getConsumerKey()
           
 java.util.Map<java.lang.String,java.lang.Object> getDump()
          Construct a verbose description of this message and its origins.
 java.lang.String getHeader(java.lang.String name)
          The value of the last HTTP header with the given name.
 java.util.List<java.util.Map.Entry<java.lang.String,java.lang.String>> getHeaders()
          All HTTP headers.
 java.lang.String getParameter(java.lang.String name)
           
protected  java.util.Map<java.lang.String,java.lang.String> getParameterMap()
           
 java.util.List<java.util.Map.Entry<java.lang.String,java.lang.String>> getParameters()
           
 java.lang.String getSignature()
           
 java.lang.String getSignatureMethod()
           
 java.lang.String getToken()
           
static java.lang.String readAll(java.io.InputStream from, java.lang.String encoding)
          Read all the data from the given stream, and close it.
 java.lang.String readBodyAsString()
          Read the body of the HTTP request or response and convert it to a String.
 void requireParameters(java.lang.String... names)
          Verify that the required parameter names are contained in the actual collection.
 void sign(OAuthAccessor accessor)
          Add a signature to the message.
 HttpMessage toHttpRequest(OAuthClient.ParameterStyle style)
          Deprecated. use HttpMessage.newRequest
 java.lang.String toString()
           
 void validateMessage(OAuthAccessor accessor, OAuthValidator validator)
          Deprecated. use validator.validateMessage(this, accessor) instead.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

method

public java.lang.String method

URL

public java.lang.String URL

AUTH_SCHEME

public static final java.lang.String AUTH_SCHEME
See Also:
Constant Field Values

GET

public static final java.lang.String GET
See Also:
Constant Field Values

POST

public static final java.lang.String POST
See Also:
Constant Field Values

PUT

public static final java.lang.String PUT
See Also:
Constant Field Values

DELETE

public static final java.lang.String DELETE
See Also:
Constant Field Values
Constructor Detail

OAuthMessage

public OAuthMessage(java.lang.String method,
                    java.lang.String URL,
                    java.util.Collection<? extends java.util.Map.Entry> parameters)

OAuthMessage

public OAuthMessage(java.lang.String method,
                    java.lang.String URL,
                    java.util.Collection<? extends java.util.Map.Entry> parameters,
                    java.io.InputStream bodyAsStream)
Method Detail

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

completeParameters

protected void completeParameters()
                           throws java.io.IOException
Finish adding parameters; for example read an HTTP response body and parse parameters from it.

Throws:
java.io.IOException

getParameters

public java.util.List<java.util.Map.Entry<java.lang.String,java.lang.String>> getParameters()
                                                                                     throws java.io.IOException
Throws:
java.io.IOException

addParameter

public void addParameter(java.lang.String key,
                         java.lang.String value)

addParameter

public void addParameter(java.util.Map.Entry<java.lang.String,java.lang.String> parameter)

addParameters

public void addParameters(java.util.Collection<? extends java.util.Map.Entry<java.lang.String,java.lang.String>> parameters)

getParameter

public java.lang.String getParameter(java.lang.String name)
                              throws java.io.IOException
Throws:
java.io.IOException

getConsumerKey

public java.lang.String getConsumerKey()
                                throws java.io.IOException
Throws:
java.io.IOException

getToken

public java.lang.String getToken()
                          throws java.io.IOException
Throws:
java.io.IOException

getSignatureMethod

public java.lang.String getSignatureMethod()
                                    throws java.io.IOException
Throws:
java.io.IOException

getSignature

public java.lang.String getSignature()
                              throws java.io.IOException
Throws:
java.io.IOException

getParameterMap

protected java.util.Map<java.lang.String,java.lang.String> getParameterMap()
                                                                    throws java.io.IOException
Throws:
java.io.IOException

getBodyType

public java.lang.String getBodyType()
The MIME type of the body of this message.

Returns:
the MIME type, or null to indicate the type is unknown.

getBodyEncoding

public java.lang.String getBodyEncoding()
The character encoding of the body of this message.

Returns:
the name of an encoding, or "ISO-8859-1" if no charset has been specified.

getHeader

public final java.lang.String getHeader(java.lang.String name)
The value of the last HTTP header with the given name. The name is case insensitive.

Returns:
the value of the last header, or null to indicate that there is no such header in this message.

getHeaders

public final java.util.List<java.util.Map.Entry<java.lang.String,java.lang.String>> getHeaders()
All HTTP headers. You can add headers to this list.


readBodyAsString

public final java.lang.String readBodyAsString()
                                        throws java.io.IOException
Read the body of the HTTP request or response and convert it to a String. This method isn't repeatable, since it consumes and closes getBodyAsStream.

Returns:
the body, or null to indicate there is no body.
Throws:
java.io.IOException

getBodyAsStream

public java.io.InputStream getBodyAsStream()
                                    throws java.io.IOException
Get a stream from which to read the body of the HTTP request or response. This is designed to support efficient streaming of a large message. The caller must close the returned stream, to release the underlying resources such as the TCP connection for an HTTP response.

Returns:
a stream from which to read the body, or null to indicate there is no body.
Throws:
java.io.IOException

getDump

public java.util.Map<java.lang.String,java.lang.Object> getDump()
                                                         throws java.io.IOException
Construct a verbose description of this message and its origins.

Throws:
java.io.IOException

dump

protected void dump(java.util.Map<java.lang.String,java.lang.Object> into)
             throws java.io.IOException
Throws:
java.io.IOException

requireParameters

public void requireParameters(java.lang.String... names)
                       throws OAuthProblemException,
                              java.io.IOException
Verify that the required parameter names are contained in the actual collection.

Throws:
OAuthProblemException - one or more parameters are absent.
java.io.IOException

addRequiredParameters

public void addRequiredParameters(OAuthAccessor accessor)
                           throws OAuthException,
                                  java.io.IOException,
                                  java.net.URISyntaxException
Add some of the parameters needed to request access to a protected resource, if they aren't already in the message.

Throws:
java.io.IOException
java.net.URISyntaxException
OAuthException

sign

public void sign(OAuthAccessor accessor)
          throws java.io.IOException,
                 OAuthException,
                 java.net.URISyntaxException
Add a signature to the message.

Throws:
java.net.URISyntaxException
java.io.IOException
OAuthException

toHttpRequest

public HttpMessage toHttpRequest(OAuthClient.ParameterStyle style)
                          throws java.io.IOException
Deprecated. use HttpMessage.newRequest

Construct an HTTP request from this OAuth message.

Parameters:
style - where to put the OAuth parameters, within the HTTP request
Throws:
java.io.IOException

validateMessage

public void validateMessage(OAuthAccessor accessor,
                            OAuthValidator validator)
                     throws OAuthException,
                            java.io.IOException,
                            java.net.URISyntaxException
Deprecated. use validator.validateMessage(this, accessor) instead.

Check that the message is valid.

Throws:
java.io.IOException
java.net.URISyntaxException
OAuthProblemException - the message is invalid
OAuthException

getAuthorizationHeader

public java.lang.String getAuthorizationHeader(java.lang.String realm)
                                        throws java.io.IOException
Construct a WWW-Authenticate or Authentication header value, containing the given realm plus all the parameters whose names begin with "oauth_".

Throws:
java.io.IOException

readAll

public static java.lang.String readAll(java.io.InputStream from,
                                       java.lang.String encoding)
                                throws java.io.IOException
Read all the data from the given stream, and close it.

Returns:
null if from is null, or the data from the stream converted to a String
Throws:
java.io.IOException

decodeAuthorization

public static java.util.List<OAuth.Parameter> decodeAuthorization(java.lang.String authorization)
Parse the parameters from an OAuth Authorization or WWW-Authenticate header. The realm is included as a parameter. If the given header doesn't start with "OAuth ", return an empty list.



Copyright © 2009. All Rights Reserved.