001: /*
002:
003: This software is OSI Certified Open Source Software.
004: OSI Certified is a certification mark of the Open Source Initiative.
005:
006: The license (Mozilla version 1.0) can be read at the MMBase site.
007: See http://www.MMBase.org/license
008:
009: */
010: package org.mmbase.framework;
011:
012: import java.util.*;
013: import javax.servlet.http.*;
014: import javax.servlet.*;
015: import java.io.*;
016: import org.mmbase.util.functions.*;
017: import org.mmbase.util.GenericResponseWrapper;
018:
019: import org.mmbase.util.logging.Logger;
020: import org.mmbase.util.logging.Logging;
021:
022: /**
023: * A Renderer implementation based on a jsp.
024: *
025: * @author Michiel Meeuwissen
026: * @version $Id: JspRenderer.java,v 1.27 2008/01/25 09:32:23 michiel Exp $
027: * @since MMBase-1.9
028: */
029: public class JspRenderer extends AbstractRenderer {
030: private static final Logger log = Logging
031: .getLoggerInstance(JspRenderer.class);
032:
033: public static String JSP_ROOT = "/mmbase/components/";
034:
035: protected final String path;
036:
037: public JspRenderer(String t, String p, Block parent) {
038: super (t, parent);
039: path = p;
040: }
041:
042: public String getPath() {
043: return path.charAt(0) == '/' ? path : JSP_ROOT
044: + getBlock().getComponent().getName() + '/' + path;
045: }
046:
047: public Parameter[] getParameters() {
048: return new Parameter[] { Parameter.RESPONSE, Parameter.REQUEST };
049: }
050:
051: private static class Status {
052: public int code = 200;
053: public String mesg = null;
054: }
055:
056: public void render(Parameters blockParameters,
057: Parameters frameworkParameters, Writer w, WindowState state)
058: throws FrameworkException {
059: try {
060: HttpServletResponse response = blockParameters
061: .get(Parameter.RESPONSE);
062: HttpServletRequest request = blockParameters
063: .get(Parameter.REQUEST);
064: final Status status = new Status();
065: GenericResponseWrapper respw = new GenericResponseWrapper(
066: response) {
067: public void setStatus(int s) {
068: status.code = s;
069: }
070:
071: public void sendError(int s) throws IOException {
072: status.code = s;
073: }
074:
075: public void sendError(int s, String m)
076: throws IOException {
077: status.code = s;
078: status.mesg = m;
079: }
080: };
081: //String url = getFramework().getInternalUrl(getPath(), this, getBlock().getComponent(), blockParameters, frameworkParameters).toString();
082:
083: String url = getPath();
084: RequestDispatcher requestDispatcher = request
085: .getRequestDispatcher(url);
086:
087: for (Map.Entry<String, ?> entry : blockParameters.toMap()
088: .entrySet()) {
089: request.setAttribute(entry.getKey(), entry.getValue());
090: }
091: if (log.isDebugEnabled()) {
092: log.debug("Using block.parameters "
093: + blockParameters.toMap());
094: log
095: .debug("Rendering " + url + " status "
096: + status.code);
097: }
098: requestDispatcher.include(request, respw);
099: log.debug("Status " + status.code);
100: if (status.code == 401) {
101: DeniedRenderer denied = new DeniedRenderer(getType(),
102: getBlock());
103: denied.render(blockParameters, frameworkParameters, w,
104: state);
105: } else if (status.code != 200) {
106: ErrorRenderer error = new ErrorRenderer(getType(),
107: getBlock(), url, status.code, status.mesg);
108: error.render(blockParameters, frameworkParameters, w,
109: state);
110: } else {
111: w.write(respw.toString());
112: }
113: } catch (ServletException se) {
114: throw new FrameworkException(se.getMessage(), se);
115: } catch (IOException ioe) {
116: throw new FrameworkException(ioe.getMessage(), ioe);
117: }
118: }
119:
120: public String toString() {
121: Parameter.Wrapper wrapper = getBlock().specific;
122: return getPath() + (wrapper == null ? "" : "?" + wrapper);
123: }
124:
125: public java.net.URI getUri() {
126: try {
127: return org.mmbase.util.ResourceLoader.getWebRoot()
128: .getResource(getPath()).toURI();
129: } catch (Exception e) {
130: log.error(e);
131: return null;
132: }
133: }
134: }
|