1 23 24 package com.sun.appserv.web.cache.mapping; 25 26 import java.text.MessageFormat ; 27 28 import java.util.logging.Logger ; 29 import java.util.ResourceBundle ; 30 31 import javax.servlet.ServletContext ; 32 33 import javax.servlet.http.HttpServletRequest ; 34 import javax.servlet.http.HttpSession ; 35 import javax.servlet.http.Cookie ; 36 37 import com.sun.enterprise.web.logging.pwc.LogDomains; 38 39 public class Field { 40 41 private static Logger _logger; 43 46 private static ResourceBundle _rb = null; 47 48 protected String name; 50 51 protected int scope; 53 54 59 public Field (String name, String scope) throws IllegalArgumentException { 60 _logger = LogDomains.getLogger(LogDomains.PWC_LOGGER); 62 _rb = _logger.getResourceBundle(); 63 64 this.name = name; 65 this.scope = parseScope(scope); 66 } 67 68 72 public void setName(String name) { 73 this.name = name; 74 } 75 76 80 public void setScope(int scope) { 81 this.scope = scope; 82 } 83 84 88 private int parseScope(String value) throws IllegalArgumentException { 89 int scope; 90 if ("context.attribute".equals(value)) 91 scope = Constants.SCOPE_CONTEXT_ATTRIBUTE; 92 else if ("request.header".equals(value)) 93 scope = Constants.SCOPE_REQUEST_HEADER; 94 else if ("request.parameter".equals(value)) 95 scope = Constants.SCOPE_REQUEST_PARAMETER; 96 else if ("request.cookie".equals(value)) 97 scope = Constants.SCOPE_REQUEST_COOKIE; 98 else if ("request.attribute".equals(value)) 99 scope = Constants.SCOPE_REQUEST_ATTRIBUTE; 100 else if ("session.attribute".equals(value)) 101 scope = Constants.SCOPE_SESSION_ATTRIBUTE; 102 else if ("session.id".equals(value)) 103 scope = Constants.SCOPE_SESSION_ID; 104 else { 105 String msg = _rb.getString("cache.mapping.incorrectScope"); 106 Object [] params = { value, name }; 107 msg = MessageFormat.format(msg, params); 108 109 throw new IllegalArgumentException (msg); 110 } 111 return scope; 112 } 113 114 118 public String getName() { 119 return name; 120 } 121 122 126 public int getScope() { 127 return scope; 128 } 129 130 135 public Object getValue(ServletContext context, 136 HttpServletRequest request) { 137 138 Object value = null; 139 switch (scope) { 140 case Constants.SCOPE_CONTEXT_ATTRIBUTE: 141 value = context.getAttribute(name); 142 break; 143 case Constants.SCOPE_REQUEST_HEADER: 144 value = request.getHeader(name); 145 break; 146 case Constants.SCOPE_REQUEST_PARAMETER: 147 value = request.getParameter(name); 148 break; 149 case Constants.SCOPE_REQUEST_COOKIE: 150 Cookie cookies[] = request.getCookies(); 151 for (int i = 0; i < cookies.length; i++) { 152 if (name.equals(cookies[i].getName())) { 153 value = cookies[i].getValue(); 154 break; 155 } 156 } 157 break; 158 case Constants.SCOPE_REQUEST_ATTRIBUTE: 159 value = request.getAttribute(name); 160 break; 161 case Constants.SCOPE_SESSION_ID: 162 { 163 HttpSession session = request.getSession(false); 164 if (session != null) { 165 value = session.getId(); 166 } 167 } 168 break; 169 case Constants.SCOPE_SESSION_ATTRIBUTE: 170 { 171 HttpSession session = request.getSession(false); 172 if (session != null) { 173 value = session.getAttribute(name); 174 } 175 } 176 break; 177 } 178 return value; 179 } 180 } 181 | Popular Tags |