You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@eagle.apache.org by ha...@apache.org on 2016/07/27 10:46:13 UTC
incubator-eagle git commit: [EAGLE-399] Implement consistent
RESTResponse Wrapper for operation (CREAT/MODIFY/DELETE) request
Repository: incubator-eagle
Updated Branches:
refs/heads/develop 043a103fd -> c017fe4cd
[EAGLE-399] Implement consistent RESTResponse Wrapper for operation (CREAT/MODIFY/DELETE) request
https://issues.apache.org/jira/browse/EAGLE-399
Author: Hao Chen <ha...@apache.org>
Closes #281 from haoch/EAGLE-399.
Project: http://git-wip-us.apache.org/repos/asf/incubator-eagle/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-eagle/commit/c017fe4c
Tree: http://git-wip-us.apache.org/repos/asf/incubator-eagle/tree/c017fe4c
Diff: http://git-wip-us.apache.org/repos/asf/incubator-eagle/diff/c017fe4c
Branch: refs/heads/develop
Commit: c017fe4cdfcba0c95b9a32f665e8314a8bf99271
Parents: 043a103
Author: Hao Chen <ha...@apache.org>
Authored: Wed Jul 27 18:45:54 2016 +0800
Committer: Hao Chen <ha...@apache.org>
Committed: Wed Jul 27 18:45:54 2016 +0800
----------------------------------------------------------------------
.../eagle/app/resource/ApplicationResource.java | 39 +++++--
.../apache/eagle/app/test/AppSimulatorImpl.java | 2 +-
.../eagle/app/TestApplicationTestSuite.java | 2 +-
.../eagle/metadata/resource/RestResponse.java | 107 +++++--------------
.../eagle/metadata/resource/SiteResource.java | 10 +-
.../app/example/ExampleApplicationTest.java | 2 +-
6 files changed, 68 insertions(+), 94 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-eagle/blob/c017fe4c/eagle-core/eagle-app/eagle-app-base/src/main/java/org/apache/eagle/app/resource/ApplicationResource.java
----------------------------------------------------------------------
diff --git a/eagle-core/eagle-app/eagle-app-base/src/main/java/org/apache/eagle/app/resource/ApplicationResource.java b/eagle-core/eagle-app/eagle-app-base/src/main/java/org/apache/eagle/app/resource/ApplicationResource.java
index b0e9988..7878587 100644
--- a/eagle-core/eagle-app/eagle-app-base/src/main/java/org/apache/eagle/app/resource/ApplicationResource.java
+++ b/eagle-core/eagle-app/eagle-app-base/src/main/java/org/apache/eagle/app/resource/ApplicationResource.java
@@ -23,6 +23,7 @@ import org.apache.eagle.app.service.ApplicationOperations;
import org.apache.eagle.app.service.ApplicationProviderService;
import org.apache.eagle.metadata.model.ApplicationDesc;
import org.apache.eagle.metadata.model.ApplicationEntity;
+import org.apache.eagle.metadata.resource.RESTResponse;
import org.apache.eagle.metadata.service.ApplicationEntityService;
import javax.ws.rs.*;
@@ -62,9 +63,12 @@ public class ApplicationResource {
@PUT
@Path("/providers/reload")
@Produces(MediaType.APPLICATION_JSON)
- public Collection<ApplicationDesc> reloadApplicationDescs(){
- providerService.reload();
- return providerService.getApplicationDescs();
+ public RESTResponse<Collection<ApplicationDesc>> reloadApplicationDescs(){
+ return RESTResponse.<Collection<ApplicationDesc>>async((builder)-> {
+ providerService.reload();
+ builder.message("Successfully reload application providers");
+ builder.data(providerService.getApplicationDescs());
+ }).get();
}
@GET
@@ -97,8 +101,12 @@ public class ApplicationResource {
@Path("/install")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
- public ApplicationEntity installApplication(ApplicationOperations.InstallOperation operation){
- return applicationManagementService.install(operation);
+ public RESTResponse<ApplicationEntity> installApplication(ApplicationOperations.InstallOperation operation){
+ return RESTResponse.<ApplicationEntity>async((builder)-> {
+ ApplicationEntity entity = applicationManagementService.install(operation);
+ builder.message("Successfully installed application "+operation.getAppType()+" onto site "+operation.getSiteId());
+ builder.data(entity);
+ }).get();
}
/**
@@ -115,8 +123,11 @@ public class ApplicationResource {
@Path("/uninstall")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
- public ApplicationEntity uninstallApplication(ApplicationOperations.UninstallOperation operation){
- return applicationManagementService.uninstall(operation);
+ public RESTResponse<Void> uninstallApplication(ApplicationOperations.UninstallOperation operation){
+ return RESTResponse.<Void>async((builder)-> {
+ ApplicationEntity entity = applicationManagementService.uninstall(operation);
+ builder.success(true).message("Successfully uninstalled application "+entity.getUuid());
+ }).get();
}
/**
@@ -132,8 +143,11 @@ public class ApplicationResource {
@Path("/start")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
- public ApplicationEntity startApplication(ApplicationOperations.StartOperation operation){
- return applicationManagementService.start(operation);
+ public RESTResponse<Void> startApplication(ApplicationOperations.StartOperation operation){
+ return RESTResponse.<Void>async((builder)-> {
+ ApplicationEntity entity = applicationManagementService.start(operation);
+ builder.success(true).message("Successfully started application "+entity.getUuid());
+ }).get();
}
/**
@@ -149,7 +163,10 @@ public class ApplicationResource {
@Path("/stop")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
- public ApplicationEntity stopApplication(ApplicationOperations.StopOperation operation){
- return applicationManagementService.stop(operation);
+ public RESTResponse<Void> stopApplication(ApplicationOperations.StopOperation operation){
+ return RESTResponse.<Void>async((builder)-> {
+ ApplicationEntity entity = applicationManagementService.stop(operation);
+ builder.success(true).message("Successfully stopped application "+entity.getUuid());
+ }).get();
}
}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-eagle/blob/c017fe4c/eagle-core/eagle-app/eagle-app-base/src/main/java/org/apache/eagle/app/test/AppSimulatorImpl.java
----------------------------------------------------------------------
diff --git a/eagle-core/eagle-app/eagle-app-base/src/main/java/org/apache/eagle/app/test/AppSimulatorImpl.java b/eagle-core/eagle-app/eagle-app-base/src/main/java/org/apache/eagle/app/test/AppSimulatorImpl.java
index 2e4d33b..14edbf8 100644
--- a/eagle-core/eagle-app/eagle-app-base/src/main/java/org/apache/eagle/app/test/AppSimulatorImpl.java
+++ b/eagle-core/eagle-app/eagle-app-base/src/main/java/org/apache/eagle/app/test/AppSimulatorImpl.java
@@ -55,7 +55,7 @@ public class AppSimulatorImpl extends ApplicationSimulator {
siteResource.createSite(siteEntity);
Assert.assertNotNull(siteEntity.getUuid());
// Install application
- ApplicationEntity applicationEntity = applicationResource.installApplication(new ApplicationOperations.InstallOperation(siteEntity.getSiteId(),appType, ApplicationEntity.Mode.LOCAL));
+ ApplicationEntity applicationEntity = applicationResource.installApplication(new ApplicationOperations.InstallOperation(siteEntity.getSiteId(),appType, ApplicationEntity.Mode.LOCAL)).getData();
// Start application
applicationResource.startApplication(new ApplicationOperations.StartOperation(applicationEntity.getUuid()));
}
http://git-wip-us.apache.org/repos/asf/incubator-eagle/blob/c017fe4c/eagle-core/eagle-app/eagle-app-base/src/test/java/org/apache/eagle/app/TestApplicationTestSuite.java
----------------------------------------------------------------------
diff --git a/eagle-core/eagle-app/eagle-app-base/src/test/java/org/apache/eagle/app/TestApplicationTestSuite.java b/eagle-core/eagle-app/eagle-app-base/src/test/java/org/apache/eagle/app/TestApplicationTestSuite.java
index 3014b16..9688951 100644
--- a/eagle-core/eagle-app/eagle-app-base/src/test/java/org/apache/eagle/app/TestApplicationTestSuite.java
+++ b/eagle-core/eagle-app/eagle-app-base/src/test/java/org/apache/eagle/app/TestApplicationTestSuite.java
@@ -56,7 +56,7 @@ public class TestApplicationTestSuite {
Assert.assertNotNull(siteEntity.getUuid());
// Install application
- ApplicationEntity applicationEntity = applicationResource.installApplication(new ApplicationOperations.InstallOperation("test_site","TEST_APPLICATION", ApplicationEntity.Mode.LOCAL));
+ ApplicationEntity applicationEntity = applicationResource.installApplication(new ApplicationOperations.InstallOperation("test_site","TEST_APPLICATION", ApplicationEntity.Mode.LOCAL)).getData();
// Start application
applicationResource.startApplication(new ApplicationOperations.StartOperation(applicationEntity.getUuid()));
// Stop application
http://git-wip-us.apache.org/repos/asf/incubator-eagle/blob/c017fe4c/eagle-core/eagle-metadata/eagle-metadata-base/src/main/java/org/apache/eagle/metadata/resource/RestResponse.java
----------------------------------------------------------------------
diff --git a/eagle-core/eagle-metadata/eagle-metadata-base/src/main/java/org/apache/eagle/metadata/resource/RestResponse.java b/eagle-core/eagle-metadata/eagle-metadata-base/src/main/java/org/apache/eagle/metadata/resource/RestResponse.java
index 995693a..4431c8c 100644
--- a/eagle-core/eagle-metadata/eagle-metadata-base/src/main/java/org/apache/eagle/metadata/resource/RestResponse.java
+++ b/eagle-core/eagle-metadata/eagle-metadata-base/src/main/java/org/apache/eagle/metadata/resource/RestResponse.java
@@ -16,24 +16,21 @@
*/
package org.apache.eagle.metadata.resource;
-import com.fasterxml.jackson.annotation.JsonInclude;
+import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import org.apache.commons.lang3.exception.ExceptionUtils;
-import org.apache.commons.lang3.time.StopWatch;
+import javax.ws.rs.WebApplicationException;
+import javax.ws.rs.core.Response;
import java.util.concurrent.CompletableFuture;
import java.util.function.Consumer;
import java.util.function.Supplier;
-import javax.servlet.http.HttpServletRequest;
-import javax.ws.rs.core.Response;
-@JsonInclude(JsonInclude.Include.NON_NULL)
-public class RestResponse<T>{
- private Long timestamp;
+@JsonSerialize(include= JsonSerialize.Inclusion.NON_NULL)
+public class RESTResponse<T>{
private boolean success = false;
private String message;
private String exception;
private T data;
- private Long time;
public T getData() {
return data;
@@ -64,27 +61,23 @@ public class RestResponse<T>{
}
public static <E> RestResponseBuilder<E> of(E data){
- return RestResponse.<E>builder().data(data);
+ return RESTResponse.<E>builder().data(data);
}
public static <E> RestResponseBuilder<E> of(Consumer<RestResponseBuilder<E>> func){
- return RestResponse.<E>builder().of(func);
+ return RESTResponse.<E>builder().of(func);
}
public static <E> RestResponseBuilder<E> of(Supplier<E> func){
- return RestResponse.<E>builder().of(func);
+ return RESTResponse.<E>builder().of(func);
}
public static <E> RestResponseBuilder<E> async(UnhandledSupplier<E,Exception> func) {
- return RestResponse.<E>builder().async(func);
+ return RESTResponse.<E>builder().async(func);
}
public static <E> RestResponseBuilder<E> async(UnhandledConsumer<RestResponseBuilder<E>, Exception> func){
- return RestResponse.<E>builder().async(func);
- }
-
- public static <E> RestResponseBuilder<E> verbose(boolean verbose) {
- return RestResponse.<E>builder().verbose(verbose);
+ return RESTResponse.<E>builder().async(func);
}
public String getException() {
@@ -99,36 +92,21 @@ public class RestResponse<T>{
this.exception = exception;
}
- public Long getTimestamp() {
- return timestamp;
- }
-
- public void setTimestamp(Long timestamp) {
- this.timestamp = timestamp;
- }
-
- public Long getTime() {
- return time;
- }
-
- public void setTime(Long time) {
- this.time = time;
- }
public static class RestResponseBuilder<E>{
- RestResponse<E> current;
- Response.Status status = Response.Status.OK;
- boolean verbose = true;
-
- public RestResponseBuilder(){
- current = new RestResponse<>();
- }
+ private RESTResponse current = new RESTResponse();
+ private Response.Status status = Response.Status.OK;
public RestResponseBuilder<E> success(boolean success){
this.current.setSuccess(success);
return this;
}
+ public RestResponseBuilder<E> status(Response.Status status){
+ this.status = status;
+ return this;
+ }
+
public RestResponseBuilder<E> message(String message){
this.current.setMessage(message);
return this;
@@ -139,54 +117,28 @@ public class RestResponse<T>{
return this;
}
- public RestResponseBuilder<E> status(Response.Status status){
- this.status = status;
- return this;
- }
public RestResponseBuilder<E> exception(Throwable exception){
this.current.setThrowable(exception);
return this;
}
- public RestResponseBuilder<E> type(Class<?> clazz){
- return this;
- }
-
- public RestResponseBuilder<E> spend(Long spendMillis){
- this.current.setTime(spendMillis);
- return this;
- }
-
- public RestResponseBuilder<E> verbose(boolean verbose){
- this.verbose = verbose;
- return this;
- }
-
public RestResponseBuilder<E> of(Consumer<RestResponseBuilder<E>> func){
- StopWatch stopWatch = new StopWatch();
try {
- stopWatch.start();
this.success(true).status(Response.Status.OK);
func.accept(this);
} catch (Exception ex){
this.success(false).data(null).status(Response.Status.INTERNAL_SERVER_ERROR).message(ex.getMessage());
- } finally {
- stopWatch.stop();
- this.spend(stopWatch.getTime());
+ raiseWebAppException(ex);
}
return this;
}
public RestResponseBuilder<E> of(Supplier<E> func){
- StopWatch stopWatch = new StopWatch();
try {
- stopWatch.start();
this.success(true).status(Response.Status.OK).data(func.get());
} catch (Throwable ex){
this.success(false).status(Response.Status.INTERNAL_SERVER_ERROR).message(ex.getMessage());
- } finally {
- stopWatch.stop();
- this.spend(stopWatch.getTime());
+ raiseWebAppException(ex);
}
return this;
}
@@ -197,6 +149,7 @@ public class RestResponse<T>{
this.status(Response.Status.OK).success(true).data(func.get());
} catch (Throwable e) {
this.success(false).status(Response.Status.INTERNAL_SERVER_ERROR).message(e.getMessage()).exception(e);
+ raiseWebAppException(e);
}
});
runAsync(future);
@@ -204,9 +157,7 @@ public class RestResponse<T>{
}
private void runAsync(CompletableFuture future){
- StopWatch stopWatch = new StopWatch();
try {
- stopWatch.start();
future.get();
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
@@ -214,12 +165,14 @@ public class RestResponse<T>{
this.success(false).status(Response.Status.INTERNAL_SERVER_ERROR).message(ex.getMessage()).exception(ex);
} catch (Throwable ex) {
this.success(false).status(Response.Status.INTERNAL_SERVER_ERROR).message(ex.getMessage()).exception(ex);
- } finally {
- stopWatch.stop();
- this.spend(stopWatch.getTime());
+ raiseWebAppException(ex);
}
}
+ private void raiseWebAppException(Throwable ex){
+ throw new WebApplicationException(ex,Response.status(this.status).entity(this.current).build());
+ }
+
public RestResponseBuilder<E> async(UnhandledConsumer<RestResponseBuilder<E>, Exception> func){
CompletableFuture future = CompletableFuture.runAsync(() -> {
try {
@@ -227,6 +180,7 @@ public class RestResponse<T>{
this.success(true);
} catch (Throwable ex) {
this.success(false).status(Response.Status.INTERNAL_SERVER_ERROR).message(ex.getMessage()).exception(ex);
+ raiseWebAppException(ex);
}
});
runAsync(future);
@@ -238,16 +192,13 @@ public class RestResponse<T>{
func.accept(this);
} catch (Throwable ex) {
this.success(false).status(Response.Status.INTERNAL_SERVER_ERROR).message(ex.getMessage()).exception(ex);
+ raiseWebAppException(ex);
}
return this;
}
- public Response get(){
- this.current.setTimestamp(System.currentTimeMillis());
- if(!this.verbose){
- this.current.setException(null);
- }
- return Response.status(this.status).entity(this.current).build();
+ public RESTResponse<E> get(){
+ return current;
}
public RestResponseBuilder<E> status(boolean success, Response.Status status) {
http://git-wip-us.apache.org/repos/asf/incubator-eagle/blob/c017fe4c/eagle-core/eagle-metadata/eagle-metadata-base/src/main/java/org/apache/eagle/metadata/resource/SiteResource.java
----------------------------------------------------------------------
diff --git a/eagle-core/eagle-metadata/eagle-metadata-base/src/main/java/org/apache/eagle/metadata/resource/SiteResource.java b/eagle-core/eagle-metadata/eagle-metadata-base/src/main/java/org/apache/eagle/metadata/resource/SiteResource.java
index 0e09e7c..bf642c2 100644
--- a/eagle-core/eagle-metadata/eagle-metadata-base/src/main/java/org/apache/eagle/metadata/resource/SiteResource.java
+++ b/eagle-core/eagle-metadata/eagle-metadata-base/src/main/java/org/apache/eagle/metadata/resource/SiteResource.java
@@ -25,6 +25,8 @@ import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import java.util.Collection;
+import static org.apache.eagle.metadata.resource.RESTResponse.async;
+
@Path("/sites")
@Singleton
public class SiteResource {
@@ -46,8 +48,12 @@ public class SiteResource {
@Path("/")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
- public SiteEntity createSite(SiteEntity siteEntity){
- return siteEntityService.create(siteEntity);
+ public RESTResponse<SiteEntity> createSite(SiteEntity siteEntity){
+ return RESTResponse.<SiteEntity>async((builder)-> {
+ SiteEntity entity = siteEntityService.create(siteEntity);
+ builder.message("Successfully created site (siteId:"+entity.getSiteId()+", uuid: "+entity.getUuid()+")");
+ builder.data(entity);
+ }).get();
}
@GET
http://git-wip-us.apache.org/repos/asf/incubator-eagle/blob/c017fe4c/eagle-examples/eagle-app-example/src/test/java/org/apache/eagle/app/example/ExampleApplicationTest.java
----------------------------------------------------------------------
diff --git a/eagle-examples/eagle-app-example/src/test/java/org/apache/eagle/app/example/ExampleApplicationTest.java b/eagle-examples/eagle-app-example/src/test/java/org/apache/eagle/app/example/ExampleApplicationTest.java
index 51b8c60..d4cc082 100644
--- a/eagle-examples/eagle-app-example/src/test/java/org/apache/eagle/app/example/ExampleApplicationTest.java
+++ b/eagle-examples/eagle-app-example/src/test/java/org/apache/eagle/app/example/ExampleApplicationTest.java
@@ -64,7 +64,7 @@ public class ExampleApplicationTest {
Assert.assertNotNull(siteEntity.getUuid());
// Install application
- ApplicationEntity applicationEntity = applicationResource.installApplication(new ApplicationOperations.InstallOperation("test_site","EXAMPLE_APPLICATION", ApplicationEntity.Mode.LOCAL));
+ ApplicationEntity applicationEntity = applicationResource.installApplication(new ApplicationOperations.InstallOperation("test_site","EXAMPLE_APPLICATION", ApplicationEntity.Mode.LOCAL)).getData();
// Start application
applicationResource.startApplication(new ApplicationOperations.StartOperation(applicationEntity.getUuid()));
// Stop application