Skip to content

Commit 04bd9e7

Browse files
committed
Fix reddit submit
1 parent ea84d8c commit 04bd9e7

4 files changed

Lines changed: 45 additions & 29 deletions

File tree

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

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
import org.springframework.security.oauth2.client.resource.UserRedirectRequiredException;
1818
import org.springframework.stereotype.Controller;
1919
import org.springframework.ui.Model;
20+
import org.springframework.util.LinkedMultiValueMap;
21+
import org.springframework.util.MultiValueMap;
2022
import org.springframework.web.bind.annotation.RequestMapping;
2123
import org.springframework.web.bind.annotation.RequestParam;
2224

@@ -43,28 +45,29 @@ public String getInfo(Model model) {
4345
throw e;
4446
} catch (Exception e) {
4547
LOGGER.error("Error occurred", e);
46-
model.addAttribute("error", e.getLocalizedMessage());
48+
model.addAttribute("error", e.getMessage());
4749
}
4850
return "reddit";
4951
}
5052

5153
@RequestMapping("/submit")
5254
public String submit(Model model, @RequestParam Map<String, String> formParams) {
5355
try {
54-
System.out.println(formParams.keySet());
55-
HttpHeaders headers = new HttpHeaders();
56-
headers.setContentType(MediaType.APPLICATION_JSON);
57-
HttpEntity req = new HttpEntity(headers);
58-
59-
Map<String, String> param = new HashMap<String, String>();
60-
param.put("api_type", "json");
61-
param.put("kind", "link");
62-
param.putAll(formParams);
63-
64-
System.out.println(param.keySet());
65-
System.out.println(param.entrySet());
66-
ResponseEntity<String> result = redditRestTemplate.postForEntity("https://oauth.reddit.com/api/submit", req, String.class, param);
67-
String responseMsg = parseResponseMessage(result.getBody());
56+
MultiValueMap<String, String> param = new LinkedMultiValueMap<String, String>();
57+
param.add("api_type", "json");
58+
param.add("kind", "link");
59+
param.add("resubmit", "true");
60+
param.add("sendreplies", "false");
61+
param.add("then", "comments");
62+
63+
for (Map.Entry<String, String> entry : formParams.entrySet()) {
64+
param.add(entry.getKey(), entry.getValue());
65+
}
66+
67+
LOGGER.info("User submitting Link with these parameters: " + formParams.entrySet());
68+
String result = redditRestTemplate.postForObject("https://oauth.reddit.com/api/submit", param, String.class);
69+
LOGGER.info("Full Reddit Response: " + result);
70+
String responseMsg = parseResponse(result);
6871
model.addAttribute("msg", responseMsg);
6972
} catch (UserApprovalRequiredException e) {
7073
throw e;
@@ -97,7 +100,7 @@ public String showSubmissionForm(Model model) throws JsonProcessingException, IO
97100
return "submissionForm";
98101
}
99102

100-
//
103+
// === private
101104

102105
private List<String> getSubreddit() throws JsonProcessingException, IOException {
103106
String result = redditRestTemplate.getForObject("https://oauth.reddit.com/subreddits/popular?limit=50", String.class);
@@ -128,15 +131,20 @@ private String getNewCaptcha() {
128131
return split[split.length - 2];
129132
}
130133

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);
134+
private String parseResponse(String responseBody) throws JsonProcessingException, IOException {
135+
String result = "";
136+
JsonNode node = new ObjectMapper().readTree(responseBody);
137+
JsonNode errorNode = node.get("json").get("errors").get(0);
138+
for (JsonNode child : errorNode) {
139+
result = result + child.toString().replaceAll("\"|null", "") + "<br>";
139140
}
141+
if (result.length() == 0) {
142+
if (node.get("json").get("data") != null && node.get("json").get("data").get("url") != null)
143+
return "Post submitted successfully <a href=\"" + node.get("json").get("data").get("url").asText() + "\"> check it out </a>";
144+
else
145+
return "Error Occurred";
146+
}
147+
return result;
140148
}
141149

142150
public void setRedditRestTemplate(OAuth2RestTemplate redditRestTemplate) {

‎spring-security-oauth/src/main/webapp/WEB-INF/jsp/reddit.jsp‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,24 @@
33
<head>
44

55
<title>Spring Security OAuth</title>
6+
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css">
7+
68
</head>
79
<body>
10+
<div class="container">
811
<c:choose>
912
<c:when test="${info != null}">
1013
<h1>Your Reddit Info</h1>
1114
<b>Your reddit username is </b>${info}
1215
<br><br><br>
13-
<a href="post">Submit to Reddit</a>
16+
<a href="post" class="btn btn-primary">Submit to Reddit</a>
1417
</c:when>
1518
<c:otherwise>
1619
<b>Sorry, error occurred</b>
1720
<br><br>
1821
<div>${error}</div>
1922
</c:otherwise>
2023
</c:choose>
24+
</div>
2125
</body>
2226
</html>

‎spring-security-oauth/src/main/webapp/WEB-INF/jsp/submissionResponse.jsp‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@
33
<head>
44

55
<title>Spring Security OAuth</title>
6+
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css">
7+
68
</head>
79
<body>
10+
<div class="container">
811
<h1>${msg}</h1>
9-
<a href="post">Submit another link to Reddit</a>
10-
12+
<a href="post" class="btn btn-primary">Submit another link to Reddit</a>
13+
</div>
1114
</body>
1215
</html>

‎spring-security-oauth/src/main/webapp/index.jsp‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@
55
<head>
66
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
77
<title>Spring Security OAuth</title>
8+
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css">
89
</head>
910
<body>
10-
11+
<div class="container">
1112
<h1>Welcome to Spring Security OAuth</h1>
12-
<a href="info">Login with Reddit</a>
13+
<a href="info" class="btn btn-primary">Login with Reddit</a>
1314

1415
</body>
1516
</html>

0 commit comments

Comments
 (0)