RemoveApplicationPrivilegeResources.java revision a4544a5a0e622ef69e38641f87ab1b5685e05911
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster/*
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Copyright (c) 2009 Sun Microsystems Inc. All Rights Reserved
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * The contents of this file are subject to the terms
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * of the Common Development and Distribution License
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * (the License). You may not use this file except in
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * compliance with the License.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * You can obtain a copy of the License at
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * https://opensso.dev.java.net/public/CDDLv1.0.html or
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * opensso/legal/CDDLv1.0.txt
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * See the License for the specific language governing
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * permission and limitations under the License.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * When distributing Covered Code, include this CDDL
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Header Notice in each file and include the License file
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * at opensso/legal/CDDLv1.0.txt.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * If applicable, add the following below the CDDL Header,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * with the fields enclosed by brackets [] replaced by
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * your own identifying information:
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * "Portions Copyrighted [year] [name of copyright owner]"
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * $Id: RemoveApplicationPrivilegeResources.java,v 1.2 2009/11/19 01:02:02 veiming Exp $
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Portions Copyrighted 2015 ForgeRock AS.
e60a1cf74ca44a3bb3e3fe63b106e6ef6dca910fPhill Cunnington */
e60a1cf74ca44a3bb3e3fe63b106e6ef6dca910fPhill Cunnington
e60a1cf74ca44a3bb3e3fe63b106e6ef6dca910fPhill Cunningtonpackage com.sun.identity.cli.entitlement;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.sun.identity.cli.CLIException;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.sun.identity.cli.ExitCodes;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.sun.identity.cli.IArgument;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.sun.identity.cli.LogWriter;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.sun.identity.cli.RequestContext;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.sun.identity.entitlement.ApplicationPrivilege;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.sun.identity.entitlement.ApplicationPrivilegeManager;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.sun.identity.entitlement.EntitlementException;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.sun.identity.entitlement.opensso.SubjectUtils;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport org.forgerock.openam.entitlement.service.ResourceTypeService;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport java.text.MessageFormat;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport java.util.Map;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport java.util.Set;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport java.util.logging.Level;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport javax.inject.Inject;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport javax.security.auth.Subject;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster/**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @author dennis
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterpublic class RemoveApplicationPrivilegeResources extends ApplicationPrivilegeBase {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster @Inject
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public RemoveApplicationPrivilegeResources(ResourceTypeService resourceTypeService) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster super(resourceTypeService);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Services a Commandline Request.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param rc Request Context.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @throws CLIException if the request cannot serviced.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster @Override
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public void handleRequest(RequestContext rc)
e60a1cf74ca44a3bb3e3fe63b106e6ef6dca910fPhill Cunnington throws CLIException {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster super.handleRequest(rc);
e60a1cf74ca44a3bb3e3fe63b106e6ef6dca910fPhill Cunnington String realm = getStringOptionValue(IArgument.REALM_NAME);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String name = getStringOptionValue(PARAM_NAME);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String[] params = {realm, name};
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster try {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Map<String, Set<String>> mapAppToResources =
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster getApplicationResourcesMap(rc, realm);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Subject userSubject = SubjectUtils.createSubject(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster getAdminSSOToken());
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ApplicationPrivilegeManager apm =
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ApplicationPrivilegeManager.getInstance(realm, userSubject);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster writeLog(LogWriter.LOG_ACCESS, Level.INFO,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster "ATTEMPT_UPDATE_APPLICATION_PRIVILEGE", params);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ApplicationPrivilege appPrivilege = apm.getPrivilege(name);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Map<String, Set<String>> origAppToResources =
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster getApplicationToResources(appPrivilege);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster removeFromMap(origAppToResources, mapAppToResources);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (origAppToResources.isEmpty()) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throw new CLIException(getResourceString(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster "remove-application-privilege-resources-emptied-resources"),
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ExitCodes.REQUEST_CANNOT_BE_PROCESSED);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster appPrivilege.setApplicationResources(origAppToResources);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster apm.replacePrivilege(appPrivilege);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Object[] msgParam = {name};
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster getOutputWriter().printlnMessage(MessageFormat.format(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster getResourceString("update-application-privilege-succeeded"),
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster msgParam));
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster writeLog(LogWriter.LOG_ACCESS, Level.INFO,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster "SUCCEEDED_UPDATE_APPLICATION_PRIVILEGE", params);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } catch (EntitlementException ex) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String[] paramExs = {realm, name, ex.getMessage()};
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster writeLog(LogWriter.LOG_ACCESS, Level.INFO,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster "FAILED_UPDATE_APPLICATION_PRIVILEGE", paramExs);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throw new CLIException(ex, ExitCodes.REQUEST_CANNOT_BE_PROCESSED);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster}
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster