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,28 +45,29 @@ 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- String responseMsg = parseResponseMessage (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 );
6871 model .addAttribute ("msg" , responseMsg );
6972 } catch (UserApprovalRequiredException e ) {
7073 throw e ;
@@ -97,7 +100,7 @@ public String showSubmissionForm(Model model) throws JsonProcessingException, IO
97100 return "submissionForm" ;
98101 }
99102
100- //
103+ // === private
101104
102105 private List <String > getSubreddit () throws JsonProcessingException , IOException {
103106 String result = redditRestTemplate .getForObject ("https://oauth.reddit.com/subreddits/popular?limit=50" , String .class );
@@ -128,15 +131,20 @@ private String getNewCaptcha() {
128131 return split [split .length - 2 ];
129132 }
130133
131- private String parseResponseMessage (String responseBody ) {
132- System .out .println (responseBody );
133- int index = responseBody .indexOf ("error" );
134- if (index == -1 ) {
135- return "Post submitted successfully" ;
136- } else {
137- int msgEnd = responseBody .indexOf ("\" " , index );
138- return responseBody .substring (index , msgEnd );
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+ for (JsonNode child : errorNode ) {
139+ result = result + child .toString ().replaceAll ("\" |null" , "" ) + "<br>" ;
139140 }
141+ if (result .length () == 0 ) {
142+ if (node .get ("json" ).get ("data" ) != null && node .get ("json" ).get ("data" ).get ("url" ) != null )
143+ return "Post submitted successfully <a href=\" " + node .get ("json" ).get ("data" ).get ("url" ).asText () + "\" > check it out </a>" ;
144+ else
145+ return "Error Occurred" ;
146+ }
147+ return result ;
140148 }
141149
142150 public void setRedditRestTemplate (OAuth2RestTemplate redditRestTemplate ) {
0 commit comments