small cleanup work

This commit is contained in:
eugenp 2015-02-27 22:28:59 +02:00
parent bace2a4416
commit a9834d20ca
2 changed files with 21 additions and 26 deletions

View File

@ -23,9 +23,6 @@ import org.springframework.util.MultiValueMap;
public class MyAuthorizationCodeAccessTokenProvider extends AuthorizationCodeAccessTokenProvider implements Serializable { public class MyAuthorizationCodeAccessTokenProvider extends AuthorizationCodeAccessTokenProvider implements Serializable {
/**
*
*/
private static final long serialVersionUID = 3822611002661972274L; private static final long serialVersionUID = 3822611002661972274L;
private StateKeyGenerator stateKeyGenerator = new DefaultStateKeyGenerator(); private StateKeyGenerator stateKeyGenerator = new DefaultStateKeyGenerator();

View File

@ -24,7 +24,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
@Controller @Controller
public class RedditController { public class RedditController {
private final Logger LOGGER = LoggerFactory.getLogger(getClass()); private final Logger logger = LoggerFactory.getLogger(getClass());
@Autowired @Autowired
private OAuth2RestTemplate redditRestTemplate; private OAuth2RestTemplate redditRestTemplate;
@ -33,38 +33,37 @@ public class RedditController {
@RequestMapping("/info") @RequestMapping("/info")
public final String getInfo(Model model) { public final String getInfo(Model model) {
JsonNode node = redditRestTemplate.getForObject("https://oauth.reddit.com/api/v1/me", JsonNode.class); final JsonNode node = redditRestTemplate.getForObject("https://oauth.reddit.com/api/v1/me", JsonNode.class);
String name = node.get("name").asText(); final String name = node.get("name").asText();
model.addAttribute("info", name); model.addAttribute("info", name);
return "reddit"; return "reddit";
} }
@RequestMapping("/submit") @RequestMapping("/submit")
public final String submit(Model model, @RequestParam Map<String, String> formParams) { public final String submit(Model model, @RequestParam Map<String, String> formParams) {
MultiValueMap<String, String> param = new LinkedMultiValueMap<String, String>(); final MultiValueMap<String, String> param = new LinkedMultiValueMap<String, String>();
param.add("api_type", "json"); param.add("api_type", "json");
param.add("kind", "link"); param.add("kind", "link");
param.add("resubmit", "true"); param.add("resubmit", "true");
param.add("sendreplies", "false"); param.add("sendreplies", "false");
param.add("then", "comments"); param.add("then", "comments");
for (final Map.Entry<String, String> entry : formParams.entrySet()) {
for (Map.Entry<String, String> entry : formParams.entrySet()) {
param.add(entry.getKey(), entry.getValue()); param.add(entry.getKey(), entry.getValue());
} }
LOGGER.info("User submitting Link with these parameters: " + formParams.entrySet()); logger.info("User submitting Link with these parameters: " + formParams.entrySet());
JsonNode node = redditRestTemplate.postForObject("https://oauth.reddit.com/api/submit", param, JsonNode.class); JsonNode node = redditRestTemplate.postForObject("https://oauth.reddit.com/api/submit", param, JsonNode.class);
LOGGER.info("Full Reddit Response: " + node.toString()); logger.info("Full Reddit Response: " + node.toString());
String responseMsg = parseResponse(node); String responseMsg = parseResponse(node);
model.addAttribute("msg", responseMsg); model.addAttribute("msg", responseMsg);
return "submissionResponse"; return "submissionResponse";
} }
@RequestMapping("/post") @RequestMapping("/post")
public final String showSubmissionForm(Model model) { public final String showSubmissionForm(final Model model) {
String needsCaptchaResult = needsCaptcha(); final String needsCaptchaResult = needsCaptcha();
if (needsCaptchaResult.equalsIgnoreCase("true")) { if (needsCaptchaResult.equalsIgnoreCase("true")) {
String iden = getNewCaptcha(); final String iden = getNewCaptcha();
model.addAttribute("iden", iden); model.addAttribute("iden", iden);
} }
return "submissionForm"; return "submissionForm";
@ -72,34 +71,33 @@ public class RedditController {
// === private // === private
List<String> getSubreddit() throws JsonProcessingException, IOException { final List<String> getSubreddit() throws JsonProcessingException, IOException {
String result = redditRestTemplate.getForObject("https://oauth.reddit.com/subreddits/popular?limit=50", String.class); final String result = redditRestTemplate.getForObject("https://oauth.reddit.com/subreddits/popular?limit=50", String.class);
JsonNode node = new ObjectMapper().readTree(result); final JsonNode node = new ObjectMapper().readTree(result).get("data").get("children");
node = node.get("data").get("children"); final List<String> subreddits = new ArrayList<String>();
List<String> subreddits = new ArrayList<String>();
for (JsonNode child : node) { for (JsonNode child : node) {
subreddits.add(child.get("data").get("display_name").asText()); subreddits.add(child.get("data").get("display_name").asText());
} }
return subreddits; return subreddits;
} }
private String needsCaptcha() { private final String needsCaptcha() {
String result = redditRestTemplate.getForObject("https://oauth.reddit.com/api/needs_captcha.json", String.class); String result = redditRestTemplate.getForObject("https://oauth.reddit.com/api/needs_captcha.json", String.class);
return result; return result;
} }
private String getNewCaptcha() { private final String getNewCaptcha() {
Map<String, String> param = new HashMap<String, String>(); final Map<String, String> param = new HashMap<String, String>();
param.put("api_type", "json"); param.put("api_type", "json");
String result = redditRestTemplate.postForObject("https://oauth.reddit.com/api/new_captcha", param, String.class, param); final String result = redditRestTemplate.postForObject("https://oauth.reddit.com/api/new_captcha", param, String.class, param);
String[] split = result.split("\""); final String[] split = result.split("\"");
return split[split.length - 2]; return split[split.length - 2];
} }
private String parseResponse(JsonNode node) { private final String parseResponse(final JsonNode node) {
String result = ""; String result = "";
JsonNode errorNode = node.get("json").get("errors").get(0); final JsonNode errorNode = node.get("json").get("errors").get(0);
if (errorNode != null) { if (errorNode != null) {
for (JsonNode child : errorNode) { for (JsonNode child : errorNode) {
result = result + child.toString().replaceAll("\"|null", "") + "<br>"; result = result + child.toString().replaceAll("\"|null", "") + "<br>";