commit
c2488dc4ea
|
@ -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();
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -9,7 +9,7 @@ import org.springframework.data.jpa.repository.JpaRepository;
|
|||
|
||||
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);
|
||||
}
|
|
@ -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";
|
||||
|
|
|
@ -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<Post> posts = postReopsitory.findBySubmissionDateBefore(new Date());
|
||||
System.out.println(posts.size());
|
||||
@Scheduled(fixedRate = 1 * 60 * 1000)
|
||||
public void reportCurrentTime() {
|
||||
List<Post> 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);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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" %>
|
||||
|
||||
<html xmlns="http://www.w3.org/1999/xhtml">
|
||||
<head>
|
||||
|
||||
|
@ -7,7 +9,7 @@
|
|||
|
||||
</head>
|
||||
<body>
|
||||
<nav class="navbar navbar-inverse">
|
||||
<nav class="navbar navbar-default">
|
||||
<div class="container-fluid">
|
||||
<!-- Brand and toggle get grouped for better mobile display -->
|
||||
<div class="navbar-header">
|
||||
|
@ -34,7 +36,6 @@
|
|||
<div class="container">
|
||||
<h1>My Scheduled Posts</h1>
|
||||
<table class="table table-bordered">
|
||||
<c:forEach var="post" items="${posts}" >
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Post title</th>
|
||||
|
@ -42,9 +43,10 @@
|
|||
<th>Notes</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<c:forEach var="post" items="${posts}" >
|
||||
<tr <c:if test="${post.isSent()}"> class="success"</c:if>>
|
||||
<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>
|
||||
</tr>
|
||||
</c:forEach>
|
||||
|
|
|
@ -7,7 +7,7 @@
|
|||
|
||||
</head>
|
||||
<body>
|
||||
<nav class="navbar navbar-inverse">
|
||||
<nav class="navbar navbar-default">
|
||||
<div class="container-fluid">
|
||||
<!-- Brand and toggle get grouped for better mobile display -->
|
||||
<div class="navbar-header">
|
||||
|
|
|
@ -10,7 +10,7 @@
|
|||
|
||||
</head>
|
||||
<body>
|
||||
<nav class="navbar navbar-inverse">
|
||||
<nav class="navbar navbar-default">
|
||||
<div class="container-fluid">
|
||||
<!-- Brand and toggle get grouped for better mobile display -->
|
||||
<div class="navbar-header">
|
||||
|
|
|
@ -7,7 +7,7 @@
|
|||
|
||||
</head>
|
||||
<body>
|
||||
<nav class="navbar navbar-inverse">
|
||||
<nav class="navbar navbar-default">
|
||||
<div class="container-fluid">
|
||||
<!-- Brand and toggle get grouped for better mobile display -->
|
||||
<div class="navbar-header">
|
||||
|
|
|
@ -7,7 +7,7 @@
|
|||
|
||||
</head>
|
||||
<body>
|
||||
<nav class="navbar navbar-inverse">
|
||||
<nav class="navbar navbar-default">
|
||||
<div class="container-fluid">
|
||||
<!-- Brand and toggle get grouped for better mobile display -->
|
||||
<div class="navbar-header">
|
||||
|
|
Loading…
Reference in New Issue