Skip to content

Commit

Permalink
Removed Request repetitive code
Browse files Browse the repository at this point in the history
  • Loading branch information
fernandezpablo85 committed Mar 28, 2010
1 parent b0130f2 commit 3094cf3
Showing 1 changed file with 7 additions and 13 deletions.
20 changes: 7 additions & 13 deletions src/main/java/org/scribe/http/Request.java
Expand Up @@ -63,12 +63,11 @@ Response doSend() throws IOException{
addHeaders(connection);
if(verb.equals(Verb.PUT) || verb.equals(Verb.POST)){
if(payload != null){
addPayload(connection);
addBody(connection, payload);
}else{
addBody(connection);
addBody(connection, queryString(bodyParams));
}
}

return new Response(connection);
}

Expand All @@ -77,17 +76,10 @@ void addHeaders (HttpURLConnection conn){
conn.setRequestProperty(key, headers.get(key));
}

void addBody(HttpURLConnection conn) throws IOException{
String body = queryString(bodyParams);
conn.setRequestProperty(CONTENT_LENGTH, String.valueOf(body.getBytes().length));
void addBody(HttpURLConnection conn, String content) throws IOException{
conn.setRequestProperty(CONTENT_LENGTH, String.valueOf(content.getBytes().length));
conn.setDoOutput(true);
conn.getOutputStream().write(body.getBytes());
}

void addPayload(HttpURLConnection conn) throws IOException {
conn.setRequestProperty(CONTENT_LENGTH, String.valueOf(payload.getBytes().length));
conn.setDoOutput(true);
conn.getOutputStream().write(payload.getBytes());
conn.getOutputStream().write(content.getBytes());
}

/**
Expand Down Expand Up @@ -130,6 +122,8 @@ public void addPayload(String payload){
* @return a map containing the query string parameters
*/
public Set<Map.Entry<String, String>> getQueryStringParams(){
//TODO move this into org.scribe.encoders.URL
//TODO change name of org.scribe.encoders to org.scribe.utils
try{
Map<String, String> params = new HashMap<String, String>();
String query = new URL(url).getQuery();
Expand Down

0 comments on commit 3094cf3

Please sign in to comment.