package com.processpuzzle.application.control.control; import java.io.UnsupportedEncodingException; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Properties; import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.fileupload.FileItem; import com.processpuzzle.application.configuration.control.ServletHelper; import com.processpuzzle.file.control.MultipartManager; /** * Instances turn a request into a command, which may then be executed. * * @pattern Command (role=commandDispatcher) * @generatedBy CodePro at 2005.11.13. 6:28 * @author zsolt.zsuffa * @version $Revision$ */ public class CommandDispatcher { public static final java.lang.String ACTION_PARAMETER_NAME = "action"; private javax.servlet.http.HttpServletRequest request; private javax.servlet.http.HttpServletResponse response; private ServletContext servletContext; private java.util.Properties properties; // The properties associated with the request. private Map<String, Collection<String>> propertyValues = new HashMap<String, Collection<String>>(); public CommandDispatcher( HttpServletRequest request, HttpServletResponse response, ServletContext servletContext ) { this.request = request; this.response = response; this.servletContext = servletContext; this.properties = new ServletHelper().extractProperties( request ); resolveMultipartContent( request, properties ); } public String executeCommand() throws Exception { CommandInterface command = getCommand(); if( command == null ){ command = new UnknownFrontCommand( getProperties().getProperty( ACTION_PARAMETER_NAME ) ); } command.init( this ); return command.execute( this ); } // Public setters and getters public Properties getProperties() { return properties; } public Map<String, Collection<String>> getPropertyValues() { return propertyValues; } public HttpServletRequest getRequest() { return request; } public HttpServletResponse getResponse() { return response; } public ServletContext getServletContext() { return servletContext; } // Private helper methods private CommandInterface getCommand() { String param = getProperties().getProperty( ACTION_PARAMETER_NAME ); if( param == null ){ param = "UnknownFrontCommand"; } return CommandFactory.getCommand( param ); } private void resolveMultipartContent( HttpServletRequest request, Properties properties ) { MultipartManager mm = new MultipartManager( request ); if( mm.isMultipartContent() ){ mm.processRequest(); List<?> formItems = mm.getFormItems(); Iterator<?> i = formItems.iterator(); while( i.hasNext() ){ FileItem item = (FileItem) i.next(); if( item.isFormField() ){ // form field String value; try{ value = item.getString( "UTF-8" ); }catch( UnsupportedEncodingException e ){ value = item.getString(); } String name = item.getFieldName(); if( properties.containsKey( name ) ){ Collection<String> values = new ArrayList<String>(); values.add( properties.getProperty( name ) ); values.add( value ); propertyValues.put( name, values ); properties.remove( name ); }else if( propertyValues.containsKey( name ) ){ ((ArrayList<String>) propertyValues.get( name )).add( value ); }else properties.put( name, value ); } } } } } /* * $CPS$ This comment was generated by CodePro. Do not edit it. patternId = com.instantiations.assist.eclipse.pattern.frontControllerPattern strategyId = * com.instantiations.assist.eclipse.pattern.frontControllerPattern.frontController abstract = false commandInterface = CommandInterface commandInterfaces = * commands = Show, Browse, Save, Delete, New deploy = true dispatchClass = CommandDispatcher dispatchSuperclass = java.lang.Object factoryClass = * CommandFactory factorySuperclass = java.lang.Object final = false propertyFile = commandMapping.properties public = true publicCommand = true servletClass = * CommandControllerServlet servletInterfaces = servletPackage = com.itcodex.objectpuzzle.framework.control servletPackage.sourceFolder = ObjectPuzzle Framework * Web Tier/JavaSource servletParameter = action servletSuperclass = javax.servlet.http.HttpServlet singleThread = false supportPackage = * com.itcodex.objectpuzzle.framework.control supportPackage.sourceFolder = ObjectPuzzle Framework Web Tier/JavaSource */