KickJava   Java API By Example, From Geeks To Geeks.

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


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.getResource()</code> as well as
29  * <code>ClassLoader.getResource()</code>. The URL returned by
30  * these calls is then opened with <code>url.openStream()</code>
31  * in order to ensure that the correct data is actually read.
32  * Operation is controlled by query parameters:
33  * <ul>
34  * <li><strong>mode</strong> - Use <code>context</code> for servlet context
35  * test, or <code>class</code> for class loader test. [context]</li>
36  * <li><strong>path</strong> - Resource path to the requested resource,
37  * starting with a slash. [/WEB-INF/web.xml]</li>
38  * <li><strong>stringify</strong> - If set to any arbitrary value, the URL
39  * returned by getResource() will be converted to a String and then back
40  * to a URL before being opened. [not set]</li>
41  * </ul>
42  *
43  * @author Craig R. McClanahan
44  * @version $Revision: 1.2 $ $Date: 2004/02/27 14:58:57 $
45  */

46
47 public class Resources05 extends HttpServlet {
48
49     public void doGet(HttpServletRequest request, HttpServletResponse response)
50         throws IOException, ServletException {
51
52         // Identify our configuration parameters
53
String JavaDoc mode = request.getParameter("mode");
54         if (mode == null)
55             mode = "context";
56         String JavaDoc path = request.getParameter("path");
57         if (path == null)
58             path = "/WEB-INF/web.xml";
59         boolean stringify = (request.getParameter("stringify") != null);
60
61         // Prepare for the desired test
62
response.setContentType("text/plain");
63         PrintWriter writer = response.getWriter();
64         InputStream is = null;
65         InputStreamReader isr = null;
66         URL JavaDoc url = null;
67         StringBuffer JavaDoc results = new StringBuffer JavaDoc();
68         boolean ok = true;
69
70         // Acquire the appropriate URL
71
try {
72             if ("context".equals(mode))
73                 url = getServletContext().getResource(path);
74             else
75                 url = this.getClass().getResource(path);
76             if (url == null) {
77                 results.append(" No URL returned");
78                 ok = false;
79             }
80         } catch (MalformedURLException JavaDoc e) {
81             results.append(" getResource MalformedURLException");
82             ok = false;
83         }
84
85         // Stringify the URL if requested
86
try {
87             if (ok) {
88                 StaticLogger.write("Stringifying the URL");
89                 String JavaDoc urlString = url.toString();
90                 url = new URL JavaDoc(urlString);
91             }
92         } catch (MalformedURLException JavaDoc e) {
93             results.append(" stringify MalformedURLException");
94         }
95
96         // Open an input stream and input stream reader on this URL
97
try {
98             if (ok) {
99                 is = url.openStream();
100                 isr = new InputStreamReader(is);
101             }
102         } catch (IOException e) {
103             results.append(" Open IOException: " + e);
104             ok = false;
105         }
106
107         // Copy the contents of this stream to our output
108
try {
109             if (ok) {
110                 while (true) {
111                     int ch = isr.read();
112                     if (ch < 0)
113                         break;
114                     writer.print((char) ch);
115                 }
116             }
117         } catch (IOException e) {
118             results.append(" Copy IOException: " + e);
119             ok = false;
120         }
121
122         // Close the input stream
123
try {
124             if (ok) {
125                 isr.close();
126             }
127         } catch (IOException e) {
128             results.append(" Close IOException: " + e);
129         }
130
131         // Report any failures we have encountered
132
if (!ok) {
133             writer.print("Resources05 FAILED -");
134             writer.println(results.toString());
135         }
136
137         // Add wrapper messages as required
138
while (true) {
139             String JavaDoc message = StaticLogger.read();
140             if (message == null)
141                 break;
142             writer.println(message);
143         }
144         StaticLogger.reset();
145
146     }
147
148 }
149
Popular Tags