package pt.ist.renderers.plugin; import java.util.Collection; import java.util.Enumeration; import java.util.Hashtable; import java.util.Map; import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; import org.apache.struts.action.ActionForm; import org.apache.struts.upload.FormFile; import pt.ist.renderers.components.state.LifeCycleConstants; import pt.ist.renderers.plugin.upload.RenderersRequestWrapper; import pt.ist.renderers.plugin.upload.StrutsFile; import pt.ist.renderers.servlets.commons.UploadedFile; import pt.ist.renderers.servlets.filters.RequestWrapperFilter.FenixHttpServletRequestWrapper; /** * The standard renderers request processor. This processor is responsible for * handling any viewstate present in the request. It will parse the request, * retrieve all viewstates, and start the necessary lifecycle associated with * them before continuing with the standard struts processing. * *

* If any exception is thrown during the processing of a viewstate it will be * handled by struts like if the exceptions occured in the destiny action. This * default behaviour can be overriden by making the destiny action implement the * {@link pt.ist.renderers.plugin.ExceptionHandler} interface. * *

* The processor ensures that the current request and context are available * through {@link #getCurrentRequest()} and {@link #getCurrentContext()} * respectively during the entire request lifetime. The processor also process * multipart requests to allow any renderer to retrieve on uploaded file with * {@link #getUploadedFile(String)}. * *

* This processor extends the tiles processor to easily integrate in an * application that uses the tiles plugin. * * @author cfgi */ public class RenderersRequestProcessorImpl { public static Class implementationClass = RenderersRequestProcessor.class; static ThreadLocal currentRequest = new ThreadLocal(); static ThreadLocal currentContext = new ThreadLocal(); static ThreadLocal> fileItems = new ThreadLocal>(); public static HttpServletRequest getCurrentRequest() { return RenderersRequestProcessorImpl.currentRequest.get(); } public static ServletContext getCurrentContext() { return RenderersRequestProcessorImpl.currentContext.get(); } /** * @return the form file associated with the given field name or * null if no file exists */ public static UploadedFile getUploadedFile(String fieldName) { return RenderersRequestProcessorImpl.fileItems.get().get(fieldName); } public static Collection getAllUploadedFiles() { return RenderersRequestProcessorImpl.fileItems.get().values(); } public static String getCurrentEncoding() { HttpServletRequest currentRequest = getCurrentRequest(); return currentRequest != null ? currentRequest.getCharacterEncoding() : null; } protected static HttpServletRequest parseMultipartRequest(HttpServletRequest request, ActionForm form) { Hashtable itemsMap = getNewFileItemsMap(request); if (form == null) { return request; } else { if (form.getMultipartRequestHandler() != null) { return createWrapperFromActionForm(request, itemsMap, form); } else { return request; } } } protected static Hashtable getNewFileItemsMap(final HttpServletRequest request) { Hashtable itemsMap = (Hashtable) request .getAttribute(FenixHttpServletRequestWrapper.ITEM_MAP_ATTRIBUTE); if (itemsMap == null) { itemsMap = new Hashtable(); } RenderersRequestProcessorImpl.fileItems.set(itemsMap); return itemsMap; } protected static HttpServletRequest createWrapperFromActionForm(HttpServletRequest request, Hashtable itemsMap, ActionForm form) { RenderersRequestWrapper wrapper = new RenderersRequestWrapper(request); Hashtable textElements = form.getMultipartRequestHandler().getTextElements(); for (String name : textElements.keySet()) { String[] values = textElements.get(name); for (int i = 0; i < values.length; i++) { wrapper.addParameter(name, values[i]); } } Hashtable fileElements = form.getMultipartRequestHandler().getFileElements(); for (String name : fileElements.keySet()) { UploadedFile uploadedFile = new StrutsFile(fileElements.get(name)); if (uploadedFile.getName() != null && uploadedFile.getName().length() > 0) { itemsMap.put(name, uploadedFile); } wrapper.addParameter(name, uploadedFile.getName()); } Map existingParameters = request.getParameterMap(); for (String name : existingParameters.keySet()) { String[] values = existingParameters.get(name); for (String value : values) { wrapper.addParameter(name, value); } } Enumeration enumaration = request.getAttributeNames(); while (enumaration.hasMoreElements()) { String name = (String) enumaration.nextElement(); wrapper.setAttribute(name, request.getAttribute(name)); } return wrapper; } protected static boolean hasViewState(HttpServletRequest request) { return viewStateNotProcessed(request) && (request.getParameterValues(LifeCycleConstants.VIEWSTATE_PARAM_NAME) != null || request .getParameterValues(LifeCycleConstants.VIEWSTATE_LIST_PARAM_NAME) != null); } protected static boolean viewStateNotProcessed(HttpServletRequest request) { return request.getAttribute(LifeCycleConstants.PROCESSED_PARAM_NAME) == null; } protected static void setViewStateProcessed(HttpServletRequest request) { request.setAttribute(LifeCycleConstants.PROCESSED_PARAM_NAME, true); } }