Skip to content

Commit

Permalink
Merge pull request #139 from TAMULib/2.x-unwrap
Browse files Browse the repository at this point in the history
[Issue 138] Afford unwrapping API response
  • Loading branch information
William Welling committed Apr 12, 2022
2 parents 504448e + 44cd925 commit 510cc44
Show file tree
Hide file tree
Showing 17 changed files with 130 additions and 17 deletions.
2 changes: 1 addition & 1 deletion auth/pom.xml
Expand Up @@ -12,7 +12,7 @@
<parent>
<groupId>edu.tamu.weaver</groupId>
<artifactId>webservice-parent</artifactId>
<version>2.1.1-RC5</version>
<version>2.1.1-RC6</version>
</parent>

<dependencies>
Expand Down
21 changes: 17 additions & 4 deletions core/pom.xml
@@ -1,4 +1,5 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

<modelVersion>4.0.0</modelVersion>
Expand All @@ -12,11 +13,23 @@
<parent>
<groupId>edu.tamu.weaver</groupId>
<artifactId>webservice-parent</artifactId>
<version>2.1.1-RC5</version>
<version>2.1.1-RC6</version>
</parent>

<dependencies>

<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>3.0-alpha-1</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
Expand All @@ -28,8 +41,8 @@
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-json</artifactId>
</dependency>


Expand Down
Expand Up @@ -32,4 +32,4 @@ public static <T> T bean(String name) {
return (T) context.getBean(name);
}

}
}
2 changes: 2 additions & 0 deletions core/src/main/java/edu/tamu/weaver/response/ApiResponse.java
Expand Up @@ -4,6 +4,7 @@
import java.util.HashMap;

import com.fasterxml.jackson.annotation.JsonView;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;

/**
* Abstract class for an API response.
Expand All @@ -15,6 +16,7 @@
* @author <a href="mailto:wwelling@library.tamu.edu">William Welling</a>
*
*/
@JsonSerialize(using = ApiResponseSerializer.class)
public class ApiResponse {

@JsonView(ApiView.Partial.class)
Expand Down
16 changes: 16 additions & 0 deletions core/src/main/java/edu/tamu/weaver/response/ApiResponseAdvice.java
@@ -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());
}

}
@@ -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();
}
}

}
@@ -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;
}

}
2 changes: 1 addition & 1 deletion data/pom.xml
Expand Up @@ -12,7 +12,7 @@
<parent>
<groupId>edu.tamu.weaver</groupId>
<artifactId>webservice-parent</artifactId>
<version>2.1.1-RC5</version>
<version>2.1.1-RC6</version>
</parent>

<dependencies>
Expand Down
2 changes: 1 addition & 1 deletion email/pom.xml
Expand Up @@ -12,7 +12,7 @@
<parent>
<groupId>edu.tamu.weaver</groupId>
<artifactId>webservice-parent</artifactId>
<version>2.1.1-RC5</version>
<version>2.1.1-RC6</version>
</parent>

<dependencies>
Expand Down
2 changes: 1 addition & 1 deletion messaging/pom.xml
Expand Up @@ -12,7 +12,7 @@
<parent>
<groupId>edu.tamu.weaver</groupId>
<artifactId>webservice-parent</artifactId>
<version>2.1.1-RC5</version>
<version>2.1.1-RC6</version>
</parent>

<dependencies>
Expand Down
4 changes: 2 additions & 2 deletions pom.xml
Expand Up @@ -7,7 +7,7 @@

<artifactId>webservice-parent</artifactId>

<version>2.1.1-RC5</version>
<version>2.1.1-RC6</version>

<name>Weaver Webservice Parent</name>

Expand Down Expand Up @@ -87,7 +87,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
<version>3.2.0</version>
<configuration>
<delimiters>
<delimiter>@</delimiter>
Expand Down
2 changes: 1 addition & 1 deletion reporting/pom.xml
Expand Up @@ -12,7 +12,7 @@
<parent>
<groupId>edu.tamu.weaver</groupId>
<artifactId>webservice-parent</artifactId>
<version>2.1.1-RC5</version>
<version>2.1.1-RC6</version>
</parent>

<dependencies>
Expand Down
2 changes: 1 addition & 1 deletion token-provider/pom.xml
Expand Up @@ -13,7 +13,7 @@
<parent>
<groupId>edu.tamu.weaver</groupId>
<artifactId>webservice-parent</artifactId>
<version>2.1.1-RC5</version>
<version>2.1.1-RC6</version>
</parent>

<dependencies>
Expand Down
2 changes: 1 addition & 1 deletion token/pom.xml
Expand Up @@ -12,7 +12,7 @@
<parent>
<groupId>edu.tamu.weaver</groupId>
<artifactId>webservice-parent</artifactId>
<version>2.1.1-RC5</version>
<version>2.1.1-RC6</version>
</parent>

<dependencies>
Expand Down
2 changes: 1 addition & 1 deletion user/pom.xml
Expand Up @@ -12,7 +12,7 @@
<parent>
<groupId>edu.tamu.weaver</groupId>
<artifactId>webservice-parent</artifactId>
<version>2.1.1-RC5</version>
<version>2.1.1-RC6</version>
</parent>

<dependencies>
Expand Down
2 changes: 1 addition & 1 deletion validation/pom.xml
Expand Up @@ -12,7 +12,7 @@
<parent>
<groupId>edu.tamu.weaver</groupId>
<artifactId>webservice-parent</artifactId>
<version>2.1.1-RC5</version>
<version>2.1.1-RC6</version>
</parent>

<dependencies>
Expand Down
2 changes: 1 addition & 1 deletion wro/pom.xml
Expand Up @@ -13,7 +13,7 @@
<parent>
<groupId>edu.tamu.weaver</groupId>
<artifactId>webservice-parent</artifactId>
<version>2.1.1-RC5</version>
<version>2.1.1-RC6</version>
</parent>

<dependencies>
Expand Down

0 comments on commit 510cc44

Please sign in to comment.