Skip to content

Commit c2488dc

Browse files
author
Eugen
committed
Merge pull request eugenp#154 from Doha2012/master
modify reddit schedule
2 parents cc759f1 + e05e2c5 commit c2488dc

File tree

10 files changed

+30
-32
lines changed

10 files changed

+30
-32
lines changed

spring-security-oauth/src/main/java/org/baeldung/config/MyAuthorizationCodeAccessTokenProvider.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,6 @@
2323

2424
public class MyAuthorizationCodeAccessTokenProvider extends AuthorizationCodeAccessTokenProvider implements Serializable {
2525

26-
/**
27-
*
28-
*/
2926
private static final long serialVersionUID = 3822611002661972274L;
3027

3128
private StateKeyGenerator stateKeyGenerator = new DefaultStateKeyGenerator();

spring-security-oauth/src/main/java/org/baeldung/config/WebConfig.java

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -53,18 +53,6 @@ public void configureDefaultServletHandling(DefaultServletHandlerConfigurer conf
5353
configurer.enable();
5454
}
5555

56-
// @Bean
57-
// public RedditController redditController(OAuth2RestTemplate redditRestTemplate) {
58-
// RedditController controller = new RedditController();
59-
// controller.setRedditRestTemplate(redditRestTemplate);
60-
// return controller;
61-
// }
62-
//
63-
// @Bean
64-
// public RestExceptionHandler restExceptionHandler() {
65-
// return new RestExceptionHandler();
66-
// }
67-
//
6856
@Bean
6957
public ScheduledTasks scheduledTasks(OAuth2ProtectedResourceDetails reddit) {
7058
ScheduledTasks s = new ScheduledTasks();

spring-security-oauth/src/main/java/org/baeldung/persistence/dao/PostRepository.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
public interface PostRepository extends JpaRepository<Post, Long> {
1111

12-
public List<Post> findBySubmissionDateBefore(Date date);
12+
public List<Post> findBySubmissionDateBeforeAndIsSent(Date date, boolean isSent);
1313

1414
public List<Post> findByUser(User user);
1515
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ public final String schedule(Model model, @RequestParam Map<String, String> form
102102
post.setSubreddit(formParams.get("sr"));
103103
post.setUrl(formParams.get("url"));
104104
post.setSubmissionDate(dateFormat.parse(formParams.get("date")));
105+
post.setSubmissionResponse("Not sent yet");
105106
if (post.getSubmissionDate().before(new Date())) {
106107
model.addAttribute("msg", "Invalid date");
107108
return "submissionResponse";

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

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
package org.baeldung.web.schedule;
22

3-
import java.io.IOException;
4-
import java.text.ParseException;
53
import java.util.Arrays;
64
import java.util.Date;
75
import java.util.List;
86

97
import org.baeldung.persistence.dao.PostRepository;
108
import org.baeldung.persistence.model.Post;
119
import org.baeldung.persistence.model.User;
10+
import org.slf4j.Logger;
11+
import org.slf4j.LoggerFactory;
1212
import org.springframework.beans.factory.annotation.Autowired;
1313
import org.springframework.scheduling.annotation.EnableScheduling;
1414
import org.springframework.scheduling.annotation.Scheduled;
@@ -21,24 +21,28 @@
2121
import org.springframework.util.LinkedMultiValueMap;
2222
import org.springframework.util.MultiValueMap;
2323

24-
import com.fasterxml.jackson.core.JsonProcessingException;
2524
import com.fasterxml.jackson.databind.JsonNode;
2625

2726
@EnableScheduling
2827
public class ScheduledTasks {
2928

3029
private OAuth2RestTemplate redditRestTemplate;
30+
private final Logger logger = LoggerFactory.getLogger(getClass());
3131

3232
@Autowired
3333
private PostRepository postReopsitory;
3434

35-
@Scheduled(fixedRate = 5 * 60 * 1000)
36-
public void reportCurrentTime() throws JsonProcessingException, IOException, ParseException {
37-
List<Post> posts = postReopsitory.findBySubmissionDateBefore(new Date());
38-
System.out.println(posts.size());
35+
@Scheduled(fixedRate = 1 * 60 * 1000)
36+
public void reportCurrentTime() {
37+
List<Post> posts = postReopsitory.findBySubmissionDateBeforeAndIsSent(new Date(), false);
38+
logger.info(posts.size() + " Posts in the queue.");
3939
for (Post post : posts) {
40-
if (post.isSent())
41-
continue;
40+
submitPost(post);
41+
}
42+
}
43+
44+
private void submitPost(Post post) {
45+
try {
4246
User user = post.getUser();
4347
DefaultOAuth2AccessToken token = new DefaultOAuth2AccessToken(user.getAccessToken());
4448
token.setRefreshToken(new DefaultOAuth2RefreshToken((user.getRefreshToken())));
@@ -59,15 +63,21 @@ public void reportCurrentTime() throws JsonProcessingException, IOException, Par
5963
param.add("sr", post.getSubreddit());
6064
param.add("url", post.getUrl());
6165

66+
logger.info("Submit link with these parameters: " + param.entrySet());
6267
JsonNode node = redditRestTemplate.postForObject("https://oauth.reddit.com/api/submit", param, JsonNode.class);
6368
JsonNode errorNode = node.get("json").get("errors").get(0);
6469
if (errorNode == null) {
6570
post.setSent(true);
71+
post.setSubmissionResponse("Successfully sent");
6672
postReopsitory.save(post);
73+
logger.info("Successfully sent");
6774
} else {
6875
post.setSubmissionResponse(errorNode.toString());
6976
postReopsitory.save(post);
77+
logger.info("Error occurred: " + errorNode.toString());
7078
}
79+
} catch (Exception e) {
80+
logger.error("Error occurred", e);
7181
}
7282
}
7383

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
2+
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
3+
24
<html xmlns="http://www.w3.org/1999/xhtml">
35
<head>
46

@@ -7,7 +9,7 @@
79

810
</head>
911
<body>
10-
<nav class="navbar navbar-inverse">
12+
<nav class="navbar navbar-default">
1113
<div class="container-fluid">
1214
<!-- Brand and toggle get grouped for better mobile display -->
1315
<div class="navbar-header">
@@ -34,17 +36,17 @@
3436
<div class="container">
3537
<h1>My Scheduled Posts</h1>
3638
<table class="table table-bordered">
37-
<c:forEach var="post" items="${posts}" >
3839
<thead>
3940
<tr>
4041
<th>Post title</th>
4142
<th>Submission Date</th>
4243
<th>Notes</th>
4344
</tr>
4445
</thead>
46+
<c:forEach var="post" items="${posts}" >
4547
<tr <c:if test="${post.isSent()}"> class="success"</c:if>>
4648
<td><c:out value="${post.getTitle()}"/></td>
47-
<td><c:out value="${post.getSubmissionDate()}"/></td>
49+
<td><fmt:formatDate type="both" dateStyle="long" timeStyle="long" value="${post.getSubmissionDate()}" /></td>
4850
<td><c:out value="${post.getSubmissionResponse()}"/></td>
4951
</tr>
5052
</c:forEach>

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
@@ -7,7 +7,7 @@
77

88
</head>
99
<body>
10-
<nav class="navbar navbar-inverse">
10+
<nav class="navbar navbar-default">
1111
<div class="container-fluid">
1212
<!-- Brand and toggle get grouped for better mobile display -->
1313
<div class="navbar-header">

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
</head>
1212
<body>
13-
<nav class="navbar navbar-inverse">
13+
<nav class="navbar navbar-default">
1414
<div class="container-fluid">
1515
<!-- Brand and toggle get grouped for better mobile display -->
1616
<div class="navbar-header">

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
</head>
99
<body>
10-
<nav class="navbar navbar-inverse">
10+
<nav class="navbar navbar-default">
1111
<div class="container-fluid">
1212
<!-- Brand and toggle get grouped for better mobile display -->
1313
<div class="navbar-header">

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
</head>
99
<body>
10-
<nav class="navbar navbar-inverse">
10+
<nav class="navbar navbar-default">
1111
<div class="container-fluid">
1212
<!-- Brand and toggle get grouped for better mobile display -->
1313
<div class="navbar-header">

0 commit comments

Comments
 (0)