01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
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: import org.apache.catalina.util.IOTools;
24:
25: /**
26: * Implements the Server-side #exec command
27: *
28: * @author Bip Thelin
29: * @author Amy Roh
30: * @author Paul Speed
31: * @author Dan Sandberg
32: * @author David Becker
33: * @version $Revision: 531303 $, $Date: 2007-04-23 02:24:01 +0200 (lun., 23 avr. 2007) $
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 long process(SSIMediator ssiMediator, String commandName,
43: String[] paramNames, String[] paramValues,
44: PrintWriter writer) {
45: long lastModified = 0;
46: String configErrMsg = ssiMediator.getConfigErrMsg();
47: String paramName = paramNames[0];
48: String paramValue = paramValues[0];
49: String substitutedValue = ssiMediator
50: .substituteVariables(paramValue);
51: if (paramName.equalsIgnoreCase("cgi")) {
52: lastModified = ssiInclude.process(ssiMediator, "include",
53: new String[] { "virtual" },
54: new String[] { substitutedValue }, writer);
55: } else if (paramName.equalsIgnoreCase("cmd")) {
56: boolean foundProgram = false;
57: try {
58: Runtime rt = Runtime.getRuntime();
59: Process proc = rt.exec(substitutedValue);
60: foundProgram = true;
61: BufferedReader stdOutReader = new BufferedReader(
62: new InputStreamReader(proc.getInputStream()));
63: BufferedReader stdErrReader = new BufferedReader(
64: new InputStreamReader(proc.getErrorStream()));
65: char[] buf = new char[BUFFER_SIZE];
66: IOTools.flow(stdErrReader, writer, buf);
67: IOTools.flow(stdOutReader, writer, buf);
68: proc.waitFor();
69: lastModified = System.currentTimeMillis();
70: } catch (InterruptedException e) {
71: ssiMediator.log("Couldn't exec file: "
72: + substitutedValue, e);
73: writer.write(configErrMsg);
74: } catch (IOException e) {
75: if (!foundProgram) {
76: //apache doesn't output an error message if it can't find
77: // a program
78: }
79: ssiMediator.log("Couldn't exec file: "
80: + substitutedValue, e);
81: }
82: }
83: return lastModified;
84: }
85: }
|