001: /*
002: * <copyright>
003: *
004: * Copyright 2000-2007 BBNT Solutions, LLC
005: * under sponsorship of the Defense Advanced Research Projects
006: * Agency (DARPA).
007: *
008: * You can redistribute this software and/or modify it under the
009: * terms of the Cougaar Open Source License as published on the
010: * Cougaar Open Source Website (www.cougaar.org).
011: *
012: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
013: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
014: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
015: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
016: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
017: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
018: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
019: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
020: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
021: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
022: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
023: *
024: * </copyright>
025: */
026:
027: package org.cougaar.lib.web.service;
028:
029: import java.io.ByteArrayInputStream;
030: import java.io.File;
031: import java.io.FileInputStream;
032: import java.io.InputStream;
033: import java.io.IOException;
034: import java.io.OutputStream;
035: import java.io.PrintWriter;
036: import java.net.URLConnection;
037: import java.util.Arrays;
038: import javax.servlet.ServletOutputStream;
039: import javax.servlet.http.HttpServletRequest;
040: import javax.servlet.http.HttpServletResponse;
041: import org.cougaar.bootstrap.SystemProperties;
042: import org.cougaar.core.servlet.ComponentServlet;
043:
044: /**
045: * A servlet that provides directory and file read access.
046: * <p>
047: * For example, to read file "$CIP/foo/bar.txt" on agent "A":<pre>
048: * http://localhost:8800/$A/file/foo/bar.txt
049: * </pre>
050: * <p>
051: * We ignore most security issues. Security can be added by not
052: * loading this servlet or restricting the Java Security Policy.
053: */
054: public class FileServlet extends ComponentServlet {
055:
056: private static final String BASE_PATH = SystemProperties
057: .getProperty("org.cougaar.install.path");
058:
059: public void doGet(HttpServletRequest request,
060: HttpServletResponse response) throws IOException {
061: // get filename
062: String filename = request.getPathInfo();
063: if (filename != null) {
064: filename = filename.trim();
065: if (filename.length() == 0) {
066: filename = null;
067: }
068: }
069: if (filename == null) {
070: filename = "/";
071: }
072:
073: // trim leading and trailing "/"s
074: //
075: // Our servlet engine catches any bad paths, e.g. "//" and ".."
076: boolean listFiles = filename.endsWith("/");
077: if (listFiles) {
078: filename = filename.substring(0, filename.length() - 1);
079: }
080: if (filename.startsWith("/")) {
081: filename = filename.substring(1);
082: }
083:
084: if (listFiles) {
085: // write directory listing
086: File[] fa;
087: try {
088: File dir = locateFile(filename);
089: fa = dir.listFiles();
090: } catch (Exception e) {
091: response.setStatus(HttpServletResponse.SC_NOT_FOUND);
092: return;
093: }
094: PrintWriter out = response.getWriter();
095: String title = "Index of " + filename;
096: out.println("<html>\n<head>\n<title>" + title
097: + "</title>\n</head>\n<body>\n");
098: out.println("<h1>" + title + "</h1>");
099: out.print("<ul>");
100: if (filename.length() > 0) {
101: int sep = filename.lastIndexOf('/');
102: String s = (sep < 0 ? "" : "/"
103: + filename.substring(0, sep));
104: out.println("<li><a href=\"" + request.getServletPath()
105: + s + "/\">Parent Directory</a></li>");
106: }
107:
108: Arrays.sort(fa);
109: for (int i = 0; i < fa.length; i++) {
110: File fi = fa[i];
111: String pi = fi.getPath();
112: int sep = pi.lastIndexOf(File.separator);
113: pi = pi.substring(sep + 1);
114: if (fi.isDirectory()) {
115: pi = pi + "/";
116: }
117: String fileServletPath = request.getServletPath()
118: + (filename.length() <= 0 ? "" : "/" + filename)
119: + "/" + pi;
120:
121: out.println("<li><a href=\"" + fileServletPath + "\">"
122: + pi + "</a></li>");
123: }
124: out.println("</ul>\n</body></html>");
125: out.flush();
126: return;
127: }
128:
129: // open stream
130: InputStream fin;
131: try {
132: fin = open(filename);
133: } catch (IOException ioe) {
134: response.setStatus(HttpServletResponse.SC_NOT_FOUND);
135: return;
136: }
137:
138: // set content type
139: String contentType = URLConnection
140: .guessContentTypeFromStream(fin);
141: if (contentType != null) {
142: response.setContentType(contentType);
143: }
144:
145: // maybe add "Expires" header? See FavIconServlet for an example.
146:
147: // write data
148: OutputStream out = response.getOutputStream();
149: byte[] buf = new byte[2048];
150: while (true) {
151: int len = fin.read(buf);
152: if (len < 0) {
153: break;
154: }
155: out.write(buf, 0, len);
156: }
157: fin.close();
158: out.flush();
159: out.close();
160: }
161:
162: // maybe add "doPut" for file upload/overwrite?
163:
164: private File locateFile(String filename) {
165: return new File(BASE_PATH, filename);
166: // return ConfigFinder.locateFile(filename);
167: }
168:
169: private InputStream open(String filename) throws IOException {
170: return new FileInputStream(new File(BASE_PATH, filename));
171: //return ConfigFinder.open(filename);
172: }
173: }
|