handle submit response

This commit is contained in:
DOHA 2015-02-22 22:55:59 +02:00
parent 8671a052e7
commit ea84d8cc3e
2 changed files with 32 additions and 9 deletions

View File

@ -64,16 +64,17 @@ public class RedditController {
System.out.println(param.keySet());
System.out.println(param.entrySet());
ResponseEntity<String> result = redditRestTemplate.postForEntity("https://oauth.reddit.com/api/submit", req, String.class, param);
model.addAttribute("error", result.getBody());
String responseMsg = parseResponseMessage(result.getBody());
model.addAttribute("msg", responseMsg);
} catch (UserApprovalRequiredException e) {
throw e;
} catch (UserRedirectRequiredException e) {
throw e;
} catch (Exception e) {
LOGGER.error("Error occurred", e);
model.addAttribute("error", e.getLocalizedMessage());
model.addAttribute("msg", e.getLocalizedMessage());
}
return "reddit";
return "submissionResponse";
}
@RequestMapping("/post")
@ -81,10 +82,8 @@ public class RedditController {
try {
String needsCaptchaResult = needsCaptcha();
if (needsCaptchaResult.equalsIgnoreCase("true")) {
String newCaptchaResult = getNewCaptcha();
String[] split = newCaptchaResult.split("\"");
String iden = split[split.length - 2];
model.addAttribute("iden", iden.trim());
String iden = getNewCaptcha();
model.addAttribute("iden", iden);
}
} catch (UserApprovalRequiredException e) {
throw e;
@ -125,11 +124,23 @@ public class RedditController {
param.put("api_type", "json");
ResponseEntity<String> result = redditRestTemplate.postForEntity("https://oauth.reddit.com/api/new_captcha", req, String.class, param);
return result.getBody();
String[] split = result.getBody().split("\"");
return split[split.length - 2];
}
private String parseResponseMessage(String responseBody) {
System.out.println(responseBody);
int index = responseBody.indexOf("error");
if (index == -1) {
return "Post submitted successfully";
} else {
int msgEnd = responseBody.indexOf("\"", index);
return responseBody.substring(index, msgEnd);
}
}
public void setRedditRestTemplate(OAuth2RestTemplate redditRestTemplate) {
this.redditRestTemplate = redditRestTemplate;
}
}
}

View File

@ -0,0 +1,12 @@
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Spring Security OAuth</title>
</head>
<body>
<h1>${msg}</h1>
<a href="post">Submit another link to Reddit</a>
</body>
</html>