From 7724bd2428ae8bc0d014545b33cd00fb28b26ed5 Mon Sep 17 00:00:00 2001 From: eugenp Date: Thu, 12 Mar 2015 01:33:24 +0200 Subject: [PATCH] small cleanup --- .../org/baeldung/persistence/model/User.java | 13 ++-- .../baeldung/web/schedule/ScheduledTasks.java | 78 ++++++++++--------- 2 files changed, 48 insertions(+), 43 deletions(-) diff --git a/spring-security-oauth/src/main/java/org/baeldung/persistence/model/User.java b/spring-security-oauth/src/main/java/org/baeldung/persistence/model/User.java index ee941d10ed..e3b5553922 100644 --- a/spring-security-oauth/src/main/java/org/baeldung/persistence/model/User.java +++ b/spring-security-oauth/src/main/java/org/baeldung/persistence/model/User.java @@ -42,7 +42,7 @@ public class User { return username; } - public void setUsername(String username) { + public void setUsername(final String username) { this.username = username; } @@ -50,7 +50,7 @@ public class User { return accessToken; } - public void setAccessToken(String accessToken) { + public void setAccessToken(final String accessToken) { this.accessToken = accessToken; } @@ -58,7 +58,7 @@ public class User { return refreshToken; } - public void setRefreshToken(String refreshToken) { + public void setRefreshToken(final String refreshToken) { this.refreshToken = refreshToken; } @@ -66,7 +66,7 @@ public class User { return tokenExpiration; } - public void setTokenExpiration(Date tokenExpiration) { + public void setTokenExpiration(final Date tokenExpiration) { this.tokenExpiration = tokenExpiration; } @@ -74,10 +74,12 @@ public class User { return needCaptcha; } - public void setNeedCaptcha(boolean needCaptcha) { + public void setNeedCaptcha(final boolean needCaptcha) { this.needCaptcha = needCaptcha; } + // + @Override public int hashCode() { final int prime = 31; @@ -108,4 +110,5 @@ public class User { public String toString() { return "User [username=" + username + "]"; } + } \ No newline at end of file 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 9463086e52..e4cc42bd06 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 @@ -11,7 +11,6 @@ import org.baeldung.reddit.util.RedditApiConstants; 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; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; import org.springframework.security.core.authority.SimpleGrantedAuthority; @@ -24,7 +23,6 @@ import org.springframework.util.MultiValueMap; import com.fasterxml.jackson.databind.JsonNode; -@EnableScheduling public class ScheduledTasks { private OAuth2RestTemplate redditRestTemplate; @@ -42,48 +40,52 @@ public class ScheduledTasks { } } - private void submitPost(Post post) { + private void submitPost(final Post post) { try { - final User user = post.getUser(); - final DefaultOAuth2AccessToken token = new DefaultOAuth2AccessToken(user.getAccessToken()); - token.setRefreshToken(new DefaultOAuth2RefreshToken((user.getRefreshToken()))); - token.setExpiration(user.getTokenExpiration()); - redditRestTemplate.getOAuth2ClientContext().setAccessToken(token); - // - final UsernamePasswordAuthenticationToken userAuthToken = new UsernamePasswordAuthenticationToken(user.getUsername(), token.getValue(), Arrays.asList(new SimpleGrantedAuthority("ROLE_USER"))); - SecurityContextHolder.getContext().setAuthentication(userAuthToken); - // - final MultiValueMap param = new LinkedMultiValueMap(); - param.add(RedditApiConstants.TITLE, post.getTitle()); - param.add(RedditApiConstants.SR, post.getSubreddit()); - param.add(RedditApiConstants.URL, post.getUrl()); - param.add(RedditApiConstants.API_TYPE, "json"); - param.add(RedditApiConstants.KIND, "link"); - param.add(RedditApiConstants.RESUBMIT, "true"); - param.add(RedditApiConstants.THEN, "comments"); - if (post.isSendReplies()) { - param.add(RedditApiConstants.SENDREPLIES, "true"); - } - - logger.info("Submit link with these parameters: " + param.entrySet()); - final JsonNode node = redditRestTemplate.postForObject("https://oauth.reddit.com/api/submit", param, JsonNode.class); - final 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 " + post.toString()); - } else { - post.setSubmissionResponse(errorNode.toString()); - postReopsitory.save(post); - logger.info("Error occurred: " + errorNode.toString() + "while submitting post " + post.toString()); - } + submitPostInternal(post); } catch (final Exception e) { logger.error("Error occurred while submitting post " + post.toString(), e); } } - public void setRedditRestTemplate(OAuth2RestTemplate redditRestTemplate) { + private void submitPostInternal(final Post post) { + final User user = post.getUser(); + final DefaultOAuth2AccessToken token = new DefaultOAuth2AccessToken(user.getAccessToken()); + token.setRefreshToken(new DefaultOAuth2RefreshToken((user.getRefreshToken()))); + token.setExpiration(user.getTokenExpiration()); + redditRestTemplate.getOAuth2ClientContext().setAccessToken(token); + // + final UsernamePasswordAuthenticationToken userAuthToken = new UsernamePasswordAuthenticationToken(user.getUsername(), token.getValue(), Arrays.asList(new SimpleGrantedAuthority("ROLE_USER"))); + SecurityContextHolder.getContext().setAuthentication(userAuthToken); + // + final MultiValueMap param = new LinkedMultiValueMap(); + param.add(RedditApiConstants.TITLE, post.getTitle()); + param.add(RedditApiConstants.SR, post.getSubreddit()); + param.add(RedditApiConstants.URL, post.getUrl()); + param.add(RedditApiConstants.API_TYPE, "json"); + param.add(RedditApiConstants.KIND, "link"); + param.add(RedditApiConstants.RESUBMIT, "true"); + param.add(RedditApiConstants.THEN, "comments"); + if (post.isSendReplies()) { + param.add(RedditApiConstants.SENDREPLIES, "true"); + } + + logger.info("Submit link with these parameters: " + param.entrySet()); + final JsonNode node = redditRestTemplate.postForObject("https://oauth.reddit.com/api/submit", param, JsonNode.class); + final 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 " + post.toString()); + } else { + post.setSubmissionResponse(errorNode.toString()); + postReopsitory.save(post); + logger.info("Error occurred: " + errorNode.toString() + "while submitting post " + post.toString()); + } + } + + public void setRedditRestTemplate(final OAuth2RestTemplate redditRestTemplate) { this.redditRestTemplate = redditRestTemplate; } } \ No newline at end of file