1111import org .baeldung .persistence .dao .UserRepository ;
1212import org .baeldung .persistence .model .Post ;
1313import org .baeldung .persistence .model .User ;
14+ import org .baeldung .reddit .util .RedditApiConstants ;
1415import org .slf4j .Logger ;
1516import org .slf4j .LoggerFactory ;
1617import org .springframework .beans .factory .annotation .Autowired ;
@@ -43,47 +44,47 @@ public class RedditController {
4344
4445 @ RequestMapping ("/info" )
4546 public final String getInfo (Model model ) {
46- JsonNode node = redditRestTemplate .getForObject ("https://oauth.reddit.com/api/v1/me" , JsonNode .class );
47- String name = node .get ("name" ).asText ();
47+ final JsonNode node = redditRestTemplate .getForObject ("https://oauth.reddit.com/api/v1/me" , JsonNode .class );
48+ final String name = node .get ("name" ).asText ();
4849 addUser (name , redditRestTemplate .getAccessToken ());
4950 model .addAttribute ("info" , name );
5051 return "reddit" ;
5152 }
5253
5354 @ RequestMapping ("/submit" )
5455 public final String submit (Model model , @ RequestParam Map <String , String > formParams ) {
55- MultiValueMap <String , String > param = new LinkedMultiValueMap <String , String >();
56- param .add ("api_type" , "json" );
57- param .add ("kind" , "link" );
58- param .add ("resubmit" , "true" );
59- param .add ("sendreplies" , "false" );
60- param .add ("then" , "comments" );
61-
62- for (Map .Entry <String , String > entry : formParams .entrySet ()) {
56+ final MultiValueMap <String , String > param = new LinkedMultiValueMap <String , String >();
57+ param .add (RedditApiConstants . API_TYPE , "json" );
58+ param .add (RedditApiConstants . KIND , "link" );
59+ param .add (RedditApiConstants . RESUBMIT , "true" );
60+ param .add (RedditApiConstants . SENDREPLIES , "false" );
61+ param .add (RedditApiConstants . THEN , "comments" );
62+
63+ for (final Map .Entry <String , String > entry : formParams .entrySet ()) {
6364 param .add (entry .getKey (), entry .getValue ());
6465 }
6566
6667 logger .info ("User submitting Link with these parameters: " + formParams .entrySet ());
67- JsonNode node = redditRestTemplate .postForObject ("https://oauth.reddit.com/api/submit" , param , JsonNode .class );
68+ final JsonNode node = redditRestTemplate .postForObject ("https://oauth.reddit.com/api/submit" , param , JsonNode .class );
6869 logger .info ("Full Reddit Response: " + node .toString ());
69- String responseMsg = parseResponse (node );
70+ final String responseMsg = parseResponse (node );
7071 model .addAttribute ("msg" , responseMsg );
7172 return "submissionResponse" ;
7273 }
7374
7475 @ RequestMapping ("/post" )
7576 public final String showSubmissionForm (Model model ) {
76- String needsCaptchaResult = needsCaptcha ();
77+ final String needsCaptchaResult = needsCaptcha ();
7778 if (needsCaptchaResult .equalsIgnoreCase ("true" )) {
78- String iden = getNewCaptcha ();
79+ final String iden = getNewCaptcha ();
7980 model .addAttribute ("iden" , iden );
8081 }
8182 return "submissionForm" ;
8283 }
8384
8485 @ RequestMapping ("/postSchedule" )
8586 public final String showSchedulePostForm (Model model ) {
86- String needsCaptchaResult = needsCaptcha ();
87+ final String needsCaptchaResult = needsCaptcha ();
8788 if (needsCaptchaResult .equalsIgnoreCase ("true" )) {
8889 model .addAttribute ("msg" , "Sorry, You do not have enought karma" );
8990 return "submissionResponse" ;
@@ -94,8 +95,8 @@ public final String showSchedulePostForm(Model model) {
9495 @ RequestMapping ("/schedule" )
9596 public final String schedule (Model model , @ RequestParam Map <String , String > formParams ) throws ParseException {
9697 logger .info ("User scheduling Post with these parameters: " + formParams .entrySet ());
97- User user = userReopsitory .findByAccessToken (redditRestTemplate .getAccessToken ().getValue ());
98- Post post = new Post ();
98+ final User user = userReopsitory .findByAccessToken (redditRestTemplate .getAccessToken ().getValue ());
99+ final Post post = new Post ();
99100 post .setUser (user );
100101 post .setSent (false );
101102 post .setTitle (formParams .get ("title" ));
@@ -108,48 +109,49 @@ public final String schedule(Model model, @RequestParam Map<String, String> form
108109 return "submissionResponse" ;
109110 }
110111 postReopsitory .save (post );
111- List <Post > posts = postReopsitory .findByUser (user );
112+ final List <Post > posts = postReopsitory .findByUser (user );
112113 model .addAttribute ("posts" , posts );
113114 return "postListView" ;
114115 }
115116
116117 @ RequestMapping ("/posts" )
117118 public final String getScheduledPosts (Model model ) {
118- User user = userReopsitory .findByAccessToken (redditRestTemplate .getAccessToken ().getValue ());
119- List <Post > posts = postReopsitory .findByUser (user );
119+ final User user = userReopsitory .findByAccessToken (redditRestTemplate .getAccessToken ().getValue ());
120+ final List <Post > posts = postReopsitory .findByUser (user );
120121 model .addAttribute ("posts" , posts );
121122 return "postListView" ;
122123 }
123124
124125 // === private
125126
126127 private final String needsCaptcha () {
127- String result = redditRestTemplate .getForObject ("https://oauth.reddit.com/api/needs_captcha.json" , String .class );
128+ final String result = redditRestTemplate .getForObject ("https://oauth.reddit.com/api/needs_captcha.json" , String .class );
128129 return result ;
129130 }
130131
131132 private final String getNewCaptcha () {
132- Map <String , String > param = new HashMap <String , String >();
133+ final Map <String , String > param = new HashMap <String , String >();
133134 param .put ("api_type" , "json" );
134135
135- String result = redditRestTemplate .postForObject ("https://oauth.reddit.com/api/new_captcha" , param , String .class , param );
136- String [] split = result .split ("\" " );
136+ final String result = redditRestTemplate .postForObject ("https://oauth.reddit.com/api/new_captcha" , param , String .class , param );
137+ final String [] split = result .split ("\" " );
137138 return split [split .length - 2 ];
138139 }
139140
140141 private final String parseResponse (JsonNode node ) {
141142 String result = "" ;
142- JsonNode errorNode = node .get ("json" ).get ("errors" ).get (0 );
143+ final JsonNode errorNode = node .get ("json" ).get ("errors" ).get (0 );
143144 if (errorNode != null ) {
144- for (JsonNode child : errorNode ) {
145+ for (final JsonNode child : errorNode ) {
145146 result = result + child .toString ().replaceAll ("\" |null" , "" ) + "<br>" ;
146147 }
147148 return result ;
148149 } else {
149- if (node .get ("json" ).get ("data" ) != null && node .get ("json" ).get ("data" ).get ("url" ) != null )
150+ if (( node .get ("json" ).get ("data" ) != null ) && ( node .get ("json" ).get ("data" ).get ("url" ) != null )) {
150151 return "Post submitted successfully <a href=\" " + node .get ("json" ).get ("data" ).get ("url" ).asText () + "\" > check it out </a>" ;
151- else
152+ } else {
152153 return "Error Occurred" ;
154+ }
153155 }
154156 }
155157
0 commit comments