1 23 package com.sun.enterprise.tools.verifier.tests.web; 24 25 import com.sun.enterprise.tools.verifier.tests.web.WebTest; 26 import java.util.jar.*; 27 import java.util.*; 28 import java.io.*; 29 import java.util.regex.Pattern ; 30 import com.sun.enterprise.deployment.*; 31 import com.sun.enterprise.tools.verifier.*; 32 import com.sun.enterprise.tools.verifier.tests.*; 33 34 38 public class WelcomeFile extends WebTest implements WebCheck { 39 40 48 public Result check(WebBundleDescriptor descriptor) { 49 50 Result result = getInitializedResult(); 51 ComponentNameConstructor compName = getVerifierContext().getComponentNameConstructor(); 52 53 if(!isApplicable(descriptor, result)) { 54 return result; 55 } 56 57 boolean syntaxOK = checkSyntax(descriptor, result); 59 60 boolean exists = true; 63 64 if (!syntaxOK) { 66 result.setStatus(Result.FAILED); 67 } else if (!exists) { 68 result.setStatus(Result.WARNING); 69 } else { 70 result.setStatus(Result.PASSED); 71 } 72 73 return result; 74 } 75 76 private boolean isApplicable(WebBundleDescriptor descriptor, Result result) { 77 boolean applicable = true; 78 if (!descriptor.getWelcomeFiles().hasMoreElements()) { 79 ComponentNameConstructor compName = getVerifierContext().getComponentNameConstructor(); 80 addNaDetails(result, compName); 81 result.notApplicable(smh.getLocalString 82 (getClass().getName() + ".notApplicable", 83 "There are no welcome files within the web archive [ {0} ]", 84 new Object [] {descriptor.getName()})); 85 applicable = false; 86 } 87 return applicable; 88 } 89 90 private boolean checkSyntax(WebBundleDescriptor descriptor, Result result) { 91 boolean syntaxOK = true; 92 ComponentNameConstructor compName = getVerifierContext().getComponentNameConstructor(); 93 for (Enumeration e = descriptor.getWelcomeFiles() ; e.hasMoreElements() ;) { 94 String welcomefile = (String ) e.nextElement(); 95 if (welcomefile.startsWith("/") || welcomefile.endsWith("/")) { 96 addErrorDetails(result, compName); 97 result.addErrorDetails(smh.getLocalString( 98 getClass().getName() + ".failed1", 99 "Error : Servlet 2.3 Spec 9.9 Welcome file URL [ {0} ] must be partial URLs with no trailing or leading /", 100 new Object [] {welcomefile, descriptor.getName()})); 101 syntaxOK = false; 102 } 103 } 104 return syntaxOK; 105 } 106 107 private boolean checkExists(WebBundleDescriptor descriptor, Result result) { 108 findDynamicResourceURIs(descriptor); 109 boolean exists = false; 110 ComponentNameConstructor compName = getVerifierContext().getComponentNameConstructor(); 111 for (Enumeration e = descriptor.getWelcomeFiles() ; e.hasMoreElements() ;) { 112 String welcomeFile = (String ) e.nextElement(); 113 if(fileExists(descriptor, welcomeFile) || urlMatches(welcomeFile)) { 114 exists = true; 115 addGoodDetails(result, compName); 116 result.addGoodDetails(smh.getLocalString 117 (getClass().getName() + ".passed", 118 "Welcome file [ {0} ] contains the file name to use as a default welcome file within web application [ {1} ]", 119 new Object [] {welcomeFile, descriptor.getName()})); 120 } else { 121 addWarningDetails(result, compName); 122 result.addWarningDetails(smh.getLocalString 123 (getClass().getName() + ".failed", 124 "Error: Welcome file [ {0} ] is not found within [ {1} ] or does not contain the file name to use as a default welcome file within web application [ {2} ]", 125 new Object [] {welcomeFile, descriptor.getModuleDescriptor().getArchiveUri(), descriptor.getName()})); 126 } 127 } 128 return exists; 129 } 130 131 private boolean fileExists(WebBundleDescriptor descriptor, String fileName) { 132 File webCompRoot = new File(getAbstractArchiveUri(descriptor)); 133 File welcomeFile = new File(webCompRoot, fileName); 134 return welcomeFile.exists(); 135 } 136 137 private Set dynamicResourceUrlPatterns = new HashSet(); 138 139 private void findDynamicResourceURIs(WebBundleDescriptor descriptor) { 140 Set webComponentDescriptors = descriptor.getWebComponentDescriptorsSet(); 141 for(Iterator iter = webComponentDescriptors.iterator(); iter.hasNext(); ) { 142 WebComponentDescriptor webComponentDescriptor = (WebComponentDescriptor) iter.next(); 143 dynamicResourceUrlPatterns.addAll(webComponentDescriptor.getUrlPatternsSet()); 144 } 145 Set newUrlPatterns = new HashSet(); 147 for(Iterator iter = dynamicResourceUrlPatterns.iterator(); iter.hasNext() ;) { 148 String urlPattern = (String ) iter.next(); 149 if (urlPattern.startsWith("/")) { 150 urlPattern = urlPattern.substring(1); 151 } 152 if (urlPattern.endsWith("/")) { 153 urlPattern = urlPattern.substring(0, urlPattern.length() - 1); 154 } 155 newUrlPatterns.add(urlPattern); 156 } 157 dynamicResourceUrlPatterns = newUrlPatterns; 158 } 159 160 private boolean urlMatches(String url) { 161 for(Iterator iter = dynamicResourceUrlPatterns.iterator(); iter.hasNext() ;) { 162 boolean matches = Pattern.matches((String )iter.next(), url); 163 if (matches) { 164 return true; 165 } 166 } 167 return false; 168 } 169 } 170 | Popular Tags |