View Javadoc

1   /*--------------------------------------------------------------------------
2    *  Copyright 2007 utgenome.org
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  // UTGBMedaka Project
18  //
19  // WebApplicationResource.java
20  // Since: Aug 9, 2007
21  //
22  // $URL$ 
23  // $Author$
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   * Helper class to retrieve web application resources contained in webapp folders (/), /WEB-INF, etc.
50   * 
51   * @author leo
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  	 * Retrieves web application resources from the ServletContext
68  	 * @param basePath the resource path, which must begin with "/". For example, "/WEB-INF", "/" etc.  
69  	 * @param fileNamePattern the file name pattern
70  	 * @param recursive when true, it recursively searches sub folders 
71  	 * @return the list of found resource paths
72  	 */
73  	public ArrayList<String> find(String basePath, String fileNamePattern, boolean recursive)
74  	{
75  		return find(_context, basePath, fileNamePattern, recursive);
76  	}
77  	
78  	/**
79  	 * Retrieves web application resources from the ServletContext
80  	 * @param context the servlet context
81  	 * @param basePath the resource path, which must begin with "/". For example, "/WEB-INF", "/" etc.  
82  	 * @param fileNamePattern the file name pattern
83  	 * @param recursive when true, it recursively searches sub folders 
84  	 * @return the list of found resource paths
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 	 * Gets a {@link BufferedReader} of the specified web application resource
121 	 * @param context the servlet context
122 	 * @param path the resource path
123 	 * @return a {@link BufferedReader} of the specified resource
124 	 * @throws FileNotFoundException when the specified resource is not found
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 	 * @param context the servlet context
138 	 * @param path the resource path, which must begin with "/" 
139 	 * @return null if the specified resource is not found 
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 	 * Gets a {@link BufferedReader} of the specified web application resource
162 	 * @param path the resource path
163 	 * @return a {@link BufferedReader} of the specified resource
164 	 * @throws FileNotFoundException when the specified resource is not found
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