diff --git a/java/3p-resources/pom.xml b/java/3p-resources/pom.xml index f8ac14b..251d5d3 100644 --- a/java/3p-resources/pom.xml +++ b/java/3p-resources/pom.xml @@ -40,6 +40,11 @@ limitations under the License. gson 2.9.1 + + org.apache.httpcomponents + httpclient + 4.5.1 + diff --git a/java/3p-resources/src/main/java/Create3pResources.java b/java/3p-resources/src/main/java/Create3pResources.java index 65821dc..923b6a5 100644 --- a/java/3p-resources/src/main/java/Create3pResources.java +++ b/java/3p-resources/src/main/java/Create3pResources.java @@ -15,12 +15,12 @@ */ // [START add_ons_3p_resources] -import java.io.UnsupportedEncodingException; -import java.net.URLEncoder; import java.util.Arrays; import java.util.HashMap; import java.util.Map; +import org.apache.http.client.utils.URIBuilder; + import com.google.cloud.functions.HttpFunction; import com.google.cloud.functions.HttpRequest; import com.google.cloud.functions.HttpResponse; @@ -223,7 +223,7 @@ JsonObject createCaseInputCard(JsonObject event, Map errors, boo * @param event The event object containing form inputs. * @return The navigation action. */ - JsonObject submitCaseCreationForm(JsonObject event) throws UnsupportedEncodingException{ + JsonObject submitCaseCreationForm(JsonObject event) throws Exception { JsonObject formInputs = event.getAsJsonObject("commonEventObject").getAsJsonObject("formInputs"); Map caseDetails = new HashMap(); if (formInputs != null) { @@ -246,8 +246,11 @@ JsonObject submitCaseCreationForm(JsonObject event) throws UnsupportedEncodingEx return createCaseInputCard(event, errors, /* isUpdate= */ true); } else { String title = String.format("Case %s", caseDetails.get("name")); - String url = "https://example.com/support/cases/" + URLEncoder.encode(gson.toJson(caseDetails), "UTF-8").replaceAll("\\+", "%20").replaceAll("\\%21", "!").replaceAll("\\%27", "'").replaceAll("\\%28", "(").replaceAll("\\%29", ")").replaceAll("\\%7E", "~"); - return createLinkRenderAction(title, url); + URIBuilder uriBuilder = new URIBuilder("https://example.com/support/cases/"); + for (String caseDetailKey : caseDetails.keySet()) { + uriBuilder.addParameter(caseDetailKey, caseDetails.get(caseDetailKey)); + } + return createLinkRenderAction(title, uriBuilder.build().toURL().toString()); } } diff --git a/java/3p-resources/src/main/java/CreateLinkPreview.java b/java/3p-resources/src/main/java/CreateLinkPreview.java index a73203b..41b13d4 100644 --- a/java/3p-resources/src/main/java/CreateLinkPreview.java +++ b/java/3p-resources/src/main/java/CreateLinkPreview.java @@ -26,6 +26,8 @@ import java.io.UnsupportedEncodingException; import java.net.URL; import java.net.URLDecoder; +import java.util.HashMap; +import java.util.Map; public class CreateLinkPreview implements HttpFunction { private static final Gson gson = new Gson(); @@ -46,7 +48,7 @@ public void service(HttpRequest request, HttpResponse response) throws Exception URL parsedURL = new URL(url); if ("example.com".equals(parsedURL.getHost())) { if (parsedURL.getPath().startsWith("/support/cases/")) { - response.getWriter().write(gson.toJson(caseLinkPreview(url))); + response.getWriter().write(gson.toJson(caseLinkPreview(parsedURL))); return; } @@ -67,17 +69,18 @@ public void service(HttpRequest request, HttpResponse response) throws Exception * @param url A URL. * @return A case link preview card. */ - JsonObject caseLinkPreview(String url) throws UnsupportedEncodingException { - String[] segments = url.split("/"); - JsonObject caseDetails = gson.fromJson(URLDecoder.decode(segments[segments.length - 1].replace("+", "%2B"), "UTF-8").replace("%2B", "+"), JsonObject.class); - String caseName = String.format("Case %s", caseDetails.get("name").getAsString()); - String caseDescription = caseDetails.get("description").getAsString(); + JsonObject caseLinkPreview(URL url) throws UnsupportedEncodingException { + Map caseDetails = new HashMap(); + for (String pair : url.getQuery().split("&")) { + caseDetails.put(URLDecoder.decode(pair.split("=")[0], "UTF-8"), URLDecoder.decode(pair.split("=")[1], "UTF-8")); + } JsonObject cardHeader = new JsonObject(); + String caseName = String.format("Case %s", caseDetails.get("name")); cardHeader.add("title", new JsonPrimitive(caseName)); JsonObject textParagraph = new JsonObject(); - textParagraph.add("text", new JsonPrimitive(caseDescription)); + textParagraph.add("text", new JsonPrimitive(caseDetails.get("description"))); JsonObject widget = new JsonObject(); widget.add("textParagraph", textParagraph);