Search in sources :

Example 21 with ServletFileUpload

use of org.apache.commons.fileupload.servlet.ServletFileUpload in project fess by codelibs.

the class FessMultipartRequestHandler method handleRequest.

// ===================================================================================
//                                                                      Handle Request
//                                                                      ==============
@Override
public void handleRequest(final HttpServletRequest request) throws ServletException {
    // /- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    // copied from super's method and extends it
    // basically for JVN#14876762
    // thought not all problems are resolved however the main case is safety
    // - - - - - - - - - -/
    final ServletFileUpload upload = createServletFileUpload(request);
    prepareElementsHash();
    try {
        final List<FileItem> items = parseRequest(request, upload);
        mappingParameter(request, items);
    } catch (final SizeLimitExceededException e) {
        handleSizeLimitExceededException(request, e);
    } catch (final FileUploadException e) {
        handleFileUploadException(e);
    }
}
Also used : FileItem(org.apache.commons.fileupload.FileItem) SizeLimitExceededException(org.apache.commons.fileupload.FileUploadBase.SizeLimitExceededException) ServletFileUpload(org.apache.commons.fileupload.servlet.ServletFileUpload) FileUploadException(org.apache.commons.fileupload.FileUploadException)

Example 22 with ServletFileUpload

use of org.apache.commons.fileupload.servlet.ServletFileUpload in project fess by codelibs.

the class FessMultipartRequestHandler method createServletFileUpload.

// ===================================================================================
//                                                            Create ServletFileUpload
//                                                            ========================
protected ServletFileUpload createServletFileUpload(final HttpServletRequest request) {
    final DiskFileItemFactory fileItemFactory = createDiskFileItemFactory();
    final ServletFileUpload upload = newServletFileUpload(fileItemFactory);
    upload.setHeaderEncoding(request.getCharacterEncoding());
    upload.setSizeMax(getSizeMax());
    return upload;
}
Also used : ServletFileUpload(org.apache.commons.fileupload.servlet.ServletFileUpload) DiskFileItemFactory(org.apache.commons.fileupload.disk.DiskFileItemFactory)

Example 23 with ServletFileUpload

use of org.apache.commons.fileupload.servlet.ServletFileUpload in project ghostdriver by detro.

the class DirectFileUploadTest method checkFileUploadCompletes.

@Test
public void checkFileUploadCompletes() throws IOException {
    WebDriver d = getDriver();
    if (!(d instanceof PhantomJSDriver)) {
        // The command under test is only available when using PhantomJS
        return;
    }
    PhantomJSDriver phantom = (PhantomJSDriver) d;
    String buttonId = "upload";
    // Create the test file for uploading
    File testFile = File.createTempFile("webdriver", "tmp");
    testFile.deleteOnExit();
    BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(testFile.getAbsolutePath()), "utf-8"));
    writer.write(FILE_HTML);
    writer.close();
    server.setHttpHandler("POST", new HttpRequestCallback() {

        @Override
        public void call(HttpServletRequest req, HttpServletResponse res) throws IOException {
            if (ServletFileUpload.isMultipartContent(req) && req.getPathInfo().endsWith("/upload")) {
                // Create a factory for disk-based file items
                DiskFileItemFactory factory = new DiskFileItemFactory(1024, new File(System.getProperty("java.io.tmpdir")));
                // Create a new file upload handler
                ServletFileUpload upload = new ServletFileUpload(factory);
                // Parse the request
                List<FileItem> items;
                try {
                    items = upload.parseRequest(req);
                } catch (FileUploadException fue) {
                    throw new IOException(fue);
                }
                res.setHeader("Content-Type", "text/html; charset=UTF-8");
                InputStream is = items.get(0).getInputStream();
                OutputStream os = res.getOutputStream();
                IOUtils.copy(is, os);
                os.write("<script>window.top.window.onUploadDone();</script>".getBytes());
                IOUtils.closeQuietly(is);
                IOUtils.closeQuietly(os);
                return;
            }
            res.sendError(400);
        }
    });
    // Upload the temp file
    phantom.get(server.getBaseUrl() + "/common/upload.html");
    phantom.executePhantomJS("var page = this; page.uploadFile('input#" + buttonId + "', '" + testFile.getAbsolutePath() + "');");
    phantom.findElement(By.id("go")).submit();
    // Uploading files across a network may take a while, even if they're really small.
    // Wait for the loading label to disappear.
    WebDriverWait wait = new WebDriverWait(phantom, 10);
    wait.until(ExpectedConditions.invisibilityOfElementLocated(By.id("upload_label")));
    phantom.switchTo().frame("upload_target");
    wait = new WebDriverWait(phantom, 5);
    wait.until(ExpectedConditions.textToBePresentInElementLocated(By.xpath("//body"), LOREM_IPSUM_TEXT));
    // Navigate after file upload to verify callbacks are properly released.
    phantom.get("http://www.google.com/");
}
Also used : WebDriver(org.openqa.selenium.WebDriver) PhantomJSDriver(org.openqa.selenium.phantomjs.PhantomJSDriver) HttpRequestCallback(ghostdriver.server.HttpRequestCallback) HttpServletResponse(javax.servlet.http.HttpServletResponse) DiskFileItemFactory(org.apache.commons.fileupload.disk.DiskFileItemFactory) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletFileUpload(org.apache.commons.fileupload.servlet.ServletFileUpload) WebDriverWait(org.openqa.selenium.support.ui.WebDriverWait) List(java.util.List) FileUploadException(org.apache.commons.fileupload.FileUploadException) Test(org.junit.Test)

Example 24 with ServletFileUpload

use of org.apache.commons.fileupload.servlet.ServletFileUpload in project ghostdriver by detro.

the class FileUploadTest method checkFileUploadCompletes.

@Test
public void checkFileUploadCompletes() throws IOException {
    WebDriver d = getDriver();
    // Create the test file for uploading
    File testFile = File.createTempFile("webdriver", "tmp");
    testFile.deleteOnExit();
    BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(testFile.getAbsolutePath()), "utf-8"));
    writer.write(FILE_HTML);
    writer.close();
    server.setHttpHandler("POST", new HttpRequestCallback() {

        @Override
        public void call(HttpServletRequest req, HttpServletResponse res) throws IOException {
            if (ServletFileUpload.isMultipartContent(req) && req.getPathInfo().endsWith("/upload")) {
                // Create a factory for disk-based file items
                DiskFileItemFactory factory = new DiskFileItemFactory(1024, new File(System.getProperty("java.io.tmpdir")));
                // Create a new file upload handler
                ServletFileUpload upload = new ServletFileUpload(factory);
                // Parse the request
                List<FileItem> items;
                try {
                    items = upload.parseRequest(req);
                } catch (FileUploadException fue) {
                    throw new IOException(fue);
                }
                res.setHeader("Content-Type", "text/html; charset=UTF-8");
                InputStream is = items.get(0).getInputStream();
                OutputStream os = res.getOutputStream();
                IOUtils.copy(is, os);
                os.write("<script>window.top.window.onUploadDone();</script>".getBytes());
                IOUtils.closeQuietly(is);
                IOUtils.closeQuietly(os);
                return;
            }
            res.sendError(400);
        }
    });
    // Upload the temp file
    d.get(server.getBaseUrl() + "/common/upload.html");
    d.findElement(By.id("upload")).sendKeys(testFile.getAbsolutePath());
    d.findElement(By.id("go")).submit();
    // Uploading files across a network may take a while, even if they're really small.
    // Wait for the loading label to disappear.
    WebDriverWait wait = new WebDriverWait(d, 10);
    wait.until(ExpectedConditions.invisibilityOfElementLocated(By.id("upload_label")));
    d.switchTo().frame("upload_target");
    wait = new WebDriverWait(d, 5);
    wait.until(ExpectedConditions.textToBePresentInElementLocated(By.xpath("//body"), LOREM_IPSUM_TEXT));
    // Navigate after file upload to verify callbacks are properly released.
    d.get("http://www.google.com/");
}
Also used : WebDriver(org.openqa.selenium.WebDriver) HttpRequestCallback(ghostdriver.server.HttpRequestCallback) HttpServletResponse(javax.servlet.http.HttpServletResponse) DiskFileItemFactory(org.apache.commons.fileupload.disk.DiskFileItemFactory) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletFileUpload(org.apache.commons.fileupload.servlet.ServletFileUpload) WebDriverWait(org.openqa.selenium.support.ui.WebDriverWait) List(java.util.List) FileUploadException(org.apache.commons.fileupload.FileUploadException) Test(org.junit.Test)

Example 25 with ServletFileUpload

use of org.apache.commons.fileupload.servlet.ServletFileUpload in project hudson-2.x by hudson.

the class PluginManager method doUploadPlugin.

/**
     * Uploads a plugin.
     */
public HttpResponse doUploadPlugin(StaplerRequest req) throws IOException, ServletException {
    try {
        Hudson.getInstance().checkPermission(Hudson.ADMINISTER);
        ServletFileUpload upload = new ServletFileUpload(new DiskFileItemFactory());
        // Parse the request
        FileItem fileItem = (FileItem) upload.parseRequest(req).get(0);
        String fileName = Util.getFileName(fileItem.getName());
        if ("".equals(fileName))
            return new HttpRedirect("advanced");
        if (!fileName.endsWith(".hpi"))
            throw new Failure(hudson.model.Messages.Hudson_NotAPlugin(fileName));
        fileItem.write(new File(rootDir, fileName));
        fileItem.delete();
        pluginUploaded = true;
        return new HttpRedirect(".");
    } catch (IOException e) {
        throw e;
    } catch (Exception e) {
        // grrr. fileItem.write throws this
        throw new ServletException(e);
    }
}
Also used : ServletException(javax.servlet.ServletException) FileItem(org.apache.commons.fileupload.FileItem) HttpRedirect(org.kohsuke.stapler.HttpRedirect) ServletFileUpload(org.apache.commons.fileupload.servlet.ServletFileUpload) IOException(java.io.IOException) DiskFileItemFactory(org.apache.commons.fileupload.disk.DiskFileItemFactory) File(java.io.File) Failure(hudson.model.Failure) ServletException(javax.servlet.ServletException) CycleDetectedException(hudson.util.CyclicGraphDetector.CycleDetectedException) IOException(java.io.IOException)

Aggregations

ServletFileUpload (org.apache.commons.fileupload.servlet.ServletFileUpload)43 FileItem (org.apache.commons.fileupload.FileItem)29 DiskFileItemFactory (org.apache.commons.fileupload.disk.DiskFileItemFactory)29 FileUploadException (org.apache.commons.fileupload.FileUploadException)20 File (java.io.File)17 IOException (java.io.IOException)13 ArrayList (java.util.ArrayList)13 InputStream (java.io.InputStream)7 HashMap (java.util.HashMap)7 FileItemFactory (org.apache.commons.fileupload.FileItemFactory)7 FileItemStream (org.apache.commons.fileupload.FileItemStream)7 FileItemIterator (org.apache.commons.fileupload.FileItemIterator)6 ServletException (javax.servlet.ServletException)5 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)5 List (java.util.List)4 Locale (java.util.Locale)4 GZIPInputStream (java.util.zip.GZIPInputStream)4 OpenClinicaSystemException (org.akaza.openclinica.exception.OpenClinicaSystemException)4 DataBinder (org.springframework.validation.DataBinder)4 Errors (org.springframework.validation.Errors)4