1 18 19 package org.apache.struts.config; 20 21 import javax.servlet.ServletContext ; 22 import javax.servlet.http.HttpServletRequest ; 23 import javax.servlet.http.HttpServletResponse ; 24 import javax.servlet.http.HttpSession ; 25 import javax.sql.DataSource ; 26 27 import org.apache.struts.Globals; 28 import org.apache.struts.action.ActionForm; 29 import org.apache.struts.action.ActionFormBean; 30 import org.apache.struts.action.ActionForward; 31 import org.apache.struts.action.ActionMapping; 32 import org.apache.struts.action.ActionMessages; 33 import org.apache.struts.upload.MultipartRequestWrapper; 34 import org.apache.struts.util.MessageResources; 35 import org.apache.struts.util.RequestUtils; 36 37 63 public class ConfigHelper implements ConfigHelperInterface { 64 65 67 70 private ServletContext application = null; 71 72 76 public void setApplication(ServletContext application) { 77 this.application = application; 78 } 79 80 83 private HttpSession session = null; 84 85 88 public void setSession(HttpSession session) { 89 this.session = session; 90 } 91 92 95 private HttpServletRequest request = null; 96 97 101 public void setRequest(HttpServletRequest request) { 102 this.request = request; 103 if (this.request == null) 104 setSession(null); 105 else 106 setSession(this.request.getSession()); 107 } 108 109 112 private HttpServletResponse response = null; 113 114 118 public void setResponse(HttpServletResponse response) { 119 this.response = response; 120 } 121 122 125 private ActionForward forward = null; 126 127 130 public void setForward(ActionForward forward) { 131 this.forward = forward; 132 } 133 134 148 public void setResources( 149 ServletContext application, 150 HttpServletRequest request, 151 HttpServletResponse response) { 152 153 setApplication(application); 154 setRequest(request); 155 setResponse(response); 156 } 157 158 public ConfigHelper() { 159 super(); 160 } 161 162 public ConfigHelper( 163 ServletContext application, 164 HttpServletRequest request, 165 HttpServletResponse response) { 166 167 super(); 168 this.setResources(application, request, response); 169 } 170 171 172 174 180 public DataSource getDataSource() { 181 182 if (this.application == null) 183 return null; 184 return (DataSource ) this.application.getAttribute(Globals.DATA_SOURCE_KEY); 185 186 } 187 188 public ActionMessages getActionMessages() { 189 190 if (this.application == null) 191 return null; 192 return (ActionMessages) this.application.getAttribute(Globals.MESSAGE_KEY); 193 194 } 195 196 199 public MessageResources getMessageResources() { 200 201 if (this.application == null) { 202 return null; 203 } 204 return (MessageResources) this.application.getAttribute(Globals.MESSAGES_KEY); 205 206 } 207 208 213 public String getServletMapping() { 214 215 if (this.application == null) { 216 return null; 217 } 218 return (String ) this.application.getAttribute(Globals.SERVLET_KEY); 219 220 } 221 222 224 227 public String getToken() { 228 229 if (this.session == null) { 230 return null; 231 } 232 return (String ) session.getAttribute(Globals.TRANSACTION_TOKEN_KEY); 233 234 } 235 236 238 243 public Throwable getException() { 244 245 if (this.request == null) { 246 return null; 247 } 248 return (Throwable ) this.request.getAttribute(Globals.EXCEPTION_KEY); 249 250 } 251 252 255 public MultipartRequestWrapper getMultipartRequestWrapper() { 256 257 if (this.request == null) { 258 return null; 259 } 260 return (MultipartRequestWrapper) this.request.getAttribute(Globals.MULTIPART_KEY); 261 } 262 263 267 public ActionMapping getMapping() { 268 269 if (this.request == null) { 270 return null; 271 } 272 return (ActionMapping) this.request.getAttribute(Globals.MAPPING_KEY); 273 274 } 275 276 278 284 public boolean isMessage(String key) { 285 286 MessageResources resources = getMessageResources(); 288 289 if (resources == null) { 290 return false; 291 } 292 293 return resources.isPresent(RequestUtils.getUserLocale(request, null), key); 295 296 } 297 298 304 public ActionForm getActionForm() { 305 306 ActionMapping mapping = getMapping(); 308 if (mapping == null) 309 return (null); 310 311 String attribute = mapping.getAttribute(); 313 if (attribute == null) 314 return (null); 315 316 ActionForm instance = null; 318 if ("request".equals(mapping.getScope())) { 319 instance = (ActionForm) this.request.getAttribute(attribute); 320 } else { 321 instance = (ActionForm) this.session.getAttribute(attribute); 322 } 323 324 return instance; 325 } 326 327 333 public ActionFormBean getFormBean(String name) { 334 return null; 335 } 336 337 343 public ActionForward getActionForward(String name) { 344 return null; 345 } 346 347 353 public ActionMapping getActionMapping(String path) { 354 return null; 355 } 356 357 368 public String getActionMappingName(String action) { 369 370 String value = action; 371 int question = action.indexOf("?"); 372 if (question >= 0) 373 value = value.substring(0, question); 374 int slash = value.lastIndexOf("/"); 375 int period = value.lastIndexOf("."); 376 if ((period >= 0) && (period > slash)) 377 value = value.substring(0, period); 378 if (value.startsWith("/")) 379 return (value); 380 else 381 return ("/" + value); 382 383 } 384 385 388 public String getActionMappingURL(String action) { 389 390 StringBuffer value = new StringBuffer (this.request.getContextPath()); 391 392 String servletMapping = getServletMapping(); 394 395 if (servletMapping != null) { 396 String queryString = null; 397 int question = action.indexOf("?"); 398 if (question >= 0) 399 queryString = action.substring(question); 400 String actionMapping = getActionMappingName(action); 401 if (servletMapping.startsWith("*.")) { 402 value.append(actionMapping); 403 value.append(servletMapping.substring(1)); 404 } else if (servletMapping.endsWith("/*")) { 405 value.append(servletMapping.substring(0, servletMapping.length() - 2)); 406 value.append(actionMapping); 407 } 408 if (queryString != null) 409 value.append(queryString); 410 } 411 412 else { 415 if (!action.startsWith("/")) 416 value.append("/"); 417 value.append(action); 418 } 419 420 return (value.toString()); 422 423 } 424 425 428 public String getEncodeURL(String url) { 429 430 if ((session != null) && (response != null)) { 431 432 boolean redirect = false; 433 if (forward != null) 434 redirect = forward.getRedirect(); 435 436 if (redirect) 437 return response.encodeRedirectURL(url); 438 else 439 return response.encodeURL(url); 440 } else 441 return (url); 442 } 443 444 446 449 public String getOrigRef() { 450 451 453 if (request == null) 454 return null; 455 StringBuffer result = RequestUtils.requestToServerUriStringBuffer(request); 456 return result.toString(); 457 } 458 459 462 public String getBaseRef() { 463 464 if (request == null) 465 return null; 466 467 StringBuffer result = RequestUtils.requestToServerStringBuffer(request); 468 String path = null; 469 if (forward == null) 470 path = request.getRequestURI(); 471 else 472 path = request.getContextPath() + forward.getPath(); 473 result.append(path); 474 475 return result.toString(); 476 } 477 478 484 public String getLink(String name) { 485 486 ActionForward forward = getActionForward(name); 487 if (forward == null) 488 return null; 489 490 StringBuffer path = new StringBuffer (this.request.getContextPath()); 491 path.append(forward.getPath()); 492 493 495 return getEncodeURL(path.toString()); 496 497 } 498 499 505 public String getMessage(String key) { 506 507 MessageResources resources = getMessageResources(); 508 if (resources == null) 509 return null; 510 511 return resources.getMessage(RequestUtils.getUserLocale(request, null), key); 512 513 } 514 515 521 public String getMessage(String key, Object args[]) { 522 523 MessageResources resources = getMessageResources(); 524 525 if (resources == null) 526 return null; 527 528 if (args == null) 530 return resources.getMessage( 531 RequestUtils.getUserLocale(request, null), 532 key); 533 else 534 return resources.getMessage( 535 RequestUtils.getUserLocale(request, null), 536 key, 537 args); 538 539 } 540 541 547 public String getAction(String path) { 548 return getEncodeURL(getActionMappingURL(path)); 549 } 550 551 552 554 559 public String link(String name) { 560 return getLink(name); 561 } 562 563 568 public String message(String key) { 569 return getMessage(key); 570 } 571 572 578 public String message(String key, Object args[]) { 579 return getMessage(key, args); 580 } 581 582 587 public String action(String path) { 588 return getAction(path); 589 } 590 591 592 593 594 595 596 } 597 | Popular Tags |