This is an automated email from the ASF dual-hosted git repository. dmitriusan pushed a commit to branch branch-2.7 in repository https://gitbox.apache.org/repos/asf/ambari.git
The following commit(s) were added to refs/heads/branch-2.7 by this push: new e4e6cd5 AMBARI-25204. Ambari returns stack trace in HTML doc when an error occurs retrieving details for an Ambari View resource that does not exist (#2912) e4e6cd5 is described below commit e4e6cd5842191c6b89e6cb0b2a006fbe19069e7b Author: Lisnichenko Dmitro <dmitriu...@apache.org> AuthorDate: Fri Apr 5 19:08:14 2019 +0300 AMBARI-25204. Ambari returns stack trace in HTML doc when an error occurs retrieving details for an Ambari View resource that does not exist (#2912) * AMBARI-25204. Ambari returns stack trace in HTML doc when an error occurs retrieving details for an Ambari View resource that does not exist (dlysnichenko) * AMBARI-25204. Ambari returns stack trace in HTML doc when an error occurs retrieving details for an Ambari View resource that does not exist. Add javadocs to fix checkstyle (dlysnichenko) * AMBARI-25204. Ambari returns stack trace in HTML doc when an error occurs retrieving details for an Ambari View resource that does not exist. Add javadocs to fix checkstyle (dlysnichenko) --- .../ambari/server/api/services/BaseService.java | 1 + .../api/services/views/ViewInstanceService.java | 140 ++++++++++++++++++++- 2 files changed, 137 insertions(+), 4 deletions(-) diff --git a/ambari-server/src/main/java/org/apache/ambari/server/api/services/BaseService.java b/ambari-server/src/main/java/org/apache/ambari/server/api/services/BaseService.java index b14ffa7..c439524 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/api/services/BaseService.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/api/services/BaseService.java @@ -55,6 +55,7 @@ public abstract class BaseService { public static final String MSG_INVALID_REQUEST = "Invalid request"; public static final String MSG_CLUSTER_NOT_FOUND = "Cluster not found"; public static final String MSG_CLUSTER_OR_HOST_NOT_FOUND = "Cluster or host not found"; + public static final String MSG_VIEW_NOT_FOUND = "View not found"; public static final String MSG_NOT_AUTHENTICATED = "Not authenticated"; public static final String MSG_PERMISSION_DENIED = "Not permitted to perform the operation"; public static final String MSG_SERVER_ERROR = "Internal server error"; diff --git a/ambari-server/src/main/java/org/apache/ambari/server/api/services/views/ViewInstanceService.java b/ambari-server/src/main/java/org/apache/ambari/server/api/services/views/ViewInstanceService.java index 788a63a..53e6742 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/api/services/views/ViewInstanceService.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/api/services/views/ViewInstanceService.java @@ -38,6 +38,10 @@ import javax.ws.rs.core.UriInfo; import org.apache.ambari.server.api.resources.ResourceInstance; import org.apache.ambari.server.api.services.BaseService; import org.apache.ambari.server.api.services.Request; +import org.apache.ambari.server.api.services.Result; +import org.apache.ambari.server.api.services.ResultImpl; +import org.apache.ambari.server.api.services.ResultStatus; +import org.apache.ambari.server.api.services.serializers.JsonSerializer; import org.apache.ambari.server.controller.ViewInstanceResponse; import org.apache.ambari.server.controller.spi.Resource; import org.apache.ambari.server.orm.entities.ViewInstanceEntity; @@ -316,19 +320,147 @@ public class ViewInstanceService extends BaseService { ViewRegistry.getInstance().getInstanceDefinition(viewName, version, instanceName); if (instanceDefinition == null) { - throw new IllegalArgumentException("A view instance " + - viewName + "/" + instanceName + " can not be found."); + String msg = "A view instance " + + viewName + "/" + instanceName + " can not be found."; + + return new NotFoundResponse(msg); } Object service = instanceDefinition.getService(resources); if (service == null) { - throw new IllegalArgumentException("A resource type " + resources + " for view instance " + - viewName + "/" + instanceName + " can not be found."); + String msg = "A resource type " + resources + " for view instance " + + viewName + "/" + instanceName + " can not be found."; + return new NotFoundResponse(msg); } return service; } + /** + * Stub class for 404 error response + * + */ + @Path("/") + public class NotFoundResponse { + + String msg; + + NotFoundResponse(String msg){ + this.msg=msg; + } + + /** + * Handles: GET /{resourceName} + * Handle GET resource with 404 response + * @return 404 response with msg + */ + @GET + @Produces(MediaType.TEXT_PLAIN) + @ApiOperation(value = "Handle GET resource with 404 response") + public Response get() { + return getResponse(); + } + + /** + * Handles: POST /{resourceName} + * Handle POST resource with 404 response + * @return 404 response with msg + */ + @POST + @Produces(MediaType.TEXT_PLAIN) + @ApiOperation(value = "Handle POST resource with 404 response") + public Response post() { + return getResponse(); + } + + /** + * Handles: PUT /{resourceName} + * Handle PUT resource with 404 response + * @return 404 response with msg + */ + @PUT + @Produces(MediaType.TEXT_PLAIN) + @ApiOperation(value = "Handle PUT resource with 404 response") + public Response put() { + return getResponse(); + } + + /** + * Handles: DELETE /{resourceName} + * Handle DELETE resource with 404 response + * @return 404 response with msg + */ + @DELETE + @Produces(MediaType.TEXT_PLAIN) + @ApiOperation(value = "Handle DELETE resource with 404 response") + public Response delete() { + return getResponse(); + } + + /** + * Handles: GET /{resourceName}/{.*} + * Handle GET sub-resource with 404 response + * @return 404 response with msg + */ + @GET + @Path("{path: .*}") + @Produces(MediaType.TEXT_PLAIN) + @ApiOperation(value = "Handle GET sub-resource with 404 response") + public Response getSub() { + return getResponse(); + } + + /** + * Handles: POST /{resourceName}/{.*} + * Handle POST sub-resource with 404 response + * @return 404 response with msg + */ + @POST + @Path("{path: .*}") + @Produces(MediaType.TEXT_PLAIN) + @ApiOperation(value = "Handle POST sub-resource with 404 response") + public Response postSub() { + return getResponse(); + } + + /** + * Handles: PUT /{resourceName}/{.*} + * Handle PUT sub-resource with 404 response + * @return 404 response with msg + */ + @PUT + @Path("{path: .*}") + @Produces(MediaType.TEXT_PLAIN) + @ApiOperation(value = "Handle PUT sub-resource with 404 response") + public Response putSub() { + return getResponse(); + } + + /** + * Handles: DELETE /{resourceName}/{.*} + * Handle DELETE sub-resource with 404 response + * @return 404 response with msg + */ + @DELETE + @Path("{path: .*}") + @Produces(MediaType.TEXT_PLAIN) + @ApiOperation(value = "Handle DELETE sub-resource with 404 response") + public Response deleteSub() { + return getResponse(); + } + + /** + * Build 404 response with msg + * @return 404 response with msg + */ + public Response getResponse() { + Result result = new ResultImpl(new ResultStatus(ResultStatus.STATUS.NOT_FOUND, msg)); + Response.ResponseBuilder builder = Response.status(result.getStatus().getStatusCode()).entity(new JsonSerializer().serialize(result)); + return builder.build(); + } + + } + // ----- helper methods ---------------------------------------------------- /**