KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tester > Resources03


1 /*
2  * Copyright 1999, 2000, 2001 ,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.tester;
18
19
20 import java.io.*;
21 import java.net.MalformedURLException JavaDoc;
22 import java.net.URL JavaDoc;
23 import javax.servlet.*;
24 import javax.servlet.http.*;
25
26
27 /**
28  * Positive test for <code>ServletContext.getResourceAsStream()</code> as well
29  * as <code>ClassLoader.getResourceAsStream()</code>. Operation is controlled
30  * by query parameters:
31  * <ul>
32  * <li><strong>mode</strong> - Use <code>context</code> for servlet context
33  * test, or <code>class</code> for class loader test. [context]</li>
34  * <li><strong>path</strong> - Resource path to the requested resource,
35  * starting with a slash. [/WEB-INF/web.xml]</li>
36  * </ul>
37  *
38  * @author Craig R. McClanahan
39  * @version $Revision: 1.2 $ $Date: 2004/02/27 14:58:57 $
40  */

41
42 public class Resources03 extends HttpServlet {
43
44     public void doGet(HttpServletRequest request, HttpServletResponse response)
45         throws IOException, ServletException {
46
47         // Identify our configuration parameters
48
String JavaDoc mode = request.getParameter("mode");
49         if (mode == null)
50             mode = "context";
51         String JavaDoc path = request.getParameter("path");
52         if (path == null)
53             path = "/WEB-INF/web.xml";
54
55         // Execute the desired test
56
response.setContentType("text/plain");
57         PrintWriter writer = response.getWriter();
58         InputStream is = null;
59         URL JavaDoc url = null;
60         try {
61             if ("context".equals(mode)) {
62                 is = getServletContext().getResourceAsStream(path);
63                 url = getServletContext().getResource(path);
64             } else {
65                 is = this.getClass().getResourceAsStream(path);
66                 url = this.getClass().getResource(path);
67             }
68             if (url == null) {
69                 if (is == null)
70                     writer.println("Resources03 FAILED - No IS or URL was returned");
71                 else
72                     writer.println("Resources03 FAILED - Returned IS but no URL");
73             } else {
74                 if (is == null)
75                     writer.println("Resources03 FAILED - Returned URL but no IS");
76                 else {
77                     InputStreamReader isr = new InputStreamReader(is);
78                     while (true) {
79                         int c = isr.read();
80                         if (c < 0)
81                             break;
82                         char ch = (char) c;
83                         if (ch < ' ')
84                             break;
85                         writer.print(ch);
86                     }
87                     isr.close();
88                 }
89                 writer.println();
90                 writer.println("url = " + url.toString());
91             }
92         } catch (MalformedURLException JavaDoc e) {
93             writer.println("Resources03 FAILED - MalformedURLException: "
94                            + e);
95         } catch (IOException e) {
96             writer.println("Resources03 FAILED - IOException: " + e);
97         }
98
99         // Add wrapper messages as required
100
while (true) {
101             String JavaDoc message = StaticLogger.read();
102             if (message == null)
103                 break;
104             writer.println(message);
105         }
106         StaticLogger.reset();
107
108     }
109
110 }
111
Popular Tags