org.opentestsystem.shared.permissions.rest.APIHandler.java Source code

Java tutorial

Introduction

Here is the source code for org.opentestsystem.shared.permissions.rest.APIHandler.java

Source

/*******************************************************************************
 * Educational Online Test Delivery System
 * Copyright (c) 2013 American Institutes for Research
 * 
 * Distributed under the AIR Open Source License, Version 1.0
 * See accompanying file AIR-License-1_0.txt or at 
 * http://www.smarterapp.org/documents/American_Institutes_for_Research_Open_Source_Software_License.pdf
 ******************************************************************************/
package org.opentestsystem.shared.permissions.rest;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.http.client.HttpResponseException;
import org.opentestsystem.shared.permissions.data.Component;
import org.opentestsystem.shared.permissions.data.Permission;
import org.opentestsystem.shared.permissions.data.Role;
import org.opentestsystem.shared.permissions.exceptions.PermissionsRetrievalException;
import org.opentestsystem.shared.permissions.rest.data.ReturnStatus;
import org.opentestsystem.shared.permissions.rest.data.StatusEnum;
import org.opentestsystem.shared.progman.client.domain.Tenant;
import org.opentestsystem.shared.security.service.UserService;
import org.springframework.security.access.annotation.Secured;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import AIR.Common.Utilities.SpringApplicationContext;

@Controller
//@RequestMapping("/rest/")
public class APIHandler extends AbstractHandler {
    @RequestMapping(value = "role", method = RequestMethod.GET)
    @ResponseBody
    //@Secured({ "ROLE_Administrator Read" })
    public ReturnStatus<List<Role>> getRoles(@RequestParam(value = "role", required = false) String roleName,
            @RequestParam(value = "component", required = false) String componentName,
            @RequestParam(value = "permission", required = false) String permissionName)
            throws PermissionsRetrievalException, HttpResponseException {
        List<Role> roles;
        if (!StringUtils.isEmpty(componentName)) {
            //if the component does not exist
            if (getPersister().getComponentByComponentName(componentName) == null) {
                throw (new HttpResponseException(404,
                        String.format("Role by component:'%s' is not found.", componentName)));
            }
            if (!StringUtils.isEmpty(permissionName)) {
                // We are going to return roles by component name and
                // permission.
                roles = getPersister().getRoleByComponentandPermission(componentName, permissionName);
            } else {
                // just by component name
                roles = getPersister().getRoleByComponent(componentName);
            }
        } else if (!StringUtils.isEmpty(roleName)) {
            // we have been passed a role name.
            roles = new ArrayList<Role>();
            Role role = getPersister().getRoleByRoleName(roleName);
            if (role == null) {
                throw (new HttpResponseException(404, String.format("Role:'%s' is not found.", roleName)));
            }
            roles.add(role);
        } else {
            // no parameters have been passed. return all roles.
            roles = new ArrayList<Role>();
            roles.addAll(getPersister().getAllRoles());
        }
        ReturnStatus<List<Role>> status = new ReturnStatus<List<Role>>(StatusEnum.SUCCESS, null);
        status.setValue(roles);
        return status;
    }

    @RequestMapping(value = "getTenants", method = RequestMethod.GET)
    @ResponseBody
    public List<Tenant> getTenants() throws PermissionsRetrievalException, HttpResponseException {
        UserService _userService = SpringApplicationContext.getBean(UserService.class);
        return _userService.getUniqueTenantsForUser().getTenants();
    }

    @RequestMapping(value = "getAssetsForTenant", method = RequestMethod.GET)
    @ResponseBody
    public List<Map<String, String>> getAssetsForTenant()
            throws PermissionsRetrievalException, HttpResponseException {
        List<Map<String, String>> assetsList = null;
        UserService _userService = SpringApplicationContext.getBean(UserService.class);
        List<Tenant> tenantsList = _userService.getUniqueTenantsForUser().getTenants();
        if (tenantsList != null && tenantsList.size() == 1) {
            Tenant tenant = tenantsList.get(0);

            Map<String, Object> assets = _userService.getAssetsForTenant(tenant.getId());
            if (assets != null && !assets.isEmpty()) {
                assetsList = (List<Map<String, String>>) assets.get("assets");
            }
        }
        return assetsList;
    }

    @RequestMapping(value = "component", method = RequestMethod.GET)
    @ResponseBody
    //@Secured({ "ROLE_Administrator Read" })
    public ReturnStatus<List<Component>> getComponents(
            @RequestParam(value = "role", required = false) String roleName,
            @RequestParam(value = "component", required = false) String componentName)
            throws PermissionsRetrievalException, HttpResponseException {
        List<Component> components;
        if (!StringUtils.isEmpty(roleName)) {
            if (getPersister().getRoleByRoleName(roleName) == null) {
                throw (new HttpResponseException(404,
                        String.format("Component by role:'%s' is not found.", roleName)));
            }
            components = getPersister().getComponentByRole(roleName);
        } else if (!StringUtils.isEmpty(componentName)) {
            components = new ArrayList<Component>();
            Component component = getPersister().getComponentByComponentName(componentName);
            if (component == null) {
                throw (new HttpResponseException(404,
                        String.format("Component:'%s' is not found.", componentName)));
            }
            components.add(component);
        } else
            components = (List<Component>) getPersister().getAllComponents();
        ReturnStatus<List<Component>> status = new ReturnStatus<List<Component>>(StatusEnum.SUCCESS, null);
        status.setValue(components);
        return status;
    }

    @RequestMapping(value = "permission", method = RequestMethod.GET)
    @ResponseBody
    //@Secured({ "ROLE_Administrator Read" })
    public ReturnStatus<List<Permission>> getPermission(
            @RequestParam(value = "role", required = false) String roleName,
            @RequestParam(value = "component", required = false) String componentName)
            throws PermissionsRetrievalException, HttpResponseException {
        List<Permission> listP;

        if (!StringUtils.isEmpty(roleName) && !StringUtils.isEmpty(componentName)) {
            listP = getPersister().getPermissionByRoleAndComponent(roleName, componentName);
            if (listP.size() <= 0) {
                throw new HttpResponseException(404, String
                        .format("No permission by rolename:%s and component name:%s.", roleName, componentName));
            }
        } else if (!StringUtils.isEmpty(componentName)) {
            listP = getPersister().getPermissionsByComponent(componentName);
            if (listP.size() <= 0) {
                throw new HttpResponseException(404,
                        String.format("No permission by component name:%s.", componentName));
            }
        } else {
            listP = getPersister().getAllPermissions();
        }

        ReturnStatus<List<Permission>> status = new ReturnStatus<List<Permission>>(StatusEnum.SUCCESS, null);
        status.setValue(listP);
        return status;
    }

    @SuppressWarnings("rawtypes")
    @ExceptionHandler(HttpResponseException.class)
    @ResponseBody
    public ReturnStatus handleException(HttpResponseException e, HttpServletResponse response) {
        response.setStatus(e.getStatusCode());
        return new ReturnStatus(StatusEnum.FAILURE, e.getMessage());
    }

}