Skip to content

Commit d86a6a4

Browse files
committed
Merge remote-tracking branch 'upstream/master'
2 parents a838368 + 3133aac commit d86a6a4

File tree

5 files changed

+66
-26
lines changed

5 files changed

+66
-26
lines changed

spring-security-oauth/src/main/java/org/baeldung/config/PersistenceJPAConfig.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
6767

6868
final Properties additionalProperties() {
6969
final Properties hibernateProperties = new Properties();
70-
hibernateProperties.setProperty("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto"));
70+
hibernateProperties.setProperty("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto", "create-drop"));
7171
hibernateProperties.setProperty("hibernate.dialect", env.getProperty("hibernate.dialect"));
7272
return hibernateProperties;
7373
}

spring-security-oauth/src/main/java/org/baeldung/web/RedditController.java

Lines changed: 38 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public class RedditController {
4343
private PostRepository postReopsitory;
4444

4545
@RequestMapping("/info")
46-
public final String getInfo(Model model) {
46+
public final String getInfo(final Model model) {
4747
final JsonNode node = redditRestTemplate.getForObject("https://oauth.reddit.com/api/v1/me", JsonNode.class);
4848
final String name = node.get("name").asText();
4949
addUser(name, redditRestTemplate.getAccessToken());
@@ -52,28 +52,19 @@ public final String getInfo(Model model) {
5252
}
5353

5454
@RequestMapping("/submit")
55-
public final String submit(Model model, @RequestParam Map<String, String> formParams) {
56-
final MultiValueMap<String, String> param = new LinkedMultiValueMap<String, String>();
57-
param.add(RedditApiConstants.API_TYPE, "json");
58-
param.add(RedditApiConstants.KIND, "link");
59-
param.add(RedditApiConstants.RESUBMIT, "true");
60-
param.add(RedditApiConstants.SENDREPLIES, "false");
61-
param.add(RedditApiConstants.THEN, "comments");
55+
public final String submit(final Model model, @RequestParam final Map<String, String> formParams) {
56+
final MultiValueMap<String, String> param1 = constructParams(formParams);
6257

63-
for (final Map.Entry<String, String> entry : formParams.entrySet()) {
64-
param.add(entry.getKey(), entry.getValue());
65-
}
66-
67-
logger.info("User submitting Link with these parameters: " + formParams.entrySet());
68-
final JsonNode node = redditRestTemplate.postForObject("https://oauth.reddit.com/api/submit", param, JsonNode.class);
69-
logger.info("Full Reddit Response: " + node.toString());
58+
logger.info("Submitting Link with these parameters: " + param1);
59+
final JsonNode node = redditRestTemplate.postForObject("https://oauth.reddit.com/api/submit", param1, JsonNode.class);
60+
logger.info("Submitted Link - Full Response from Reddit: " + node.toString());
7061
final String responseMsg = parseResponse(node);
7162
model.addAttribute("msg", responseMsg);
7263
return "submissionResponse";
7364
}
7465

7566
@RequestMapping("/post")
76-
public final String showSubmissionForm(Model model) {
67+
public final String showSubmissionForm(final Model model) {
7768
final String needsCaptchaResult = needsCaptcha();
7869
if (needsCaptchaResult.equalsIgnoreCase("true")) {
7970
final String iden = getNewCaptcha();
@@ -83,7 +74,7 @@ public final String showSubmissionForm(Model model) {
8374
}
8475

8576
@RequestMapping("/postSchedule")
86-
public final String showSchedulePostForm(Model model) {
77+
public final String showSchedulePostForm(final Model model) {
8778
final String needsCaptchaResult = needsCaptcha();
8879
if (needsCaptchaResult.equalsIgnoreCase("true")) {
8980
model.addAttribute("msg", "Sorry, You do not have enought karma");
@@ -93,7 +84,7 @@ public final String showSchedulePostForm(Model model) {
9384
}
9485

9586
@RequestMapping("/schedule")
96-
public final String schedule(Model model, @RequestParam Map<String, String> formParams) throws ParseException {
87+
public final String schedule(final Model model, @RequestParam final Map<String, String> formParams) throws ParseException {
9788
logger.info("User scheduling Post with these parameters: " + formParams.entrySet());
9889
final User user = userReopsitory.findByAccessToken(redditRestTemplate.getAccessToken().getValue());
9990
final Post post = new Post();
@@ -115,7 +106,7 @@ public final String schedule(Model model, @RequestParam Map<String, String> form
115106
}
116107

117108
@RequestMapping("/posts")
118-
public final String getScheduledPosts(Model model) {
109+
public final String getScheduledPosts(final Model model) {
119110
final User user = userReopsitory.findByAccessToken(redditRestTemplate.getAccessToken().getValue());
120111
final List<Post> posts = postReopsitory.findByUser(user);
121112
model.addAttribute("posts", posts);
@@ -124,6 +115,32 @@ public final String getScheduledPosts(Model model) {
124115

125116
// === private
126117

118+
private final MultiValueMap<String, String> constructParams(final Map<String, String> formParams) {
119+
final MultiValueMap<String, String> param = new LinkedMultiValueMap<String, String>();
120+
param.add(RedditApiConstants.API_TYPE, "json");
121+
param.add(RedditApiConstants.KIND, "link");
122+
param.add(RedditApiConstants.RESUBMIT, "true");
123+
param.add(RedditApiConstants.SENDREPLIES, "false");
124+
param.add(RedditApiConstants.THEN, "comments");
125+
for (final Map.Entry<String, String> entry : formParams.entrySet()) {
126+
param.add(entry.getKey(), entry.getValue());
127+
}
128+
return param;
129+
}
130+
131+
private final Map<String, String> constructParams2(final Map<String, String> formParams) {
132+
final Map<String, String> param = new HashMap<String, String>();
133+
param.put(RedditApiConstants.API_TYPE, "json");
134+
param.put(RedditApiConstants.KIND, "link");
135+
param.put(RedditApiConstants.RESUBMIT, "true");
136+
param.put(RedditApiConstants.SENDREPLIES, "false");
137+
param.put(RedditApiConstants.THEN, "comments");
138+
for (final Map.Entry<String, String> entry : formParams.entrySet()) {
139+
param.put(entry.getKey(), entry.getValue());
140+
}
141+
return param;
142+
}
143+
127144
private final String needsCaptcha() {
128145
final String result = redditRestTemplate.getForObject("https://oauth.reddit.com/api/needs_captcha.json", String.class);
129146
return result;
@@ -138,7 +155,7 @@ private final String getNewCaptcha() {
138155
return split[split.length - 2];
139156
}
140157

141-
private final String parseResponse(JsonNode node) {
158+
private final String parseResponse(final JsonNode node) {
142159
String result = "";
143160
final JsonNode errorNode = node.get("json").get("errors").get(0);
144161
if (errorNode != null) {
@@ -155,7 +172,7 @@ private final String parseResponse(JsonNode node) {
155172
}
156173
}
157174

158-
private final void addUser(String name, OAuth2AccessToken token) {
175+
private final void addUser(final String name, final OAuth2AccessToken token) {
159176
User user = userReopsitory.findByUsername(name);
160177
if (user == null) {
161178
user = new User();

spring-security-oauth/src/main/java/org/baeldung/web/RestExceptionHandler.java

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,17 @@
22

33
import java.io.Serializable;
44

5+
import javax.servlet.http.HttpServletResponse;
6+
57
import org.springframework.http.HttpHeaders;
68
import org.springframework.http.HttpStatus;
79
import org.springframework.http.ResponseEntity;
10+
import org.springframework.security.oauth2.client.resource.OAuth2AccessDeniedException;
811
import org.springframework.security.oauth2.client.resource.UserApprovalRequiredException;
912
import org.springframework.security.oauth2.client.resource.UserRedirectRequiredException;
1013
import org.springframework.web.bind.annotation.ControllerAdvice;
1114
import org.springframework.web.bind.annotation.ExceptionHandler;
15+
import org.springframework.web.client.HttpClientErrorException;
1216
import org.springframework.web.context.request.WebRequest;
1317
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
1418

@@ -23,18 +27,36 @@ public RestExceptionHandler() {
2327

2428
// API
2529

30+
// 4xx
31+
32+
@ExceptionHandler({ OAuth2AccessDeniedException.class })
33+
public ResponseEntity<Object> handleOAuth2AccessDeniedException(final OAuth2AccessDeniedException ex, final WebRequest request) {
34+
logger.error("403 Status Code", ex);
35+
final String response = "Error Occurred - Forbidden: " + ex.getMessage();
36+
return handleExceptionInternal(ex, response, new HttpHeaders(), HttpStatus.FORBIDDEN, request);
37+
}
38+
39+
@ExceptionHandler({ HttpClientErrorException.class })
40+
public ResponseEntity<Object> handleHttpClientErrorException(final HttpClientErrorException ex, final WebRequest request) {
41+
logger.error("400 Status Code", ex);
42+
final String response = "Error Occurred - To Many Requests: " + ex.getMessage();
43+
return handleExceptionInternal(ex, response, new HttpHeaders(), HttpStatus.TOO_MANY_REQUESTS, request);
44+
}
45+
46+
// HttpClientErrorException
2647
// 500
48+
2749
@ExceptionHandler({ UserApprovalRequiredException.class, UserRedirectRequiredException.class })
2850
public ResponseEntity<Object> handleRedirect(final RuntimeException ex, final WebRequest request) {
2951
logger.info(ex.getLocalizedMessage());
3052
throw ex;
3153
}
3254

3355
@ExceptionHandler({ Exception.class })
34-
public ResponseEntity<Object> handleInternal(final RuntimeException ex, final WebRequest request) {
35-
logger.info(request.getHeader("x-ratelimit-remaining"));
56+
public ResponseEntity<Object> handleInternal(final RuntimeException ex, final WebRequest request, final HttpServletResponse response) {
57+
logger.info(response.getHeader("x-ratelimit-remaining"));
3658
logger.error("500 Status Code", ex);
37-
final String response = "Error Occurred : " + ex.getMessage();
38-
return handleExceptionInternal(ex, response, new HttpHeaders(), HttpStatus.INTERNAL_SERVER_ERROR, request);
59+
final String message = "Error Occurred: " + ex.getMessage();
60+
return handleExceptionInternal(ex, message, new HttpHeaders(), HttpStatus.INTERNAL_SERVER_ERROR, request);
3961
}
4062
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
persistence.properties

spring-security-oauth/src/main/resources/persistence.properties renamed to spring-security-oauth/src/main/resources/persistence.properties.sample

File renamed without changes.

0 commit comments

Comments
 (0)