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.PrintWriter;
20:
21: /**
22: * Return the result associated with the supplied Server Variable.
23: *
24: * @author Bip Thelin
25: * @author Paul Speed
26: * @author Dan Sandberg
27: * @author David Becker
28: * @version $Revision: 531303 $, $Date: 2007-04-23 02:24:01 +0200 (lun., 23 avr. 2007) $
29: */
30: public class SSIEcho implements SSICommand {
31: protected final static String DEFAULT_ENCODING = "entity";
32: protected final static String MISSING_VARIABLE_VALUE = "(none)";
33:
34: /**
35: * @see SSICommand
36: */
37: public long process(SSIMediator ssiMediator, String commandName,
38: String[] paramNames, String[] paramValues,
39: PrintWriter writer) {
40: long lastModified = 0;
41: String encoding = DEFAULT_ENCODING;
42: String errorMessage = ssiMediator.getConfigErrMsg();
43: for (int i = 0; i < paramNames.length; i++) {
44: String paramName = paramNames[i];
45: String paramValue = paramValues[i];
46: if (paramName.equalsIgnoreCase("var")) {
47: String variableValue = ssiMediator.getVariableValue(
48: paramValue, encoding);
49: if (variableValue == null) {
50: variableValue = MISSING_VARIABLE_VALUE;
51: }
52: writer.write(variableValue);
53: lastModified = System.currentTimeMillis();
54: } else if (paramName.equalsIgnoreCase("encoding")) {
55: if (isValidEncoding(paramValue)) {
56: encoding = paramValue;
57: } else {
58: ssiMediator.log("#echo--Invalid encoding: "
59: + paramValue);
60: writer.write(errorMessage);
61: }
62: } else {
63: ssiMediator.log("#echo--Invalid attribute: "
64: + paramName);
65: writer.write(errorMessage);
66: }
67: }
68: return lastModified;
69: }
70:
71: protected boolean isValidEncoding(String encoding) {
72: return encoding.equalsIgnoreCase("url")
73: || encoding.equalsIgnoreCase("entity")
74: || encoding.equalsIgnoreCase("none");
75: }
76: }
|