88
99import org .slf4j .Logger ;
1010import org .slf4j .LoggerFactory ;
11+ import org .springframework .beans .factory .annotation .Autowired ;
1112import org .springframework .security .oauth2 .client .OAuth2RestTemplate ;
1213import org .springframework .stereotype .Controller ;
1314import org .springframework .ui .Model ;
2223
2324@ Controller
2425public class RedditController {
25- private OAuth2RestTemplate redditRestTemplate ;
2626
2727 private final Logger LOGGER = LoggerFactory .getLogger (getClass ());
2828
29+ @ Autowired
30+ private OAuth2RestTemplate redditRestTemplate ;
31+
32+ // API
33+
2934 @ RequestMapping ("/info" )
30- public String getInfo (Model model ) {
35+ public final String getInfo (Model model ) {
3136 JsonNode node = redditRestTemplate .getForObject ("https://oauth.reddit.com/api/v1/me" , JsonNode .class );
3237 String name = node .get ("name" ).asText ();
3338 model .addAttribute ("info" , name );
3439 return "reddit" ;
3540 }
3641
3742 @ RequestMapping ("/submit" )
38- public String submit (Model model , @ RequestParam Map <String , String > formParams ) {
43+ public final String submit (Model model , @ RequestParam Map <String , String > formParams ) {
3944 MultiValueMap <String , String > param = new LinkedMultiValueMap <String , String >();
4045 param .add ("api_type" , "json" );
4146 param .add ("kind" , "link" );
@@ -56,7 +61,7 @@ public String submit(Model model, @RequestParam Map<String, String> formParams)
5661 }
5762
5863 @ RequestMapping ("/post" )
59- public String showSubmissionForm (Model model ) {
64+ public final String showSubmissionForm (Model model ) {
6065 String needsCaptchaResult = needsCaptcha ();
6166 if (needsCaptchaResult .equalsIgnoreCase ("true" )) {
6267 String iden = getNewCaptcha ();
@@ -67,7 +72,7 @@ public String showSubmissionForm(Model model) {
6772
6873 // === private
6974
70- public List <String > getSubreddit () throws JsonProcessingException , IOException {
75+ List <String > getSubreddit () throws JsonProcessingException , IOException {
7176 String result = redditRestTemplate .getForObject ("https://oauth.reddit.com/subreddits/popular?limit=50" , String .class );
7277 JsonNode node = new ObjectMapper ().readTree (result );
7378 node = node .get ("data" ).get ("children" );
@@ -108,8 +113,4 @@ private String parseResponse(JsonNode node) {
108113 }
109114 }
110115
111- public void setRedditRestTemplate (OAuth2RestTemplate redditRestTemplate ) {
112- this .redditRestTemplate = redditRestTemplate ;
113- }
114-
115116}
0 commit comments