Skip to content

Commit

Permalink
Adding documentation to the br.ufpe.cin.groundhog.http package and ad…
Browse files Browse the repository at this point in the history
…ding 'this' to the references to local object's properties. Related to #6 and #20.
  • Loading branch information
Rodrigo Alves Vieira committed May 4, 2013
1 parent 87159c5 commit 35729c5
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 11 deletions.
11 changes: 8 additions & 3 deletions src/java/main/br/ufpe/cin/groundhog/http/ParamBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,24 @@
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.message.BasicNameValuePair;

/**
* The URL parameter builder class
* @author fjsj
*
*/
public class ParamBuilder {
private List<NameValuePair> params;

public ParamBuilder() {
params = new ArrayList<NameValuePair>();
this.params = new ArrayList<NameValuePair>();
}

public ParamBuilder add(String name, String value) {
params.add(new BasicNameValuePair(name, value));
this.params.add(new BasicNameValuePair(name, value));
return this;
}

public String build() {
return URLEncodedUtils.format(params, "UTF-8");
return URLEncodedUtils.format(this.params, "UTF-8");
}
}
29 changes: 21 additions & 8 deletions src/java/main/br/ufpe/cin/groundhog/http/Requests.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,21 @@
import com.ning.http.client.ListenableFuture;

public class Requests {

private AsyncHttpClient asyncClient;

public Requests() {
asyncClient = new AsyncHttpClient();
this.asyncClient = new AsyncHttpClient();
}

/**
* Gets the response body of the given URL
* @param urlStr
* @return
* @throws IOException
*/
public String get(String urlStr) throws IOException {
try {
return asyncClient.prepareGet(urlStr).execute().get().getResponseBody();
return this.asyncClient.prepareGet(urlStr).execute().get().getResponseBody();
} catch (InterruptedException e) {
e.printStackTrace();
throw new HttpException(e);
Expand All @@ -31,9 +36,15 @@ public String get(String urlStr) throws IOException {
}
}

/**
* Downloads the response body of the given URL
* @param urlStr an URL of a page whose body will be downloaded
* @return
* @throws IOException
*/
public InputStream download(String urlStr) throws IOException {
try {
return asyncClient.prepareGet(urlStr).setFollowRedirects(true).execute().get().getResponseBodyAsStream();
return this.asyncClient.prepareGet(urlStr).setFollowRedirects(true).execute().get().getResponseBodyAsStream();
} catch (InterruptedException e) {
e.printStackTrace();
throw new HttpException(e);
Expand All @@ -59,7 +70,7 @@ public String encodeURL(String s) {
/**
*
* @param s a String representing an URL
* @return an UTF-8 decoded String derivated from the given URL
* @return an UTF-8 decoded String derived from the given URL
*/
public String decodeURL(String s) {
try {
Expand All @@ -69,9 +80,11 @@ public String decodeURL(String s) {
throw new HttpException(e);
}
}


/**
* Closes the connection
*/
public void close() {
asyncClient.close();
this.asyncClient.close();
}

}

0 comments on commit 35729c5

Please sign in to comment.