/*
* Copyright 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.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.chain.Command;
import org.apache.commons.chain.Context;
import org.apache.struts.chain.contexts.ServletActionContext;
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;
/**
* Struts 1.3 panels dispatcher command.
*
* @author Christoph Beck
*/
public class PanelsCommand implements Command {
/**
* Search for panel definition for the given path.
* If found, then forward/include to that panels's template path.
* @param context our context
* @throws ServletException
* @throws IOException
*/
public boolean execute(Context ctx) throws ServletException, IOException {
ServletActionContext context = (ServletActionContext)ctx;
ForwardConfig forward = context.getForwardConfig();
if (forward == null || forward.getPath() == null || forward.getRedirect()) {
return false;
}
if (forward.getModule() != null && !forward.getModule().equals(context.getModuleConfig().getPrefix())) {
return false;
}
HttpServletRequest request = context.getRequest();
PanelsSupport support = PanelsSupport.getInstance(request);
Locale locale = I18nSupport.getInstance(request).getLocale(request);
PanelConfig panel = support.findPanelConfig(forward.getPath(), locale);
if (panel != null) {
String template = panel.findTemplate(locale);
if (template == null) {
throw new ServletException("Cannot find template for panel " + panel.getName());
}
PanelsContext panels = support.getOrCreateContext(request, locale);
boolean include = !panels.isEmpty() || context.getResponse().isCommitted();
panels.push(panel);
try {
RequestDispatcher rd = context.getContext().getRequestDispatcher(template);
if (include) {
rd.include(request, context.getResponse());
} else {
rd.forward(request, context.getResponse());
}
} finally {
panels.pop();
}
return true;
}
return false;
}
}