Skip to content

Instantly share code, notes, and snippets.

@paddycarver
Created July 11, 2011 19:21
Show Gist options
  • Save paddycarver/1076582 to your computer and use it in GitHub Desktop.
Save paddycarver/1076582 to your computer and use it in GitHub Desktop.
package com.suchagit.android2cloud.util;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntityEnclosingRequest;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.CoreProtocolPNames;
import oauth.signpost.commonshttp.CommonsHttpOAuthConsumer;
import oauth.signpost.exception.OAuthCommunicationException;
import oauth.signpost.exception.OAuthExpectationFailedException;
import oauth.signpost.exception.OAuthMessageSignerException;
public class HttpRequest {
private String method = "GET";
private String url;
private List<NameValuePair> data = new ArrayList<NameValuePair>();
private String consumer_key;
private String consumer_secret;
private String token;
private String token_secret;
private CommonsHttpOAuthConsumer consumer;
private HttpRequestBase request;
private UrlEncodedFormEntity entity;
public void setMethod(String newMethod) {
this.method = newMethod;
}
public String getMethod() {
return this.method;
}
public void setUrl(String newUrl) {
this.url = newUrl;
}
public String getUrl() {
return this.url;
}
public void addData(String name, String value) {
NameValuePair newData = new BasicNameValuePair(name, value);
this.data.add(newData);
}
public void removeData(String name) {
this.data.clear();
}
public void setConsumerKey(String newKey) {
this.consumer_key = newKey;
}
public void setConsumerSecret(String newSecret) {
this.consumer_secret = newSecret;
}
public String getConsumerKey() {
return this.consumer_key;
}
public String getConsumerSecret() {
return this.consumer_secret;
}
public void setToken(String newToken) {
this.token = newToken;
}
public String getToken() {
return this.token;
}
public void setTokenSecret(String newTokenSecret) {
this.token_secret = newTokenSecret;
}
public String getTokenSecret() {
return this.token_secret;
}
public void setConsumer(CommonsHttpOAuthConsumer newConsumer) {
this.consumer = newConsumer;
}
public CommonsHttpOAuthConsumer getConsumer() {
return this.consumer;
}
public HttpRequestBase getRequest() {
return this.request;
}
public void setRequest(HttpRequestBase newRequest) {
this.request = newRequest;
}
public UrlEncodedFormEntity getEntity() {
return this.entity;
}
public void setEntity(UrlEncodedFormEntity newEntity) {
this.entity = newEntity;
}
public HttpRequest(String consumer_key, String consumer_secret, String token, String token_secret) {
this.setConsumerKey(consumer_key);
this.setConsumerSecret(consumer_secret);
this.setConsumer(new CommonsHttpOAuthConsumer(this.getConsumerKey(), this.getConsumerSecret()));
this.setToken(token);
this.setTokenSecret(token_secret);
this.consumer.setTokenWithSecret(this.getToken(), this.getTokenSecret());
}
public void sign() {
if(this.getMethod().equals("POST")) {
this.setRequest(new HttpPost(this.url));
} else if(this.getMethod().equals("GET")) {
this.setRequest(new HttpGet(this.url));
}
try {
this.setEntity(new UrlEncodedFormEntity(this.data, "UTF-8"));
} catch (UnsupportedEncodingException e1) {
//TODO: Handle encoding error for URLs
}
HttpRequestBase request = this.getRequest();
request.getParams().setBooleanParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, false);
request.addHeader("Content-Type", "application/x-www-form-urlencoded");
if(this.getMethod().equals("POST")) {
((HttpEntityEnclosingRequest) request).setEntity(this.getEntity());
}
try {
this.getConsumer().sign(this.getRequest());
} catch(OAuthMessageSignerException e) {
//TODO: Handle error
} catch(OAuthExpectationFailedException e) {
//TODO: Handle error
} catch(OAuthCommunicationException e) {
//TODO: Handle error
}
}
public class SendLinkRequest extends HttpRequest {
public SendLinkRequest(String consumerKey, String consumerSecret,
String token, String tokenSecret) {
super(consumerKey, consumerSecret, token, tokenSecret);
// TODO Auto-generated constructor stub
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment