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.ByteArrayOutputStream;
030: import java.io.IOException;
031: import java.io.InputStream;
032: import java.io.OutputStream;
033: import java.text.DateFormat;
034: import java.text.SimpleDateFormat;
035: import java.util.Date;
036: import java.util.List;
037:
038: import javax.servlet.Servlet;
039: import javax.servlet.ServletConfig;
040: import javax.servlet.ServletException;
041: import javax.servlet.ServletRequest;
042: import javax.servlet.ServletResponse;
043: import javax.servlet.http.HttpServletRequest;
044: import javax.servlet.http.HttpServletResponse;
045:
046: import org.cougaar.bootstrap.SystemProperties;
047: import org.cougaar.core.servlet.ComponentServlet;
048: import org.cougaar.util.ConfigFinder;
049:
050: /**
051: * This servlet handles "/favicon.ico" requests with our local "favicon.ico"
052: * image file.
053: * <p>
054: * We first check our config path for the file, then the jar resources.
055: * <p>
056: * Browsers ask for this image and display it on the URL line as a tiny icon.
057: * Unless we load this servlet, Mozilla will ask for this URL after every
058: * request.
059: *
060: * @property org.cougaar.lib.web.service.favIcon_ttd=360000
061: * How long a browser should cache the "/favicon.ico" image, defaults to one
062: * hour in milliseconds
063: */
064: public class FavIconServlet extends ComponentServlet {
065:
066: // default to one hour
067: private long expireMillis = SystemProperties.getLong(
068: "org.cougaar.lib.web.service.favIcon_ttd", 60 * 60 * 1000);
069:
070: public void setParameter(Object o) {
071: super .setParameter(o);
072: if (!(o instanceof List))
073: return;
074: List l = (List) o;
075: if (l.size() < 2)
076: return;
077: String s = (String) l.get(1);
078: expireMillis = Long.parseLong(s);
079: }
080:
081: protected String getPath() {
082: String ret = super .getPath();
083: return (ret == null ? "/favicon.ico" : ret);
084: }
085:
086: public void doGet(HttpServletRequest req, HttpServletResponse res)
087: throws ServletException, IOException {
088:
089: // set "Expires" header to force browser-side caching
090: String expireDate = null;
091: if (expireMillis > 0) {
092: try {
093: long now = System.currentTimeMillis();
094: long expireTime = now + expireMillis;
095: String format = "EEE, dd MMM yyyy HH:mm:ss zzz";
096: DateFormat formatter = new SimpleDateFormat(format);
097: expireDate = formatter.format(new Date(expireTime));
098: } catch (Exception e) {
099: // ignore, shouldn't happen
100: }
101: }
102: if (expireDate != null) {
103: res.setHeader("Expires", expireDate);
104: }
105:
106: // read icon data to buffer
107: ByteArrayOutputStream baos;
108: try {
109: InputStream in;
110: try {
111: // try config finder
112: in = ConfigFinder.getInstance().open("favicon.ico");
113: } catch (Exception e) {
114: // else try jar
115: in = getClass().getResourceAsStream("favicon.ico");
116: }
117: baos = new ByteArrayOutputStream(4096);
118: byte[] buf = new byte[4096];
119: while (true) {
120: int count = in.read(buf);
121: if (count < 0)
122: break;
123: baos.write(buf, 0, count);
124: }
125: } catch (Exception e2) {
126: // not found
127: baos = null;
128: }
129:
130: // write buffered icon data to browser
131: int length = (baos == null ? 0 : baos.size());
132: if (length <= 0) {
133: res.sendError(HttpServletResponse.SC_NOT_FOUND);
134: return;
135: }
136: res.setContentType("image/x-icon");
137: res.setContentLength(length);
138: OutputStream out = res.getOutputStream();
139: baos.writeTo(out);
140: out.close();
141: }
142: }
|