Skip to content

Commit 7ae4b80

Browse files
author
Eugen
committed
Merge pull request eugenp#149 from Doha2012/master
fix submit reddit
2 parents b79277e + 3b9ded0 commit 7ae4b80

File tree

4 files changed

+67
-27
lines changed

4 files changed

+67
-27
lines changed

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

Lines changed: 44 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,48 +45,48 @@ 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-
model.addAttribute("error", 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);
71+
model.addAttribute("msg", responseMsg);
6872
} catch (UserApprovalRequiredException e) {
6973
throw e;
7074
} catch (UserRedirectRequiredException e) {
7175
throw e;
7276
} catch (Exception e) {
7377
LOGGER.error("Error occurred", e);
74-
model.addAttribute("error", e.getLocalizedMessage());
78+
model.addAttribute("msg", e.getLocalizedMessage());
7579
}
76-
return "reddit";
80+
return "submissionResponse";
7781
}
7882

7983
@RequestMapping("/post")
8084
public String showSubmissionForm(Model model) throws JsonProcessingException, IOException {
8185
try {
8286
String needsCaptchaResult = needsCaptcha();
8387
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());
88+
String iden = getNewCaptcha();
89+
model.addAttribute("iden", iden);
8890
}
8991
} catch (UserApprovalRequiredException e) {
9092
throw e;
@@ -98,7 +100,7 @@ public String showSubmissionForm(Model model) throws JsonProcessingException, IO
98100
return "submissionForm";
99101
}
100102

101-
//
103+
// === private
102104

103105
private List<String> getSubreddit() throws JsonProcessingException, IOException {
104106
String result = redditRestTemplate.getForObject("https://oauth.reddit.com/subreddits/popular?limit=50", String.class);
@@ -125,11 +127,29 @@ private String getNewCaptcha() {
125127
param.put("api_type", "json");
126128

127129
ResponseEntity<String> result = redditRestTemplate.postForEntity("https://oauth.reddit.com/api/new_captcha", req, String.class, param);
128-
return result.getBody();
130+
String[] split = result.getBody().split("\"");
131+
return split[split.length - 2];
132+
}
133+
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+
if (errorNode != null) {
139+
for (JsonNode child : errorNode) {
140+
result = result + child.toString().replaceAll("\"|null", "") + "<br>";
141+
}
142+
return result;
143+
} else {
144+
if (node.get("json").get("data") != null && node.get("json").get("data").get("url") != null)
145+
return "Post submitted successfully <a href=\"" + node.get("json").get("data").get("url").asText() + "\"> check it out </a>";
146+
else
147+
return "Error Occurred";
148+
}
129149
}
130150

131151
public void setRedditRestTemplate(OAuth2RestTemplate redditRestTemplate) {
132152
this.redditRestTemplate = redditRestTemplate;
133153
}
134154

135-
}
155+
}

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>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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+
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css">
7+
8+
</head>
9+
<body>
10+
<div class="container">
11+
<h1>${msg}</h1>
12+
<a href="post" class="btn btn-primary">Submit another link to Reddit</a>
13+
</div>
14+
</body>
15+
</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)