1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 package org.utgenome.gwt.utgb.server.util;
26
27 import java.io.BufferedReader;
28 import java.io.File;
29 import java.io.FileNotFoundException;
30 import java.io.IOException;
31 import java.io.InputStream;
32 import java.io.InputStreamReader;
33 import java.io.StringWriter;
34 import java.net.MalformedURLException;
35 import java.net.URISyntaxException;
36 import java.net.URL;
37 import java.util.ArrayList;
38 import java.util.Iterator;
39 import java.util.Set;
40 import java.util.regex.Pattern;
41
42 import javax.servlet.ServletContext;
43
44 import org.xerial.util.FileResource;
45 import org.xerial.util.StringUtil;
46 import org.xerial.util.log.Logger;
47
48
49
50
51
52
53
54 public class WebApplicationResource {
55
56 private static Logger _logger = Logger.getLogger(WebApplicationResource.class);
57
58 private ServletContext _context;
59
60
61 public WebApplicationResource(ServletContext context)
62 {
63 _context = context;
64 }
65
66
67
68
69
70
71
72
73 public ArrayList<String> find(String basePath, String fileNamePattern, boolean recursive)
74 {
75 return find(_context, basePath, fileNamePattern, recursive);
76 }
77
78
79
80
81
82
83
84
85
86 public static ArrayList<String> find(ServletContext context, String basePath, String fileNamePattern, boolean recursive)
87 {
88 ArrayList<String> foundPath = new ArrayList<String>();
89 Set pathSet = context.getResourcePaths(basePath);
90 if(pathSet == null)
91 return foundPath;
92
93 for(Iterator it = pathSet.iterator(); it.hasNext(); )
94 {
95 String path = it.next().toString();
96 URL resource;
97 try {
98 resource = context.getResource(path);
99 } catch (MalformedURLException e) {
100 _logger.error(e);
101 continue;
102 }
103 if(resource == null)
104 continue;
105 if(resource.getPath().endsWith("/") && recursive)
106 foundPath.addAll(find(context, path, fileNamePattern, recursive));
107 else
108 {
109 if(Pattern.matches(fileNamePattern, resource.getFile())){
110 foundPath.add(path);
111 }
112 }
113 }
114 return foundPath;
115 }
116
117
118
119
120
121
122
123
124
125
126 public static BufferedReader openResource(ServletContext context, String path) throws FileNotFoundException
127 {
128 InputStream in = context.getResourceAsStream(path);
129 if(in == null)
130 throw new FileNotFoundException(path);
131
132 return new BufferedReader(new InputStreamReader(in));
133
134 }
135
136
137
138
139
140
141 public static File getResoruceFile(ServletContext context, String path)
142 {
143 try
144 {
145 URL resourceURL = context.getResource(path);
146 return new File(resourceURL.toURI());
147 }
148 catch (MalformedURLException e)
149 {
150 _logger.error(e);
151 }
152 catch (URISyntaxException e)
153 {
154 _logger.error(e);
155 }
156 return null;
157 }
158
159
160
161
162
163
164
165
166 public BufferedReader openResource(String path) throws FileNotFoundException
167 {
168 return openResource(_context, path);
169 }
170
171 public static String getContent(ServletContext context, String path) throws IOException
172 {
173 BufferedReader reader = openResource(context, path);
174 String line;
175 StringWriter writer = new StringWriter();
176 while((line = reader.readLine()) != null)
177 {
178 writer.append(line);
179 writer.append(StringUtil.newline());
180 }
181 return writer.toString();
182 }
183 }
184
185
186
187