001: /*
002: * Copyright 2004, 2005, 2006 Odysseus Software GmbH
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package de.odysseus.calyxo.panels.control;
017:
018: import java.io.IOException;
019: import java.util.Locale;
020:
021: import javax.servlet.ServletException;
022: import javax.servlet.http.HttpServletRequest;
023: import javax.servlet.http.HttpServletResponse;
024:
025: import de.odysseus.calyxo.base.I18nSupport;
026: import de.odysseus.calyxo.base.ModuleContext;
027:
028: import de.odysseus.calyxo.control.conf.DispatchConfig;
029: import de.odysseus.calyxo.control.impl.DefaultDispatcher;
030:
031: import de.odysseus.calyxo.panels.PanelsContext;
032: import de.odysseus.calyxo.panels.PanelsSupport;
033: import de.odysseus.calyxo.panels.conf.PanelConfig;
034:
035: /**
036: * Panels dispatcher implementation.
037: *
038: * This dispatcher modifies the standard behaviour for
039: * {@link de.odysseus.calyxo.control.conf.DispatchConfig} elements with
040: * the <code>path</code> property set. It searches for a toplevel
041: * panel element with <code>panel.name == dispatch.path</code>. If
042: * it finds one, it creates a panels context stack in request scope,
043: * pushes the panel and dispatches to <code>panel.path</code>.
044: *
045: * @author Christoph Beck
046: */
047: public class PanelsDispatcher extends DefaultDispatcher {
048: private PanelsSupport support;
049: private I18nSupport i18n;
050:
051: public PanelsDispatcher(ModuleContext context) {
052: super (context);
053:
054: support = PanelsSupport.getInstance(context);
055: i18n = I18nSupport.getInstance(context);
056: }
057:
058: /* (non-Javadoc)
059: * @see de.odysseus.calyxo.control.Dispatcher#dispatch(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, de.odysseus.calyxo.control.conf.DispatchConfig)
060: */
061: public void dispatch(HttpServletRequest request,
062: HttpServletResponse response, DispatchConfig dispatch)
063: throws IOException, ServletException {
064:
065: if (dispatch.getPath() != null && !dispatch.isRedirect()) {
066: Locale locale = i18n.getLocale(request);
067:
068: // lookup panel
069: PanelConfig panel = support.findPanelConfig(dispatch
070: .getPath(), locale);
071: if (panel != null) {
072: if (log.isTraceEnabled())
073: log.trace("panel " + panel.getName());
074:
075: String template = panel.findTemplate(locale);
076: if (template == null) {
077: throw new ServletException(
078: "Could not find path for panel "
079: + panel.toInlineString());
080: }
081: template = addParams(template, dispatch, "UTF-8");
082: // log.debug("path " + path);
083:
084: PanelsContext context = support.getOrCreateContext(
085: request, locale);
086:
087: // log.debug("push " + panel.getName());
088: context.push(panel);
089:
090: // add dispatch parameters to current panel
091: // Iterator params = dispatch.getParamConfigs();
092: // while (params.hasNext()) {
093: // ParamConfig param = (ParamConfig)params.next();
094: // context.addParamConfig(
095: // new DynamicParamConfig(param.getName(), param.getValue())
096: // );
097: // }
098:
099: try {
100: dispatch(request, response, template, false);
101: } finally {
102: // log.debug("pop " + panel.getName());
103: context.pop();
104: }
105: return;
106: }
107: }
108: super.dispatch(request, response, dispatch);
109: }
110: }
|