01: /*
02: * Copyright 1999,2004 The Apache Software Foundation.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16:
17: package org.apache.catalina.ssi;
18:
19: import java.io.BufferedReader;
20: import java.io.IOException;
21: import java.io.InputStreamReader;
22: import java.io.PrintWriter;
23:
24: import org.apache.catalina.util.IOTools;
25:
26: /**
27: * Implements the Server-side #exec command
28: *
29: * @author Bip Thelin
30: * @author Amy Roh
31: * @author Dan Sandberg
32: * @version $Revision: 1.3 $, $Date: 2004/02/27 14:58:47 $
33: *
34: */
35: public class SSIExec implements SSICommand {
36: protected SSIInclude ssiInclude = new SSIInclude();
37: protected final static int BUFFER_SIZE = 1024;
38:
39: /**
40: * @see SSICommand
41: */
42: public void process(SSIMediator ssiMediator, String[] paramNames,
43: String[] paramValues, PrintWriter writer) {
44:
45: String configErrMsg = ssiMediator.getConfigErrMsg();
46: String paramName = paramNames[0];
47: String paramValue = paramValues[0];
48:
49: if (paramName.equalsIgnoreCase("cgi")) {
50: ssiInclude.process(ssiMediator, new String[] { "virtual" },
51: new String[] { paramValue }, writer);
52: } else if (paramName.equalsIgnoreCase("cmd")) {
53: boolean foundProgram = false;
54: try {
55: Runtime rt = Runtime.getRuntime();
56: Process proc = rt.exec(paramValue);
57: foundProgram = true;
58:
59: BufferedReader stdOutReader = new BufferedReader(
60: new InputStreamReader(proc.getInputStream()));
61: BufferedReader stdErrReader = new BufferedReader(
62: new InputStreamReader(proc.getErrorStream()));
63:
64: char[] buf = new char[BUFFER_SIZE];
65: IOTools.flow(stdErrReader, writer, buf);
66: IOTools.flow(stdOutReader, writer, buf);
67: proc.waitFor();
68: } catch (InterruptedException e) {
69: ssiMediator.log("Couldn't exec file: " + paramValue, e);
70: writer.write(configErrMsg);
71: } catch (IOException e) {
72: if (!foundProgram) {
73: //apache doesn't output an error message if it can't find a program
74: }
75: ssiMediator.log("Couldn't exec file: " + paramValue, e);
76: }
77: }
78: }
79: }
|