@@ -64,27 +64,26 @@ public String submit(Model model, @RequestParam Map<String, String> formParams)
6464 System .out .println (param .keySet ());
6565 System .out .println (param .entrySet ());
6666 ResponseEntity <String > result = redditRestTemplate .postForEntity ("https://oauth.reddit.com/api/submit" , req , String .class , param );
67- model .addAttribute ("error" , result .getBody ());
67+ String responseMsg = parseResponseMessage (result .getBody ());
68+ model .addAttribute ("msg" , responseMsg );
6869 } catch (UserApprovalRequiredException e ) {
6970 throw e ;
7071 } catch (UserRedirectRequiredException e ) {
7172 throw e ;
7273 } catch (Exception e ) {
7374 LOGGER .error ("Error occurred" , e );
74- model .addAttribute ("error " , e .getLocalizedMessage ());
75+ model .addAttribute ("msg " , e .getLocalizedMessage ());
7576 }
76- return "reddit " ;
77+ return "submissionResponse " ;
7778 }
7879
7980 @ RequestMapping ("/post" )
8081 public String showSubmissionForm (Model model ) throws JsonProcessingException , IOException {
8182 try {
8283 String needsCaptchaResult = needsCaptcha ();
8384 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 ());
85+ String iden = getNewCaptcha ();
86+ model .addAttribute ("iden" , iden );
8887 }
8988 } catch (UserApprovalRequiredException e ) {
9089 throw e ;
@@ -125,11 +124,23 @@ private String getNewCaptcha() {
125124 param .put ("api_type" , "json" );
126125
127126 ResponseEntity <String > result = redditRestTemplate .postForEntity ("https://oauth.reddit.com/api/new_captcha" , req , String .class , param );
128- return result .getBody ();
127+ String [] split = result .getBody ().split ("\" " );
128+ return split [split .length - 2 ];
129+ }
130+
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 );
139+ }
129140 }
130141
131142 public void setRedditRestTemplate (OAuth2RestTemplate redditRestTemplate ) {
132143 this .redditRestTemplate = redditRestTemplate ;
133144 }
134145
135- }
146+ }
0 commit comments