1 18 19 package org.apache.struts.action; 20 21 import java.util.Locale ; 22 23 import javax.servlet.ServletContext ; 24 import javax.servlet.ServletRequest ; 25 import javax.servlet.ServletResponse ; 26 import javax.servlet.http.HttpServletRequest ; 27 import javax.servlet.http.HttpServletResponse ; 28 import javax.servlet.http.HttpSession ; 29 import javax.sql.DataSource ; 30 31 import org.apache.struts.Globals; 32 import org.apache.struts.config.ModuleConfig; 33 import org.apache.struts.util.MessageResources; 34 import org.apache.struts.util.ModuleUtils; 35 import org.apache.struts.util.RequestUtils; 36 import org.apache.struts.util.TokenProcessor; 37 38 69 public class Action { 70 71 74 private static TokenProcessor token = TokenProcessor.getInstance(); 75 78 79 81 82 88 protected static Locale defaultLocale = Locale.getDefault(); 89 91 92 95 protected ActionServlet servlet = null; 96 97 98 100 101 104 public ActionServlet getServlet() { 105 106 return (this.servlet); 107 108 } 109 110 111 118 public void setServlet(ActionServlet servlet) { 119 120 this.servlet = servlet; 121 123 124 } 125 126 127 129 130 151 public ActionForward execute( 152 ActionMapping mapping, 153 ActionForm form, 154 ServletRequest request, 155 ServletResponse response) 156 throws Exception { 157 158 try { 159 return execute( 160 mapping, 161 form, 162 (HttpServletRequest ) request, 163 (HttpServletResponse ) response); 164 165 } catch (ClassCastException e) { 166 return null; 167 } 168 169 } 170 171 172 189 public ActionForward execute( 190 ActionMapping mapping, 191 ActionForm form, 192 HttpServletRequest request, 193 HttpServletResponse response) 194 throws Exception { 195 196 return null; 197 198 } 199 200 201 203 204 215 protected void addMessages( 216 HttpServletRequest request, 217 ActionMessages messages) { 218 219 if (messages == null){ 220 return; 222 } 223 224 ActionMessages requestMessages = (ActionMessages) request.getAttribute(Globals.MESSAGE_KEY); 226 if (requestMessages == null){ 227 requestMessages = new ActionMessages(); 228 } 229 requestMessages.add(messages); 231 232 if (requestMessages.isEmpty()) { 234 request.removeAttribute(Globals.MESSAGE_KEY); 235 return; 236 } 237 238 request.setAttribute(Globals.MESSAGE_KEY, requestMessages); 240 } 241 242 243 253 protected void addErrors( 254 HttpServletRequest request, 255 ActionMessages errors) { 256 257 if (errors == null){ 258 return; 260 } 261 262 ActionMessages requestErrors = (ActionMessages)request.getAttribute(Globals.ERROR_KEY); 264 if (requestErrors == null){ 265 requestErrors = new ActionMessages(); 266 } 267 requestErrors.add(errors); 269 270 if (requestErrors.isEmpty()) { 272 request.removeAttribute(Globals.ERROR_KEY); 273 return; 274 } 275 276 request.setAttribute(Globals.ERROR_KEY, requestErrors); 278 } 279 280 281 287 protected String generateToken(HttpServletRequest request) { 288 return token.generateToken(request); 289 } 290 291 292 299 protected DataSource getDataSource(HttpServletRequest request) { 300 301 return (getDataSource(request, Globals.DATA_SOURCE_KEY)); 302 303 } 304 305 306 307 316 protected DataSource getDataSource(HttpServletRequest request, String key) { 317 318 ServletContext context = getServlet().getServletContext(); 320 ModuleConfig moduleConfig = 321 ModuleUtils.getInstance().getModuleConfig(request, context); 322 323 return (DataSource ) context.getAttribute(key + moduleConfig.getPrefix()); 324 } 325 326 327 336 protected ActionMessages getErrors(HttpServletRequest request) { 337 ActionMessages errors = 338 (ActionMessages) request.getAttribute(Globals.ERROR_KEY); 339 if (errors == null) { 340 errors = new ActionMessages(); 341 } 342 return errors; 343 } 344 345 346 351 protected Locale getLocale(HttpServletRequest request) { 352 353 return RequestUtils.getUserLocale(request, null); 354 355 } 356 357 358 367 protected ActionMessages getMessages(HttpServletRequest request) { 368 ActionMessages messages = 369 (ActionMessages) request.getAttribute(Globals.MESSAGE_KEY); 370 if (messages == null) { 371 messages = new ActionMessages(); 372 } 373 return messages; 374 } 375 376 377 383 protected MessageResources getResources(HttpServletRequest request) { 384 385 return ((MessageResources) request.getAttribute(Globals.MESSAGES_KEY)); 386 387 } 388 389 390 400 protected MessageResources getResources( 401 HttpServletRequest request, 402 String key) { 403 404 ServletContext context = getServlet().getServletContext(); 406 ModuleConfig moduleConfig = 407 ModuleUtils.getInstance().getModuleConfig(request, context); 408 409 return (MessageResources) context.getAttribute( 411 key + moduleConfig.getPrefix()); 412 413 } 414 415 416 428 protected boolean isCancelled(HttpServletRequest request) { 429 430 return (request.getAttribute(Globals.CANCEL_KEY) != null); 431 432 } 433 434 435 450 protected boolean isTokenValid(HttpServletRequest request) { 451 452 return token.isTokenValid(request, false); 453 454 } 455 456 457 473 protected boolean isTokenValid(HttpServletRequest request, boolean reset) { 474 475 return token.isTokenValid(request, reset); 476 477 } 478 479 480 487 protected void resetToken(HttpServletRequest request) { 488 489 token.resetToken(request); 490 491 } 492 493 494 505 protected void saveErrors(HttpServletRequest request, ActionErrors errors) { 506 507 this.saveErrors(request,(ActionMessages)errors); 508 510 } 511 512 513 523 protected void saveErrors(HttpServletRequest request, ActionMessages errors) { 524 525 if ((errors == null) || errors.isEmpty()) { 527 request.removeAttribute(Globals.ERROR_KEY); 528 return; 529 } 530 531 request.setAttribute(Globals.ERROR_KEY, errors); 533 534 } 535 536 537 549 protected void saveMessages( 550 HttpServletRequest request, 551 ActionMessages messages) { 552 553 if ((messages == null) || messages.isEmpty()) { 555 request.removeAttribute(Globals.MESSAGE_KEY); 556 return; 557 } 558 559 request.setAttribute(Globals.MESSAGE_KEY, messages); 561 } 562 563 564 576 protected void saveMessages( 577 HttpSession session, 578 ActionMessages messages) { 579 580 if ((messages == null) || messages.isEmpty()) { 582 session.removeAttribute(Globals.MESSAGE_KEY); 583 return; 584 } 585 586 session.setAttribute(Globals.MESSAGE_KEY, messages); 588 } 589 590 591 603 protected void saveErrors( 604 HttpSession session, 605 ActionMessages errors) { 606 607 if ((errors == null) || errors.isEmpty()) { 609 session.removeAttribute(Globals.ERROR_KEY); 610 return; 611 } 612 613 session.setAttribute(Globals.ERROR_KEY, errors); 615 } 616 617 618 624 protected void saveToken(HttpServletRequest request) { 625 token.saveToken(request); 626 } 627 628 629 637 protected void setLocale(HttpServletRequest request, Locale locale) { 638 639 HttpSession session = request.getSession(); 640 if (locale == null) { 641 locale = Locale.getDefault(); 642 } 643 session.setAttribute(Globals.LOCALE_KEY, locale); 644 645 } 646 647 } 648 | Popular Tags |