From: Pramila Singh Date: Thu, 27 Jun 2013 00:26:53 +0000 (-0700) Subject: Added StatusCode to Response.Status Mapping X-Git-Tag: releasepom-0.1.0~328^2 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=commitdiff_plain;h=d20924de68a22e96a718d406139ea9b171c0c328 Added StatusCode to Response.Status Mapping Signed-off-by: Pramila Singh --- diff --git a/opendaylight/northbound/commons/src/main/java/org/opendaylight/controller/northbound/commons/utils/NorthboundUtils.java b/opendaylight/northbound/commons/src/main/java/org/opendaylight/controller/northbound/commons/utils/NorthboundUtils.java index 3fedd0c9f8..f98189319b 100644 --- a/opendaylight/northbound/commons/src/main/java/org/opendaylight/controller/northbound/commons/utils/NorthboundUtils.java +++ b/opendaylight/northbound/commons/src/main/java/org/opendaylight/controller/northbound/commons/utils/NorthboundUtils.java @@ -1,14 +1,74 @@ package org.opendaylight.controller.northbound.commons.utils; +import java.util.HashMap; +import java.util.Map; + +import javax.ws.rs.core.Response; + import org.opendaylight.controller.containermanager.IContainerAuthorization; import org.opendaylight.controller.sal.authorization.Privilege; import org.opendaylight.controller.sal.authorization.UserLevel; import org.opendaylight.controller.sal.utils.GlobalConstants; import org.opendaylight.controller.sal.utils.ServiceHelper; +import org.opendaylight.controller.sal.utils.Status; +import org.opendaylight.controller.sal.utils.StatusCode; import org.opendaylight.controller.usermanager.IUserManager; public class NorthboundUtils { + private static final Map ResponseStatusMapping = new HashMap() { + private static final long serialVersionUID = 1L; + { + put(StatusCode.SUCCESS, Response.Status.OK); + put(StatusCode.BADREQUEST, Response.Status.BAD_REQUEST); + put(StatusCode.UNAUTHORIZED, Response.Status.UNAUTHORIZED); + put(StatusCode.FORBIDDEN, Response.Status.FORBIDDEN); + put(StatusCode.NOTFOUND, Response.Status.NOT_FOUND); + put(StatusCode.NOTALLOWED, Response.Status.FORBIDDEN); + put(StatusCode.NOTACCEPTABLE, Response.Status.NOT_ACCEPTABLE); + put(StatusCode.TIMEOUT, Response.Status.GONE); + put(StatusCode.CONFLICT, Response.Status.CONFLICT); + put(StatusCode.GONE, Response.Status.GONE); + put(StatusCode.UNSUPPORTED, Response.Status.BAD_REQUEST); + put(StatusCode.INTERNALERROR, Response.Status.INTERNAL_SERVER_ERROR); + put(StatusCode.NOTIMPLEMENTED, Response.Status.NOT_ACCEPTABLE); + put(StatusCode.NOSERVICE, Response.Status.SERVICE_UNAVAILABLE); + put(StatusCode.UNDEFINED, Response.Status.BAD_REQUEST); + } + }; + + // Suppress default constructor for noninstantiability + private NorthboundUtils() { + } + + /** + * Returns Response.Status for a given status. If the status is null or if + * the corresponding StatusCode is not present in the ResponseStatusMapping, + * it returns null. + * + * @param status + * The Status + * @return The Response.Status for a given status + */ + public static Response.Status getResponseStatus(Status status) { + return ResponseStatusMapping.get(status.getCode()); + } + + /** + * Returns Response for a given status. If the status provided is null or if + * the corresponding StatusCode is not present in the ResponseStatusMapping, + * it returns Response with StatusType as INTERNAL_SERVER_ERROR. + * + * @param status + * The Status + * @return The Response for a given status. + */ + public static Response getResponse(Status status) { + if ((status == null) || (!ResponseStatusMapping.containsKey(status.getCode()))) { + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity("Action Result Unknown").build(); + } + return Response.status(ResponseStatusMapping.get(status.getCode())).entity(status.getDescription()).build(); + } /** * Returns whether the current user has the required privilege on the @@ -24,12 +84,10 @@ public class NorthboundUtils { * Class from where the function is invoked * @return The Status of the request, either Success or Unauthorized */ - public static boolean isAuthorized(String userName, String containerName, - Privilege required,Object bundle) { + public static boolean isAuthorized(String userName, String containerName, Privilege required, Object bundle) { - if (containerName.equals(GlobalConstants.DEFAULT.toString())) { - IUserManager auth = (IUserManager) ServiceHelper.getGlobalInstance( - IUserManager.class, bundle); + if (containerName.equals(GlobalConstants.DEFAULT.toString())) { + IUserManager auth = (IUserManager) ServiceHelper.getGlobalInstance(IUserManager.class, bundle); switch (required) { case WRITE: @@ -41,15 +99,14 @@ public class NorthboundUtils { } } else { - IContainerAuthorization auth = (IContainerAuthorization) ServiceHelper - .getGlobalInstance(IContainerAuthorization.class, bundle); + IContainerAuthorization auth = (IContainerAuthorization) ServiceHelper.getGlobalInstance( + IContainerAuthorization.class, bundle); if (auth == null) { return false; } - Privilege current = auth.getResourcePrivilege(userName, - containerName); + Privilege current = auth.getResourcePrivilege(userName, containerName); if (required.ordinal() > current.ordinal()) { return false; }