Search in sources :

Example 36 with WebResource

use of org.apache.catalina.WebResource in project tomcat by apache.

the class DefaultServlet method doPut.

/**
     * Process a PUT request for the specified resource.
     *
     * @param req The servlet request we are processing
     * @param resp The servlet response we are creating
     *
     * @exception IOException if an input/output error occurs
     * @exception ServletException if a servlet-specified error occurs
     */
@Override
protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    if (readOnly) {
        resp.sendError(HttpServletResponse.SC_FORBIDDEN);
        return;
    }
    String path = getRelativePath(req);
    WebResource resource = resources.getResource(path);
    Range range = parseContentRange(req, resp);
    InputStream resourceInputStream = null;
    try {
        // Assume just one range is specified for now
        if (range != null) {
            File contentFile = executePartialPut(req, range, path);
            resourceInputStream = new FileInputStream(contentFile);
        } else {
            resourceInputStream = req.getInputStream();
        }
        if (resources.write(path, resourceInputStream, true)) {
            if (resource.exists()) {
                resp.setStatus(HttpServletResponse.SC_NO_CONTENT);
            } else {
                resp.setStatus(HttpServletResponse.SC_CREATED);
            }
        } else {
            resp.sendError(HttpServletResponse.SC_CONFLICT);
        }
    } finally {
        if (resourceInputStream != null) {
            try {
                resourceInputStream.close();
            } catch (IOException ioe) {
            // Ignore
            }
        }
    }
}
Also used : BufferedInputStream(java.io.BufferedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) WebResource(org.apache.catalina.WebResource) IOException(java.io.IOException) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 37 with WebResource

use of org.apache.catalina.WebResource in project tomcat by apache.

the class WebdavStatus method parseProperties.

/**
     * Propfind helper method.
     *
     * @param req The servlet request
     * @param resources Resources object associated with this context
     * @param generatedXML XML response to the Propfind request
     * @param path Path of the current resource
     * @param type Propfind type
     * @param propertiesVector If the propfind type is find properties by
     * name, then this Vector contains those properties
     */
private void parseProperties(HttpServletRequest req, XMLWriter generatedXML, String path, int type, Vector<String> propertiesVector) {
    // Exclude any resource in the /WEB-INF and /META-INF subdirectories
    if (isSpecialPath(path))
        return;
    WebResource resource = resources.getResource(path);
    if (!resource.exists()) {
        // Broken symlink or odd permission settings?
        return;
    }
    String href = req.getContextPath() + req.getServletPath();
    if ((href.endsWith("/")) && (path.startsWith("/")))
        href += path.substring(1);
    else
        href += path;
    if (resource.isDirectory() && (!href.endsWith("/")))
        href += "/";
    String rewrittenUrl = rewriteUrl(href);
    generatePropFindResponse(generatedXML, rewrittenUrl, path, type, propertiesVector, resource.isFile(), false, resource.getCreation(), resource.getLastModified(), resource.getContentLength(), getServletContext().getMimeType(resource.getName()), resource.getETag());
}
Also used : WebResource(org.apache.catalina.WebResource)

Example 38 with WebResource

use of org.apache.catalina.WebResource in project tomcat by apache.

the class AbstractTestResourceSet method testGetResourceCaseSensitive.

@Test
public final void testGetResourceCaseSensitive() {
    WebResource webResource = resourceRoot.getResource(getMount() + "/d1/d1-F1.txt");
    Assert.assertFalse(webResource.exists());
}
Also used : WebResource(org.apache.catalina.WebResource) Test(org.junit.Test)

Example 39 with WebResource

use of org.apache.catalina.WebResource in project tomcat by apache.

the class AbstractTestResourceSet method testWriteDirA.

@Test
public final void testWriteDirA() {
    WebResource d1 = resourceRoot.getResource(getMount() + "/d1");
    InputStream is = new ByteArrayInputStream("test".getBytes());
    if (d1.exists()) {
        Assert.assertFalse(resourceRoot.write(getMount() + "/d1", is, false));
    } else if (d1.isVirtual()) {
        Assert.assertTrue(resourceRoot.write(getMount() + "/d1", is, false));
        File file = new File(getBaseDir(), "d1");
        Assert.assertTrue(file.exists());
        Assert.assertTrue(file.delete());
    } else {
        Assert.fail("Unhandled condition in unit test");
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) WebResource(org.apache.catalina.WebResource) File(java.io.File) Test(org.junit.Test)

Example 40 with WebResource

use of org.apache.catalina.WebResource in project tomcat by apache.

the class AbstractTestResourceSet method testGetResourceDirA.

@Test
public final void testGetResourceDirA() {
    WebResource webResource = resourceRoot.getResource(getMount() + "/d1");
    Assert.assertTrue(webResource.isDirectory());
    Assert.assertEquals("d1", webResource.getName());
    Assert.assertEquals(getMount() + "/d1/", webResource.getWebappPath());
    Assert.assertEquals(-1, webResource.getContentLength());
    Assert.assertNull(webResource.getContent());
    Assert.assertNull(webResource.getInputStream());
}
Also used : WebResource(org.apache.catalina.WebResource) Test(org.junit.Test)

Aggregations

WebResource (org.apache.catalina.WebResource)47 Test (org.junit.Test)14 InputStream (java.io.InputStream)10 File (java.io.File)9 ByteArrayInputStream (java.io.ByteArrayInputStream)8 IOException (java.io.IOException)7 URL (java.net.URL)7 BufferedInputStream (java.io.BufferedInputStream)5 FileInputStream (java.io.FileInputStream)5 InputSource (org.xml.sax.InputSource)5 PrintWriter (java.io.PrintWriter)3 RandomAccessFile (java.io.RandomAccessFile)3 Manifest (java.util.jar.Manifest)3 DocumentBuilder (javax.xml.parsers.DocumentBuilder)3 WebResourceSet (org.apache.catalina.WebResourceSet)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 OutputStreamWriter (java.io.OutputStreamWriter)2 StringWriter (java.io.StringWriter)2 Hashtable (java.util.Hashtable)2 LinkedHashSet (java.util.LinkedHashSet)2