From ea84d8cc3e5411a2c0f7cb3fef7c03f2cafc6d5e Mon Sep 17 00:00:00 2001 From: DOHA Date: Sun, 22 Feb 2015 22:55:59 +0200 Subject: [PATCH 1/3] handle submit response --- .../org/baeldung/web/RedditController.java | 29 +++++++++++++------ .../webapp/WEB-INF/jsp/submissionResponse.jsp | 12 ++++++++ 2 files changed, 32 insertions(+), 9 deletions(-) create mode 100755 spring-security-oauth/src/main/webapp/WEB-INF/jsp/submissionResponse.jsp diff --git a/spring-security-oauth/src/main/java/org/baeldung/web/RedditController.java b/spring-security-oauth/src/main/java/org/baeldung/web/RedditController.java index 1ce1417159..4e4719a02f 100644 --- a/spring-security-oauth/src/main/java/org/baeldung/web/RedditController.java +++ b/spring-security-oauth/src/main/java/org/baeldung/web/RedditController.java @@ -64,16 +64,17 @@ public class RedditController { System.out.println(param.keySet()); System.out.println(param.entrySet()); ResponseEntity result = redditRestTemplate.postForEntity("https://oauth.reddit.com/api/submit", req, String.class, param); - model.addAttribute("error", result.getBody()); + String responseMsg = parseResponseMessage(result.getBody()); + model.addAttribute("msg", responseMsg); } catch (UserApprovalRequiredException e) { throw e; } catch (UserRedirectRequiredException e) { throw e; } catch (Exception e) { LOGGER.error("Error occurred", e); - model.addAttribute("error", e.getLocalizedMessage()); + model.addAttribute("msg", e.getLocalizedMessage()); } - return "reddit"; + return "submissionResponse"; } @RequestMapping("/post") @@ -81,10 +82,8 @@ public class RedditController { try { String needsCaptchaResult = needsCaptcha(); if (needsCaptchaResult.equalsIgnoreCase("true")) { - String newCaptchaResult = getNewCaptcha(); - String[] split = newCaptchaResult.split("\""); - String iden = split[split.length - 2]; - model.addAttribute("iden", iden.trim()); + String iden = getNewCaptcha(); + model.addAttribute("iden", iden); } } catch (UserApprovalRequiredException e) { throw e; @@ -125,11 +124,23 @@ public class RedditController { param.put("api_type", "json"); ResponseEntity result = redditRestTemplate.postForEntity("https://oauth.reddit.com/api/new_captcha", req, String.class, param); - return result.getBody(); + String[] split = result.getBody().split("\""); + return split[split.length - 2]; + } + + private String parseResponseMessage(String responseBody) { + System.out.println(responseBody); + int index = responseBody.indexOf("error"); + if (index == -1) { + return "Post submitted successfully"; + } else { + int msgEnd = responseBody.indexOf("\"", index); + return responseBody.substring(index, msgEnd); + } } public void setRedditRestTemplate(OAuth2RestTemplate redditRestTemplate) { this.redditRestTemplate = redditRestTemplate; } -} \ No newline at end of file +} diff --git a/spring-security-oauth/src/main/webapp/WEB-INF/jsp/submissionResponse.jsp b/spring-security-oauth/src/main/webapp/WEB-INF/jsp/submissionResponse.jsp new file mode 100755 index 0000000000..b337691e35 --- /dev/null +++ b/spring-security-oauth/src/main/webapp/WEB-INF/jsp/submissionResponse.jsp @@ -0,0 +1,12 @@ +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> + + + +Spring Security OAuth + + +

${msg}

+Submit another link to Reddit + + + \ No newline at end of file From 04bd9e72edf9d1c486de0f2b21a21c3df250b454 Mon Sep 17 00:00:00 2001 From: DOHA Date: Mon, 23 Feb 2015 14:16:17 +0200 Subject: [PATCH 2/3] Fix reddit submit --- .../org/baeldung/web/RedditController.java | 52 +++++++++++-------- .../src/main/webapp/WEB-INF/jsp/reddit.jsp | 6 ++- .../webapp/WEB-INF/jsp/submissionResponse.jsp | 7 ++- .../src/main/webapp/index.jsp | 5 +- 4 files changed, 43 insertions(+), 27 deletions(-) diff --git a/spring-security-oauth/src/main/java/org/baeldung/web/RedditController.java b/spring-security-oauth/src/main/java/org/baeldung/web/RedditController.java index 4e4719a02f..292549363b 100644 --- a/spring-security-oauth/src/main/java/org/baeldung/web/RedditController.java +++ b/spring-security-oauth/src/main/java/org/baeldung/web/RedditController.java @@ -17,6 +17,8 @@ import org.springframework.security.oauth2.client.resource.UserApprovalRequiredE import org.springframework.security.oauth2.client.resource.UserRedirectRequiredException; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; +import org.springframework.util.LinkedMultiValueMap; +import org.springframework.util.MultiValueMap; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; @@ -43,7 +45,7 @@ public class RedditController { throw e; } catch (Exception e) { LOGGER.error("Error occurred", e); - model.addAttribute("error", e.getLocalizedMessage()); + model.addAttribute("error", e.getMessage()); } return "reddit"; } @@ -51,20 +53,21 @@ public class RedditController { @RequestMapping("/submit") public String submit(Model model, @RequestParam Map formParams) { try { - System.out.println(formParams.keySet()); - HttpHeaders headers = new HttpHeaders(); - headers.setContentType(MediaType.APPLICATION_JSON); - HttpEntity req = new HttpEntity(headers); + MultiValueMap param = new LinkedMultiValueMap(); + param.add("api_type", "json"); + param.add("kind", "link"); + param.add("resubmit", "true"); + param.add("sendreplies", "false"); + param.add("then", "comments"); - Map param = new HashMap(); - param.put("api_type", "json"); - param.put("kind", "link"); - param.putAll(formParams); + for (Map.Entry entry : formParams.entrySet()) { + param.add(entry.getKey(), entry.getValue()); + } - System.out.println(param.keySet()); - System.out.println(param.entrySet()); - ResponseEntity result = redditRestTemplate.postForEntity("https://oauth.reddit.com/api/submit", req, String.class, param); - String responseMsg = parseResponseMessage(result.getBody()); + LOGGER.info("User submitting Link with these parameters: " + formParams.entrySet()); + String result = redditRestTemplate.postForObject("https://oauth.reddit.com/api/submit", param, String.class); + LOGGER.info("Full Reddit Response: " + result); + String responseMsg = parseResponse(result); model.addAttribute("msg", responseMsg); } catch (UserApprovalRequiredException e) { throw e; @@ -97,7 +100,7 @@ public class RedditController { return "submissionForm"; } - // + // === private private List getSubreddit() throws JsonProcessingException, IOException { String result = redditRestTemplate.getForObject("https://oauth.reddit.com/subreddits/popular?limit=50", String.class); @@ -128,15 +131,20 @@ public class RedditController { return split[split.length - 2]; } - private String parseResponseMessage(String responseBody) { - System.out.println(responseBody); - int index = responseBody.indexOf("error"); - if (index == -1) { - return "Post submitted successfully"; - } else { - int msgEnd = responseBody.indexOf("\"", index); - return responseBody.substring(index, msgEnd); + private String parseResponse(String responseBody) throws JsonProcessingException, IOException { + String result = ""; + JsonNode node = new ObjectMapper().readTree(responseBody); + JsonNode errorNode = node.get("json").get("errors").get(0); + for (JsonNode child : errorNode) { + result = result + child.toString().replaceAll("\"|null", "") + "
"; } + if (result.length() == 0) { + if (node.get("json").get("data") != null && node.get("json").get("data").get("url") != null) + return "Post submitted successfully check it out "; + else + return "Error Occurred"; + } + return result; } public void setRedditRestTemplate(OAuth2RestTemplate redditRestTemplate) { diff --git a/spring-security-oauth/src/main/webapp/WEB-INF/jsp/reddit.jsp b/spring-security-oauth/src/main/webapp/WEB-INF/jsp/reddit.jsp index bde4587fc4..dd2e0c486a 100755 --- a/spring-security-oauth/src/main/webapp/WEB-INF/jsp/reddit.jsp +++ b/spring-security-oauth/src/main/webapp/WEB-INF/jsp/reddit.jsp @@ -3,14 +3,17 @@ Spring Security OAuth + + +

Your Reddit Info

Your reddit username is ${info}


- Submit to Reddit + Submit to Reddit
Sorry, error occurred @@ -18,5 +21,6 @@
${error}
+
\ No newline at end of file diff --git a/spring-security-oauth/src/main/webapp/WEB-INF/jsp/submissionResponse.jsp b/spring-security-oauth/src/main/webapp/WEB-INF/jsp/submissionResponse.jsp index b337691e35..ef7243984f 100755 --- a/spring-security-oauth/src/main/webapp/WEB-INF/jsp/submissionResponse.jsp +++ b/spring-security-oauth/src/main/webapp/WEB-INF/jsp/submissionResponse.jsp @@ -3,10 +3,13 @@ Spring Security OAuth + + + \ No newline at end of file diff --git a/spring-security-oauth/src/main/webapp/index.jsp b/spring-security-oauth/src/main/webapp/index.jsp index 244927908f..a3e546464a 100755 --- a/spring-security-oauth/src/main/webapp/index.jsp +++ b/spring-security-oauth/src/main/webapp/index.jsp @@ -5,11 +5,12 @@ Spring Security OAuth + - +

Welcome to Spring Security OAuth

-Login with Reddit +Login with Reddit \ No newline at end of file From 3b9ded093e45475133fca3c8a80e3ed240e1c1f2 Mon Sep 17 00:00:00 2001 From: DOHA Date: Mon, 23 Feb 2015 15:28:11 +0200 Subject: [PATCH 3/3] minor fix --- .../main/java/org/baeldung/web/RedditController.java | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/spring-security-oauth/src/main/java/org/baeldung/web/RedditController.java b/spring-security-oauth/src/main/java/org/baeldung/web/RedditController.java index 292549363b..ef80ff1747 100644 --- a/spring-security-oauth/src/main/java/org/baeldung/web/RedditController.java +++ b/spring-security-oauth/src/main/java/org/baeldung/web/RedditController.java @@ -135,16 +135,17 @@ public class RedditController { String result = ""; JsonNode node = new ObjectMapper().readTree(responseBody); JsonNode errorNode = node.get("json").get("errors").get(0); - for (JsonNode child : errorNode) { - result = result + child.toString().replaceAll("\"|null", "") + "
"; - } - if (result.length() == 0) { + if (errorNode != null) { + for (JsonNode child : errorNode) { + result = result + child.toString().replaceAll("\"|null", "") + "
"; + } + return result; + } else { if (node.get("json").get("data") != null && node.get("json").get("data").get("url") != null) return "Post submitted successfully check it out "; else return "Error Occurred"; } - return result; } public void setRedditRestTemplate(OAuth2RestTemplate redditRestTemplate) {