@@ -43,7 +43,7 @@ public class RedditController {
4343 private PostRepository postReopsitory ;
4444
4545 @ RequestMapping ("/info" )
46- public final String getInfo (Model model ) {
46+ public final String getInfo (final Model model ) {
4747 final JsonNode node = redditRestTemplate .getForObject ("https://oauth.reddit.com/api/v1/me" , JsonNode .class );
4848 final String name = node .get ("name" ).asText ();
4949 addUser (name , redditRestTemplate .getAccessToken ());
@@ -52,28 +52,19 @@ public final String getInfo(Model model) {
5252 }
5353
5454 @ RequestMapping ("/submit" )
55- public final String submit (Model model , @ RequestParam Map <String , String > formParams ) {
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" );
55+ public final String submit (final Model model , @ RequestParam final Map <String , String > formParams ) {
56+ final MultiValueMap <String , String > param1 = constructParams (formParams );
6257
63- for (final Map .Entry <String , String > entry : formParams .entrySet ()) {
64- param .add (entry .getKey (), entry .getValue ());
65- }
66-
67- logger .info ("User submitting Link with these parameters: " + formParams .entrySet ());
68- final JsonNode node = redditRestTemplate .postForObject ("https://oauth.reddit.com/api/submit" , param , JsonNode .class );
69- logger .info ("Full Reddit Response: " + node .toString ());
58+ logger .info ("Submitting Link with these parameters: " + param1 );
59+ final JsonNode node = redditRestTemplate .postForObject ("https://oauth.reddit.com/api/submit" , param1 , JsonNode .class );
60+ logger .info ("Submitted Link - Full Response from Reddit: " + node .toString ());
7061 final String responseMsg = parseResponse (node );
7162 model .addAttribute ("msg" , responseMsg );
7263 return "submissionResponse" ;
7364 }
7465
7566 @ RequestMapping ("/post" )
76- public final String showSubmissionForm (Model model ) {
67+ public final String showSubmissionForm (final Model model ) {
7768 final String needsCaptchaResult = needsCaptcha ();
7869 if (needsCaptchaResult .equalsIgnoreCase ("true" )) {
7970 final String iden = getNewCaptcha ();
@@ -83,7 +74,7 @@ public final String showSubmissionForm(Model model) {
8374 }
8475
8576 @ RequestMapping ("/postSchedule" )
86- public final String showSchedulePostForm (Model model ) {
77+ public final String showSchedulePostForm (final Model model ) {
8778 final String needsCaptchaResult = needsCaptcha ();
8879 if (needsCaptchaResult .equalsIgnoreCase ("true" )) {
8980 model .addAttribute ("msg" , "Sorry, You do not have enought karma" );
@@ -93,7 +84,7 @@ public final String showSchedulePostForm(Model model) {
9384 }
9485
9586 @ RequestMapping ("/schedule" )
96- public final String schedule (Model model , @ RequestParam Map <String , String > formParams ) throws ParseException {
87+ public final String schedule (final Model model , @ RequestParam final Map <String , String > formParams ) throws ParseException {
9788 logger .info ("User scheduling Post with these parameters: " + formParams .entrySet ());
9889 final User user = userReopsitory .findByAccessToken (redditRestTemplate .getAccessToken ().getValue ());
9990 final Post post = new Post ();
@@ -115,7 +106,7 @@ public final String schedule(Model model, @RequestParam Map<String, String> form
115106 }
116107
117108 @ RequestMapping ("/posts" )
118- public final String getScheduledPosts (Model model ) {
109+ public final String getScheduledPosts (final Model model ) {
119110 final User user = userReopsitory .findByAccessToken (redditRestTemplate .getAccessToken ().getValue ());
120111 final List <Post > posts = postReopsitory .findByUser (user );
121112 model .addAttribute ("posts" , posts );
@@ -124,6 +115,32 @@ public final String getScheduledPosts(Model model) {
124115
125116 // === private
126117
118+ private final MultiValueMap <String , String > constructParams (final Map <String , String > formParams ) {
119+ final MultiValueMap <String , String > param = new LinkedMultiValueMap <String , String >();
120+ param .add (RedditApiConstants .API_TYPE , "json" );
121+ param .add (RedditApiConstants .KIND , "link" );
122+ param .add (RedditApiConstants .RESUBMIT , "true" );
123+ param .add (RedditApiConstants .SENDREPLIES , "false" );
124+ param .add (RedditApiConstants .THEN , "comments" );
125+ for (final Map .Entry <String , String > entry : formParams .entrySet ()) {
126+ param .add (entry .getKey (), entry .getValue ());
127+ }
128+ return param ;
129+ }
130+
131+ private final Map <String , String > constructParams2 (final Map <String , String > formParams ) {
132+ final Map <String , String > param = new HashMap <String , String >();
133+ param .put (RedditApiConstants .API_TYPE , "json" );
134+ param .put (RedditApiConstants .KIND , "link" );
135+ param .put (RedditApiConstants .RESUBMIT , "true" );
136+ param .put (RedditApiConstants .SENDREPLIES , "false" );
137+ param .put (RedditApiConstants .THEN , "comments" );
138+ for (final Map .Entry <String , String > entry : formParams .entrySet ()) {
139+ param .put (entry .getKey (), entry .getValue ());
140+ }
141+ return param ;
142+ }
143+
127144 private final String needsCaptcha () {
128145 final String result = redditRestTemplate .getForObject ("https://oauth.reddit.com/api/needs_captcha.json" , String .class );
129146 return result ;
@@ -138,7 +155,7 @@ private final String getNewCaptcha() {
138155 return split [split .length - 2 ];
139156 }
140157
141- private final String parseResponse (JsonNode node ) {
158+ private final String parseResponse (final JsonNode node ) {
142159 String result = "" ;
143160 final JsonNode errorNode = node .get ("json" ).get ("errors" ).get (0 );
144161 if (errorNode != null ) {
@@ -155,7 +172,7 @@ private final String parseResponse(JsonNode node) {
155172 }
156173 }
157174
158- private final void addUser (String name , OAuth2AccessToken token ) {
175+ private final void addUser (final String name , final OAuth2AccessToken token ) {
159176 User user = userReopsitory .findByUsername (name );
160177 if (user == null ) {
161178 user = new User ();
0 commit comments