/*
* Copyright 2004, 2005, 2006 Odysseus Software GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.odysseus.calyxo.struts.panels;
import java.io.IOException;
import java.util.Locale;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.RequestProcessor;
import org.apache.struts.config.ForwardConfig;
import de.odysseus.calyxo.base.I18nSupport;
import de.odysseus.calyxo.panels.PanelsContext;
import de.odysseus.calyxo.panels.PanelsSupport;
import de.odysseus.calyxo.panels.conf.PanelConfig;
/**
* Panels aware request processor.
* Module internal forwards/includes to some path are intercepted. If the path matches
* a panel path, we dispatch to the panel's template instead.
*
* @author Christoph Beck
*/
public class PanelsRequestProcessor extends RequestProcessor {
/**
* Search for panel definition for the given path.
* If found, then forward/include to that panels's template path.
* @param request our request
* @param response our response
* @param path dispatch path (used to match panel path)
* @param doInclude perform include instead of forward
* @return true iff a panel definition was found and the request has been handled (include or
* forward has been done)
* @throws ServletException
* @throws IOException
*/
protected boolean dispatch(HttpServletRequest request, HttpServletResponse response, String path, boolean doInclude) throws ServletException, IOException {
PanelsSupport support = PanelsSupport.getInstance(request);
Locale locale = I18nSupport.getInstance(request).getLocale(request);
PanelConfig panel = support.findPanelConfig(path, locale);
if (panel != null) {
if (log.isTraceEnabled()) {
log.trace("panel " + panel.getName());
}
String template = panel.findTemplate(locale);
if (template == null) {
throw new ServletException("Cannot find template for panel " + panel.getName());
}
PanelsContext context = support.getOrCreateContext(request, locale);
context.push(panel);
try {
if (doInclude) {
doInclude(template, request, response);
} else {
doForward(template, request, response);
}
} finally {
context.pop();
}
return true;
}
return false;
}
protected void processForwardConfig(HttpServletRequest request, HttpServletResponse response, ForwardConfig forward) throws IOException, ServletException {
if (forward != null && forward.getModule() == null && !forward.getRedirect()) {
if (!dispatch(request, response, forward.getPath(), false)) {
super.processForwardConfig(request, response, forward);
}
}
}
protected void internalModuleRelativeForward(String uri, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
if (!dispatch(request, response, uri, false)) {
super.internalModuleRelativeForward(uri, request, response);
}
}
protected void internalModuleRelativeInclude(String uri, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
if (!dispatch(request, response, uri, true)) {
super.internalModuleRelativeForward(uri, request, response);
}
}
}