Changeset 8a3a1466


Ignore:
Timestamp:
Apr 27, 2010 12:52:17 PM (10 years ago)
Author:
zzz <zzz@…>
Branches:
master
Children:
d4713e1
Parents:
a5af9dc
Message:
  • i2psnark: Serve downloaded files from the servlet rather than with a file: link
File:
1 edited

Legend:

Unmodified
Added
Removed
  • apps/i2psnark/java/src/org/klomp/snark/web/I2PSnarkServlet.java

    ra5af9dc r8a3a1466  
    88import java.text.Collator;
    99import java.util.ArrayList;
     10import java.util.Enumeration;
    1011import java.util.Iterator;
    1112import java.util.List;
     
    1617
    1718import javax.servlet.ServletConfig;
     19import javax.servlet.ServletContext;
    1820import javax.servlet.ServletException;
    1921import javax.servlet.http.HttpServlet;
     
    3537import org.klomp.snark.TrackerClient;
    3638
     39import org.mortbay.jetty.servlet.Default;
     40
    3741/**
    38  *
     42 *  We extend Default instead of HTTPServlet so we can handle
     43 *  i2psnark/ file requests with http:// instead of the flaky and
     44 *  often-blocked-by-the-browser file://
    3945 */
    40 public class I2PSnarkServlet extends HttpServlet {
     46public class I2PSnarkServlet extends Default {
    4147    private I2PAppContext _context;
    4248    private Log _log;
     
    4854    @Override
    4955    public void init(ServletConfig cfg) throws ServletException {
    50         super.init(cfg);
    5156        _context = I2PAppContext.getGlobalContext();
    5257        _log = _context.logManager().getLog(I2PSnarkServlet.class);
     
    5863        _manager.loadConfig(configFile);
    5964        _manager.start();
    60     }
    61    
     65        super.init(new DefaultServletConfig(cfg, _manager.getDataDir().getAbsolutePath()));
     66    }
     67   
     68    /**
     69     *  A ServletConfig we will pass to super
     70     */
     71    private static class DefaultServletConfig implements ServletConfig {
     72        private final ServletConfig _sc;
     73        private final String _path;
     74
     75        DefaultServletConfig(ServletConfig sc, String path) {
     76            _sc = sc;
     77            _path = path;
     78        }
     79
     80        public String getInitParameter(String name) {
     81            if ("acceptRanges".equals(name) || "dirAllowed".equals(name))
     82                return "true";
     83            if ("resourceBase".equals(name))
     84                return _path;
     85            return _sc.getInitParameter(name);
     86        }
     87
     88        public Enumeration getInitParameterNames() {
     89            return _sc.getInitParameterNames();
     90        }
     91
     92        public ServletContext getServletContext() {
     93            return _sc.getServletContext();
     94        }
     95
     96        public String getServletName() {
     97            return _sc.getServletName();
     98        }
     99    }
     100
    62101    @Override
    63102    public void destroy() {
     
    68107    @Override
    69108    public void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
     109        // this is the part after /i2psnark
     110        String path = req.getServletPath();
     111        // index.jsp doesn't work, it is grabbed by the war handler before here
     112        if (!(path == null || path.equals("/") || path.equals("/index.jsp") || path.equals("/index.html"))) {
     113            super.service(req, resp);
     114            return;
     115        }
     116
    70117        req.setCharacterEncoding("UTF-8");
    71118        resp.setCharacterEncoding("UTF-8");
     
    557604       
    558605        if (remaining == 0) {
    559             out.write("<a href=\"" + _manager.linkPrefix() + snark.meta.getName()
    560                       + "\" title=\"");
     606            out.write("<a href=\"" + snark.meta.getName());
     607            if (snark.meta.getFiles() != null)
     608                out.write("/");
     609            out.write("\" title=\"");
    561610            if (snark.meta.getFiles() != null)
    562611                out.write(_("View files"));
Note: See TracChangeset for help on using the changeset viewer.