01: /*
02: * Copyright 2005 Joe Walker
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: package org.directwebremoting.servlet;
17:
18: import java.io.IOException;
19: import java.util.Map;
20:
21: import javax.servlet.http.HttpServletRequest;
22: import javax.servlet.http.HttpServletResponse;
23:
24: /**
25: * A {@link org.directwebremoting.extend.Handler} that allows some very simple
26: * search and replace templating. The general recommended syntax is to use
27: * ${search} as the string to be searched for, to allow future expansion to a
28: * more EL-like syntax.
29: * @author Joe Walker [joe at getahead dot ltd dot uk]
30: */
31: public abstract class TemplateHandler extends CachingFileHandler {
32: /* (non-Javadoc)
33: * @see org.directwebremoting.servlet.CachingFileHandler#generate(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
34: */
35: @Override
36: protected String generateCachableContent(
37: HttpServletRequest request, HttpServletResponse response)
38: throws IOException {
39: String template = generateTemplate(request, response);
40:
41: Map<String, String> replace = getSearchReplacePairs();
42: if (replace != null) {
43: for (Map.Entry<String, String> entry : replace.entrySet()) {
44: String search = entry.getKey();
45: if (template.contains(search)) {
46: template = template.replace(search, entry
47: .getValue());
48: }
49: }
50: }
51:
52: return template;
53: }
54:
55: /**
56: * Generate a template to undergo search and replace processing according to
57: * the search and replace pairs from {@link #getSearchReplacePairs()}.
58: * @param request The HTTP request data
59: * @param response Where we write the HTTP response data
60: * @return A template string containing ${} sections to be replaced
61: */
62: protected abstract String generateTemplate(
63: HttpServletRequest request, HttpServletResponse response)
64: throws IOException;
65:
66: /**
67: * Mostly when we send a file out, we don't change anything so the default
68: * set of search and replaces is empty.
69: * Engine.js can override this with strings to customize the output
70: * @return a map of search (key) and replace (value) strings
71: */
72: public Map<String, String> getSearchReplacePairs() {
73: return null;
74: }
75: }
|