1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.myfaces.view.facelets.mock;
20
21 import java.io.File;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.net.MalformedURLException;
25 import java.net.URI;
26 import java.net.URL;
27 import java.util.Collections;
28 import java.util.HashSet;
29 import java.util.Set;
30 import java.util.logging.Logger;
31
32 import javax.servlet.RequestDispatcher;
33
34
35
36
37
38
39 public class MockServletContext extends
40 org.apache.myfaces.test.mock.MockServletContext
41 {
42
43
44 private static Logger log = Logger.getLogger(MockServletContext.class.getName());
45
46 protected final URI base;
47
48 public MockServletContext(URI base)
49 {
50 this.base = base;
51 File f = new File(base);
52 if (!f.exists())
53 {
54 throw new IllegalArgumentException("File: " + base.getPath()
55 + " doesn't exist");
56 }
57 }
58
59 public Set getResourcePaths(String path)
60 {
61 URI uri = this.resolve(path);
62 if (uri != null)
63 {
64 File f = new File(uri);
65 if (f.exists() && f.isDirectory())
66 {
67 File[] c = f.listFiles();
68 Set s = new HashSet();
69 int start = f.getAbsolutePath().length();
70 for (int i = 0; i < c.length; i++)
71 {
72 s.add(c[i].getAbsolutePath().substring(start));
73 }
74 return s;
75 }
76 }
77 return Collections.EMPTY_SET;
78 }
79
80 public URL getResource(String path) throws MalformedURLException
81 {
82 URI uri = this.resolve(path);
83 if (uri != null)
84 {
85 File f = new File(uri);
86 if (f.exists())
87 {
88 return uri.toURL();
89 }
90 }
91 return null;
92 }
93
94 public InputStream getResourceAsStream(String path)
95 {
96 URI uri = this.resolve(path);
97 if (uri != null)
98 {
99 try
100 {
101 File f = new File(uri);
102 if (f.exists())
103 {
104 return uri.toURL().openStream();
105 }
106 }
107 catch (MalformedURLException e)
108 {
109 this.log.severe(e.getMessage());
110 return null;
111 }
112 catch (IOException e)
113 {
114 this.log.severe(e.getMessage());
115 return null;
116 }
117 }
118 return null;
119 }
120
121 public RequestDispatcher getRequestDispatcher(String path)
122 {
123 URI uri = this.resolve(path);
124 if (uri != null)
125 {
126 File f = new File(uri);
127 if (f.exists())
128 {
129 try
130 {
131 return new MockRequestDispatcher(uri.toURL());
132 }
133 catch (MalformedURLException e)
134 {
135 this.log.severe(e.getMessage());
136 return null;
137 }
138 }
139
140 }
141 return null;
142 }
143
144 public String getRealPath(String path)
145 {
146 URI uri = this.resolve(path);
147 if (uri != null)
148 {
149 File f = new File(uri);
150 if (f.exists())
151 {
152 return f.getAbsolutePath();
153 }
154 }
155 return null;
156 }
157
158 private final URI resolve(String path)
159 {
160 if (path == null)
161 {
162 throw new NullPointerException("Path cannot be null");
163 }
164 if (path.charAt(0) == '/')
165 {
166 if (path.length() > 1)
167 {
168 return this.base.resolve(path.substring(1));
169 }
170 return this.base;
171 }
172 return null;
173 }
174
175 }