001: // ExecCommand.java
002: // $Id: ExecCommand.java,v 1.4 2000/08/16 21:37:47 ylafon Exp $
003: // (c) COPYRIGHT MIT and INRIA, 1996.
004: // Please first read the full copyright statement in file COPYRIGHT.html
005:
006: package org.w3c.jigsaw.ssi.commands;
007:
008: import java.io.IOException;
009: import java.io.StringBufferInputStream;
010:
011: import java.util.Dictionary;
012: import java.util.Vector;
013:
014: import org.w3c.www.http.HTTP;
015:
016: import org.w3c.util.ArrayDictionary;
017:
018: import org.w3c.jigsaw.http.Reply;
019: import org.w3c.jigsaw.http.Request;
020:
021: import org.w3c.jigsaw.ssi.DelayedInputStream;
022: import org.w3c.jigsaw.ssi.SSIFrame;
023:
024: /**
025: * Implementation of the SSI <code>exec</code> command.
026: * It inserts the output from a CGI script or a shell command in the
027: * document. Note that in the Jigsaw architecture CGI scripts are just
028: * another resource class, so that no distinction is made between
029: * executing a CGI script or including a file.
030: * Relies on variables set by DefaultCommandRegistry.
031: * @author Antonio Ramirez <anto@mit.edu>
032: */
033: public class ExecCommand extends BasicCommand {
034: private final static String NAME = "exec";
035:
036: public Reply execute(SSIFrame ssiframe, Request request,
037: ArrayDictionary parameters, Dictionary variables) {
038: String cmd = (String) parameters.get("cmd");
039: if (cmd != null)
040: return executeCmd(ssiframe, request, parameters, variables,
041: cmd);
042:
043: // "cgi" would be handled here (just like an include...)
044: Reply reply = ssiframe.createDefaultReply(request, HTTP.OK);
045: reply.setContent("[unimplemented: use include]");
046: handleSimpleIMS(request, reply);
047: return reply;
048: }
049:
050: /**
051: * return true if reply can be cached.
052: * @return a boolean.
053: */
054: public boolean acceptCaching() {
055: return true;
056: }
057:
058: private Reply executeCmd(SSIFrame ssiframe, Request request,
059: ArrayDictionary parameters, Dictionary variables, String cmd) {
060: // Deny command if secure flag was set
061: if (((Boolean) variables.get("secure")).booleanValue()) {
062: Reply reply = ssiframe.createCommandReply(request, HTTP.OK);
063: reply.setContent("[exec cmd not allowed: secure SSI]");
064: return reply;
065: }
066:
067: ArrayDictionary ssiVars = (ArrayDictionary) variables
068: .get("ssiVars");
069:
070: if (ssiVars == null) {
071: Reply reply = ssiframe.createCommandReply(request, HTTP.OK);
072: reply.setContent("[exec: can't find environment]");
073: handleSimpleIMS(request, reply);
074: return reply;
075: }
076:
077: String[] env = new String[ssiVars.size()];
078: for (int i = 0, j = 0; i < ssiVars.capacity()
079: && ssiVars.keyAt(i) != null; i++)
080: env[j++] = (ssiVars.keyAt(i).toString()) + "="
081: + (ssiVars.elementAt(i).toString());
082:
083: Reply reply = new Reply(request.getClient());
084: reply.setStream(new DelayedProcessStream(cmd, env));
085: return reply;
086: }
087:
088: private final void addEnv(Vector env, String var, String val) {
089: if (var != null && val != null)
090: env.addElement(var + '=' + val);
091: }
092:
093: public String getName() {
094: return NAME;
095: }
096:
097: public String getValue(Dictionary variables, String variable,
098: Request request) {
099: return "null";
100: }
101:
102: }
103:
104: class DelayedProcessStream extends DelayedInputStream {
105: private String cmd = null;
106: private String[] env = null;
107:
108: DelayedProcessStream(String cmd, String[] env) {
109: this .cmd = cmd;
110: this .env = env;
111: }
112:
113: protected final void init() {
114: Process proc = null;
115: try {
116: proc = Runtime.getRuntime().exec(cmd, env);
117: } catch (IOException ex) {
118: proc = null;
119: }
120:
121: if (proc == null)
122: in = new StringBufferInputStream(
123: "[exec: cannot start process: \"" + cmd + "\"]");
124: else
125: in = proc.getInputStream();
126: }
127:
128: }
|