/**
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) 2007 Sun Microsystems Inc. All Rights Reserved
*
* The contents of this file are subject to the terms
* of the Common Development and Distribution License
* (the License). You may not use this file except in
* compliance with the License.
*
* You can obtain a copy of the License at
* https://opensso.dev.java.net/public/CDDLv1.0.html or
* opensso/legal/CDDLv1.0.txt
* See the License for the specific language governing
* permission and limitations under the License.
*
* When distributing Covered Code, include this CDDL
* Header Notice in each file and include the License file
* at opensso/legal/CDDLv1.0.txt.
* If applicable, add the following below the CDDL Header,
* with the fields enclosed by brackets [] replaced by
* your own identifying information:
* "Portions Copyrighted [year] [name of copyright owner]"
*
* $Id: WebCLIHelper.java,v 1.16 2009/11/04 23:36:45 veiming Exp $
*
*/
/*
* Portions Copyrighted 2011 ForgeRock AS
* Portions Copyrighted 2012 Open Source Solution Technology Corporation
*/
package com.sun.identity.cli;
import com.iplanet.sso.SSOToken;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;
import javax.servlet.http.HttpServletRequest;
import org.owasp.esapi.ESAPI;
/**
* Helper class for web based CLI JSP.
*/
public class WebCLIHelper {
private BufferOutputWriter outputWriter;
private String jspName;
private CommandManager cmdMgr;
public WebCLIHelper(
HttpServletRequest request,
String definitionClass,
String commandName,
String jspName
) throws CLIException {
outputWriter = new BufferOutputWriter();
this.jspName = jspName;
Map env = new HashMap();
env.put(CLIConstants.SYS_PROPERTY_OUTPUT_WRITER, outputWriter);
env.put(CLIConstants.ARGUMENT_LOCALE, request.getLocale());
env.put(CLIConstants.SYS_PROPERTY_DEFINITION_FILES,
definitionClass);
env.put(CLIConstants.SYS_PROPERTY_COMMAND_NAME, commandName);
env.put(CLIConstants.WEB_ENABLED_URL, jspName);
cmdMgr = new CommandManager(env);
}
public String getHTML(HttpServletRequest request, SSOToken ssoToken)
throws CLIException {
String html;
String cmdName = request.getParameter("cmd");
if (cmdName != null) {
StringBuilder buff = new StringBuilder();
buff.append(getNavBackLinkHTML());
String submit = request.getParameter("submit");
if (submit == null) {
buff.append(autogenUI(cmdName));
} else {
if (!request.getMethod().equals("POST")) {
String message = cmdMgr.getResourceBundle().getString(
"error-message-http-post-required");
throw new CLIException(
message, ExitCodes.REQUEST_CANNOT_BE_PROCESSED);
}
buff.append(processRequest(cmdName, request, ssoToken));
}
html = buff.toString();
} else {
html = getUsageHTML();
}
outputWriter.clearBuffer();
return html;
}
private String processRequest(
String cmdName,
HttpServletRequest request,
SSOToken ssoToken
) throws CLIException {
SubCommand cmd = cmdMgr.getSubCommand(cmdName);
if (!cmd.webEnabled()) {
return "";
}
List list = new ArrayList();
Map map = request.getParameterMap();
for (Iterator i = map.keySet().iterator(); i.hasNext(); ) {
String key = (String)i.next();
if (!key.equals("submit") && !key.equals("cmd") &&
!key.endsWith("lblb")
) {
String[] values = (String[])map.get(key);
if (cmd.isUnaryOption(key)) {
list.add("--" + key);
} else {
List temp = new ArrayList();
for (int j = 0; j < values.length; j++) {
String str = values[j];
str = str.trim();
if (str.length() > 0) {
temp.add(str);
}
}
if (!temp.isEmpty()) {
list.add("--" + key);
list.addAll(temp);
}
}
}
}
int sz = list.size();
String[] args = new String[sz+1];
args[0] = cmdName;
for (int i = 0; i < sz; i++) {
args[i+1] = (String)list.get(i);
}
CLIRequest req = new CLIRequest(null, args, ssoToken);
cmdMgr.addToRequestQueue(req);
cmdMgr.serviceRequestQueue();
return escapeTags(outputWriter.getBuffer());
}
private String getNavBackLinkHTML() {
return "" +
cmdMgr.getResourceBundle().getString(
"web-interface-goto-main-page") +
"
";
}
private String getUsageHTML()
throws CLIException {
String[] arg = {"--help"};
CLIRequest req = new CLIRequest(null, arg);
cmdMgr.addToRequestQueue(req);
cmdMgr.serviceRequestQueue();
String strHelp = outputWriter.getBuffer();
int idx = strHelp.indexOf(" ");
SubCommand cmd = cmdMgr.getSubCommand(cmdName);
if ((cmd == null) || !cmd.webEnabled()) {
throw new CLIException(rb.getString(
"web-interface-cmd-name-not-found"),
ExitCodes.INVALID_SUBCOMMAND);
}
buff.append(cmd.getDescription())
.append("
");
buff.append("
") .append(" | |
") .append(" ") .append("") .append(" |