1 23 24 package com.sun.enterprise.tools.verifier.tests.web.ias; 25 26 import java.util.*; 27 import com.sun.enterprise.deployment.*; 28 import com.sun.enterprise.deployment.web.*; 29 import com.sun.enterprise.tools.verifier.*; 30 import com.sun.enterprise.tools.verifier.tests.*; 31 import com.sun.enterprise.deployment.ResourceReferenceDescriptor; 32 import com.sun.enterprise.deployment.WebBundleDescriptor; 33 import com.sun.enterprise.tools.verifier.tests.web.*; 34 import com.sun.enterprise.tools.common.dd.webapp.*; 35 36 39 41 public class ASCacheMapping extends ASCache implements WebCheck { 42 43 44 boolean oneWarning = false; 45 public Result check(WebBundleDescriptor descriptor) { 46 47 Result result = getInitializedResult(); 48 WebComponentNameConstructor compName = new WebComponentNameConstructor(descriptor); 49 50 boolean oneFailed = false; 51 boolean notApp = false; 52 Cache cache = getCache(descriptor); 53 54 CacheMapping[] cacheMapp=null; 55 56 String servletName=null; 57 String urlPattern=null; 58 String timeout=null; 59 String [] httpMethods; 60 String cacheHelperRef; 62 63 64 65 if (cache != null ){ 66 cacheMapp=cache.getCacheMapping(); 67 68 } 69 70 71 if (cache != null && cacheMapp !=null && cacheMapp.length !=0 ) { 72 for(int rep=0;rep < cacheMapp.length;rep++){ 73 servletName = cacheMapp[rep].getServletName(); 74 urlPattern = cacheMapp[rep].getUrlPattern(); 75 timeout = cacheMapp[rep].getTimeout(); 76 httpMethods = cacheMapp[rep].getHttpMethod(); 77 cacheHelperRef = cacheMapp[rep].getCacheHelperRef(); 78 79 if(servletName !=null){ 80 if(validServletName(servletName,descriptor)){ 81 82 result.passed(smh.getLocalString 83 (getClass().getName() + ".passed", 84 "PASSED [AS-WEB cache-mapping] servlet-name [ {0} ] properly defined.", 85 new Object [] {servletName})); 86 87 }else{ 88 89 result.failed(smh.getLocalString 90 (getClass().getName() + ".failed", 91 "FAILED [AS-WEB cache-mapping] servlet-name [ {0} ], does not exist in the web application.", 92 new Object [] {servletName})); 93 oneFailed = true; 94 95 } 96 97 } 98 else if(urlPattern !=null) 99 { 100 if(validURL(urlPattern)){ 101 102 result.passed(smh.getLocalString 103 (getClass().getName() + ".passed1", 104 "PASSED [AS-WEB cache-mapping] url-pattern [ {0} ] properly defined.", 105 new Object [] {urlPattern})); 106 107 }else{ 108 109 result.failed(smh.getLocalString 110 (getClass().getName() + ".failed1", 111 "FAILED [AS-WEB cache-mapping] url-pattern [ {0} ], does not exist in the web application.", 112 new Object [] {urlPattern})); 113 oneFailed = true; 114 } 115 116 } 117 118 if(cacheHelperRef !=null){ 119 if(validCacheHelperRef(cacheHelperRef,cache)){ 121 122 result.passed(smh.getLocalString 123 (getClass().getName() + ".passed2", 124 "PASSED [AS-WEB cache-mapping] cache-helper-ref element [ {0} ] defined properly.", 125 new Object [] {cacheHelperRef})); 126 127 } 128 else{ 129 oneFailed = true; 130 result.failed(smh.getLocalString 131 (getClass().getName() + ".failed2", 132 "FAILED [AS-WEB cache-mapping] cache-helper-ref [ {0} ] not valid, either empty or cache-helper not defined for it.", 133 new Object [] {cacheHelperRef})); 134 135 } 136 137 } else { 138 139 if(timeout != null){ 140 String timeoutName=cacheMapp[rep].getAttributeValue("Timeout","Name"); 141 142 if(validTimeout(timeout,timeoutName)){ 143 144 result.passed(smh.getLocalString 145 (getClass().getName() + ".passed3", 146 "PASSED [AS-WEB cache-mapping] timeout element [ {0} ] properly defined.", 147 new Object [] {new Integer (timeout)})); 148 149 } 150 else{ 151 oneFailed = true; 152 result.failed(smh.getLocalString 153 (getClass().getName() + ".failed3", 154 "FAILED [AS-WEB cache-mapping] timeout element [{0}] must be a Long ( Not less than -1 and not more that MAX_LONG) and its name attribute [{1}] can not be empty/null.", 155 new Object [] {timeout,timeoutName})); 156 157 158 159 } 160 } 161 if(cacheMapp[rep].isRefreshField()) 163 { 164 String cacheMapName = null; 165 if(cacheMapp[rep].getServletName()!=null) 166 cacheMapName = cacheMapp[rep].getServletName(); 167 else 168 cacheMapName = cacheMapp[rep].getUrlPattern(); 169 String name = cacheMapp[rep].getAttributeValue(cacheMapp[rep].REFRESH_FIELD,"name"); 170 if(name!=null && name.length()==0) 171 { 172 result.failed(smh.getLocalString(getClass().getName()+".failed3a", 173 "FAILED [AS-WEB cache-mapping] for {0}, refresh-field name [{1}] cannot be empty/null string", 174 new Object []{cacheMapName,name})); 175 oneFailed = true; 176 } 177 else 178 { 179 result.passed(smh.getLocalString(getClass().getName()+".passed3a", 180 "PASSED [AS-WEB cache-mapping] for {0}, refresh-field name [{1}] has been furnished", 181 new Object []{cacheMapName,name})); 182 } 183 } 184 186 if(checkHTTPMethodList(httpMethods,result,compName,descriptor)){ 187 188 } 189 else{ 190 oneFailed = true; 191 result.failed(smh.getLocalString 192 (getClass().getName() + ".failed4", 193 "FAILED [AS-WEB cache-mapping] http-method - List of HTTP methods is not proper, "+ 194 " atleast one of the method name in the list is empty/null ")); 195 196 } 197 198 int numKeyFields = cacheMapp[rep].sizeKeyField(); 200 if(numKeyFields>0) 201 testKeyFields(cacheMapp[rep],result,oneFailed); 202 204 205 } 206 207 } 208 209 } else { 210 notApp = true; 211 result.notApplicable(smh.getLocalString 212 (getClass().getName() + ".notApplicable", 213 "NOT APPLICABLE [AS-WEB cache-mapping] element not defined", 214 new Object [] {descriptor.getName()})); 215 } 216 if (oneFailed) { 217 result.setStatus(Result.FAILED); 218 }else if(oneWarning) { 219 result.setStatus(Result.WARNING); 220 } else if(notApp) { 221 result.setStatus(Result.NOT_APPLICABLE); 222 }else { 223 result.setStatus(Result.PASSED); 224 } 225 return result; 226 } 227 228 229 boolean validURL(String URL){ 230 boolean valid=false; 231 if (URL != null) { 232 if ((URL.startsWith("/")) || 233 ((URL.startsWith("/")) && (URL.endsWith("/*"))) || 234 (URL.startsWith("*."))) { 235 valid = true; 236 } 237 } 238 return valid; 239 } 240 241 boolean validServletName(String servletName, WebBundleDescriptor descriptor){ 242 boolean valid=false; 243 if (servletName != null && servletName.length() != 0) { 244 Set servlets = descriptor.getServletDescriptors(); 245 Iterator itr = servlets.iterator(); 246 while (itr.hasNext()) { 248 ServletDescriptor servlet = (ServletDescriptor) itr.next(); 249 String thisServletName = servlet.getCanonicalName(); 250 if (servletName.equals(thisServletName)) { 251 valid = true; 252 break; 253 } 254 } 255 256 } 257 return valid; 258 } 259 260 boolean validTimeout(String timeout,String timeoutName){ 261 boolean valid=false; 262 if (timeout != null) { 263 try{ 264 long timeoutValue = Long.parseLong(timeout); 265 if(timeoutValue >= -1 && timeoutValue <= Long.MAX_VALUE){ 266 if(timeoutName !=null && timeoutName.length() != 0) 269 valid = true; 270 } 271 } catch(NumberFormatException exception){ 272 } 274 275 } else { valid = true; 277 } 278 return valid; 279 } 280 281 boolean checkHTTPMethodList(String [] httpMethods, Result result, WebComponentNameConstructor compName,WebBundleDescriptor descriptor ){ 282 283 boolean valid=true; 284 if (httpMethods != null) { 285 for(int rep=0;rep < httpMethods.length;rep++){ 286 if(httpMethods[rep]!=null && !(httpMethods[rep].trim().equals(""))) 287 { 288 if((httpMethods[rep].equalsIgnoreCase("GET") 289 || httpMethods[rep].equalsIgnoreCase("POST") || httpMethods[rep].equalsIgnoreCase("HEAD"))) 290 { 291 292 result.passed(smh.getLocalString 293 (getClass().getName() + ".passed4", 294 "PASSED [AS-WEB cache-mapping ] http-method [ {0} ] properly defined in the WEB DD.", 295 new Object [] {httpMethods[rep]})); 296 }else{ 297 298 oneWarning = true; 299 result.warning(smh.getLocalString 300 (getClass().getName() + ".warning", 301 "WARNING [AS-WEB cache-mapping] http-method name [ {0} ] present, suggested to be GET | POST | HEAD.", 302 new Object [] {httpMethods[rep]})); 303 304 305 306 } 307 308 } 309 else{ 310 311 result.failed(smh.getLocalString 312 (getClass().getName() + ".failed5", 313 "FAILED [AS-WEB cache-mapping] http-method name [ {0} ] is invalid, it should be GET | POST | HEAD.", 314 new Object [] {httpMethods[rep]})); 315 316 valid=false; 317 } 318 319 } 320 321 } 322 323 return valid; 324 } 325 326 boolean validCacheHelperRef(String helperRef, Cache cache){ 327 boolean valid=false; 328 if (helperRef.length() != 0) { 329 330 CacheHelper[] helperClasses=null; 331 CacheHelper helperClass=null; 332 String name=null; 333 334 if (cache != null ) 335 helperClasses=cache.getCacheHelper(); 336 337 if (cache != null && helperClasses !=null) { 338 339 for(int rep=0;rep < helperClasses.length;rep++){ 340 helperClass=helperClasses[rep]; 341 if(helperClass==null) 342 continue; 343 name = helperClass.getAttributeValue("name"); 344 if(helperRef.equals(name)){ 346 valid=true; 348 break; 349 } 350 } 351 } 352 353 } 354 return valid; 355 } 356 357 public void testKeyFields(CacheMapping cacheMap, Result result, boolean oneFailed) 359 { 360 String cacheMapName = null; 361 if(cacheMap.getServletName()!=null && cacheMap.getServletName().length()!=0) 362 cacheMapName = cacheMap.getServletName(); 363 else 364 cacheMapName = cacheMap.getUrlPattern(); 365 366 for(int i=0;i<cacheMap.sizeKeyField();i++) 367 { 368 if(cacheMap.isKeyField(i)) 369 { 370 String name = cacheMap.getAttributeValue(cacheMap.KEY_FIELD,i,"name"); 371 if(name!=null && name.length()==0) 373 { 374 result.failed(smh.getLocalString(getClass().getName()+".failed6", 375 "FAILED [AS-WEB cache-mapping] for {0}, key-field #{1}, name cannot be an empty string", 376 new Object []{cacheMapName,new Integer (i)})); 377 oneFailed = true; 378 } 379 else 380 { 381 result.passed(smh.getLocalString(getClass().getName()+".passed5", 382 "PASSED [AS-WEB cache-mapping] for {0}, key-field #{1} name value furnished", 383 new Object []{cacheMapName,new Integer (i)})); 384 } 385 } 386 } 387 } 388 } 390 391 | Popular Tags |