Changeset 4ba8f02


Ignore:
Timestamp:
Jan 23, 2013 8:26:50 PM (8 years ago)
Author:
zzz <zzz@…>
Branches:
master
Children:
18e369b
Parents:
f57f49c3
Message:
  • Eliminate Jetty dependencies in i2psnark for good. Required due to webapp classloader changes in Jetty 7, we can no longer access or extend Jetty classes.
    • Extend javax HttpServlet? instead of Jetty's DefaultServlet?
    • Implement BasicServlet? to replace functions of DefaultServlet?
    • Add MimeTypes? implementation to add to servlet's defaults
    • Add local mime.properties file, remove checks in I2PSnarkServlet for those we were missing
    • Eliminate all use of Jetty utility classes including MimeType?, Resource, Buffer, Cache, URIUtil, …

TODO

  • Use servlet path everywhere, so the war can be renamed
  • Don't override service()
Location:
apps/i2psnark
Files:
4 added
2 edited

Legend:

Unmodified
Added
Removed
  • apps/i2psnark/java/build.xml

    rf57f49c3 r4ba8f02  
    105105            <fileset dir="../icons/" />
    106106        </copy>
     107        <!-- mime.properties must be in with the classes -->
     108        <copy file="../mime.properties" todir="build/obj/org/klomp/snark/web" />
    107109        <war destfile="../i2psnark.war" webxml="../web.xml" >
    108110          <!-- include only the web stuff, as of 0.7.12 the router will add i2psnark.jar to the classpath for the war -->
    109           <classes dir="./build/obj" includes="**/web/*.class" />
     111          <classes dir="./build/obj" includes="**/web/*" />
    110112            <fileset dir="build/icons/" />
    111113            <manifest>
  • apps/i2psnark/java/src/org/klomp/snark/web/I2PSnarkServlet.java

    rf57f49c3 r4ba8f02  
    2525import javax.servlet.ServletConfig;
    2626import javax.servlet.ServletException;
     27import javax.servlet.http.HttpServlet;
    2728import javax.servlet.http.HttpServletRequest;
    2829import javax.servlet.http.HttpServletResponse;
     
    4546import org.klomp.snark.dht.DHT;
    4647
    47 import org.eclipse.jetty.servlet.DefaultServlet;
    48 import org.eclipse.jetty.util.URIUtil;
    49 import org.eclipse.jetty.util.resource.Resource;
    50 
    5148/**
    52  *  We extend Default instead of HTTPServlet so we can handle
    53  *  i2psnark/ file requests with http:// instead of the flaky and
    54  *  often-blocked-by-the-browser file://
     49 *  Refactored to eliminate Jetty dependencies
    5550 */
    56 public class I2PSnarkServlet extends DefaultServlet {
    57     private I2PAppContext _context;
    58     private Log _log;
     51public class I2PSnarkServlet extends BasicServlet {
     52    /** generally "/i2psnark" */
     53    private String _contextPath;
    5954    private SnarkManager _manager;
    6055    private static long _nonce;
    61     private Resource _resourceBase;
    6256    private String _themePath;
    6357    private String _imgPath;
     
    6660    public static final String PROP_CONFIG_FILE = "i2psnark.configFile";
    6761 
     62    public I2PSnarkServlet() {
     63        super();
     64    }
     65
    6866    @Override
    6967    public void init(ServletConfig cfg) throws ServletException {
    70         _context = I2PAppContext.getGlobalContext();
    71         _log = _context.logManager().getLog(I2PSnarkServlet.class);
     68        super.init(cfg);
     69        _contextPath = getServletContext().getContextPath();
    7270        _nonce = _context.random().nextLong();
    7371        _manager = new SnarkManager(_context);
     
    7775        _manager.loadConfig(configFile);
    7876        _manager.start();
    79         try {
    80             _resourceBase = Resource.newResource(_manager.getDataDir().getAbsolutePath());
    81         } catch (IOException ioe) {}
    82         super.init(cfg);
     77        loadMimeMap("org/klomp/snark/web/mime");
     78        setResourceBase(_manager.getDataDir());
     79        setWarBase("/.icons/");
    8380    }
    8481   
     
    9693     */
    9794    @Override
    98     public Resource getResource(String pathInContext)
     95    public File getResource(String pathInContext)
    9996    {
    10097        if (pathInContext == null || pathInContext.equals("/") || pathInContext.equals("/index.jsp") ||
     
    10299            return super.getResource(pathInContext);
    103100        // files in the i2psnark/ directory
    104         try {
    105             return _resourceBase.addPath(pathInContext);
    106         } catch (IOException ioe) {
    107             throw new RuntimeException(ioe);
    108         }
     101        return new File(_resourceBase, pathInContext);
    109102    }
    110103
     
    140133    @Override
    141134    public void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
     135        //_log.error("Service " + req.getMethod() + " \"" + req.getContextPath() + "\" \"" + req.getServletPath() + "\" \"" + req.getPathInfo() + '"');
    142136        // since we are not overriding handle*(), do this here
    143137        String method = req.getMethod();
     
    179173                // bypass the horrid Resource.getListHTML()
    180174                String pathInfo = req.getPathInfo();
    181                 String pathInContext = URIUtil.addPaths(path, pathInfo);
     175                String pathInContext = addPaths(path, pathInfo);
    182176                req.setCharacterEncoding("UTF-8");
    183177                resp.setCharacterEncoding("UTF-8");
    184178                resp.setContentType("text/html; charset=UTF-8");
    185                 Resource resource = getResource(pathInContext);
    186                 if (resource == null || (!resource.exists())) {
     179                File resource = getResource(pathInContext);
     180                if (resource == null) {
    187181                    resp.sendError(404);
    188182                } else {
    189                     String base = URIUtil.addPaths(req.getRequestURI(), "/");
     183                    String base = addPaths(req.getRequestURI(), "/");
    190184                    String listing = getListHTML(resource, base, true, method.equals("POST") ? req.getParameterMap() : null);
    191185                    if (method.equals("POST")) {
     
    20322026     * @since 0.7.14
    20332027     */
    2034     private String getListHTML(Resource r, String base, boolean parent, Map postParams)
     2028    private String getListHTML(File r, String base, boolean parent, Map postParams)
    20352029        throws IOException
    20362030    {
     
    20412035        }  // if r is not a directory, we are only showing torrent info section
    20422036       
    2043         String title = URIUtil.decodePath(base);
     2037        String title = decodePath(base);
    20442038        if (title.startsWith("/i2psnark/"))
    20452039            title = title.substring("/i2psnark/".length());
     
    22292223        buf.append("</tr>\n</thead>\n");
    22302224        buf.append("<tr><td colspan=\"" + (showPriority ? '5' : '4') + "\" class=\"ParentDir\"><A HREF=\"");
    2231         buf.append(URIUtil.addPaths(base,"../"));
     2225        buf.append(addPaths(base,"../"));
    22322226        buf.append("\"><img alt=\"\" border=\"0\" src=\"" + _imgPath + "up.png\"> ")
    22332227           .append(_("Up to higher level directory"))
     
    22402234        for (int i=0 ; i< ls.length ; i++)
    22412235        {   
    2242             String encoded=URIUtil.encodePath(ls[i]);
     2236            String encoded = encodePath(ls[i]);
    22432237            // bugfix for I2P - Backport from Jetty 6 (zero file lengths and last-modified times)
    22442238            // http://jira.codehaus.org/browse/JETTY-361?page=com.atlassian.jira.plugin.system.issuetabpanels%3Achangehistory-tabpanel#issue-tabs
    22452239            // See resource.diff attachment
    22462240            //Resource item = addPath(encoded);
    2247             Resource item = r.addPath(ls[i]);
     2241            File item = new File(r, ls[i]);
    22482242           
    22492243            String rowClass = (i % 2 == 0 ? "snarkTorrentEven" : "snarkTorrentOdd");
     
    22652259                    Storage storage = snark.getStorage();
    22662260                    try {
    2267                         File f = item.getFile();
     2261                        File f = item;
    22682262                        if (f != null) {
    22692263                            long remaining = storage.remaining(f.getCanonicalPath());
     
    22952289            }
    22962290
    2297             String path=URIUtil.addPaths(base,encoded);
     2291            String path=addPaths(base,encoded);
    22982292            if (item.isDirectory() && !path.endsWith("/"))
    2299                 path=URIUtil.addPaths(path,"/");
     2293                path=addPaths(path,"/");
    23002294            String icon = toIcon(item);
    23012295
     
    23322326            if (showPriority) {
    23332327                buf.append("<td class=\"priority\">");
    2334                 File f = item.getFile();
     2328                File f = item;
    23352329                if ((!complete) && (!item.isDirectory()) && f != null) {
    23362330                    int pri = snark.getStorage().getPriority(f.getCanonicalPath());
     
    23692363
    23702364    /** @since 0.7.14 */
    2371     private String toIcon(Resource item) {
     2365    private String toIcon(File item) {
    23722366        if (item.isDirectory())
    23732367            return "folder";
     
    23822376    private String toIcon(String path) {
    23832377        String icon;
    2384         // Should really just add to the mime.properties file in org.mortbay.jetty.jar
    2385         // instead of this mishmash. We can't get to HttpContext.setMimeMapping()
    2386         // from here? We could do it from a web.xml perhaps.
    2387         // Or could we put our own org/mortbay/http/mime.properties file in the war?
     2378        // Note that for this to work well, our custom mime.properties file must be loaded.
    23882379        String plc = path.toLowerCase(Locale.US);
    2389         String mime = getServletContext().getMimeType(path);
     2380        String mime = getMimeType(path);
    23902381        if (mime == null)
    23912382            mime = "";
    23922383        if (mime.equals("text/html"))
    23932384            icon = "html";
    2394         else if (mime.equals("text/plain") || plc.endsWith(".nfo") ||
     2385        else if (mime.equals("text/plain") ||
    23952386                 mime.equals("application/rtf"))
    23962387            icon = "page";
    2397         else if (mime.equals("application/java-archive") || plc.endsWith(".war") ||
     2388        else if (mime.equals("application/java-archive") ||
    23982389                 plc.endsWith(".deb"))
    23992390            icon = "package";
     
    24022393        else if (mime.equals("application/pdf"))
    24032394            icon = "page_white_acrobat";
    2404         else if (mime.startsWith("image/") || plc.endsWith(".ico"))
     2395        else if (mime.startsWith("image/"))
    24052396            icon = "photo";
    2406         else if (mime.startsWith("audio/") || mime.equals("application/ogg") ||
    2407                  plc.endsWith(".flac") || plc.endsWith(".m4a") || plc.endsWith(".wma") ||
    2408                  plc.endsWith(".ape") || plc.endsWith(".oga"))
     2397        else if (mime.startsWith("audio/") || mime.equals("application/ogg"))
    24092398            icon = "music";
    2410         else if (mime.startsWith("video/") || plc.endsWith(".mkv") || plc.endsWith(".m4v") ||
    2411                  plc.endsWith(".mp4") || plc.endsWith(".wmv") || plc.endsWith(".flv") ||
    2412                  plc.endsWith(".ogm") || plc.endsWith(".ogv"))
     2399        else if (mime.startsWith("video/"))
    24132400            icon = "film";
    24142401        else if (mime.equals("application/zip") || mime.equals("application/x-gtar") ||
    24152402                 mime.equals("application/compress") || mime.equals("application/gzip") ||
    2416                  mime.equals("application/x-tar") ||
    2417                  plc.endsWith(".rar") || plc.endsWith(".bz2") || plc.endsWith(".7z"))
     2403                 mime.equals("application/x-tar"))
    24182404            icon = "compress";
    24192405        else if (plc.endsWith(".exe"))
Note: See TracChangeset for help on using the changeset viewer.