1111import org .slf4j .Logger ;
1212import org .slf4j .LoggerFactory ;
1313import org .springframework .beans .factory .annotation .Autowired ;
14- import org .springframework .scheduling .annotation .EnableScheduling ;
1514import org .springframework .scheduling .annotation .Scheduled ;
1615import org .springframework .security .authentication .UsernamePasswordAuthenticationToken ;
1716import org .springframework .security .core .authority .SimpleGrantedAuthority ;
2423
2524import com .fasterxml .jackson .databind .JsonNode ;
2625
27- @ EnableScheduling
2826public class ScheduledTasks {
2927
3028 private OAuth2RestTemplate redditRestTemplate ;
@@ -42,48 +40,52 @@ public void reportCurrentTime() {
4240 }
4341 }
4442
45- private void submitPost (Post post ) {
43+ private void submitPost (final Post post ) {
4644 try {
47- final User user = post .getUser ();
48- final DefaultOAuth2AccessToken token = new DefaultOAuth2AccessToken (user .getAccessToken ());
49- token .setRefreshToken (new DefaultOAuth2RefreshToken ((user .getRefreshToken ())));
50- token .setExpiration (user .getTokenExpiration ());
51- redditRestTemplate .getOAuth2ClientContext ().setAccessToken (token );
52- //
53- final UsernamePasswordAuthenticationToken userAuthToken = new UsernamePasswordAuthenticationToken (user .getUsername (), token .getValue (), Arrays .asList (new SimpleGrantedAuthority ("ROLE_USER" )));
54- SecurityContextHolder .getContext ().setAuthentication (userAuthToken );
55- //
56- final MultiValueMap <String , String > param = new LinkedMultiValueMap <String , String >();
57- param .add (RedditApiConstants .TITLE , post .getTitle ());
58- param .add (RedditApiConstants .SR , post .getSubreddit ());
59- param .add (RedditApiConstants .URL , post .getUrl ());
60- param .add (RedditApiConstants .API_TYPE , "json" );
61- param .add (RedditApiConstants .KIND , "link" );
62- param .add (RedditApiConstants .RESUBMIT , "true" );
63- param .add (RedditApiConstants .THEN , "comments" );
64- if (post .isSendReplies ()) {
65- param .add (RedditApiConstants .SENDREPLIES , "true" );
66- }
67-
68- logger .info ("Submit link with these parameters: " + param .entrySet ());
69- final JsonNode node = redditRestTemplate .postForObject ("https://oauth.reddit.com/api/submit" , param , JsonNode .class );
70- final JsonNode errorNode = node .get ("json" ).get ("errors" ).get (0 );
71- if (errorNode == null ) {
72- post .setSent (true );
73- post .setSubmissionResponse ("Successfully sent" );
74- postReopsitory .save (post );
75- logger .info ("Successfully sent " + post .toString ());
76- } else {
77- post .setSubmissionResponse (errorNode .toString ());
78- postReopsitory .save (post );
79- logger .info ("Error occurred: " + errorNode .toString () + "while submitting post " + post .toString ());
80- }
45+ submitPostInternal (post );
8146 } catch (final Exception e ) {
8247 logger .error ("Error occurred while submitting post " + post .toString (), e );
8348 }
8449 }
8550
86- public void setRedditRestTemplate (OAuth2RestTemplate redditRestTemplate ) {
51+ private void submitPostInternal (final Post post ) {
52+ final User user = post .getUser ();
53+ final DefaultOAuth2AccessToken token = new DefaultOAuth2AccessToken (user .getAccessToken ());
54+ token .setRefreshToken (new DefaultOAuth2RefreshToken ((user .getRefreshToken ())));
55+ token .setExpiration (user .getTokenExpiration ());
56+ redditRestTemplate .getOAuth2ClientContext ().setAccessToken (token );
57+ //
58+ final UsernamePasswordAuthenticationToken userAuthToken = new UsernamePasswordAuthenticationToken (user .getUsername (), token .getValue (), Arrays .asList (new SimpleGrantedAuthority ("ROLE_USER" )));
59+ SecurityContextHolder .getContext ().setAuthentication (userAuthToken );
60+ //
61+ final MultiValueMap <String , String > param = new LinkedMultiValueMap <String , String >();
62+ param .add (RedditApiConstants .TITLE , post .getTitle ());
63+ param .add (RedditApiConstants .SR , post .getSubreddit ());
64+ param .add (RedditApiConstants .URL , post .getUrl ());
65+ param .add (RedditApiConstants .API_TYPE , "json" );
66+ param .add (RedditApiConstants .KIND , "link" );
67+ param .add (RedditApiConstants .RESUBMIT , "true" );
68+ param .add (RedditApiConstants .THEN , "comments" );
69+ if (post .isSendReplies ()) {
70+ param .add (RedditApiConstants .SENDREPLIES , "true" );
71+ }
72+
73+ logger .info ("Submit link with these parameters: " + param .entrySet ());
74+ final JsonNode node = redditRestTemplate .postForObject ("https://oauth.reddit.com/api/submit" , param , JsonNode .class );
75+ final JsonNode errorNode = node .get ("json" ).get ("errors" ).get (0 );
76+ if (errorNode == null ) {
77+ post .setSent (true );
78+ post .setSubmissionResponse ("Successfully sent" );
79+ postReopsitory .save (post );
80+ logger .info ("Successfully sent " + post .toString ());
81+ } else {
82+ post .setSubmissionResponse (errorNode .toString ());
83+ postReopsitory .save (post );
84+ logger .info ("Error occurred: " + errorNode .toString () + "while submitting post " + post .toString ());
85+ }
86+ }
87+
88+ public void setRedditRestTemplate (final OAuth2RestTemplate redditRestTemplate ) {
8789 this .redditRestTemplate = redditRestTemplate ;
8890 }
8991}
0 commit comments