1717import org .springframework .security .oauth2 .client .resource .UserRedirectRequiredException ;
1818import org .springframework .stereotype .Controller ;
1919import org .springframework .ui .Model ;
20+ import org .springframework .util .LinkedMultiValueMap ;
21+ import org .springframework .util .MultiValueMap ;
2022import org .springframework .web .bind .annotation .RequestMapping ;
2123import org .springframework .web .bind .annotation .RequestParam ;
2224
@@ -43,48 +45,48 @@ public String getInfo(Model model) {
4345 throw e ;
4446 } catch (Exception e ) {
4547 LOGGER .error ("Error occurred" , e );
46- model .addAttribute ("error" , e .getLocalizedMessage ());
48+ model .addAttribute ("error" , e .getMessage ());
4749 }
4850 return "reddit" ;
4951 }
5052
5153 @ RequestMapping ("/submit" )
5254 public String submit (Model model , @ RequestParam Map <String , String > formParams ) {
5355 try {
54- System .out .println (formParams .keySet ());
55- HttpHeaders headers = new HttpHeaders ();
56- headers .setContentType (MediaType .APPLICATION_JSON );
57- HttpEntity req = new HttpEntity (headers );
58-
59- Map <String , String > param = new HashMap <String , String >();
60- param .put ("api_type" , "json" );
61- param .put ("kind" , "link" );
62- param .putAll (formParams );
63-
64- System .out .println (param .keySet ());
65- System .out .println (param .entrySet ());
66- ResponseEntity <String > result = redditRestTemplate .postForEntity ("https://oauth.reddit.com/api/submit" , req , String .class , param );
67- model .addAttribute ("error" , result .getBody ());
56+ MultiValueMap <String , String > param = new LinkedMultiValueMap <String , String >();
57+ param .add ("api_type" , "json" );
58+ param .add ("kind" , "link" );
59+ param .add ("resubmit" , "true" );
60+ param .add ("sendreplies" , "false" );
61+ param .add ("then" , "comments" );
62+
63+ for (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+ String result = redditRestTemplate .postForObject ("https://oauth.reddit.com/api/submit" , param , String .class );
69+ LOGGER .info ("Full Reddit Response: " + result );
70+ String responseMsg = parseResponse (result );
71+ model .addAttribute ("msg" , responseMsg );
6872 } catch (UserApprovalRequiredException e ) {
6973 throw e ;
7074 } catch (UserRedirectRequiredException e ) {
7175 throw e ;
7276 } catch (Exception e ) {
7377 LOGGER .error ("Error occurred" , e );
74- model .addAttribute ("error " , e .getLocalizedMessage ());
78+ model .addAttribute ("msg " , e .getLocalizedMessage ());
7579 }
76- return "reddit " ;
80+ return "submissionResponse " ;
7781 }
7882
7983 @ RequestMapping ("/post" )
8084 public String showSubmissionForm (Model model ) throws JsonProcessingException , IOException {
8185 try {
8286 String needsCaptchaResult = needsCaptcha ();
8387 if (needsCaptchaResult .equalsIgnoreCase ("true" )) {
84- String newCaptchaResult = getNewCaptcha ();
85- String [] split = newCaptchaResult .split ("\" " );
86- String iden = split [split .length - 2 ];
87- model .addAttribute ("iden" , iden .trim ());
88+ String iden = getNewCaptcha ();
89+ model .addAttribute ("iden" , iden );
8890 }
8991 } catch (UserApprovalRequiredException e ) {
9092 throw e ;
@@ -98,7 +100,7 @@ public String showSubmissionForm(Model model) throws JsonProcessingException, IO
98100 return "submissionForm" ;
99101 }
100102
101- //
103+ // === private
102104
103105 private List <String > getSubreddit () throws JsonProcessingException , IOException {
104106 String result = redditRestTemplate .getForObject ("https://oauth.reddit.com/subreddits/popular?limit=50" , String .class );
@@ -125,11 +127,29 @@ private String getNewCaptcha() {
125127 param .put ("api_type" , "json" );
126128
127129 ResponseEntity <String > result = redditRestTemplate .postForEntity ("https://oauth.reddit.com/api/new_captcha" , req , String .class , param );
128- return result .getBody ();
130+ String [] split = result .getBody ().split ("\" " );
131+ return split [split .length - 2 ];
132+ }
133+
134+ private String parseResponse (String responseBody ) throws JsonProcessingException , IOException {
135+ String result = "" ;
136+ JsonNode node = new ObjectMapper ().readTree (responseBody );
137+ JsonNode errorNode = node .get ("json" ).get ("errors" ).get (0 );
138+ if (errorNode != null ) {
139+ for (JsonNode child : errorNode ) {
140+ result = result + child .toString ().replaceAll ("\" |null" , "" ) + "<br>" ;
141+ }
142+ return result ;
143+ } else {
144+ if (node .get ("json" ).get ("data" ) != null && node .get ("json" ).get ("data" ).get ("url" ) != null )
145+ return "Post submitted successfully <a href=\" " + node .get ("json" ).get ("data" ).get ("url" ).asText () + "\" > check it out </a>" ;
146+ else
147+ return "Error Occurred" ;
148+ }
129149 }
130150
131151 public void setRedditRestTemplate (OAuth2RestTemplate redditRestTemplate ) {
132152 this .redditRestTemplate = redditRestTemplate ;
133153 }
134154
135- }
155+ }
0 commit comments