KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > servletunit > struts > StrutsServletContextWrapper


1 // StrutsTestCase - a JUnit extension for testing Struts actions
2
// within the context of the ActionServlet.
3
// Copyright (C) 2002 Deryl Seale
4
//
5
// This library is free software; you can redistribute it and/or
6
// modify it under the terms of the Apache Software License as
7
// published by the Apache Software Foundation; either version 1.1
8
// of the License, or (at your option) any later version.
9
//
10
// This library is distributed in the hope that it will be useful,
11
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
// Apache Software Foundation Licens for more details.
14
//
15
// You may view the full text here: http://www.apache.org/LICENSE.txt
16

17 package servletunit.struts;
18
19 import org.apache.cactus.server.ServletContextWrapper;
20 import servletunit.RequestDispatcherSimulator;
21
22 import javax.servlet.RequestDispatcher JavaDoc;
23 import javax.servlet.ServletContext JavaDoc;
24 import java.net.URL JavaDoc;
25 import java.net.MalformedURLException JavaDoc;
26 import java.io.File JavaDoc;
27 import java.io.InputStream JavaDoc;
28 import java.io.FileInputStream JavaDoc;
29 import java.io.FileNotFoundException JavaDoc;
30
31 /**
32  * A wrapper for the ServletContext class. This is used in
33  * CactusStrutsTestCase so that we can retrieve the forward
34  * processed by the ActionServlet and use absolute paths
35  * for Struts resources. This allows us to to use
36  * the ActionServlet as a black box, rather than mimic its
37  * behavior as was previously the case.
38  */

39 public class StrutsServletContextWrapper extends ServletContextWrapper {
40
41     boolean processRequest = false;
42     private String JavaDoc dispatchedResource;
43
44     public StrutsServletContextWrapper(ServletContext JavaDoc context) {
45         super(context);
46     }
47
48     public void setProcessRequest(boolean flag) {
49         this.processRequest = flag;
50     }
51
52     public RequestDispatcher JavaDoc getRequestDispatcher(String JavaDoc path) {
53         dispatchedResource = path;
54         if (!processRequest)
55             return new RequestDispatcherSimulator(path);
56         else
57             return super.getRequestDispatcher(path);
58     }
59
60     public String JavaDoc getForward() {
61         return dispatchedResource;
62     }
63
64     /**
65      * Override the getResource method to look for resources in the file system, allowing
66      * the use of absolute paths for Struts configuration files. If the resource path exists
67      * in the file system, this method will return a URL based on the supplied path; otherwise,
68      * it defers to the ServletContext loader.
69      */

70     public URL JavaDoc getResource(String JavaDoc pathname) throws MalformedURLException JavaDoc {
71         File JavaDoc file = new File JavaDoc(pathname);
72         if (file.exists())
73             return file.toURL();
74         else
75             return super.getResource(pathname);
76     }
77
78     /**
79      * Override the getResourceAsStream method to look for resources in the file system, allowing
80      * the use of absolute paths for Struts configuration files. If the resource path exists
81      * in the file system, this method will return a URL based on the supplied path; otherwise,
82      * it defers to the ServletContext loader.
83      */

84     public InputStream JavaDoc getResourceAsStream(String JavaDoc pathname) {
85         File JavaDoc file = new File JavaDoc(pathname);
86         if (file.exists())
87             try {
88                 return new FileInputStream JavaDoc(file);
89             } catch (FileNotFoundException JavaDoc e) {
90                 return super.getResourceAsStream(pathname);
91             }
92         else
93             return super.getResourceAsStream(pathname);
94     }
95
96 }
97
98
Popular Tags