Skip to content

Commit

Permalink
Upgrade from json encoded string to URL params in Java
Browse files Browse the repository at this point in the history
  • Loading branch information
pierrick committed Jan 20, 2024
1 parent 4544efa commit 471a5d6
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 12 deletions.
5 changes: 5 additions & 0 deletions java/3p-resources/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ limitations under the License.
<artifactId>gson</artifactId>
<version>2.9.1</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.1</version>
</dependency>
</dependencies>

<!-- Required for Java 11 functions in the inline editor -->
Expand Down
13 changes: 8 additions & 5 deletions java/3p-resources/src/main/java/Create3pResources.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -223,7 +223,7 @@ JsonObject createCaseInputCard(JsonObject event, Map<String, String> 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<String, String> caseDetails = new HashMap<String, String>();
if (formInputs != null) {
Expand All @@ -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());
}
}

Expand Down
17 changes: 10 additions & 7 deletions java/3p-resources/src/main/java/CreateLinkPreview.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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;
}

Expand All @@ -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<String, String> caseDetails = new HashMap<String, String>();
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);
Expand Down

0 comments on commit 471a5d6

Please sign in to comment.