modify reddit schedule

This commit is contained in:
DOHA 2015-03-03 14:44:39 +02:00
parent 9c3ef0416d
commit e05e2c5e09
10 changed files with 30 additions and 32 deletions

View File

@ -23,9 +23,6 @@ import org.springframework.util.MultiValueMap;
public class MyAuthorizationCodeAccessTokenProvider extends AuthorizationCodeAccessTokenProvider implements Serializable { public class MyAuthorizationCodeAccessTokenProvider extends AuthorizationCodeAccessTokenProvider implements Serializable {
/**
*
*/
private static final long serialVersionUID = 3822611002661972274L; private static final long serialVersionUID = 3822611002661972274L;
private StateKeyGenerator stateKeyGenerator = new DefaultStateKeyGenerator(); private StateKeyGenerator stateKeyGenerator = new DefaultStateKeyGenerator();

View File

@ -53,18 +53,6 @@ public class WebConfig extends WebMvcConfigurerAdapter {
configurer.enable(); 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 @Bean
public ScheduledTasks scheduledTasks(OAuth2ProtectedResourceDetails reddit) { public ScheduledTasks scheduledTasks(OAuth2ProtectedResourceDetails reddit) {
ScheduledTasks s = new ScheduledTasks(); ScheduledTasks s = new ScheduledTasks();

View File

@ -9,7 +9,7 @@ import org.springframework.data.jpa.repository.JpaRepository;
public interface PostRepository extends JpaRepository<Post, Long> { public interface PostRepository extends JpaRepository<Post, Long> {
public List<Post> findBySubmissionDateBefore(Date date); public List<Post> findBySubmissionDateBeforeAndIsSent(Date date, boolean isSent);
public List<Post> findByUser(User user); public List<Post> findByUser(User user);
} }

View File

@ -102,6 +102,7 @@ public class RedditController {
post.setSubreddit(formParams.get("sr")); post.setSubreddit(formParams.get("sr"));
post.setUrl(formParams.get("url")); post.setUrl(formParams.get("url"));
post.setSubmissionDate(dateFormat.parse(formParams.get("date"))); post.setSubmissionDate(dateFormat.parse(formParams.get("date")));
post.setSubmissionResponse("Not sent yet");
if (post.getSubmissionDate().before(new Date())) { if (post.getSubmissionDate().before(new Date())) {
model.addAttribute("msg", "Invalid date"); model.addAttribute("msg", "Invalid date");
return "submissionResponse"; return "submissionResponse";

View File

@ -1,7 +1,5 @@
package org.baeldung.web.schedule; package org.baeldung.web.schedule;
import java.io.IOException;
import java.text.ParseException;
import java.util.Arrays; import java.util.Arrays;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
@ -9,6 +7,8 @@ import java.util.List;
import org.baeldung.persistence.dao.PostRepository; import org.baeldung.persistence.dao.PostRepository;
import org.baeldung.persistence.model.Post; import org.baeldung.persistence.model.Post;
import org.baeldung.persistence.model.User; import org.baeldung.persistence.model.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled; 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.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap; import org.springframework.util.MultiValueMap;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.JsonNode;
@EnableScheduling @EnableScheduling
public class ScheduledTasks { public class ScheduledTasks {
private OAuth2RestTemplate redditRestTemplate; private OAuth2RestTemplate redditRestTemplate;
private final Logger logger = LoggerFactory.getLogger(getClass());
@Autowired @Autowired
private PostRepository postReopsitory; private PostRepository postReopsitory;
@Scheduled(fixedRate = 5 * 60 * 1000) @Scheduled(fixedRate = 1 * 60 * 1000)
public void reportCurrentTime() throws JsonProcessingException, IOException, ParseException { public void reportCurrentTime() {
List<Post> posts = postReopsitory.findBySubmissionDateBefore(new Date()); List<Post> posts = postReopsitory.findBySubmissionDateBeforeAndIsSent(new Date(), false);
System.out.println(posts.size()); logger.info(posts.size() + " Posts in the queue.");
for (Post post : posts) { for (Post post : posts) {
if (post.isSent()) submitPost(post);
continue; }
}
private void submitPost(Post post) {
try {
User user = post.getUser(); User user = post.getUser();
DefaultOAuth2AccessToken token = new DefaultOAuth2AccessToken(user.getAccessToken()); DefaultOAuth2AccessToken token = new DefaultOAuth2AccessToken(user.getAccessToken());
token.setRefreshToken(new DefaultOAuth2RefreshToken((user.getRefreshToken()))); token.setRefreshToken(new DefaultOAuth2RefreshToken((user.getRefreshToken())));
@ -59,15 +63,21 @@ public class ScheduledTasks {
param.add("sr", post.getSubreddit()); param.add("sr", post.getSubreddit());
param.add("url", post.getUrl()); 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 node = redditRestTemplate.postForObject("https://oauth.reddit.com/api/submit", param, JsonNode.class);
JsonNode errorNode = node.get("json").get("errors").get(0); JsonNode errorNode = node.get("json").get("errors").get(0);
if (errorNode == null) { if (errorNode == null) {
post.setSent(true); post.setSent(true);
post.setSubmissionResponse("Successfully sent");
postReopsitory.save(post); postReopsitory.save(post);
logger.info("Successfully sent");
} else { } else {
post.setSubmissionResponse(errorNode.toString()); post.setSubmissionResponse(errorNode.toString());
postReopsitory.save(post); postReopsitory.save(post);
logger.info("Error occurred: " + errorNode.toString());
} }
} catch (Exception e) {
logger.error("Error occurred", e);
} }
} }

View File

@ -1,4 +1,6 @@
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<html xmlns="http://www.w3.org/1999/xhtml"> <html xmlns="http://www.w3.org/1999/xhtml">
<head> <head>
@ -7,7 +9,7 @@
</head> </head>
<body> <body>
<nav class="navbar navbar-inverse"> <nav class="navbar navbar-default">
<div class="container-fluid"> <div class="container-fluid">
<!-- Brand and toggle get grouped for better mobile display --> <!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header"> <div class="navbar-header">
@ -34,7 +36,6 @@
<div class="container"> <div class="container">
<h1>My Scheduled Posts</h1> <h1>My Scheduled Posts</h1>
<table class="table table-bordered"> <table class="table table-bordered">
<c:forEach var="post" items="${posts}" >
<thead> <thead>
<tr> <tr>
<th>Post title</th> <th>Post title</th>
@ -42,9 +43,10 @@
<th>Notes</th> <th>Notes</th>
</tr> </tr>
</thead> </thead>
<c:forEach var="post" items="${posts}" >
<tr <c:if test="${post.isSent()}"> class="success"</c:if>> <tr <c:if test="${post.isSent()}"> class="success"</c:if>>
<td><c:out value="${post.getTitle()}"/></td> <td><c:out value="${post.getTitle()}"/></td>
<td><c:out value="${post.getSubmissionDate()}"/></td> <td><fmt:formatDate type="both" dateStyle="long" timeStyle="long" value="${post.getSubmissionDate()}" /></td>
<td><c:out value="${post.getSubmissionResponse()}"/></td> <td><c:out value="${post.getSubmissionResponse()}"/></td>
</tr> </tr>
</c:forEach> </c:forEach>

View File

@ -7,7 +7,7 @@
</head> </head>
<body> <body>
<nav class="navbar navbar-inverse"> <nav class="navbar navbar-default">
<div class="container-fluid"> <div class="container-fluid">
<!-- Brand and toggle get grouped for better mobile display --> <!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header"> <div class="navbar-header">

View File

@ -10,7 +10,7 @@
</head> </head>
<body> <body>
<nav class="navbar navbar-inverse"> <nav class="navbar navbar-default">
<div class="container-fluid"> <div class="container-fluid">
<!-- Brand and toggle get grouped for better mobile display --> <!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header"> <div class="navbar-header">

View File

@ -7,7 +7,7 @@
</head> </head>
<body> <body>
<nav class="navbar navbar-inverse"> <nav class="navbar navbar-default">
<div class="container-fluid"> <div class="container-fluid">
<!-- Brand and toggle get grouped for better mobile display --> <!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header"> <div class="navbar-header">

View File

@ -7,7 +7,7 @@
</head> </head>
<body> <body>
<nav class="navbar navbar-inverse"> <nav class="navbar navbar-default">
<div class="container-fluid"> <div class="container-fluid">
<!-- Brand and toggle get grouped for better mobile display --> <!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header"> <div class="navbar-header">