KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > verifier > tests > web > elements > MimeTypeElement


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23 package com.sun.enterprise.tools.verifier.tests.web.elements;
24
25 import com.sun.enterprise.tools.verifier.tests.web.WebTest;
26 import java.util.*;
27 import java.util.logging.Level JavaDoc;
28 import com.sun.enterprise.deployment.*;
29 import com.sun.enterprise.tools.verifier.tests.web.WebCheck;
30 import com.sun.enterprise.tools.verifier.*;
31 import com.sun.enterprise.tools.verifier.tests.*;
32
33
34 /**
35  * Servlet mime-type element contains a defined mime type. i.e. "text/plain"
36  */

37 public class MimeTypeElement extends WebTest implements WebCheck, MimeTypes {
38
39     /**
40      * Servlet mime-type element contains a defined mime type. i.e. "text/plain"
41      *
42      * @param descriptor the Web deployment descriptor
43      *
44      * @return <code>Result</code> the results for this assertion
45      */

46     public Result check(WebBundleDescriptor descriptor) {
47
48     Result result = getInitializedResult();
49     ComponentNameConstructor compName = getVerifierContext().getComponentNameConstructor();
50
51     if (descriptor.getMimeMappings().hasMoreElements()) {
52         boolean oneFailed = false;
53         boolean foundIt = false;
54         // get the mimeType's in this .war
55
for (Enumeration e = descriptor.getMimeMappings() ; e.hasMoreElements() ;) {
56         foundIt = false;
57         MimeMappingDescriptor mimemapping = (MimeMappingDescriptor)e.nextElement();
58         String JavaDoc mimeType = mimemapping.getMimeType();
59         logger.log(Level.FINE, "servlet mimeType: " + mimeType);
60         int pos = mimeType.indexOf("/");
61         // user defined
62
// see http://www-dos.uniinc.msk.ru/tech1/1995/mime/m_tech.htm#Type
63

64         if (mimeType.substring(pos+1).startsWith("X-") || mimeType.substring(pos+1).startsWith("x-")) {
65             foundIt = true;
66         } else if (mimeType.startsWith("X-")) {
67                     foundIt = true;
68                 } else if (mimeType.substring(0,pos).equals("text")) {
69             if (Arrays.asList(text).contains(mimeType.substring(pos+1,mimeType.length()))) {
70             foundIt = true;
71             }
72         } else if (mimeType.substring(0,pos).equals("multipart")) {
73             if (Arrays.asList(multipart).contains(mimeType.substring(pos+1,mimeType.length()))) {
74             foundIt = true;
75             }
76         } else if (mimeType.substring(0,pos).equals("message")) {
77             if (Arrays.asList(message).contains(mimeType.substring(pos+1,mimeType.length()))) {
78             foundIt = true;
79             }
80         } else if (mimeType.substring(0,pos).equals("application")) {
81             if (Arrays.asList(application).contains(mimeType.substring(pos+1,mimeType.length()))) {
82             foundIt = true;
83             }
84         } else if (mimeType.substring(0,pos).equals("image")) {
85             if (Arrays.asList(image).contains(mimeType.substring(pos+1,mimeType.length()))) {
86             foundIt = true;
87             }
88         } else if (mimeType.substring(0,pos).equals("audio")) {
89             if (Arrays.asList(audio).contains(mimeType.substring(pos+1,mimeType.length()))) {
90             foundIt = true;
91             }
92         } else if (mimeType.substring(0,pos).equals("video")) {
93             if (Arrays.asList(video).contains(mimeType.substring(pos+1,mimeType.length()))) {
94             foundIt = true;
95             }
96         } else if (mimeType.substring(0,pos).equals("model")) {
97             if (Arrays.asList(model).contains(mimeType.substring(pos+1,mimeType.length()))) {
98             foundIt = true;
99             }
100         }
101    
102         if (foundIt) {
103             result.addGoodDetails(smh.getLocalString
104                        ("tests.componentNameConstructor",
105                     "For [ {0} ]",
106                     new Object JavaDoc[] {compName.toString()}));
107             result.addGoodDetails(smh.getLocalString
108                       (getClass().getName() + ".passed",
109                        "Servlet mime-type [ {0} ] defined for this web application [ {1} ]",
110                        new Object JavaDoc[] {mimeType, descriptor.getName()}));
111         } else {
112             if (!oneFailed) {
113             oneFailed = true;
114             }
115             result.addErrorDetails(smh.getLocalString
116                        ("tests.componentNameConstructor",
117                     "For [ {0} ]",
118                     new Object JavaDoc[] {compName.toString()}));
119             result.addErrorDetails(smh.getLocalString
120                        (getClass().getName() + ".failed",
121                         "Error: Servlet mime-type [ {0} ] not defined for this web application [ {1} ]",
122                         new Object JavaDoc[] {mimeType, descriptor.getName()}));
123         }
124         }
125         if (oneFailed) {
126         result.setStatus(Result.FAILED);
127         } else {
128         result.setStatus(Result.PASSED);
129         }
130     } else {
131         result.addNaDetails(smh.getLocalString
132                        ("tests.componentNameConstructor",
133                     "For [ {0} ]",
134                     new Object JavaDoc[] {compName.toString()}));
135         result.notApplicable(smh.getLocalString
136                  (getClass().getName() + ".notApplicable",
137                   "There are no mimemappings within the web archive [ {0} ]",
138                   new Object JavaDoc[] {descriptor.getName()}));
139     }
140     return result;
141     }
142 }
143
Popular Tags