1 package org.jahia.services.applications; 2 3 import javax.servlet.ServletException ; 4 import java.util.Enumeration ; 5 6 16 public class StrutsGlobalAttribute { 17 18 public static String REQUEST_SCOPE = "REQUEST"; 19 public static String SESSION_SCOPE = "SESSION"; 20 21 private String attributeName; 22 private String attributeScope; 23 private Object attributeValue; 24 private boolean attributeExist = false; 25 26 30 public StrutsGlobalAttribute (String attributeName, String attributeScope) { 31 this.attributeName = attributeName; 32 this.attributeScope = attributeScope; 33 } 34 35 42 public void backupAttribute (ServletIncludeRequestWrapper request, 43 ServletIncludeResponseWrapper response) 44 throws ServletException , java.io.IOException { 45 if (isRequestScope ()) { 46 if (checkAttribute (request.getAttributeNames ())) { 47 this.attributeValue = request.getAttribute (this.attributeName); 48 request.removeAttribute (this.attributeName); 49 } 50 } else { 51 if (checkAttribute (request.getAttributeNames ())) { 52 this.attributeValue = request.getSession () 53 .getAttribute (this.attributeName); 54 request.getSession ().removeAttribute (this.attributeName); 55 } 56 } 57 } 58 59 66 public void restoreAttribute (ServletIncludeRequestWrapper request, 67 ServletIncludeResponseWrapper response) 68 throws ServletException , java.io.IOException { 69 if (this.attributeExist) { 70 if (isRequestScope ()) { 71 request.setAttribute (this.attributeName, this.attributeValue); 72 } else { 73 request.getSession ().setAttribute (this.attributeName, 74 this.attributeValue); 75 } 76 } 77 } 78 79 private boolean isRequestScope () { 80 return REQUEST_SCOPE.equals (this.attributeScope); 81 } 82 83 private boolean checkAttribute (Enumeration names) { 84 while (names.hasMoreElements ()) { 85 String name = (String ) names.nextElement (); 86 if (name.equals (this.attributeName)) { 87 this.attributeExist = true; 88 break; 89 } 90 } 91 return this.attributeExist; 92 } 93 94 } 95 | Popular Tags |