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 {
/**
*
*/
private static final long serialVersionUID = 3822611002661972274L;
private StateKeyGenerator stateKeyGenerator = new DefaultStateKeyGenerator();

View File

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