001: // ServletCommand.java
002: // $Id: ServletCommand.java,v 1.12 2000/08/16 21:37:48 ylafon Exp $
003: // (c) COPYRIGHT MIT and INRIA, 1997.
004: // Please first read the full copyright statement in file COPYRIGHT.html
005:
006: package org.w3c.jigsaw.ssi.servlets;
007:
008: import java.util.Dictionary;
009: import java.util.Hashtable;
010:
011: import java.net.MalformedURLException;
012: import java.net.URL;
013:
014: import org.w3c.www.http.HTTP;
015: import org.w3c.www.http.HttpMessage;
016:
017: import org.w3c.jigsaw.http.Reply;
018: import org.w3c.jigsaw.http.Request;
019: import org.w3c.jigsaw.http.httpd;
020:
021: import org.w3c.tools.resources.FramedResource;
022: import org.w3c.tools.resources.InvalidResourceException;
023: import org.w3c.tools.resources.LookupResult;
024: import org.w3c.tools.resources.LookupState;
025: import org.w3c.tools.resources.ProtocolException;
026: import org.w3c.tools.resources.Resource;
027: import org.w3c.tools.resources.ResourceException;
028: import org.w3c.tools.resources.ResourceReference;
029:
030: import org.w3c.jigsaw.servlet.JigsawHttpServletRequest;
031: import org.w3c.jigsaw.servlet.JigsawHttpServletResponse;
032: import org.w3c.jigsaw.servlet.ServletWrapper;
033:
034: import org.w3c.util.ArrayDictionary;
035:
036: import org.w3c.jigsaw.ssi.commands.Command;
037:
038: import org.w3c.jigsaw.ssi.SSIFrame;
039:
040: import org.w3c.tools.resources.ProtocolException;
041: import org.w3c.tools.resources.ResourceException;
042:
043: /**
044: * Implementation of the SSI <code>servlet</code> command.
045: * Servlet can be executed
046: * simply by providing a url path to a servlet class.
047: * @author Benoit Mahe <bmahe@sophia.inria.fr>
048: */
049: public class ServletCommand implements Command {
050: private final static String NAME = "servlet";
051: private final static boolean debug = true;
052:
053: private static final String keys[] = { "code", "param", "value",
054: "name" };
055:
056: protected static Hashtable wrappers = null; // <classname , wrapper>
057:
058: static {
059: wrappers = new Hashtable(10);
060: }
061:
062: protected void addParam(Dictionary d, String name, String param,
063: String value) {
064: Hashtable params = (Hashtable) d.get(getClass().getName() + "."
065: + name);
066: if (params == null) {
067: params = new Hashtable(5);
068: params.put(param, value);
069: } else {
070: Object ovalue = params.get(param);
071: if (ovalue == null) {
072: params.put(param, value);
073: } else if (ovalue instanceof String[]) {
074: String oldValues[] = (String[]) ovalue;
075: String newValues[] = new String[oldValues.length + 1];
076: System.arraycopy(oldValues, 0, newValues, 0,
077: oldValues.length);
078: newValues[oldValues.length] = value;
079: params.put(param, newValues);
080: } else {
081: String newValues[] = new String[2];
082: newValues[0] = (String) ovalue;
083: newValues[1] = value;
084: params.put(param, newValues);
085: }
086: }
087: d.put(getClass().getName() + "." + name, params);
088: }
089:
090: protected Hashtable getParams(Dictionary d, String name) {
091: return (Hashtable) d.get(getClass().getName() + "." + name);
092: }
093:
094: public String getName() {
095: return NAME;
096: }
097:
098: public String getValue(Dictionary variables, String var,
099: Request request) {
100: return null;
101: }
102:
103: protected boolean isRemote(String code) {
104: try {
105: URL url = new URL(code);
106: } catch (MalformedURLException ex) {
107: return false;
108: }
109: return true;
110: }
111:
112: /**
113: * return true if reply can be cached.
114: * @return a boolean.
115: */
116: public boolean acceptCaching() {
117: return false;
118: }
119:
120: public Reply execute(SSIFrame ssiframe, Request request,
121: ArrayDictionary parameters, Dictionary variables) {
122: Object values[] = parameters.getMany(keys);
123: String code = (String) values[0];
124: String param = (String) values[1];
125: String value = (String) values[2];
126: String name = (String) values[3];
127:
128: if (name != null) {
129: if ((param != null) && (value != null)) {
130: //store a new param for servlet "name"
131: addParam(variables, name, param, value);
132: }
133: if (code != null) { // remote or not ??
134: ResourceReference r_wrapper = (ResourceReference) wrappers
135: .get(code);
136: if (r_wrapper == null) { //lookup for wrapper
137: httpd server = (httpd) ssiframe.getFileResource()
138: .getServer();
139: ResourceReference rr_root = server
140: .getRootReference();
141: try {
142: FramedResource root = (FramedResource) rr_root
143: .lock();
144: LookupState ls = new LookupState(code);
145: LookupResult lr = new LookupResult(rr_root);
146: ResourceReference wrap = null;
147: if (root.lookup(ls, lr))
148: wrap = lr.getTarget();
149: if (wrap != null) {
150: try {
151: if (wrap.lock() instanceof ServletWrapper) {
152: wrappers.put(code, wrap);
153: r_wrapper = wrap;
154: }
155: } catch (InvalidResourceException ex) {
156: ex.printStackTrace();
157: r_wrapper = null;
158: } finally {
159: wrap.unlock();
160: }
161: }
162: } catch (ProtocolException ex) {
163: ex.printStackTrace();
164: r_wrapper = null;
165: } catch (InvalidResourceException ex) {
166: ex.printStackTrace();
167: r_wrapper = null;
168: } finally {
169: rr_root.unlock();
170: }
171: }
172: if (r_wrapper != null) {
173: //initialize the wrapper (params)
174: Hashtable params = getParams(variables, name);
175: request.setState(
176: JigsawHttpServletRequest.STATE_PARAMETERS,
177: params);
178: //perform the request
179: try {
180: FramedResource wrapper = (FramedResource) r_wrapper
181: .lock();
182: Request req = (Request) request.getClone();
183: req.setState(
184: JigsawHttpServletResponse.INCLUDED,
185: Boolean.TRUE);
186: return (Reply) wrapper.perform(req);
187: } catch (ProtocolException ex) {
188: ex.printStackTrace();
189: // return default reply
190: } catch (ResourceException ex2) {
191: ex2.printStackTrace();
192: // return default reply
193: } catch (InvalidResourceException ex3) {
194: ex3.printStackTrace();
195: // return default reply
196: } finally {
197: r_wrapper.unlock();
198: }
199: }
200: }
201: }
202: // We are NOT doing notMod hack here (tricky and useless ?)
203: //Reply reply = ssiframe.createCommandReply(request, HTTP.OK);
204: Reply reply = request.makeReply(HTTP.OK);
205: reply.setContent("");
206: return reply;
207: }
208: }
|