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