01: /* ClassWebServlet.java
02:
03: {{IS_NOTE
04: Purpose:
05:
06: Description:
07:
08: History:
09: Mon Sep 19 20:55:40 2005, Created by tomyeh
10: }}IS_NOTE
11:
12: Copyright (C) 2005 Potix Corporation. All Rights Reserved.
13:
14: {{IS_RIGHT
15: This program is distributed under GPL Version 2.0 in the hope that
16: it will be useful, but WITHOUT ANY WARRANTY.
17: }}IS_RIGHT
18: */
19: package org.zkoss.web.util.resource;
20:
21: import java.io.IOException;
22:
23: import javax.servlet.ServletConfig;
24: import javax.servlet.ServletContext;
25: import javax.servlet.ServletException;
26: import javax.servlet.http.HttpServletRequest;
27: import javax.servlet.http.HttpServletResponse;
28: import javax.servlet.http.HttpServlet;
29:
30: import org.zkoss.lang.D;
31: import org.zkoss.util.logging.Log;
32: import org.zkoss.web.servlet.http.Https;
33:
34: /**
35: * Loads the resource from the class path starting with "/web".
36: *
37: * <p>You don't need this servlet if you are using ZK (because
38: * DHtmlUpdateServlet implements such function).
39: *
40: * @author tomyeh
41: */
42: public class ClassWebServlet extends HttpServlet {
43: private static final Log log = Log.lookup(ClassWebServlet.class);
44:
45: private ServletContext _ctx;
46: private String _mappingURI;
47: private ClassWebResource _cwr;
48:
49: public void init(ServletConfig config) throws ServletException {
50: //super.init(config);
51: //Note callback super to avoid saving config
52:
53: _ctx = config.getServletContext();
54:
55: _mappingURI = config.getInitParameter("mapping-uri");
56: if (_mappingURI == null || _mappingURI.length() == 0
57: || _mappingURI.charAt(0) != '/')
58: throw new ServletException(
59: "The mapping-uri parameter must be specified and starts with /");
60: if (_mappingURI.charAt(_mappingURI.length() - 1) == '\\') {
61: if (_mappingURI.length() == 1)
62: throw new ServletException(
63: "The mapping-uri parameter cannot contain only /");
64: _mappingURI = _mappingURI.substring(0,
65: _mappingURI.length() - 1);
66: //remove the trailing '\\' if any
67: }
68:
69: _cwr = ClassWebResource.getInstance(_ctx, _mappingURI);
70: }
71:
72: protected void doGet(HttpServletRequest request,
73: HttpServletResponse response) throws ServletException,
74: IOException {
75: final String pi = Https.getThisPathInfo(request);
76: if (pi != null)
77: _cwr.service(request, response, pi
78: .substring(ClassWebResource.PATH_PREFIX.length()));
79: else
80: log.error("Path info not specified");
81: }
82:
83: protected void doPost(HttpServletRequest request,
84: HttpServletResponse response) throws ServletException,
85: IOException {
86: doGet(request, response);
87: }
88: }
|