Skip to content

Commit

Permalink
Added sane default connect & read timeouts to DefaultIOHandler and
Browse files Browse the repository at this point in the history
ability to adjust timeouts. Also bumped version.
  • Loading branch information
james-jory committed Apr 23, 2014
1 parent ae56378 commit 2bb82b2
Show file tree
Hide file tree
Showing 2 changed files with 158 additions and 99 deletions.
9 changes: 1 addition & 8 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<groupId>fi.foyt</groupId>
<artifactId>foursquare-api</artifactId>
<packaging>jar</packaging>
<version>1.0.4-VinTank</version>
<version>1.0.5-VinTank</version>
<name>Foursquare API</name>
<licenses>
<license>
Expand Down Expand Up @@ -45,13 +45,6 @@
</dependency>
</dependencies>

<distributionManagement>
<repository>
<id>foursquareapijava</id>
<url>svn:https://foursquare-api-java.googlecode.com/svn/repository</url>
</repository>
</distributionManagement>

<build>
<plugins>
<plugin>
Expand Down
248 changes: 157 additions & 91 deletions src/main/java/fi/foyt/foursquare/api/io/DefaultIOHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,101 +27,167 @@
*
*/
public class DefaultIOHandler extends IOHandler {

@Override
public Response fetchData(String url, Method method) {
int code = 200;

try {
URL aUrl = new URL(url);
HttpURLConnection connection = (HttpURLConnection) aUrl.openConnection();
try {
connection.setDoInput(true);
if("POST".equals(method.name())) {
connection.setDoOutput(true);
}
connection.setRequestMethod(method.name());
connection.connect();

code = connection.getResponseCode();
if (code == 200) {
InputStream inputStream = connection.getInputStream();
return new Response(readStream(inputStream), code, connection.getResponseMessage());
} else {
return new Response("", code, getMessageByCode(code));
}

} finally {
connection.disconnect();
}
} catch (MalformedURLException e) {
return new Response("", 400, "Malformed URL: " + url);
} catch (IOException e) {
return new Response("", 500, e.getMessage());
}
}

@Override
public Response fetchDataMultipartMime(String url, MultipartParameter... parameters) {
int code = 200;

try {
URL aUrl = new URL(url);
HttpURLConnection connection = (HttpURLConnection) aUrl.openConnection();
try {
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + BOUNDARY);
connection.connect();

OutputStream outputStream = connection.getOutputStream();
private int connectTimeout = 15000;
private int readTimeout = 15000;
private boolean allowUserInteraction = false;

/**
* Timeout in milliseconds to wait for connection.
* @return
*/
public int getConnectTimeout() {
return connectTimeout;
}

/**
* Set connect timeout (in milliseconds).
*/
public void setConnectTimeout(int connectTimeout) {
this.connectTimeout = connectTimeout;
}

/**
* Timeout in milliseconds to wait for data on a connection.
* @return
*/
public int getReadTimeout() {
return readTimeout;
}

/**
* Set read timeout (in milliseconds).
* @param readTimeout
*/
public void setReadTimeout(int readTimeout) {
this.readTimeout = readTimeout;
}

/**
* Should user interaction based be supported for HTTP connections.
* @return
*/
public boolean isAllowUserInteraction() {
return allowUserInteraction;
}

public void setAllowUserInteraction(boolean allowUserInteraction) {
this.allowUserInteraction = allowUserInteraction;
}

@Override
public Response fetchData(String url, Method method) {
int code = 200;

try {
URL aUrl = new URL(url);
HttpURLConnection connection = (HttpURLConnection)aUrl.openConnection();

try {
if (connectTimeout > 0)
connection.setConnectTimeout(connectTimeout);
if (readTimeout > 0)
connection.setReadTimeout(readTimeout);

connection.setAllowUserInteraction(allowUserInteraction);
connection.setDoInput(true);
if ("POST".equals(method.name()))
connection.setDoOutput(true);
connection.setRequestMethod(method.name());
connection.connect();

code = connection.getResponseCode();
if (code == 200) {
InputStream inputStream = connection.getInputStream();
return new Response(readStream(inputStream), code, connection.getResponseMessage());
}

return new Response("", code, getMessageByCode(code));
}
finally {
connection.disconnect();
}
}
catch (MalformedURLException e) {
return new Response("", 400, "Malformed URL: " + url);
}
catch (IOException e) {
return new Response("", 500, e.getMessage());
}
}

@Override
public Response fetchDataMultipartMime(String url, MultipartParameter... parameters) {
int code = 200;

try {
URL aUrl = new URL(url);
HttpURLConnection connection = (HttpURLConnection) aUrl.openConnection();

try {
if (connectTimeout > 0)
connection.setConnectTimeout(connectTimeout);
if (readTimeout > 0)
connection.setReadTimeout(readTimeout);

connection.setAllowUserInteraction(allowUserInteraction);
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + BOUNDARY);
connection.connect();

OutputStream outputStream = connection.getOutputStream();

StringBuffer startBoundaryBuilder = new StringBuffer("--")
.append(BOUNDARY)
.append("\r\n");
StringBuilder startBoundaryBuilder = new StringBuilder();
startBoundaryBuilder.append("--").append(BOUNDARY).append("\r\n");

outputStream.write(startBoundaryBuilder.toString().getBytes());
outputStream.write(startBoundaryBuilder.toString().getBytes());

for (MultipartParameter parameter : parameters) {
StringBuffer formDataBuilder = new StringBuffer()
.append("Content-Disposition: form-data; name=\"")
.append(parameter.getName())
.append("\"; filename=\"")
.append(parameter.getName())
.append("\"\r\n")
.append("Content-Type: ")
.append(parameter.getContentType())
.append("\r\n\r\n");
outputStream.write(formDataBuilder.toString().getBytes());
outputStream.write(parameter.getContent());
}
for (MultipartParameter parameter : parameters) {
StringBuilder formDataBuilder = new StringBuilder()
.append("Content-Disposition: form-data; name=\"")
.append(parameter.getName())
.append("\"; filename=\"")
.append(parameter.getName())
.append("\"\r\n")
.append("Content-Type: ")
.append(parameter.getContentType())
.append("\r\n\r\n");

outputStream.write(formDataBuilder.toString().getBytes());
outputStream.write(parameter.getContent());
}

StringBuilder endBoundaryBuilder = new StringBuilder("\r\n--")
.append(BOUNDARY)
.append("--\r\n");
outputStream.write(endBoundaryBuilder.toString().getBytes());

outputStream.flush();
outputStream.close();

code = connection.getResponseCode();
if (code == 200) {
InputStream inputStream = connection.getInputStream();
return new Response(readStream(inputStream), code, connection.getResponseMessage());
} else {
return new Response("", code, getMessageByCode(code));
}

} finally {
connection.disconnect();
}
} catch (MalformedURLException e) {
return new Response("", 400, "Malformed URL: " + url);
} catch (IOException e) {
return new Response("", 500, e.getMessage());
}
}
StringBuilder endBoundaryBuilder = new StringBuilder()
.append("\r\n--")
.append(BOUNDARY)
.append("--\r\n");

outputStream.write(endBoundaryBuilder.toString().getBytes());

outputStream.flush();
outputStream.close();

code = connection.getResponseCode();
if (code == 200) {
InputStream inputStream = connection.getInputStream();
return new Response(readStream(inputStream), code, connection.getResponseMessage());
}

return new Response("", code, getMessageByCode(code));

}
finally {
connection.disconnect();
}
}
catch (MalformedURLException e) {
return new Response("", 400, "Malformed URL: " + url);
}
catch (IOException e) {
return new Response("", 500, e.getMessage());
}
}

/**
* Reads input stream and returns it's contents as String
Expand Down

0 comments on commit 2bb82b2

Please sign in to comment.