joker-b / oauth

Automatically exported from code.google.com/p/oauth
0 stars 0 forks source link

OAuth Java library: Small changes in OAuthMessage to make it more suitable for REST-APIs #87

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
Try to use the Java library for a REST call that needs to submit XML data
in the POST body.

What is the expected output? What do you see instead?
I could not find a way to do this with the latest version of the library.
What I need to do is specify a custom InputStream to be used as the body
and I have to specify a content type (text/xml in my case).

What version of the product are you using? On what operating system?
Revision 901 from http://oauth.googlecode.com/svn/code/java/ on Linux

Please provide any additional information below.
It is not hard to fix, but I'm not sure if the way I did it is a good way:
In OAuthMessage.java:
- Introduce two new class variables:
    private InputStream bodyStream = null;
    private String bodyContentType = null;
- create getters and setters (actually the first one is already there but
always returns null):
    public InputStream getBodyAsStream() throws IOException {
        return bodyStream;
    }

    public void setBodyStream( InputStream bodyStream ) {
        this.bodyStream = bodyStream;
    }

    public String getBodyContentType() {
        return bodyContentType;
    }

    public void SetBodyContentType( String bodyContentType ) {
        this.bodyContentType = bodyContentType;
    }
- Modify toHttpRequest(ParameterStyle style) to use the custom content type
if one is set:
    public HttpMessage toHttpRequest(ParameterStyle style) throws IOException {
        final boolean isPost = POST.equalsIgnoreCase(method);
        InputStream body = getBodyAsStream();
        // NEW: -------
        if ( getBodyAsStream() != null && getBodyContentType() != null ) {
            headers.add(new OAuth.Parameter(HttpMessage.CONTENT_TYPE,
getBodyContentType()));
        }
        // ------------
        if (style == ParameterStyle.BODY && !(isPost && body == null)) {
            style = ParameterStyle.QUERY_STRING;
        }
        [...]
}

Now the library can be used line this:
OAuthMessage request = accessor.newRequestMessage( OAuthMessage.POST,
pollsURL, null );
request.setBodyStream( new ByteArrayInputStream( createPollXML.getBytes() ) );
request.SetBodyContentType( "text/xml" );
System.out.println( client.invoke( request,
ParameterStyle.AUTHORIZATION_HEADER ).readBodyAsString() );

Original issue reported on code.google.com by gubler.d...@gmail.com on 25 Feb 2009 at 10:54

GoogleCodeExporter commented 9 years ago
Starting with -r942 you can do this:
request = accessor.newRequestMessage(OAuthMessage.POST, url, null, new 
ByteArrayInputStream(xml));
request.getHeaders().add(new OAuth.Parameter(HttpMessage.CONTENT_TYPE, 
"text/xml");
response = client.access(request, ParameterStyle.AUTHORIZATION_HEADER);

Original comment by jmkrist...@gmail.com on 9 Apr 2009 at 4:25