Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Removed tabs

  • Loading branch information...
commit 050efb58c4c2d4d34f163c4a3d939288b069de90 1 parent 30e0154
authored February 07, 2012

Showing 1 changed file with 114 additions and 114 deletions. Show diff stats Hide diff stats

  1. 228  com/hub9/api/Client.java
228  com/hub9/api/Client.java
@@ -16,104 +16,104 @@
16 16
 import org.json.JSONException;
17 17
 
18 18
 public class Client{
19  
-	private static final String ROOT_DOMAIN = "api.hub9.com";
20  
-	private static final String VERSION = "v0";
21  
-	private static final int TIMEOUT = 20; //seconds
22  
-	private static final String CHAR_SET = "utf-8";
23  
-	private static final String CONTENT_TYPE = "application/json; charset=\"utf-8\"";
24  
-	private static final String USER_AGENT = "hub9-java";	
25  
-	private String username;
26  
-	private String oauth_token;
27  
-	private int timeout;
  19
+    private static final String ROOT_DOMAIN = "api.hub9.com";
  20
+    private static final String VERSION = "v0";
  21
+    private static final int TIMEOUT = 20; //seconds
  22
+    private static final String CHAR_SET = "utf-8";
  23
+    private static final String CONTENT_TYPE = "application/json; charset=\"utf-8\"";
  24
+    private static final String USER_AGENT = "hub9-java";   
  25
+    private String username;
  26
+    private String oauth_token;
  27
+    private int timeout;
28 28
 
29  
-	
30  
-	public Client(String username, String oauth_token){
31  
-		this(username, oauth_token, TIMEOUT);	
32  
-	}
33  
-	
34  
-	public Client(String username, String oauth_token, int timeout){
35  
-		this.username = username;
36  
-		this.oauth_token = oauth_token;
37  
-		this.timeout = timeout;
38  
-	}
39  
-	
40  
-	public void put(String key, JSONObject doc) throws IOException, JSONException{
41  
-		request("PUT", key, doc, null);
42  
-	}
  29
+    
  30
+    public Client(String username, String oauth_token){
  31
+        this(username, oauth_token, TIMEOUT);   
  32
+    }
  33
+    
  34
+    public Client(String username, String oauth_token, int timeout){
  35
+        this.username = username;
  36
+        this.oauth_token = oauth_token;
  37
+        this.timeout = timeout;
  38
+    }
  39
+    
  40
+    public void put(String key, JSONObject doc) throws IOException, JSONException{
  41
+        request("PUT", key, doc, null);
  42
+    }
43 43
 
44  
-	public JSONObject get(String key) throws IOException, JSONException{
45  
-		return request("GET", key, null, null);
46  
-	}
  44
+    public JSONObject get(String key) throws IOException, JSONException{
  45
+        return request("GET", key, null, null);
  46
+    }
47 47
 
48  
-	public void delete(String key) throws IOException, JSONException{
49  
-		delete(key, null);
50  
-	}
51  
-	
52  
-	public void delete(String key, String query) throws IOException, JSONException{
53  
-		HashMap<String, String> params = new HashMap<String, String>();
54  
-		params.put("query", query);
55  
-		request("DELETE", key, null, params);
56  
-	}
  48
+    public void delete(String key) throws IOException, JSONException{
  49
+        delete(key, null);
  50
+    }
  51
+    
  52
+    public void delete(String key, String query) throws IOException, JSONException{
  53
+        HashMap<String, String> params = new HashMap<String, String>();
  54
+        params.put("query", query);
  55
+        request("DELETE", key, null, params);
  56
+    }
57 57
 
58  
-	public JSONObject search(String key) throws IOException, JSONException{
  58
+    public JSONObject search(String key) throws IOException, JSONException{
59 59
         return search(key, new JSONObject());
60 60
     }
61 61
     
62  
-	public JSONObject search(String key, JSONObject options) throws IOException, JSONException{
63  
-		HashMap<String, String> params = new HashMap<String, String>();
64  
-		params.put("query", options.optString("query"));
65  
-		params.put("sort", options.optString("sort"));
66  
-		params.put("snippet", options.optString("snippet"));
67  
-		params.put("max", options.has("max") ? Integer.toString(options.optInt("max")) : "");
68  
-		params.put("token", options.optString("token"));
69  
-		return request("GET", key, null, params);
70  
-	}
  62
+    public JSONObject search(String key, JSONObject options) throws IOException, JSONException{
  63
+        HashMap<String, String> params = new HashMap<String, String>();
  64
+        params.put("query", options.optString("query"));
  65
+        params.put("sort", options.optString("sort"));
  66
+        params.put("snippet", options.optString("snippet"));
  67
+        params.put("max", options.has("max") ? Integer.toString(options.optInt("max")) : "");
  68
+        params.put("token", options.optString("token"));
  69
+        return request("GET", key, null, params);
  70
+    }
71 71
       
72  
-	private JSONObject request(String method, String key, final JSONObject doc, 
73  
-							   HashMap<String, String> params) throws IOException, JSONException{
74  
-		if(params == null){
75  
-			params = new HashMap<String, String>();
76  
-		}
77  
-		params.put("oauth_token", oauth_token);
78  
-		URL url = url(key, params);
79  
-		//System.out.println("URL " + url.toString() + " BODY " + (doc == null ? "" : doc.toString()));
80  
-		HttpsURLConnection con = (HttpsURLConnection)url.openConnection();
81  
-		con.setRequestMethod(method);
82  
-		con.setRequestProperty("Content-Type", CONTENT_TYPE);
83  
-		con.setRequestProperty("User-Agent", USER_AGENT);
84  
-		con.setUseCaches(false);
85  
-		con.setDoInput(true);
86  
-		con.setDoOutput(true);
87  
-		dump(con, doc);
88  
-		return load(con);
89  
-	}
  72
+    private JSONObject request(String method, String key, final JSONObject doc, 
  73
+                               HashMap<String, String> params) throws IOException, JSONException{
  74
+        if(params == null){
  75
+            params = new HashMap<String, String>();
  76
+        }
  77
+        params.put("oauth_token", oauth_token);
  78
+        URL url = url(key, params);
  79
+        //System.out.println("URL " + url.toString() + " BODY " + (doc == null ? "" : doc.toString()));
  80
+        HttpsURLConnection con = (HttpsURLConnection)url.openConnection();
  81
+        con.setRequestMethod(method);
  82
+        con.setRequestProperty("Content-Type", CONTENT_TYPE);
  83
+        con.setRequestProperty("User-Agent", USER_AGENT);
  84
+        con.setUseCaches(false);
  85
+        con.setDoInput(true);
  86
+        con.setDoOutput(true);
  87
+        dump(con, doc);
  88
+        return load(con);
  89
+    }
90 90
 
91  
-	private URL url(String key, HashMap<String, String> params) throws IOException{
92  
-		Vector<String> reqParams = new Vector<String>();
93  
-		for(String name : params.keySet()){
94  
-			String val = params.get(name);
95  
-			if((val != null) && (val.length() != 0)){
96  
-				reqParams.add(name + "=" + URLEncoder.encode(val, CHAR_SET));
97  
-			}
98  
-		}
99  
-		return new URL("https://" + username + '.' + ROOT_DOMAIN + "/" +
100  
-					   VERSION + key + "?" + join(reqParams, "&"));
101  
-	}
  91
+    private URL url(String key, HashMap<String, String> params) throws IOException{
  92
+        Vector<String> reqParams = new Vector<String>();
  93
+        for(String name : params.keySet()){
  94
+            String val = params.get(name);
  95
+            if((val != null) && (val.length() != 0)){
  96
+                reqParams.add(name + "=" + URLEncoder.encode(val, CHAR_SET));
  97
+            }
  98
+        }
  99
+        return new URL("https://" + username + '.' + ROOT_DOMAIN + "/" +
  100
+                       VERSION + key + "?" + join(reqParams, "&"));
  101
+    }
102 102
 
103  
-	private String join(Vector<String> vals, String delimiter){
104  
-	    Iterator<String> iter = vals.iterator();
105  
-		if (!iter.hasNext()){
106  
-			return "";
107  
-		}
108  
-		StringBuffer buffer = new StringBuffer(String.valueOf(iter.next()));
109  
-		while (iter.hasNext()){
110  
-			buffer.append(delimiter).append(String.valueOf(iter.next()));
111  
-		}
112  
-		return buffer.toString();
113  
-	}
114  
-		
115  
-	private void dump(HttpsURLConnection con, JSONObject doc) throws IOException{
116  
-	    if(doc != null){
  103
+    private String join(Vector<String> vals, String delimiter){
  104
+        Iterator<String> iter = vals.iterator();
  105
+        if (!iter.hasNext()){
  106
+            return "";
  107
+        }
  108
+        StringBuffer buffer = new StringBuffer(String.valueOf(iter.next()));
  109
+        while (iter.hasNext()){
  110
+            buffer.append(delimiter).append(String.valueOf(iter.next()));
  111
+        }
  112
+        return buffer.toString();
  113
+    }
  114
+        
  115
+    private void dump(HttpsURLConnection con, JSONObject doc) throws IOException{
  116
+        if(doc != null){
117 117
             OutputStream out = con.getOutputStream();
118 118
             try{
119 119
                 out.write(doc.toString().getBytes(CHAR_SET));
@@ -124,31 +124,31 @@ private void dump(HttpsURLConnection con, JSONObject doc) throws IOException{
124 124
                 out.close();
125 125
             }
126 126
         }
127  
-	}
128  
-	
129  
-	private JSONObject load(HttpsURLConnection con) throws IOException, JSONException{
130  
-		switch(con.getResponseCode()){
131  
-			case HttpsURLConnection.HTTP_OK:
132  
-				BufferedReader rd = new BufferedReader(new InputStreamReader(con.getInputStream()));
133  
-				try{
134  
-					String line;
135  
-					StringBuffer response = new StringBuffer(); 
136  
-					while((line = rd.readLine()) != null) {
137  
-						response.append(line);
138  
-					}
139  
-					return new JSONObject(response.toString());
140  
-				}
141  
-				finally{
142  
-					rd.close();
143  
-				}
144  
-			case HttpsURLConnection.HTTP_ACCEPTED: 
145  
-				return null;
146  
-			case HttpsURLConnection.HTTP_NOT_FOUND:
147  
-			    return null;
148  
-			default: 
149  
-				throw new IOException(con.getResponseMessage());
150  
-		}
151  
-	}
152  
-	
  127
+    }
  128
+    
  129
+    private JSONObject load(HttpsURLConnection con) throws IOException, JSONException{
  130
+        switch(con.getResponseCode()){
  131
+            case HttpsURLConnection.HTTP_OK:
  132
+                BufferedReader rd = new BufferedReader(new InputStreamReader(con.getInputStream()));
  133
+                try{
  134
+                    String line;
  135
+                    StringBuffer response = new StringBuffer(); 
  136
+                    while((line = rd.readLine()) != null) {
  137
+                        response.append(line);
  138
+                    }
  139
+                    return new JSONObject(response.toString());
  140
+                }
  141
+                finally{
  142
+                    rd.close();
  143
+                }
  144
+            case HttpsURLConnection.HTTP_ACCEPTED: 
  145
+                return null;
  146
+            case HttpsURLConnection.HTTP_NOT_FOUND:
  147
+                return null;
  148
+            default: 
  149
+                throw new IOException(con.getResponseMessage());
  150
+        }
  151
+    }
  152
+    
153 153
 }
154 154
 

0 notes on commit 050efb5

Please sign in to comment.
Something went wrong with that request. Please try again.