handle submit response
This commit is contained in:
parent
8671a052e7
commit
ea84d8cc3e
@ -64,16 +64,17 @@ public class RedditController {
|
|||||||
System.out.println(param.keySet());
|
System.out.println(param.keySet());
|
||||||
System.out.println(param.entrySet());
|
System.out.println(param.entrySet());
|
||||||
ResponseEntity<String> result = redditRestTemplate.postForEntity("https://oauth.reddit.com/api/submit", req, String.class, param);
|
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) {
|
} catch (UserApprovalRequiredException e) {
|
||||||
throw e;
|
throw e;
|
||||||
} catch (UserRedirectRequiredException e) {
|
} catch (UserRedirectRequiredException e) {
|
||||||
throw e;
|
throw e;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
LOGGER.error("Error occurred", e);
|
LOGGER.error("Error occurred", e);
|
||||||
model.addAttribute("error", e.getLocalizedMessage());
|
model.addAttribute("msg", e.getLocalizedMessage());
|
||||||
}
|
}
|
||||||
return "reddit";
|
return "submissionResponse";
|
||||||
}
|
}
|
||||||
|
|
||||||
@RequestMapping("/post")
|
@RequestMapping("/post")
|
||||||
@ -81,10 +82,8 @@ public class RedditController {
|
|||||||
try {
|
try {
|
||||||
String needsCaptchaResult = needsCaptcha();
|
String needsCaptchaResult = needsCaptcha();
|
||||||
if (needsCaptchaResult.equalsIgnoreCase("true")) {
|
if (needsCaptchaResult.equalsIgnoreCase("true")) {
|
||||||
String newCaptchaResult = getNewCaptcha();
|
String iden = getNewCaptcha();
|
||||||
String[] split = newCaptchaResult.split("\"");
|
model.addAttribute("iden", iden);
|
||||||
String iden = split[split.length - 2];
|
|
||||||
model.addAttribute("iden", iden.trim());
|
|
||||||
}
|
}
|
||||||
} catch (UserApprovalRequiredException e) {
|
} catch (UserApprovalRequiredException e) {
|
||||||
throw e;
|
throw e;
|
||||||
@ -125,11 +124,23 @@ public class RedditController {
|
|||||||
param.put("api_type", "json");
|
param.put("api_type", "json");
|
||||||
|
|
||||||
ResponseEntity<String> result = redditRestTemplate.postForEntity("https://oauth.reddit.com/api/new_captcha", req, String.class, param);
|
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) {
|
public void setRedditRestTemplate(OAuth2RestTemplate redditRestTemplate) {
|
||||||
this.redditRestTemplate = redditRestTemplate;
|
this.redditRestTemplate = redditRestTemplate;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
12
spring-security-oauth/src/main/webapp/WEB-INF/jsp/submissionResponse.jsp
Executable file
12
spring-security-oauth/src/main/webapp/WEB-INF/jsp/submissionResponse.jsp
Executable 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>
|
Loading…
x
Reference in New Issue
Block a user