diff --git a/src/main/java/com/semantics3/api/ConnectionBuilder.java b/src/main/java/com/semantics3/api/ConnectionBuilder.java new file mode 100644 index 0000000..65e11a2 --- /dev/null +++ b/src/main/java/com/semantics3/api/ConnectionBuilder.java @@ -0,0 +1,25 @@ +package com.semantics3.api; + +import java.io.IOException; +import java.net.HttpURLConnection; +import java.net.URISyntaxException; +import java.net.URL; + +public class ConnectionBuilder { + private static final String USER_AGENT_KEY = "User-Agent"; + private static final String USER_AGENT_VALUE = "Semantics3 Java Library"; + + public HttpURLConnection buildConnection(String req, ConnectionProperties connectionProperties) + throws IOException, URISyntaxException { + URL url = new URL(req); + url = url.toURI().normalize().toURL(); + + HttpURLConnection request = (HttpURLConnection) url.openConnection(); + request.setRequestProperty(USER_AGENT_KEY, USER_AGENT_VALUE); + + request.setConnectTimeout(connectionProperties.getConnectionTimeout()); + request.setReadTimeout(connectionProperties.getReadTimeout()); + + return request; + } +} diff --git a/src/main/java/com/semantics3/api/ConnectionProperties.java b/src/main/java/com/semantics3/api/ConnectionProperties.java new file mode 100644 index 0000000..472b522 --- /dev/null +++ b/src/main/java/com/semantics3/api/ConnectionProperties.java @@ -0,0 +1,25 @@ +package com.semantics3.api; + +public class ConnectionProperties { + private static final int DEFAULT_NO_TIMEOUT = 0; + + private int readTimeout = DEFAULT_NO_TIMEOUT; + private int connectionTimeout = DEFAULT_NO_TIMEOUT; + + + public int getReadTimeout() { + return readTimeout; + } + + public void setReadTimeout(int readTimeout) { + this.readTimeout = readTimeout; + } + + public int getConnectionTimeout() { + return connectionTimeout; + } + + public void setConnectionTimeout(int connectionTimeout) { + this.connectionTimeout = connectionTimeout; + } +} diff --git a/src/main/java/com/semantics3/api/Semantics3Request.java b/src/main/java/com/semantics3/api/Semantics3Request.java index f3c3b87..eae2205 100644 --- a/src/main/java/com/semantics3/api/Semantics3Request.java +++ b/src/main/java/com/semantics3/api/Semantics3Request.java @@ -13,8 +13,8 @@ import java.io.InputStream; import java.io.OutputStreamWriter; import java.net.HttpURLConnection; +import java.net.SocketTimeoutException; import java.net.URISyntaxException; -import java.net.URL; import java.net.URLEncoder; import java.util.HashMap; @@ -30,46 +30,41 @@ public class Semantics3Request{ private HashMap query = new HashMap(); private JSONObject queryResult; private StringBuffer urlBuilder; - - public Semantics3Request(String apiKey, String apiSecret, String endpoint) { - if (apiKey == null) { + private ConnectionProperties connectionProperties; + + public Semantics3Request(String apiKey, String apiSecret, String endpoint, ConnectionProperties connectionProperties) { + if (apiKey == null) { throw new Semantics3Exception( "API Credentials Missing", "You did not supply an apiKey. Please sign up at https://semantics3.com/ to obtain your api_key." - ); + ); } - if (apiSecret == null) { + if (apiSecret == null) { throw new Semantics3Exception( "API Credentials Missing", "You did not supply an apiSecret. Please sign up at https://semantics3.com/ to obtain your api_key." - ); + ); } - this.apiKey = apiKey; this.apiSecret = apiSecret; - this.endpoint = endpoint; this.consumer = new DefaultOAuthConsumer(apiKey, apiSecret); consumer.setTokenWithSecret("", ""); + + this.endpoint = endpoint; + + this.connectionProperties = connectionProperties; } - public Semantics3Request(String apiKey, String apiSecret) { - if (apiKey == null) { - throw new Semantics3Exception( - "API Credentials Missing", - "You did not supply an apiKey. Please sign up at https://semantics3.com/ to obtain your api_key." - ); - } - if (apiSecret == null) { - throw new Semantics3Exception( - "API Credentials Missing", - "You did not supply an apiSecret. Please sign up at https://semantics3.com/ to obtain your api_key." - ); - } + public Semantics3Request(String apiKey, String apiSecret, ConnectionProperties connectionProperties) { + this(apiKey, apiSecret, null, connectionProperties); + } + + public Semantics3Request(String apiKey, String apiSecret, String endpoint) { + this(apiKey, apiSecret, endpoint, new ConnectionProperties()); + } - this.apiKey = apiKey; - this.apiSecret = apiSecret; - this.consumer = new DefaultOAuthConsumer(apiKey, apiSecret); - consumer.setTokenWithSecret("", ""); + public Semantics3Request(String apiKey, String apiSecret) { + this(apiKey, apiSecret, null, new ConnectionProperties()); } protected JSONObject fetch(String endpoint, String params) throws @@ -83,10 +78,7 @@ protected JSONObject fetch(String endpoint, String params) throws .append("?q=") .append(URLEncoder.encode(params, "UTF-8")) .toString(); - URL url = new URL(req); - url = url.toURI().normalize().toURL(); - HttpURLConnection request = (HttpURLConnection) url.openConnection(); - request.setRequestProperty("User-Agent", "Semantics3 Java Library"); + HttpURLConnection request = new ConnectionBuilder().buildConnection(req, this.connectionProperties); consumer.sign(request); request.connect(); try { @@ -96,6 +88,9 @@ protected JSONObject fetch(String endpoint, String params) throws } return json; } + catch (SocketTimeoutException e) { + throw e; + } catch (IOException e) { InputStream error = ((HttpURLConnection) request).getErrorStream(); JSONObject json = new JSONObject(new JSONTokener(error)); @@ -119,10 +114,7 @@ protected JSONObject fetch(String endpoint, String method, HashMap