1 3 package jodd.madvoc.injector; 4 5 import jodd.madvoc.ActionRequest; 6 import jodd.madvoc.WebApplication; 7 import jodd.madvoc.interceptor.ScopeType; 8 import jodd.madvoc.meta.In; 9 import jodd.madvoc.meta.Out; 10 import jodd.introspector.DefaultIntrospector; 11 import jodd.introspector.ClassDescriptor; 12 import jodd.bean.BeanUtil; 13 import jodd.servlet.upload.MultipartRequestWrapper; 14 import jodd.servlet.upload.FileUpload; 15 import jodd.util.ReflectUtil; 16 17 import javax.servlet.http.HttpServletRequest ; 18 import javax.servlet.http.HttpSession ; 19 import javax.servlet.http.HttpServletResponse ; 20 import javax.servlet.ServletContext ; 21 import java.lang.reflect.Field ; 22 import java.util.*; 23 24 27 public class ActionFieldInjector implements ActionInjector { 28 29 public ActionFieldInjector() { 30 } 31 32 35 static class ScopeParams { 36 ScopeType scope; 37 InjectInfo[] injectInfos; 38 OutjectInfo[] outjectInfos; 39 } 40 static class InjectInfo { 41 Field field; 42 String name; 43 String target; 44 boolean create; 45 } 46 static class OutjectInfo { 47 Field field; 48 String name; 49 String target; 50 } 51 52 protected ScopeParams[] sp; 53 54 57 public void inject(ActionRequest request) { 58 if (sp == null) { 59 sp = inspect(request.getAction()); 60 } 61 for (ScopeParams scopeParam : sp) { 62 if (scopeParam.injectInfos == null) { 63 continue; 64 } 65 switch (scopeParam.scope) { 66 case PARAM: injectRequestParam(request, scopeParam.injectInfos); break; 67 case CONTEXT: injectContextScopes(request, scopeParam.injectInfos); break; 68 default: injectAttributes(request, scopeParam.injectInfos, scopeParam.scope); break; 69 } 70 } 71 } 72 73 public void outject(ActionRequest request) { 74 if (sp == null) { 75 sp = inspect(request.getAction()); 76 } 77 for (ScopeParams scopeParam : sp) { 78 if (scopeParam.outjectInfos == null) { 79 continue; 80 } 81 outjectAttributes(request, scopeParam.outjectInfos, scopeParam.scope); 82 } 83 } 84 85 86 88 91 @SuppressWarnings ({"unchecked"}) 92 protected ScopeParams[] inspect(Object action) { 93 ClassDescriptor cd = DefaultIntrospector.lookup(action.getClass()); 94 EnumMap<ScopeType, List[]> scopes = new EnumMap<ScopeType, List[]>(ScopeType.class); 95 Field [] fields = cd.getAllFields(true); 96 97 for(Field field : fields) { 98 In in = field.getAnnotation(In.class); 99 if (in != null) { 100 ScopeType scope = in.scope(); 101 InjectInfo ii = new InjectInfo(); 102 if (in.value().trim().length() > 0) { 103 ii.name = in.value().trim(); 104 ii.target = field.getName(); 105 } else { 106 ii.name = field.getName(); 107 ii.target = null; 108 } 109 ii.field = field; 110 ii.create = in.create(); 111 112 List[] lists = scopes.get(scope); 113 if (lists == null) { 114 lists = new List[2]; 115 scopes.put(scope, lists); 116 } 117 List listIn = lists[0]; 118 if (listIn == null) { 119 listIn = lists[0] = new ArrayList(); 120 } 121 listIn.add(ii); 122 } 123 124 Out out = field.getAnnotation(Out.class); 125 if (out != null) { 126 ScopeType scope = out.scope(); 127 OutjectInfo oi = new OutjectInfo(); 128 if (out.value().trim().length() > 0) { 129 oi.name = out.value().trim(); 130 oi.target = field.getName(); 131 } else { 132 oi.name = field.getName(); 133 oi.target = null; 134 } 135 oi.field = field; 136 137 List[] lists = scopes.get(scope); 138 if (lists == null) { 139 lists = new List[2]; 140 scopes.put(scope, lists); 141 } 142 List listOut = lists[1]; 143 if (listOut == null) { 144 listOut = lists[1] = new ArrayList(); 145 } 146 listOut.add(oi); 147 } 148 } 149 150 ScopeParams[] scopeParams = new ScopeParams[scopes.size()]; 151 int i = 0; 152 for (ScopeType scope : scopes.keySet()) { 153 scopeParams[i] = new ScopeParams(); 154 scopeParams[i].scope = scope; 155 List<InjectInfo> listIn = scopes.get(scope)[0]; 156 if (listIn != null) { 157 scopeParams[i].injectInfos = listIn.toArray(new InjectInfo[listIn.size()]); 158 } 159 List<OutjectInfo> listOut = scopes.get(scope)[1]; 160 if (listOut != null) { 161 scopeParams[i].outjectInfos = listOut.toArray(new OutjectInfo[listOut.size()]); 162 } 163 i++; 164 } 165 return scopeParams; 166 } 167 168 169 171 175 protected void injectRequestParam(ActionRequest actionRequest, InjectInfo[] injectInfos) { 176 HttpServletRequest servletRequest = actionRequest.getHttpServletRequest(); 177 Enumeration paramNames = servletRequest.getParameterNames(); 178 while (paramNames.hasMoreElements()) { 179 String paramName = (String ) paramNames.nextElement(); 180 for (InjectInfo ii : injectInfos) { 181 if (matchParamName(ii.name, paramName) == true) { 182 String [] paramValues = servletRequest.getParameterValues(paramName); 183 String name = getPropertyName(ii, paramName); 184 if (ii.create == true) { 185 BeanUtil.setDeclaredPropertyForcedSilent(actionRequest.getAction(), name, (paramValues.length == 1 ? paramValues[0] : paramValues)); 186 } else { 187 BeanUtil.setDeclaredPropertySilent(actionRequest.getAction(), name, (paramValues.length == 1 ? paramValues[0] : paramValues)); 188 } 189 } 190 } 191 } 192 if ((servletRequest instanceof MultipartRequestWrapper) == false) { 193 return; 194 } 195 MultipartRequestWrapper multipartRequest = (MultipartRequestWrapper) servletRequest; 196 if (multipartRequest.isMultipart() == false) { 197 return; 198 } 199 paramNames = multipartRequest.getFileParameterNames(); 200 while (paramNames.hasMoreElements()) { 201 String paramName = (String ) paramNames.nextElement(); 202 for (InjectInfo ii : injectInfos) { 203 if (matchParamName(ii.name, paramName) == true) { 204 FileUpload[] paramValues = multipartRequest.getFiles(paramName); 205 String name = getPropertyName(ii, paramName); 206 if (ii.create == true) { 207 BeanUtil.setDeclaredPropertyForcedSilent(actionRequest.getAction(), name, (paramValues.length == 1 ? paramValues[0] : paramValues)); 208 } else { 209 BeanUtil.setDeclaredPropertySilent(actionRequest.getAction(), name, (paramValues.length == 1 ? paramValues[0] : paramValues)); 210 } 211 } 212 } 213 } 214 215 216 217 } 218 219 222 @SuppressWarnings ({"ConstantConditions"}) 223 protected void injectAttributes(ActionRequest actionRequest, InjectInfo[] injectInfos, ScopeType scope) { 224 HttpServletRequest servletRequest = null; 225 HttpSession servletSession = null; 226 ServletContext servletContext = null; 227 Enumeration attributeNames; 228 switch (scope) { 229 case REQUEST: servletRequest = actionRequest.getHttpServletRequest(); attributeNames = servletRequest.getAttributeNames(); break; 230 case SESSION: servletSession = actionRequest.getHttpServletRequest().getSession(); attributeNames = servletSession.getAttributeNames(); break; 231 case APPLICATION: servletContext = actionRequest.getWebApplication().getServletContext(); attributeNames = servletContext.getAttributeNames(); break; 232 default: return; 233 } 234 while (attributeNames.hasMoreElements()) { 235 String attrName = (String ) attributeNames.nextElement(); 236 for (InjectInfo ii : injectInfos) { 237 if (matchParamName(ii.name, attrName) == true) { 238 Object attrValue = null; 239 switch (scope) { 240 case REQUEST: attrValue = servletRequest.getAttribute(attrName); break; 241 case SESSION: attrValue = servletSession.getAttribute(attrName); break; 242 case APPLICATION: attrValue = servletContext.getAttribute(attrName); break; 243 } 244 String name = getPropertyName(ii, attrName); 245 if (ii.create == true) { 246 BeanUtil.setDeclaredPropertyForcedSilent(actionRequest.getAction(), name, attrValue); 247 } else { 248 BeanUtil.setDeclaredPropertySilent(actionRequest.getAction(), name, attrValue); 249 } 250 } 251 } 252 } 253 } 254 255 258 protected void injectContextScopes(ActionRequest actionRequest, InjectInfo[] injectInfos) { 259 for (InjectInfo ii : injectInfos) { 260 Class fieldType = ii.field.getType(); 261 Object value = null; 262 if (ReflectUtil.isSubclass(fieldType, HttpServletRequest .class)) { 263 value = actionRequest.getHttpServletRequest(); 264 } else if (ReflectUtil.isSubclass(fieldType, HttpServletResponse .class)) { 265 value = actionRequest.getHttpServletResponse(); 266 } else if (ReflectUtil.isSubclass(fieldType, HttpSession .class)) { 267 value = actionRequest.getHttpServletRequest().getSession(); 268 } else if (ReflectUtil.isSubclass(fieldType, ServletContext .class)) { 269 value = actionRequest.getHttpServletRequest().getSession().getServletContext(); 270 } else if (ReflectUtil.isSubclass(fieldType, WebApplication.class)) { 271 value = actionRequest.getWebApplication(); 272 } 273 if (value != null) { 274 BeanUtil.setDeclaredProperty(actionRequest.getAction(), ii.field.getName(), value); 275 } 276 } 277 } 278 279 280 281 283 286 @SuppressWarnings ({"ConstantConditions"}) 287 protected void outjectAttributes(ActionRequest actionRequest, OutjectInfo[] outjectInfos, ScopeType scope) { 288 HttpServletRequest servletRequest = null; 289 HttpSession servletSession = null; 290 ServletContext servletContext = null; 291 switch (scope) { 292 case PARAM: 293 case REQUEST: servletRequest = actionRequest.getHttpServletRequest(); break; 294 case SESSION: servletSession = actionRequest.getHttpServletRequest().getSession(); break; 295 case APPLICATION: servletContext = actionRequest.getWebApplication().getServletContext(); break; 296 default: return; 297 } 298 for (OutjectInfo oi : outjectInfos) { 299 Object value; 300 if (oi.target == null) { 301 value = BeanUtil.getDeclaredProperty(actionRequest.getAction(), oi.name); 302 } else { 303 value = BeanUtil.getDeclaredProperty(actionRequest.getAction(), oi.target); 304 } 305 switch (scope) { 306 case PARAM: 307 case REQUEST: servletRequest.setAttribute(oi.name, value); break; 308 case SESSION: servletSession.setAttribute(oi.name, value); break; 309 case APPLICATION: servletContext.setAttribute(oi.name, value); break; 310 } 311 } 312 } 313 314 315 317 321 protected boolean matchParamName(String required, String param) { 322 if (param.startsWith(required) == false) { 323 return false; 324 } 325 int requiredLen = required.length(); 326 if (param.length() >= requiredLen + 1) { 327 if ((param.charAt(requiredLen) != '.') && (param.charAt(requiredLen) != '[')) { 328 return false; 329 } 330 } 331 return true; 332 } 333 334 337 protected String getPropertyName(InjectInfo ii, String attrName) { 338 if (ii.target == null) { 339 return attrName; 340 } 341 return ii.target + attrName.substring(ii.name.length()); 342 } 343 344 345 } 346 | Popular Tags |