From e05e2c5e096916a80bb89f0dbc628dd986242949 Mon Sep 17 00:00:00 2001 From: DOHA Date: Tue, 3 Mar 2015 14:44:39 +0200 Subject: [PATCH] modify reddit schedule --- ...yAuthorizationCodeAccessTokenProvider.java | 3 -- .../java/org/baeldung/config/WebConfig.java | 12 -------- .../persistence/dao/PostRepository.java | 2 +- .../org/baeldung/web/RedditController.java | 1 + .../baeldung/web/schedule/ScheduledTasks.java | 28 +++++++++++++------ .../main/webapp/WEB-INF/jsp/postListView.jsp | 8 ++++-- .../src/main/webapp/WEB-INF/jsp/reddit.jsp | 2 +- .../webapp/WEB-INF/jsp/schedulePostForm.jsp | 2 +- .../webapp/WEB-INF/jsp/submissionForm.jsp | 2 +- .../webapp/WEB-INF/jsp/submissionResponse.jsp | 2 +- 10 files changed, 30 insertions(+), 32 deletions(-) diff --git a/spring-security-oauth/src/main/java/org/baeldung/config/MyAuthorizationCodeAccessTokenProvider.java b/spring-security-oauth/src/main/java/org/baeldung/config/MyAuthorizationCodeAccessTokenProvider.java index ca37ab3f82..598f97a556 100644 --- a/spring-security-oauth/src/main/java/org/baeldung/config/MyAuthorizationCodeAccessTokenProvider.java +++ b/spring-security-oauth/src/main/java/org/baeldung/config/MyAuthorizationCodeAccessTokenProvider.java @@ -23,9 +23,6 @@ import org.springframework.util.MultiValueMap; public class MyAuthorizationCodeAccessTokenProvider extends AuthorizationCodeAccessTokenProvider implements Serializable { - /** - * - */ private static final long serialVersionUID = 3822611002661972274L; private StateKeyGenerator stateKeyGenerator = new DefaultStateKeyGenerator(); diff --git a/spring-security-oauth/src/main/java/org/baeldung/config/WebConfig.java b/spring-security-oauth/src/main/java/org/baeldung/config/WebConfig.java index 028645f8fc..6887a174b6 100644 --- a/spring-security-oauth/src/main/java/org/baeldung/config/WebConfig.java +++ b/spring-security-oauth/src/main/java/org/baeldung/config/WebConfig.java @@ -53,18 +53,6 @@ public class WebConfig extends WebMvcConfigurerAdapter { configurer.enable(); } - // @Bean - // public RedditController redditController(OAuth2RestTemplate redditRestTemplate) { - // RedditController controller = new RedditController(); - // controller.setRedditRestTemplate(redditRestTemplate); - // return controller; - // } - // - // @Bean - // public RestExceptionHandler restExceptionHandler() { - // return new RestExceptionHandler(); - // } - // @Bean public ScheduledTasks scheduledTasks(OAuth2ProtectedResourceDetails reddit) { ScheduledTasks s = new ScheduledTasks(); diff --git a/spring-security-oauth/src/main/java/org/baeldung/persistence/dao/PostRepository.java b/spring-security-oauth/src/main/java/org/baeldung/persistence/dao/PostRepository.java index 13d393bc4b..04644f03ef 100644 --- a/spring-security-oauth/src/main/java/org/baeldung/persistence/dao/PostRepository.java +++ b/spring-security-oauth/src/main/java/org/baeldung/persistence/dao/PostRepository.java @@ -9,7 +9,7 @@ import org.springframework.data.jpa.repository.JpaRepository; public interface PostRepository extends JpaRepository { - public List findBySubmissionDateBefore(Date date); + public List findBySubmissionDateBeforeAndIsSent(Date date, boolean isSent); public List findByUser(User user); } \ No newline at end of file diff --git a/spring-security-oauth/src/main/java/org/baeldung/web/RedditController.java b/spring-security-oauth/src/main/java/org/baeldung/web/RedditController.java index 4a743b1b92..7bc9ee7eb1 100644 --- a/spring-security-oauth/src/main/java/org/baeldung/web/RedditController.java +++ b/spring-security-oauth/src/main/java/org/baeldung/web/RedditController.java @@ -102,6 +102,7 @@ public class RedditController { post.setSubreddit(formParams.get("sr")); post.setUrl(formParams.get("url")); post.setSubmissionDate(dateFormat.parse(formParams.get("date"))); + post.setSubmissionResponse("Not sent yet"); if (post.getSubmissionDate().before(new Date())) { model.addAttribute("msg", "Invalid date"); return "submissionResponse"; diff --git a/spring-security-oauth/src/main/java/org/baeldung/web/schedule/ScheduledTasks.java b/spring-security-oauth/src/main/java/org/baeldung/web/schedule/ScheduledTasks.java index fec528c042..8e3b3f580d 100644 --- a/spring-security-oauth/src/main/java/org/baeldung/web/schedule/ScheduledTasks.java +++ b/spring-security-oauth/src/main/java/org/baeldung/web/schedule/ScheduledTasks.java @@ -1,7 +1,5 @@ package org.baeldung.web.schedule; -import java.io.IOException; -import java.text.ParseException; import java.util.Arrays; import java.util.Date; import java.util.List; @@ -9,6 +7,8 @@ import java.util.List; import org.baeldung.persistence.dao.PostRepository; import org.baeldung.persistence.model.Post; import org.baeldung.persistence.model.User; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.scheduling.annotation.Scheduled; @@ -21,24 +21,28 @@ import org.springframework.security.oauth2.common.DefaultOAuth2RefreshToken; import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; -import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JsonNode; @EnableScheduling public class ScheduledTasks { private OAuth2RestTemplate redditRestTemplate; + private final Logger logger = LoggerFactory.getLogger(getClass()); @Autowired private PostRepository postReopsitory; - @Scheduled(fixedRate = 5 * 60 * 1000) - public void reportCurrentTime() throws JsonProcessingException, IOException, ParseException { - List posts = postReopsitory.findBySubmissionDateBefore(new Date()); - System.out.println(posts.size()); + @Scheduled(fixedRate = 1 * 60 * 1000) + public void reportCurrentTime() { + List posts = postReopsitory.findBySubmissionDateBeforeAndIsSent(new Date(), false); + logger.info(posts.size() + " Posts in the queue."); for (Post post : posts) { - if (post.isSent()) - continue; + submitPost(post); + } + } + + private void submitPost(Post post) { + try { User user = post.getUser(); DefaultOAuth2AccessToken token = new DefaultOAuth2AccessToken(user.getAccessToken()); token.setRefreshToken(new DefaultOAuth2RefreshToken((user.getRefreshToken()))); @@ -59,15 +63,21 @@ public class ScheduledTasks { param.add("sr", post.getSubreddit()); param.add("url", post.getUrl()); + logger.info("Submit link with these parameters: " + param.entrySet()); JsonNode node = redditRestTemplate.postForObject("https://oauth.reddit.com/api/submit", param, JsonNode.class); JsonNode errorNode = node.get("json").get("errors").get(0); if (errorNode == null) { post.setSent(true); + post.setSubmissionResponse("Successfully sent"); postReopsitory.save(post); + logger.info("Successfully sent"); } else { post.setSubmissionResponse(errorNode.toString()); postReopsitory.save(post); + logger.info("Error occurred: " + errorNode.toString()); } + } catch (Exception e) { + logger.error("Error occurred", e); } } diff --git a/spring-security-oauth/src/main/webapp/WEB-INF/jsp/postListView.jsp b/spring-security-oauth/src/main/webapp/WEB-INF/jsp/postListView.jsp index fe2e19ec4a..0e9e20a674 100755 --- a/spring-security-oauth/src/main/webapp/WEB-INF/jsp/postListView.jsp +++ b/spring-security-oauth/src/main/webapp/WEB-INF/jsp/postListView.jsp @@ -1,4 +1,6 @@ <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> +<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> + @@ -7,7 +9,7 @@ -