Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #139 from TAMULib/2.x-unwrap
[Issue 138] Afford unwrapping API response
- Loading branch information
Showing
17 changed files
with
130 additions
and
17 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,4 +32,4 @@ public static <T> T bean(String name) { | |
return (T) context.getBean(name); | ||
} | ||
|
||
} | ||
} |
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
16 changes: 16 additions & 0 deletions
16
core/src/main/java/edu/tamu/weaver/response/ApiResponseAdvice.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,16 @@ | ||
package edu.tamu.weaver.response; | ||
|
||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.ControllerAdvice; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import org.springframework.web.bind.annotation.ResponseBody; | ||
|
||
@ControllerAdvice | ||
public class ApiResponseAdvice { | ||
|
||
@ExceptionHandler(ApiResponseStatusException.class) | ||
public @ResponseBody ResponseEntity<String> handleApiResponseStatusException(ApiResponseStatusException e) { | ||
return ResponseEntity.status(e.getStatus()).body(e.getMessage()); | ||
} | ||
|
||
} |
62 changes: 62 additions & 0 deletions
62
core/src/main/java/edu/tamu/weaver/response/ApiResponseSerializer.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,62 @@ | ||
package edu.tamu.weaver.response; | ||
|
||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
|
||
import com.fasterxml.jackson.core.JsonGenerator; | ||
import com.fasterxml.jackson.databind.JsonSerializer; | ||
import com.fasterxml.jackson.databind.SerializerProvider; | ||
|
||
import org.springframework.boot.jackson.JsonComponent; | ||
import org.springframework.web.context.request.RequestAttributes; | ||
import org.springframework.web.context.request.RequestContextHolder; | ||
import org.springframework.web.context.request.ServletRequestAttributes; | ||
|
||
import edu.tamu.weaver.response.ApiResponse.Meta; | ||
|
||
@JsonComponent | ||
public class ApiResponseSerializer extends JsonSerializer<ApiResponse> { | ||
|
||
@Override | ||
public void serialize(ApiResponse apiResponse, JsonGenerator jgen, SerializerProvider serializers) | ||
throws IOException { | ||
RequestAttributes attributes = RequestContextHolder.getRequestAttributes(); | ||
HttpServletRequest request = ((ServletRequestAttributes) attributes).getRequest(); | ||
if (request.getHeader("x-wvr-unwrap") != null) { | ||
|
||
Meta meta = apiResponse.getMeta(); | ||
String message = meta.getMessage(); | ||
switch (meta.getStatus()) { | ||
case ERROR: | ||
throw new ApiResponseStatusException(message, 500); | ||
case INVALID: | ||
throw new ApiResponseStatusException(message, 400); | ||
case REFRESH: | ||
case UNAUTHORIZED: | ||
throw new ApiResponseStatusException(message, 401); | ||
case INFO: | ||
case SUCCESS: | ||
case WARNING: | ||
default: | ||
break; | ||
} | ||
|
||
List<Object> values = new ArrayList<>(apiResponse.getPayload().values()); | ||
if (values.size() > 1) { | ||
jgen.writeObject(apiResponse.getPayload()); | ||
} else if (values.size() > 0) { | ||
jgen.writeObject(values.get(0)); | ||
} | ||
|
||
} else { | ||
jgen.writeStartObject(); | ||
jgen.writeObjectField("meta", apiResponse.getMeta()); | ||
jgen.writeObjectField("payload", apiResponse.getPayload()); | ||
jgen.writeEndObject(); | ||
} | ||
} | ||
|
||
} |
20 changes: 20 additions & 0 deletions
20
core/src/main/java/edu/tamu/weaver/response/ApiResponseStatusException.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,20 @@ | ||
package edu.tamu.weaver.response; | ||
|
||
public class ApiResponseStatusException extends RuntimeException { | ||
|
||
private int status; | ||
|
||
public ApiResponseStatusException(String message, int status) { | ||
super(message); | ||
this.status = status; | ||
} | ||
|
||
public int getStatus() { | ||
return status; | ||
} | ||
|
||
public void setStatus(int status) { | ||
this.status = status; | ||
} | ||
|
||
} |
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
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
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
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