Java Code Examples for org.eclipse.jetty.util.URIUtil#addPaths()

The following examples show how to use org.eclipse.jetty.util.URIUtil#addPaths() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RestServlet.java    From chipster with MIT License 5 votes vote down vote up
private File locateFile(HttpServletRequest request) throws ServletException {
	String requestPath = URIUtil.addPaths(request.getServletPath(), request.getPathInfo());
	String realPath = getServletContext().getRealPath(requestPath);
	if (realPath == null) {
		throw new ServletException("Servlet context refused to convert a request path to a real path. Make sure that serving files through symlinks is enabled");
	}
	return new File(realPath);		
}
 
Example 2
Source File: FileResource.java    From IoTgo_Android_App with MIT License 4 votes vote down vote up
@Override
public Resource addPath(String path)
    throws IOException,MalformedURLException
{
    URLResource r=null;
    String url=null;

    path = org.eclipse.jetty.util.URIUtil.canonicalPath(path);
   
    if ("/".equals(path))
        return this;
    else if (!isDirectory())
    {
        r=(FileResource)super.addPath(path);
        url=r._urlString;
    }
    else
    {
        if (path==null)
            throw new MalformedURLException();   
        
        // treat all paths being added as relative
        String rel=path;
        if (path.startsWith("/"))
            rel = path.substring(1);
        
        url=URIUtil.addPaths(_urlString,URIUtil.encodePath(rel));
        r=(URLResource)Resource.newResource(url);
    }
    
    // Check for encoding aliases
    // The encoded path should be a suffix of the resource (give or take a directory / )
    String encoded=URIUtil.encodePath(path);
    int expected=r.toString().length()-encoded.length();
    int index = r._urlString.lastIndexOf(encoded, expected);
    if (expected!=index && ((expected-1)!=index || path.endsWith("/") || !r.isDirectory()))
    {
        if (r instanceof FileResource)
        {
            ((FileResource)r)._alias=((FileResource)r)._file.getCanonicalFile().toURI().toURL();
            ((FileResource)r)._aliasChecked=true;
        }
    }                             
    return r;
}
 
Example 3
Source File: Resource.java    From IoTgo_Android_App with MIT License 4 votes vote down vote up
/** 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();
   }
 
Example 4
Source File: FileResource.java    From IoTgo_Android_App with MIT License 4 votes vote down vote up
@Override
public Resource addPath(String path)
    throws IOException,MalformedURLException
{
    URLResource r=null;
    String url=null;

    path = org.eclipse.jetty.util.URIUtil.canonicalPath(path);
   
    if ("/".equals(path))
        return this;
    else if (!isDirectory())
    {
        r=(FileResource)super.addPath(path);
        url=r._urlString;
    }
    else
    {
        if (path==null)
            throw new MalformedURLException();   
        
        // treat all paths being added as relative
        String rel=path;
        if (path.startsWith("/"))
            rel = path.substring(1);
        
        url=URIUtil.addPaths(_urlString,URIUtil.encodePath(rel));
        r=(URLResource)Resource.newResource(url);
    }
    
    // Check for encoding aliases
    // The encoded path should be a suffix of the resource (give or take a directory / )
    String encoded=URIUtil.encodePath(path);
    int expected=r.toString().length()-encoded.length();
    int index = r._urlString.lastIndexOf(encoded, expected);
    if (expected!=index && ((expected-1)!=index || path.endsWith("/") || !r.isDirectory()))
    {
        if (r instanceof FileResource)
        {
            ((FileResource)r)._alias=((FileResource)r)._file.getCanonicalFile().toURI().toURL();
            ((FileResource)r)._aliasChecked=true;
        }
    }                             
    return r;
}
 
Example 5
Source File: Resource.java    From IoTgo_Android_App with MIT License 4 votes vote down vote up
/** 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();
   }