Search in sources :

Example 71 with CLIException

use of com.sun.identity.cli.CLIException in project OpenAM by OpenRock.

the class ShowAgent method writeToFile.

private void writeToFile(String outfile, String content) throws CLIException {
    FileOutputStream fout = null;
    PrintWriter pwout = null;
    try {
        fout = new FileOutputStream(outfile, true);
        pwout = new PrintWriter(fout, true);
        pwout.write(content);
        pwout.flush();
    } catch (FileNotFoundException e) {
        debugError("ShowAgent.writeToFile", e);
        throw new CLIException(e, ExitCodes.IO_EXCEPTION);
    } catch (SecurityException e) {
        debugError("ShowAgent.writeToFile", e);
        throw new CLIException(e, ExitCodes.IO_EXCEPTION);
    } finally {
        try {
            if (fout != null) {
                fout.close();
            }
            if (pwout != null) {
                pwout.close();
            }
        } catch (IOException ex) {
        //do nothing
        }
    }
}
Also used : FileOutputStream(java.io.FileOutputStream) FileNotFoundException(java.io.FileNotFoundException) CLIException(com.sun.identity.cli.CLIException) IOException(java.io.IOException) PrintWriter(java.io.PrintWriter)

Example 72 with CLIException

use of com.sun.identity.cli.CLIException in project OpenAM by OpenRock.

the class DeleteAgents method handleRequest.

/**
     * Services a Commandline Request.
     *
     * @param rc Request Context.
     * @throws CLIException if the request cannot serviced.
     */
public void handleRequest(RequestContext rc) throws CLIException {
    super.handleRequest(rc);
    ldapLogin();
    SSOToken adminSSOToken = getAdminSSOToken();
    String realm = getStringOptionValue(IArgument.REALM_NAME);
    List agentNames = (List) rc.getOption(IArgument.AGENT_NAMES);
    String file = getStringOptionValue(IArgument.FILE);
    if (agentNames == null) {
        agentNames = new ArrayList();
    }
    if (file != null) {
        agentNames.addAll(AttributeValues.parseValues(file));
    }
    if (agentNames.isEmpty()) {
        throw new CLIException(getResourceString("missing-agent-names"), ExitCodes.REQUEST_CANNOT_BE_PROCESSED);
    }
    String displayableNames = tokenize(agentNames);
    String[] params = { realm, displayableNames };
    writeLog(LogWriter.LOG_ACCESS, Level.INFO, "ATTEMPT_DELETE_AGENTS", params);
    try {
        AMIdentityRepository amir = new AMIdentityRepository(adminSSOToken, realm);
        Set setDelete = new HashSet();
        for (Iterator i = agentNames.iterator(); i.hasNext(); ) {
            String name = (String) i.next();
            AMIdentity amid = new AMIdentity(adminSSOToken, name, IdType.AGENTONLY, realm, null);
            setDelete.add(amid);
        }
        amir.deleteIdentities(setDelete);
        IOutput outputWriter = getOutputWriter();
        outputWriter.printlnMessage(getResourceString("delete-agent-succeeded"));
        for (Iterator i = agentNames.iterator(); i.hasNext(); ) {
            outputWriter.printlnMessage("    " + (String) i.next());
        }
        writeLog(LogWriter.LOG_ACCESS, Level.INFO, "SUCCEED_DELETE_AGENTS", params);
    } catch (IdRepoException e) {
        String[] args = { realm, displayableNames, e.getMessage() };
        debugError("DeleteAgents.handleRequest", e);
        writeLog(LogWriter.LOG_ERROR, Level.INFO, "FAILED_DELETE_AGENTS", args);
        throw new CLIException(e, ExitCodes.REQUEST_CANNOT_BE_PROCESSED);
    } catch (SSOException e) {
        String[] args = { realm, displayableNames, e.getMessage() };
        debugError("DeleteAgents.handleRequest", e);
        writeLog(LogWriter.LOG_ERROR, Level.INFO, "FAILED_DELETE_AGENTS", args);
        throw new CLIException(e, ExitCodes.REQUEST_CANNOT_BE_PROCESSED);
    }
}
Also used : SSOToken(com.iplanet.sso.SSOToken) Set(java.util.Set) HashSet(java.util.HashSet) ArrayList(java.util.ArrayList) IdRepoException(com.sun.identity.idm.IdRepoException) SSOException(com.iplanet.sso.SSOException) IOutput(com.sun.identity.cli.IOutput) AMIdentity(com.sun.identity.idm.AMIdentity) AMIdentityRepository(com.sun.identity.idm.AMIdentityRepository) Iterator(java.util.Iterator) CLIException(com.sun.identity.cli.CLIException) ArrayList(java.util.ArrayList) List(java.util.List) HashSet(java.util.HashSet)

Example 73 with CLIException

use of com.sun.identity.cli.CLIException in project OpenAM by OpenRock.

the class ListAgents method handleRequest.

/**
     * Services a Commandline Request.
     *
     * @param rc Request Context.
     * @throws CLIException if the request cannot serviced.
     */
public void handleRequest(RequestContext rc) throws CLIException {
    super.handleRequest(rc);
    ldapLogin();
    SSOToken adminSSOToken = getAdminSSOToken();
    IOutput outputWriter = getOutputWriter();
    String realm = getStringOptionValue(IArgument.REALM_NAME);
    String patternType = getStringOptionValue(IArgument.AGENT_TYPE);
    String filter = getStringOptionValue(IArgument.FILTER);
    if (patternType == null) {
        patternType = "";
    }
    if ((filter == null) || (filter.length() == 0)) {
        filter = "*";
    }
    String[] params = { realm, patternType, filter };
    writeLog(LogWriter.LOG_ACCESS, Level.INFO, "ATTEMPT_LIST_AGENTS", params);
    try {
        AMIdentityRepository amir = new AMIdentityRepository(adminSSOToken, realm);
        IdSearchResults isr = amir.searchIdentities(IdType.AGENTONLY, filter, new IdSearchControl());
        Set results = isr.getSearchResults();
        if ((results != null) && !results.isEmpty()) {
            for (Iterator i = results.iterator(); i.hasNext(); ) {
                AMIdentity amid = (AMIdentity) i.next();
                if (!matchType(amid, patternType)) {
                    i.remove();
                }
            }
        }
        if ((results != null) && !results.isEmpty()) {
            for (Iterator i = results.iterator(); i.hasNext(); ) {
                AMIdentity amid = (AMIdentity) i.next();
                Object[] args = { amid.getName(), amid.getUniversalId() };
                outputWriter.printlnMessage(MessageFormat.format(getResourceString("format-search-agent-results"), args));
            }
        } else {
            outputWriter.printlnMessage(getResourceString("search-agent-no-entries"));
        }
        writeLog(LogWriter.LOG_ACCESS, Level.INFO, "SUCCEED_LIST_AGENTS", params);
    } catch (IdRepoException e) {
        String[] args = { realm, patternType, filter, e.getMessage() };
        debugError("ListAgents.handleRequest", e);
        writeLog(LogWriter.LOG_ERROR, Level.INFO, "FAILED_LIST_AGENTS", args);
        throw new CLIException(e, ExitCodes.REQUEST_CANNOT_BE_PROCESSED);
    } catch (SSOException e) {
        String[] args = { realm, patternType, filter, e.getMessage() };
        debugError("ListAgents.handleRequest", e);
        writeLog(LogWriter.LOG_ERROR, Level.INFO, "FAILED_LIST_AGENTS", args);
        throw new CLIException(e, ExitCodes.REQUEST_CANNOT_BE_PROCESSED);
    }
}
Also used : SSOToken(com.iplanet.sso.SSOToken) Set(java.util.Set) IdSearchResults(com.sun.identity.idm.IdSearchResults) IdRepoException(com.sun.identity.idm.IdRepoException) SSOException(com.iplanet.sso.SSOException) IOutput(com.sun.identity.cli.IOutput) AMIdentity(com.sun.identity.idm.AMIdentity) AMIdentityRepository(com.sun.identity.idm.AMIdentityRepository) IdSearchControl(com.sun.identity.idm.IdSearchControl) Iterator(java.util.Iterator) CLIException(com.sun.identity.cli.CLIException)

Example 74 with CLIException

use of com.sun.identity.cli.CLIException in project OpenAM by OpenRock.

the class AddAgentsToGroup method handleRequest.

/**
     * Services a Commandline Request.
     *
     * @param rc Request Context.
     * @throws CLIException if the request cannot serviced.
     */
public void handleRequest(RequestContext rc) throws CLIException {
    super.handleRequest(rc);
    ldapLogin();
    SSOToken adminSSOToken = getAdminSSOToken();
    IOutput outputWriter = getOutputWriter();
    String realm = getStringOptionValue(IArgument.REALM_NAME);
    String agentGroupName = getStringOptionValue(IArgument.AGENT_GROUP_NAME);
    List agentNames = rc.getOption(IArgument.AGENT_NAMES);
    String[] params = { realm, agentGroupName, "" };
    String agentName = "";
    try {
        AMIdentity amidGroup = new AMIdentity(adminSSOToken, agentGroupName, IdType.AGENTGROUP, realm, null);
        for (Iterator i = agentNames.iterator(); i.hasNext(); ) {
            agentName = (String) i.next();
            params[2] = agentName;
            writeLog(LogWriter.LOG_ACCESS, Level.INFO, "ATTEMPT_ADD_AGENT_TO_GROUP", params);
            AMIdentity amid = new AMIdentity(adminSSOToken, agentName, IdType.AGENTONLY, realm, null);
            AgentConfiguration.AddAgentToGroup(amidGroup, amid);
            writeLog(LogWriter.LOG_ACCESS, Level.INFO, "SUCCEED_ADD_AGENT_TO_GROUP", params);
        }
        if (agentNames.size() > 1) {
            outputWriter.printlnMessage(getResourceString("add-agent-to-group-succeeded-pural"));
        } else {
            outputWriter.printlnMessage(getResourceString("add-agent-to-group-succeeded"));
        }
    } catch (IdRepoException e) {
        String[] args = { realm, agentGroupName, agentName, e.getMessage() };
        debugError("AddAgentsToGroup.handleRequest", e);
        writeLog(LogWriter.LOG_ERROR, Level.INFO, "FAILED_ADD_AGENT_TO_GROUP", args);
        throw new CLIException(e, ExitCodes.REQUEST_CANNOT_BE_PROCESSED);
    } catch (ConfigurationException e) {
        String[] args = { realm, agentGroupName, agentName, e.getMessage() };
        debugError("AddAgentsToGroup.handleRequest", e);
        writeLog(LogWriter.LOG_ERROR, Level.INFO, "FAILED_ADD_AGENT_TO_GROUP", args);
        throw new CLIException(e, ExitCodes.REQUEST_CANNOT_BE_PROCESSED);
    } catch (SSOException e) {
        String[] args = { realm, agentGroupName, agentName, e.getMessage() };
        debugError("AddAgentsToGroup.handleRequest", e);
        writeLog(LogWriter.LOG_ERROR, Level.INFO, "FAILED_ADD_AGENT_TO_GROUP", args);
        throw new CLIException(e, ExitCodes.REQUEST_CANNOT_BE_PROCESSED);
    }
}
Also used : SSOToken(com.iplanet.sso.SSOToken) ConfigurationException(com.sun.identity.common.configuration.ConfigurationException) IOutput(com.sun.identity.cli.IOutput) AMIdentity(com.sun.identity.idm.AMIdentity) Iterator(java.util.Iterator) IdRepoException(com.sun.identity.idm.IdRepoException) CLIException(com.sun.identity.cli.CLIException) List(java.util.List) SSOException(com.iplanet.sso.SSOException)

Example 75 with CLIException

use of com.sun.identity.cli.CLIException in project OpenAM by OpenRock.

the class DeleteAgentGroups method handleRequest.

/**
     * Services a Commandline Request.
     *
     * @param rc Request Context.
     * @throws CLIException if the request cannot serviced.
     */
public void handleRequest(RequestContext rc) throws CLIException {
    super.handleRequest(rc);
    ldapLogin();
    SSOToken adminSSOToken = getAdminSSOToken();
    String realm = getStringOptionValue(IArgument.REALM_NAME);
    List names = (List) rc.getOption(IArgument.AGENT_GROUP_NAMES);
    String file = getStringOptionValue(IArgument.FILE);
    if (names == null) {
        names = new ArrayList();
    }
    if (file != null) {
        names.addAll(AttributeValues.parseValues(file));
    }
    if (names.isEmpty()) {
        throw new CLIException(getResourceString("missing-agent-group-names"), ExitCodes.REQUEST_CANNOT_BE_PROCESSED);
    }
    String displayableNames = tokenize(names);
    String[] params = { realm, displayableNames };
    writeLog(LogWriter.LOG_ACCESS, Level.INFO, "ATTEMPT_DELETE_AGENT_GROUPS", params);
    try {
        Set setDelete = new HashSet();
        for (Iterator i = names.iterator(); i.hasNext(); ) {
            String name = (String) i.next();
            AMIdentity amid = new AMIdentity(adminSSOToken, name, IdType.AGENTGROUP, realm, null);
            setDelete.add(amid);
        }
        AgentConfiguration.deleteAgentGroups(adminSSOToken, realm, setDelete);
        IOutput outputWriter = getOutputWriter();
        outputWriter.printlnMessage(getResourceString("delete-agent-group-succeeded"));
        for (Iterator i = names.iterator(); i.hasNext(); ) {
            outputWriter.printlnMessage("    " + (String) i.next());
        }
        writeLog(LogWriter.LOG_ACCESS, Level.INFO, "SUCCEED_DELETE_AGENT_GROUPS", params);
    } catch (IdRepoException e) {
        String[] args = { realm, displayableNames, e.getMessage() };
        debugError("DeleteAgentGroups.handleRequest", e);
        writeLog(LogWriter.LOG_ERROR, Level.INFO, "FAILED_DELETE_AGENT_GROUPS", args);
        throw new CLIException(e, ExitCodes.REQUEST_CANNOT_BE_PROCESSED);
    } catch (SSOException e) {
        String[] args = { realm, displayableNames, e.getMessage() };
        debugError("DeleteAgentGroups.handleRequest", e);
        writeLog(LogWriter.LOG_ERROR, Level.INFO, "FAILED_DELETE_AGENT_GROUPS", args);
        throw new CLIException(e, ExitCodes.REQUEST_CANNOT_BE_PROCESSED);
    } catch (SMSException e) {
        String[] args = { realm, displayableNames, e.getMessage() };
        debugError("DeleteAgentGroups.handleRequest", e);
        writeLog(LogWriter.LOG_ERROR, Level.INFO, "FAILED_DELETE_AGENT_GROUPS", args);
        throw new CLIException(e, ExitCodes.REQUEST_CANNOT_BE_PROCESSED);
    }
}
Also used : SSOToken(com.iplanet.sso.SSOToken) Set(java.util.Set) HashSet(java.util.HashSet) SMSException(com.sun.identity.sm.SMSException) ArrayList(java.util.ArrayList) IdRepoException(com.sun.identity.idm.IdRepoException) SSOException(com.iplanet.sso.SSOException) IOutput(com.sun.identity.cli.IOutput) AMIdentity(com.sun.identity.idm.AMIdentity) Iterator(java.util.Iterator) CLIException(com.sun.identity.cli.CLIException) ArrayList(java.util.ArrayList) List(java.util.List) HashSet(java.util.HashSet)

Aggregations

CLIException (com.sun.identity.cli.CLIException)282 SSOException (com.iplanet.sso.SSOException)171 SMSException (com.sun.identity.sm.SMSException)150 IOutput (com.sun.identity.cli.IOutput)136 SSOToken (com.iplanet.sso.SSOToken)116 Set (java.util.Set)88 Iterator (java.util.Iterator)62 List (java.util.List)61 IOException (java.io.IOException)53 IdRepoException (com.sun.identity.idm.IdRepoException)48 ServiceSchema (com.sun.identity.sm.ServiceSchema)46 AMIdentity (com.sun.identity.idm.AMIdentity)43 Map (java.util.Map)42 ServiceSchemaManager (com.sun.identity.sm.ServiceSchemaManager)33 AMIdentityRepository (com.sun.identity.idm.AMIdentityRepository)29 AttributeSchema (com.sun.identity.sm.AttributeSchema)28 CLIRequest (com.sun.identity.cli.CLIRequest)27 AfterTest (org.testng.annotations.AfterTest)27 BeforeTest (org.testng.annotations.BeforeTest)27 Test (org.testng.annotations.Test)27