Skip to content

Commit cf5c91d

Browse files
committed
Modify reddit submit method
1 parent 1dcf4a3 commit cf5c91d

3 files changed

Lines changed: 40 additions & 20 deletions

File tree

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

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,7 @@ public String submit(Model model, @RequestParam Map<String, String> formParams)
5858

5959
Map<String, String> param = new HashMap<String, String>();
6060
param.put("api_type", "json");
61-
param.put("kind", "self");
62-
param.put("sr", "api");
63-
// param.put("iden", "XCzyTdJveIcYXNhLJ4a2X9WVDswtx83u");
64-
// param.put("captcha", "BJMGMU");
65-
// param.put("title", "http2 is coming soon");
66-
// param.put("text", "http2 is coming soon what do you think about that");
61+
param.put("kind", "link");
6762
param.putAll(formParams);
6863

6964
System.out.println(param.keySet());
@@ -83,20 +78,33 @@ public String submit(Model model, @RequestParam Map<String, String> formParams)
8378

8479
@RequestMapping("/post")
8580
public String showSubmissionForm(Model model) throws JsonProcessingException, IOException {
86-
String needsCaptchaResult = needsCaptcha();
87-
if (needsCaptchaResult.equalsIgnoreCase("true")) {
88-
String newCaptchaResult = getNewCaptcha();
89-
String[] split = newCaptchaResult.split("\"");
90-
String iden = split[split.length - 2];
91-
model.addAttribute("iden", iden.trim());
81+
try {
82+
List<String> subreddits = getSubreddit();
83+
model.addAttribute("subreddits", subreddits);
84+
85+
String needsCaptchaResult = needsCaptcha();
86+
if (needsCaptchaResult.equalsIgnoreCase("true")) {
87+
String newCaptchaResult = getNewCaptcha();
88+
String[] split = newCaptchaResult.split("\"");
89+
String iden = split[split.length - 2];
90+
model.addAttribute("iden", iden.trim());
91+
}
92+
} catch (UserApprovalRequiredException e) {
93+
throw e;
94+
} catch (UserRedirectRequiredException e) {
95+
throw e;
96+
} catch (Exception e) {
97+
LOGGER.error("Error occurred", e);
98+
model.addAttribute("error", e.getLocalizedMessage());
99+
return "reddit";
92100
}
93101
return "submissionForm";
94102
}
95103

96104
//
97105

98-
public List<String> getSubreddit(Model model) throws JsonProcessingException, IOException {
99-
String result = redditRestTemplate.getForObject("https://oauth.reddit.com/subreddits/popular", String.class);
106+
private List<String> getSubreddit() throws JsonProcessingException, IOException {
107+
String result = redditRestTemplate.getForObject("https://oauth.reddit.com/subreddits/popular?limit=50", String.class);
100108
JsonNode node = new ObjectMapper().readTree(result);
101109
node = node.get("data").get("children");
102110
List<String> subreddits = new ArrayList<String>();
@@ -106,12 +114,12 @@ public List<String> getSubreddit(Model model) throws JsonProcessingException, IO
106114
return subreddits;
107115
}
108116

109-
public String needsCaptcha() {
117+
private String needsCaptcha() {
110118
String result = redditRestTemplate.getForObject("https://oauth.reddit.com/api/needs_captcha.json", String.class);
111119
return result;
112120
}
113121

114-
public String getNewCaptcha() {
122+
private String getNewCaptcha() {
115123
HttpHeaders headers = new HttpHeaders();
116124
headers.setContentType(MediaType.APPLICATION_JSON);
117125
HttpEntity req = new HttpEntity(headers);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<c:when test="${info != null}">
1010
<h1>Your Reddit Info</h1>
1111
<b>Your reddit username is </b>${info}
12-
<br>
12+
<br><br><br>
1313
<a href="post">Submit to Reddit</a>
1414
</c:when>
1515
<c:otherwise>

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

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,22 @@
1717
</div>
1818
<br><br>
1919
<div class="form-group">
20-
<label class="col-sm-3">Content</label>
21-
<span class="col-sm-9"><textarea placeholder="content" class="form-control"></textarea></span>
20+
<label class="col-sm-3">Url</label>
21+
<span class="col-sm-9"><input name="url" placeholder="url" class="form-control" /></span>
2222
</div>
23-
<br><br>
23+
<br><br>
24+
<div class="form-group">
25+
<label class="col-sm-3">Subreddit</label>
26+
<span class="col-sm-9">
27+
<select name="sr">
28+
<c:forEach items="${subreddits}" var="item">
29+
<option value="${item}">${item}</option>
30+
</c:forEach>
31+
</select>
32+
</span>
33+
</div>
34+
<br><br>
35+
2436
<c:if test="${iden != null}">
2537
<input type="hidden" name="iden" value="${iden}"/>
2638

0 commit comments

Comments
 (0)