Skip to content

Commit c900fae

Browse files
committed
improved connectors GET and fixed bug in ContentBuilder
1 parent 63d8ec6 commit c900fae

File tree

3 files changed

+26
-9
lines changed

3 files changed

+26
-9
lines changed

src/main/java/net/b07z/sepia/server/core/tools/Connectors.java

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -268,15 +268,27 @@ public static JSONObject httpGET(String url) {
268268
/**
269269
* Make HTTP GET request to URL and get JSON response. Check with {@code httpSuccess(...)} for status.
270270
* @param url - URL address to call including none or only some parameters
271-
* @param params - additional parameters added to URL (use e.g. "?q=search_term" or "&type=json" etc.)
271+
* @param params - additional parameters added to URL (use e.g. "?q=search_term" or "&type=json" etc.) or null
272272
* @return JSONObject response of URL call - Note: if response is not JSON it will be placed e.g. as "STRING" field in the result or "JSONARRAY" if it's an array.
273273
*/
274274
public static JSONObject httpGET(String url, String[] params) {
275+
return httpGET(url, params, null);
276+
}
277+
/**
278+
* Make HTTP GET request to URL and get JSON response. Check with {@code httpSuccess(...)} for status.
279+
* @param url - URL address to call including none or only some parameters
280+
* @param params - additional parameters added to URL (use e.g. "?q=search_term" or "&type=json" etc.) or null
281+
* @param headers - Map with request properties (keys) and values.
282+
* @return JSONObject response of URL call - Note: if response is not JSON it will be placed e.g. as "STRING" field in the result or "JSONARRAY" if it's an array.
283+
*/
284+
public static JSONObject httpGET(String url, String[] params, Map<String, String> headers) {
275285
int responseCode = -1;
276286
String success_str = HTTP_REST_SUCCESS;
277287
try{
278-
for (String s : params){
279-
url = url + s;
288+
if (params != null){
289+
for (String s : params){
290+
url = url + s;
291+
}
280292
}
281293
URL obj = new URL(url);
282294
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
@@ -285,6 +297,13 @@ public static JSONObject httpGET(String url, String[] params) {
285297
con.setRequestProperty("User-Agent", USER_AGENT);
286298
con.setConnectTimeout(CONNECT_TIMEOUT);
287299
con.setReadTimeout(READ_TIMEOUT);
300+
301+
if (headers != null){
302+
for (Map.Entry<String, String> entry : headers.entrySet()){
303+
con.setRequestProperty(entry.getKey(), entry.getValue());
304+
//System.out.println(entry.getKey() +": "+ entry.getValue());
305+
}
306+
}
288307

289308
responseCode = con.getResponseCode();
290309
//System.out.println("GET Response Code : " + responseCode); //debug
@@ -377,8 +396,7 @@ public static JSONObject httpPOST(String targetURL, String data, Map<String, Str
377396
headers.put("Content-Type", "application/json");
378397
headers.put("Content-Length", Integer.toString(data.getBytes().length));
379398
}
380-
for (Map.Entry<String, String> entry : headers.entrySet())
381-
{
399+
for (Map.Entry<String, String> entry : headers.entrySet()){
382400
connection.setRequestProperty(entry.getKey(), entry.getValue());
383401
//System.out.println(entry.getKey() +": "+ entry.getValue());
384402
}
@@ -467,8 +485,7 @@ public static JSONObject httpPUT(String targetURL, String data, Map<String, Stri
467485
connection.setConnectTimeout(CONNECT_TIMEOUT);
468486
connection.setReadTimeout(READ_TIMEOUT);
469487
//System.out.println("---headers---");
470-
for (Map.Entry<String, String> entry : headers.entrySet())
471-
{
488+
for (Map.Entry<String, String> entry : headers.entrySet()){
472489
connection.setRequestProperty(entry.getKey(), entry.getValue());
473490
//System.out.println(entry.getKey() +": "+ entry.getValue());
474491
}

src/main/java/net/b07z/sepia/server/core/tools/ContentBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public class ContentBuilder {
2020
*/
2121
public static String postForm(Object... keyValues){
2222
try {
23-
String parameters = keyValues[0] + "=" + keyValues[1];
23+
String parameters = keyValues[0] + "=" + URLEncoder.encode(keyValues[1].toString(), "UTF-8");
2424
int i = 2;
2525
while (i < keyValues.length){
2626
parameters += "&" + (keyValues[i] + "=" + URLEncoder.encode(keyValues[i+1].toString(), "UTF-8"));

src/main/java/net/b07z/sepia/server/core/tools/JSON.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ public static long getLongOrDefault(JSONObject jObj, String key, long defaultVal
211211
*
212212
* @param j_obj - JSONObject input
213213
* @param key - key name of the entry you want, ...get("key")
214-
* @return value as string or empty string (or null of value is actually null)
214+
* @return value as string or empty string (or null if value is actually null)
215215
*/
216216
public static String getString(JSONObject j_obj, String key){
217217
if (j_obj != null && j_obj.containsKey(key)){

0 commit comments

Comments
 (0)