Skip to content
Open

Util #17

Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# JWTs for Java

This is not an officially supported Google product

[![CircleCI](https://img.shields.io/circleci/project/github/auth0/java-jwt.svg?style=flat-square)](https://circleci.com/gh/auth0/java-jwt/tree/master)
Expand Down
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;
}
}