-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
385 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# EditorConfig helps developers define and maintain consistent | ||
# coding styles between different editors and IDEs | ||
# editorconfig.org | ||
|
||
root = true | ||
|
||
[*] | ||
indent_style = space | ||
indent_size = 2 | ||
end_of_line = lf | ||
charset = utf-8 | ||
trim_trailing_whitespace = true | ||
insert_final_newline = true | ||
|
||
[*.md] | ||
trim_trailing_whitespace = false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 0 additions & 48 deletions
48
src/main/java/com/faforever/api/config/GlobalControllerExceptionHandler.java
This file was deleted.
Oops, something went wrong.
9 changes: 0 additions & 9 deletions
9
src/main/java/com/faforever/api/config/error/ErrorResult.java
This file was deleted.
Oops, something went wrong.
23 changes: 23 additions & 0 deletions
23
src/main/java/com/faforever/api/config/security/oauth2/JsonApiOauthExceptionRenderer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.faforever.api.config.security.oauth2; | ||
|
||
import org.springframework.http.converter.HttpMessageConverter; | ||
import org.springframework.security.oauth2.http.converter.jaxb.JaxbOAuth2ExceptionMessageConverter; | ||
import org.springframework.security.oauth2.provider.error.DefaultOAuth2ExceptionRenderer; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class JsonApiOauthExceptionRenderer extends DefaultOAuth2ExceptionRenderer { | ||
public JsonApiOauthExceptionRenderer() { | ||
setMessageConverters(createMessageConverters()); | ||
} | ||
|
||
private List<HttpMessageConverter<?>> createMessageConverters() { | ||
List<HttpMessageConverter<?>> result = new ArrayList<HttpMessageConverter<?>>(); | ||
result.add(new JsonApiOauthMessageConverter()); | ||
result.addAll(new RestTemplate().getMessageConverters()); | ||
result.add(new JaxbOAuth2ExceptionMessageConverter()); | ||
return result; | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
src/main/java/com/faforever/api/config/security/oauth2/JsonApiOauthMessageConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.faforever.api.config.security.oauth2; | ||
|
||
import com.faforever.api.error.ErrorResponse; | ||
import com.faforever.api.error.ErrorResult; | ||
import org.springframework.http.HttpOutputMessage; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.converter.HttpMessageNotWritableException; | ||
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; | ||
import org.springframework.security.oauth2.common.exceptions.OAuth2Exception; | ||
|
||
import java.io.IOException; | ||
|
||
|
||
public class JsonApiOauthMessageConverter extends MappingJackson2HttpMessageConverter { | ||
|
||
@Override | ||
protected void writeInternal(Object object, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException { | ||
super.writeInternal(transformObject(object), outputMessage); | ||
} | ||
|
||
protected Object transformObject(Object object) { | ||
ErrorResponse response = new ErrorResponse(); | ||
if (object instanceof OAuth2Exception) { | ||
OAuth2Exception oAuth2Exception = (OAuth2Exception) object; | ||
|
||
final ErrorResult newError = new ErrorResult( | ||
String.valueOf(oAuth2Exception.getHttpErrorCode()), | ||
oAuth2Exception.getOAuth2ErrorCode(), | ||
oAuth2Exception.getMessage() | ||
); | ||
response.addError(newError); | ||
newError.setMeta(ErrorResult.createMeta(null, oAuth2Exception.getAdditionalInformation()).orElse(null)); | ||
} else { | ||
response.addError(new ErrorResult( | ||
String.valueOf(HttpStatus.INTERNAL_SERVER_ERROR.value()), | ||
"Error", | ||
object.toString() | ||
)); | ||
} | ||
return response; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...rever/api/config/error/ErrorResponse.java → ...om/faforever/api/error/ErrorResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package com.faforever.api.config.error; | ||
package com.faforever.api.error; | ||
|
||
import lombok.Data; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package com.faforever.api.error; | ||
|
||
import com.fasterxml.jackson.annotation.JsonInclude; | ||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
import java.util.Map; | ||
import java.util.Optional; | ||
|
||
@Data | ||
@RequiredArgsConstructor | ||
@AllArgsConstructor | ||
@JsonInclude(JsonInclude.Include.NON_NULL) | ||
public class ErrorResult { | ||
@JsonProperty("status") | ||
private final String httpStatusCode; | ||
private final String title; | ||
private final String detail; | ||
@JsonProperty("code") | ||
private String appCode; | ||
private Meta meta; | ||
|
||
public static Optional<Meta> createMeta(Object[] args, Map<String, String> additionalInfo) { | ||
if ((args == null || args.length == 0) && (additionalInfo == null || additionalInfo.isEmpty())) { | ||
return Optional.empty(); | ||
} | ||
return Optional.of(new Meta(args, additionalInfo)); | ||
} | ||
|
||
@Data | ||
@RequiredArgsConstructor | ||
@AllArgsConstructor | ||
@JsonInclude(JsonInclude.Include.NON_NULL) | ||
public static class Meta { | ||
private Object[] args; | ||
private Map<String, String> additionalInfo; | ||
} | ||
} |
Oops, something went wrong.