Skip to content

Commit ea84d8c

Browse files
committed
handle submit response
1 parent 8671a05 commit ea84d8c

2 files changed

Lines changed: 32 additions & 9 deletions

File tree

‎spring-security-oauth/src/main/java/org/baeldung/web/RedditController.java‎

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -64,27 +64,26 @@ public String submit(Model model, @RequestParam Map<String, String> formParams)
6464
System.out.println(param.keySet());
6565
System.out.println(param.entrySet());
6666
ResponseEntity<String> result = redditRestTemplate.postForEntity("https://oauth.reddit.com/api/submit", req, String.class, param);
67-
model.addAttribute("error", result.getBody());
67+
String responseMsg = parseResponseMessage(result.getBody());
68+
model.addAttribute("msg", responseMsg);
6869
} catch (UserApprovalRequiredException e) {
6970
throw e;
7071
} catch (UserRedirectRequiredException e) {
7172
throw e;
7273
} catch (Exception e) {
7374
LOGGER.error("Error occurred", e);
74-
model.addAttribute("error", e.getLocalizedMessage());
75+
model.addAttribute("msg", e.getLocalizedMessage());
7576
}
76-
return "reddit";
77+
return "submissionResponse";
7778
}
7879

7980
@RequestMapping("/post")
8081
public String showSubmissionForm(Model model) throws JsonProcessingException, IOException {
8182
try {
8283
String needsCaptchaResult = needsCaptcha();
8384
if (needsCaptchaResult.equalsIgnoreCase("true")) {
84-
String newCaptchaResult = getNewCaptcha();
85-
String[] split = newCaptchaResult.split("\"");
86-
String iden = split[split.length - 2];
87-
model.addAttribute("iden", iden.trim());
85+
String iden = getNewCaptcha();
86+
model.addAttribute("iden", iden);
8887
}
8988
} catch (UserApprovalRequiredException e) {
9089
throw e;
@@ -125,11 +124,23 @@ private String getNewCaptcha() {
125124
param.put("api_type", "json");
126125

127126
ResponseEntity<String> result = redditRestTemplate.postForEntity("https://oauth.reddit.com/api/new_captcha", req, String.class, param);
128-
return result.getBody();
127+
String[] split = result.getBody().split("\"");
128+
return split[split.length - 2];
129+
}
130+
131+
private String parseResponseMessage(String responseBody) {
132+
System.out.println(responseBody);
133+
int index = responseBody.indexOf("error");
134+
if (index == -1) {
135+
return "Post submitted successfully";
136+
} else {
137+
int msgEnd = responseBody.indexOf("\"", index);
138+
return responseBody.substring(index, msgEnd);
139+
}
129140
}
130141

131142
public void setRedditRestTemplate(OAuth2RestTemplate redditRestTemplate) {
132143
this.redditRestTemplate = redditRestTemplate;
133144
}
134145

135-
}
146+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
2+
<html xmlns="http://www.w3.org/1999/xhtml">
3+
<head>
4+
5+
<title>Spring Security OAuth</title>
6+
</head>
7+
<body>
8+
<h1>${msg}</h1>
9+
<a href="post">Submit another link to Reddit</a>
10+
11+
</body>
12+
</html>

0 commit comments

Comments
 (0)