Package net.oauth
Class OAuthMessage
- java.lang.Object
-
- net.oauth.OAuthMessage
-
public class OAuthMessage extends 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
-
-
Constructor Summary
Constructors Constructor Description OAuthMessage(String method, String URL, Collection<? extends Map.Entry<String,String>> parameters)
OAuthMessage(String method, String URL, Collection<? extends Map.Entry<String,String>> parameters, InputStream bodyAsStream)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addParameter(String key, String value)
void
addParameter(Map.Entry<String,String> parameter)
void
addParameters(Collection<? extends Map.Entry<String,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 List<OAuth.Parameter>
decodeAuthorization(String authorization)
Parse the parameters from an OAuth Authorization or WWW-Authenticate header.protected void
dump(Map<String,Object> into)
String
getAuthorizationHeader(String realm)
Construct a WWW-Authenticate or Authentication header value, containing the given realm plus all the parameters whose names begin with "oauth_".InputStream
getBodyAsStream()
Get a stream from which to read the body of the HTTP request or response.String
getBodyEncoding()
The character encoding of the body of this message.String
getBodyType()
The MIME type of the body of this message.String
getConsumerKey()
Map<String,Object>
getDump()
Construct a verbose description of this message and its origins.String
getHeader(String name)
The value of the last HTTP header with the given name.List<Map.Entry<String,String>>
getHeaders()
All HTTP headers.String
getParameter(String name)
protected Map<String,String>
getParameterMap()
List<Map.Entry<String,String>>
getParameters()
String
getSignature()
String
getSignatureMethod()
String
getToken()
static String
readAll(InputStream from, String encoding)
Read all the data from the given stream, and close it.String
readBodyAsString()
Read the body of the HTTP request or response and convert it to a String.void
requireParameters(String... names)
Verify that the required parameter names are contained in the actual collection.void
sign(OAuthAccessor accessor)
Add a signature to the message.String
toString()
-
-
-
Field Detail
-
AUTH_SCHEME
public static final String AUTH_SCHEME
- See Also:
- Constant Field Values
-
DELETE
public static final String DELETE
- See Also:
- Constant Field Values
-
GET
public static final String GET
- See Also:
- Constant Field Values
-
POST
public static final String POST
- See Also:
- Constant Field Values
-
PUT
public static final String PUT
- See Also:
- Constant Field Values
-
method
public String method
-
URL
public String URL
-
-
Constructor Detail
-
OAuthMessage
public OAuthMessage(String method, String URL, Collection<? extends Map.Entry<String,String>> parameters)
-
OAuthMessage
public OAuthMessage(String method, String URL, Collection<? extends Map.Entry<String,String>> parameters, InputStream bodyAsStream)
-
-
Method Detail
-
decodeAuthorization
public static List<OAuth.Parameter> decodeAuthorization(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.
-
readAll
public static String readAll(InputStream from, String encoding) throws 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:
IOException
-
addParameters
public void addParameters(Collection<? extends Map.Entry<String,String>> parameters)
-
addRequiredParameters
public void addRequiredParameters(OAuthAccessor accessor) throws OAuthException, IOException, URISyntaxException
Add some of the parameters needed to request access to a protected resource, if they aren't already in the message.
-
completeParameters
protected void completeParameters() throws IOException
Finish adding parameters; for example read an HTTP response body and parse parameters from it.- Throws:
IOException
-
dump
protected void dump(Map<String,Object> into) throws IOException
- Throws:
IOException
-
getAuthorizationHeader
public String getAuthorizationHeader(String realm) throws IOException
Construct a WWW-Authenticate or Authentication header value, containing the given realm plus all the parameters whose names begin with "oauth_".- Throws:
IOException
-
getBodyAsStream
public InputStream getBodyAsStream() throws 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:
IOException
-
getBodyEncoding
public 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.
-
getBodyType
public String getBodyType()
The MIME type of the body of this message.- Returns:
- the MIME type, or null to indicate the type is unknown.
-
getConsumerKey
public String getConsumerKey() throws IOException
- Throws:
IOException
-
getDump
public Map<String,Object> getDump() throws IOException
Construct a verbose description of this message and its origins.- Throws:
IOException
-
getHeader
public final String getHeader(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 List<Map.Entry<String,String>> getHeaders()
All HTTP headers. You can add headers to this list.
-
getParameter
public String getParameter(String name) throws IOException
- Throws:
IOException
-
getParameterMap
protected Map<String,String> getParameterMap() throws IOException
- Throws:
IOException
-
getParameters
public List<Map.Entry<String,String>> getParameters() throws IOException
- Throws:
IOException
-
getSignature
public String getSignature() throws IOException
- Throws:
IOException
-
getSignatureMethod
public String getSignatureMethod() throws IOException
- Throws:
IOException
-
getToken
public String getToken() throws IOException
- Throws:
IOException
-
readBodyAsString
public final String readBodyAsString() throws 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:
IOException
-
requireParameters
public void requireParameters(String... names) throws OAuthProblemException, IOException
Verify that the required parameter names are contained in the actual collection.- Throws:
OAuthProblemException
- one or more parameters are absent.IOException
-
sign
public void sign(OAuthAccessor accessor) throws IOException, OAuthException, URISyntaxException
Add a signature to the message.
-
-