diff --git a/pom.xml b/pom.xml index 4bd397b..2bdd38e 100755 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ fi.foyt foursquare-api jar - 1.0.4-VinTank + 1.0.5-VinTank Foursquare API @@ -45,13 +45,6 @@ - - - foursquareapijava - svn:https://foursquare-api-java.googlecode.com/svn/repository - - - diff --git a/src/main/java/fi/foyt/foursquare/api/io/DefaultIOHandler.java b/src/main/java/fi/foyt/foursquare/api/io/DefaultIOHandler.java index a4c1c0f..241b58d 100644 --- a/src/main/java/fi/foyt/foursquare/api/io/DefaultIOHandler.java +++ b/src/main/java/fi/foyt/foursquare/api/io/DefaultIOHandler.java @@ -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