From b58d4ba85a510a16994376f4f6e00ffcbcf61d53 Mon Sep 17 00:00:00 2001 From: Justin Dahmubed Date: Fri, 8 Dec 2017 15:09:33 -0800 Subject: [PATCH 1/2] Not officially supported Google product --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 487c06d..c341369 100644 --- a/README.md +++ b/README.md @@ -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) From 306735a670505ae9304c1cf1546f61f326ba004e Mon Sep 17 00:00:00 2001 From: Justin Dahmubed Date: Tue, 27 Feb 2018 11:56:38 -0800 Subject: [PATCH 2/2] Util --- .../oicclient/exceptions/UnsupportedType.java | 7 ++ lib/src/main/java/oicclient/util/Util.java | 89 +++++++++++++++++++ 2 files changed, 96 insertions(+) create mode 100644 lib/src/main/java/oicclient/exceptions/UnsupportedType.java create mode 100644 lib/src/main/java/oicclient/util/Util.java diff --git a/lib/src/main/java/oicclient/exceptions/UnsupportedType.java b/lib/src/main/java/oicclient/exceptions/UnsupportedType.java new file mode 100644 index 0000000..8b25879 --- /dev/null +++ b/lib/src/main/java/oicclient/exceptions/UnsupportedType.java @@ -0,0 +1,7 @@ +package oicclient.exceptions; + +public class UnsupportedType extends Exception{ + public UnsupportedType(String message) { + super(message); + } +} diff --git a/lib/src/main/java/oicclient/util/Util.java b/lib/src/main/java/oicclient/util/Util.java new file mode 100644 index 0000000..e411427 --- /dev/null +++ b/lib/src/main/java/oicclient/util/Util.java @@ -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 getOrPost(String uri, String method, Message request, String contentType, List accept, Map args) throws UnsupportedEncodingException, UnsupportedType, URISyntaxException { + Map 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 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 getOrPost(String uri, String method, Message request, Map args) throws UnsupportedEncodingException, UnsupportedType, URISyntaxException { + return getOrPost(uri, method, request, URL_ENCODED, null, args); + } + + private static Map splitQuery(String query) throws UnsupportedEncodingException { + Map queryPairs = new LinkedHashMap(); + 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; + } +}