1 3 package jodd.madvoc; 4 5 import jodd.madvoc.result.ActionResult; 6 import jodd.madvoc.result.ServletDispatcherResult; 7 import jodd.madvoc.interceptor.ActionInterceptor; 8 import jodd.madvoc.interceptor.ServletConfigInterceptor; 9 import jodd.madvoc.interceptor.RenderViewInterceptor; 10 import jodd.servlet.DispatcherUtil; 11 import jodd.servlet.upload.FileUploadFactory; 12 import jodd.servlet.upload.impl.AdaptiveFileUpload; 13 14 import javax.servlet.ServletContext ; 15 import javax.servlet.http.HttpServletRequest ; 16 import javax.servlet.http.HttpServletResponse ; 17 import java.util.Map ; 18 import java.util.HashMap ; 19 20 29 public class WebApplication { 30 31 @SuppressWarnings ({"unchecked"}) 32 public WebApplication(ServletContext servletContext) { 33 configs = new HashMap <String , ActionConfig>(); 34 results = new HashMap <String , ActionResult>(); 35 resultsAliases = new HashMap <String , String >(); 36 this.servletContext = servletContext; 37 defaultActionResultName = ServletDispatcherResult.NAME; 38 encoding = "UTF-8"; 39 fileUploadFactory = new AdaptiveFileUpload.Factory(); 40 defaultInterceptors = new Class []{RenderViewInterceptor.class, ServletConfigInterceptor.class}; 41 } 42 43 44 46 protected String encoding; 47 48 51 public String getEncoding() { 52 return this.encoding; 53 } 54 57 public void setEncoding(String encoding) { 58 this.encoding = encoding; 59 } 60 61 62 64 protected FileUploadFactory fileUploadFactory; 65 66 69 public FileUploadFactory getFileUploadFactory() { 70 return fileUploadFactory; 71 } 72 73 76 public void setFileUploadFactory(FileUploadFactory fileUploadFactory) { 77 this.fileUploadFactory = fileUploadFactory; 78 } 79 80 82 protected final ServletContext servletContext; 83 84 87 public ServletContext getServletContext() { 88 return servletContext; 89 } 90 91 93 protected final Map <String , ActionConfig> configs; 94 95 99 public Map <String , ActionConfig> getAllActionConfigs() { 100 return configs; 101 } 102 103 106 public void register(ActionConfig actionConfig) { 107 configs.put(actionConfig.actionPath, actionConfig); 108 } 109 110 113 public ActionConfig lookupActionConfig(String actionPath) { 114 return configs.get(actionPath); 115 } 116 117 118 124 public ActionRequest buildActionRequest(HttpServletRequest request, HttpServletResponse response) { 125 String requestPath = DispatcherUtil.getServletPath(request); 126 ActionConfig config = configs.get(requestPath); 127 if (config == null) { 128 return null; 129 } 130 return buildActionRequest(config, request, response); 131 } 132 133 134 136 protected Class <? extends ActionInterceptor>[] defaultInterceptors; 137 138 141 public Class <? extends ActionInterceptor>[] getDefaultInterceptors() { 142 return defaultInterceptors; 143 } 144 145 148 public void setDefaultInterceptors(Class <? extends ActionInterceptor>[] defaultInterceptors) { 149 this.defaultInterceptors = defaultInterceptors; 150 } 151 152 154 protected final Map <String , ActionResult> results; 155 156 159 public void register(ActionResult actionResult) { 160 results.put(actionResult.getName(), actionResult); 161 } 162 163 166 public ActionResult lookupActionResult(String resultName) { 167 return results.get(resultName); 168 } 169 170 protected String defaultActionResultName; 171 172 177 public void setDefaultActionResultName(String name) { 178 defaultActionResultName = name; 179 } 180 181 184 public String getDefaultActionResultName() { 185 return defaultActionResultName; 186 } 187 188 189 191 protected Map <String , String > resultsAliases; 192 193 196 public void setResultAlias(String alias, String target) { 197 if (alias.startsWith("/") == false) { 198 alias = '/' + alias; 199 } 200 resultsAliases.put(alias, target); 201 } 202 203 206 public String getResultAlias(String alias) { 207 String value = resultsAliases.get(alias); 208 if (value != null) { 209 return value; 210 } 211 return alias; 212 } 213 214 215 217 220 protected ActionRequest buildActionRequest(ActionConfig config, HttpServletRequest request, HttpServletResponse response) { 221 return new ActionRequest(this, config, request, response); 222 } 223 224 227 public ActionInterceptor buildInterceptor(Class <? extends ActionInterceptor> actionInterceptorClass) { 228 ActionInterceptor interceptor; 229 try { 230 interceptor = actionInterceptorClass.newInstance(); 231 } catch (InstantiationException iex) { 233 throw new MadvocException("Unable to create Madvoc action interceptor '" + actionInterceptorClass + "'.", iex); 234 } catch (IllegalAccessException iaex) { 235 throw new MadvocException("Not enough rights to create Madvoc action interceptor '" + actionInterceptorClass + "'.", iaex); 236 } 237 return interceptor; 238 } 239 240 243 public Object buildAction(ActionConfig actionConfig) { 244 Object action; 245 try { 246 action = actionConfig.actionClass.newInstance(); 247 } catch (InstantiationException iex) { 248 throw new MadvocException("Unable to create Madvoc action.", iex); 249 } catch (IllegalAccessException iaex) { 250 throw new MadvocException("Not enough rights to create Madvoc action.", iaex); 251 } 252 return action; 253 } 254 255 } 256 | Popular Tags |