Пример #1
0
  /**
   * Get the resource list as a HTML directory listing.
   *
   * @param base The base URL
   * @param parent True if the parent directory should be included
   * @return String of HTML
   */
  public String getListHTML(String base, boolean parent) throws IOException {
    base = URIUtil.canonicalPath(base);
    if (base == null || !isDirectory()) return null;

    String[] ls = list();
    if (ls == null) return null;
    Arrays.sort(ls);

    String decodedBase = URIUtil.decodePath(base);
    String title = "Directory: " + deTag(decodedBase);

    StringBuilder buf = new StringBuilder(4096);
    buf.append("<HTML><HEAD>");
    buf.append("<LINK HREF=\"")
        .append("jetty-dir.css")
        .append("\" REL=\"stylesheet\" TYPE=\"text/css\"/><TITLE>");
    buf.append(title);
    buf.append("</TITLE></HEAD><BODY>\n<H1>");
    buf.append(title);
    buf.append("</H1>\n<TABLE BORDER=0>\n");

    if (parent) {
      buf.append("<TR><TD><A HREF=\"");
      buf.append(URIUtil.addPaths(base, "../"));
      buf.append("\">Parent Directory</A></TD><TD></TD><TD></TD></TR>\n");
    }

    String encodedBase = hrefEncodeURI(base);

    DateFormat dfmt = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM);
    for (int i = 0; i < ls.length; i++) {
      Resource item = addPath(ls[i]);

      buf.append("\n<TR><TD><A HREF=\"");
      String path = URIUtil.addPaths(encodedBase, URIUtil.encodePath(ls[i]));

      buf.append(path);

      if (item.isDirectory() && !path.endsWith("/")) buf.append(URIUtil.SLASH);

      // URIUtil.encodePath(buf,path);
      buf.append("\">");
      buf.append(deTag(ls[i]));
      buf.append("&nbsp;");
      buf.append("</A></TD><TD ALIGN=right>");
      buf.append(item.length());
      buf.append(" bytes&nbsp;</TD><TD>");
      buf.append(dfmt.format(new Date(item.lastModified())));
      buf.append("</TD></TR>");
    }
    buf.append("</TABLE>\n");
    buf.append("</BODY></HTML>\n");

    return buf.toString();
  }
Пример #2
0
  /**
   * Create a canonical name for a webapp temp directory. The form of the name is: <code>
   * "Jetty_"+host+"_"+port+"__"+resourceBase+"_"+context+"_"+virtualhost+base36_hashcode_of_whole_string
   * </code> host and port uniquely identify the server context and virtual host uniquely identify
   * the webapp
   *
   * @return the canonical name for the webapp temp directory
   */
  public static String getCanonicalNameForWebAppTmpDir(WebAppContext context) {
    StringBuffer canonicalName = new StringBuffer();
    canonicalName.append("jetty-");

    // get the host and the port from the first connector
    Server server = context.getServer();
    if (server != null) {
      Connector[] connectors = context.getServer().getConnectors();

      if (connectors.length > 0) {
        // Get the host
        String host = (connectors == null || connectors[0] == null ? "" : connectors[0].getHost());
        if (host == null) host = "0.0.0.0";
        canonicalName.append(host);

        // Get the port
        canonicalName.append("-");
        // try getting the real port being listened on
        int port = (connectors == null || connectors[0] == null ? 0 : connectors[0].getLocalPort());
        // if not available (eg no connectors or connector not started),
        // try getting one that was configured.
        if (port < 0) port = connectors[0].getPort();
        canonicalName.append(port);
        canonicalName.append("-");
      }
    }

    // Resource  base
    try {
      Resource resource = context.getBaseResource();
      if (resource == null) {
        if (context.getWar() == null || context.getWar().length() == 0)
          resource = context.newResource(context.getResourceBase());

        // Set dir or WAR
        resource = context.newResource(context.getWar());
      }

      String tmp = URIUtil.decodePath(resource.getURL().getPath());
      if (tmp.endsWith("/")) tmp = tmp.substring(0, tmp.length() - 1);
      if (tmp.endsWith("!")) tmp = tmp.substring(0, tmp.length() - 1);
      // get just the last part which is the filename
      int i = tmp.lastIndexOf("/");
      canonicalName.append(tmp.substring(i + 1, tmp.length()));
      canonicalName.append("-");
    } catch (Exception e) {
      LOG.warn("Can't generate resourceBase as part of webapp tmp dir name", e);
    }

    // Context name
    String contextPath = context.getContextPath();
    contextPath = contextPath.replace('/', '_');
    contextPath = contextPath.replace('\\', '_');
    canonicalName.append(contextPath);

    // Virtual host (if there is one)
    canonicalName.append("-");
    String[] vhosts = context.getVirtualHosts();
    if (vhosts == null || vhosts.length <= 0) canonicalName.append("any");
    else canonicalName.append(vhosts[0]);

    // sanitize
    for (int i = 0; i < canonicalName.length(); i++) {
      char c = canonicalName.charAt(i);
      if (!Character.isJavaIdentifierPart(c) && "-.".indexOf(c) < 0)
        canonicalName.setCharAt(i, '.');
    }

    canonicalName.append("-");
    return canonicalName.toString();
  }