Skip to content
Open

Util #17

Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Util
  • Loading branch information
jdahmubed committed Feb 27, 2018
commit 306735a670505ae9304c1cf1546f61f326ba004e
7 changes: 7 additions & 0 deletions lib/src/main/java/oicclient/exceptions/UnsupportedType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package oicclient.exceptions;

public class UnsupportedType extends Exception{
public UnsupportedType(String message) {
super(message);
}
}
89 changes: 89 additions & 0 deletions lib/src/main/java/oicclient/util/Util.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package oicclient.util;

import com.google.common.base.Strings;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Field;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import oicclient.exceptions.UnsupportedType;

public class Util {

private static final String URL_ENCODED = "application/x-www-form-urlencoded";
public static final String JSON_ENCODED = "application/json";

public static Map<String, Object> getOrPost(String uri, String method, Message request, String contentType, List<String> accept, Map<String, Object> args) throws UnsupportedEncodingException, UnsupportedType, URISyntaxException {
Map<String, Object> response = new HashMap<>();
String urlEncoded;
Field[] keys;
Message requestClone;
if (method.equals("GET") || method.equals("DELETE")) {
keys = request.getClass().getDeclaredFields();
if(keys != null) {
requestClone = request.clone();
URI url = new URI(uri);
String query = url.getQuery();
if(!Strings.isNullOrEmpty(query)) {
requestClone.update(splitQuery(query));
}

query = requestClone.toUrlEncoded();
String urlResponse = uri + "?" + query;
response.put("uri", urlResponse);
} else {
response.put("uri", uri);
}
} else if (method.equals("POST") || method.equals("PUT")) {
response.put("uri", uri);
if (contentType.equals(URL_ENCODED)) {
response.put("body", request.toUrlEncoded());
} else if (contentType.equals(JSON_ENCODED)) {
response.put("body", request.toJSON());
} else {
throw new UnsupportedType("Unsupported content type " + contentType);
}

Map<String, Object> headers = new HashMap<>();
headers.put("Content-Type", contentType);

if (accept != null && !accept.isEmpty()) {
headers = new HashMap<>();
headers.put("Accept", accept);
}
if (args.containsKey("headers")) {
if(args.get("headers") instanceof Message) {
((Message) args.get("headers")).update(headers);
} else {
throw new IllegalArgumentException("headers should be of type Message");
}
} else {
args.put("headers", headers);
}
response.put("args", args);
} else {
throw new UnsupportedType("Unsupported HTTP method " + method);
}

return response;
}

public static Map<String, Object> getOrPost(String uri, String method, Message request, Map<String, Object> args) throws UnsupportedEncodingException, UnsupportedType, URISyntaxException {
return getOrPost(uri, method, request, URL_ENCODED, null, args);
}

private static Map<String,String> splitQuery(String query) throws UnsupportedEncodingException {
Map<String, String> queryPairs = new LinkedHashMap<String, String>();
String[] pairs = query.split("&");
for (String pair : pairs) {
int idx = pair.indexOf("=");
queryPairs.put(URLDecoder.decode(pair.substring(0, idx), "UTF-8"), URLDecoder.decode(pair.substring(idx + 1), "UTF-8"));
}

return queryPairs;
}
}