Example usage for org.apache.commons.fileupload FileItem getOutputStream

List of usage examples for org.apache.commons.fileupload FileItem getOutputStream

Introduction

In this page you can find the example usage for org.apache.commons.fileupload FileItem getOutputStream.

Prototype

OutputStream getOutputStream() throws IOException;

Source Link

Document

Returns an java.io.OutputStream OutputStream that can be used for storing the contents of the file.

Usage

From source file:hoot.services.utils.MultipartSerializerTest.java

@Ignore
@Test//from   w  w  w  .j av a  2s  .c om
@Category(UnitTest.class)
public void TestserializeUploadedFiles() throws Exception {
    // homeFolder + "/upload/" + jobId + "/" + relPath;
    // Create dummy FGDB

    String jobId = "123-456-789-testosm";
    String wkdirpath = HOME_FOLDER + "/upload/" + jobId;
    File workingDir = new File(wkdirpath);
    FileUtils.forceMkdir(workingDir);
    Assert.assertTrue(workingDir.exists());

    FileItemFactory factory = new DiskFileItemFactory(16, null);
    String textFieldName = "textField";

    FileItem item = factory.createItem(textFieldName, "application/octet-stream", true, "dummy1.osm");

    String textFieldValue = "0123456789";
    byte[] testFieldValueBytes = textFieldValue.getBytes();

    try (OutputStream os = item.getOutputStream()) {
        os.write(testFieldValueBytes);
    }

    File out = new File(wkdirpath + "/buffer.tmp");
    item.write(out);

    List<FileItem> fileItemsList = new ArrayList<>();
    fileItemsList.add(item);
    Assert.assertTrue(out.exists());

    Map<String, String> uploadedFiles = new HashMap<>();
    Map<String, String> uploadedFilesPaths = new HashMap<>();

    //        MultipartSerializer.serializeUploadedFiles(fileItemsList, uploadedFiles, uploadedFilesPaths, wkdirpath);

    Assert.assertEquals("OSM", uploadedFiles.get("dummy1"));
    Assert.assertEquals("dummy1.osm", uploadedFilesPaths.get("dummy1"));

    File content = new File(wkdirpath + "/dummy1.osm");
    Assert.assertTrue(content.exists());

    FileUtils.forceDelete(workingDir);
}

From source file:fr.paris.lutece.portal.web.xsl.XslExportJspBeanTest.java

public void testDoCreateXslExport() throws AccessDeniedException, IOException {
    MockHttpServletRequest request = new MockHttpServletRequest();
    AdminUser user = new AdminUser();
    user.setRoles(/*from w w w. j a v  a  2s . com*/
            AdminRoleHome.findAll().stream().collect(Collectors.toMap(AdminRole::getKey, Function.identity())));
    Utils.registerAdminUserWithRigth(request, user, XslExportJspBean.RIGHT_MANAGE_XSL_EXPORT);
    String randomName = getRandomName();
    Map<String, String[]> parameters = new HashMap<>();
    parameters.put("title", new String[] { randomName });
    parameters.put("description", new String[] { randomName });
    parameters.put("extension", new String[] { randomName });
    parameters.put(SecurityTokenService.PARAMETER_TOKEN, new String[] {
            SecurityTokenService.getInstance().getToken(request, "admin/xsl/create_xsl_export.html") });
    Map<String, List<FileItem>> multipartFiles = new HashMap<>();
    List<FileItem> fileItems = new ArrayList<>();
    FileItem item = new DiskFileItemFactory().createItem("id_file", "", false, "xsl");
    item.getOutputStream().write("<?xml version='1.0'?><a/>".getBytes());
    fileItems.add(item);
    multipartFiles.put("id_file", fileItems);

    _instance.init(request, XslExportJspBean.RIGHT_MANAGE_XSL_EXPORT);
    try {
        _instance.doCreateXslExport(new MultipartHttpServletRequest(request, multipartFiles, parameters));
        assertTrue(XslExportHome.getList().stream().anyMatch(e -> randomName.equals(e.getTitle())
                && randomName.equals(e.getDescription()) && randomName.equals(e.getExtension())));
    } finally {
        XslExportHome.getList().stream().filter(e -> randomName.equals(e.getTitle()))
                .forEach(e -> XslExportHome.remove(e.getIdXslExport()));
    }
}

From source file:fr.paris.lutece.portal.web.xsl.XslExportJspBeanTest.java

public void testDoCreateXslExportNoToken() throws AccessDeniedException, IOException {
    MockHttpServletRequest request = new MockHttpServletRequest();
    AdminUser user = new AdminUser();
    user.setRoles(/*w  w w.  j a v a 2 s  .c  o m*/
            AdminRoleHome.findAll().stream().collect(Collectors.toMap(AdminRole::getKey, Function.identity())));
    Utils.registerAdminUserWithRigth(request, user, XslExportJspBean.RIGHT_MANAGE_XSL_EXPORT);
    String randomName = getRandomName();
    Map<String, String[]> parameters = new HashMap<>();
    parameters.put("title", new String[] { randomName });
    parameters.put("description", new String[] { randomName });
    parameters.put("extension", new String[] { randomName });

    Map<String, List<FileItem>> multipartFiles = new HashMap<>();
    List<FileItem> fileItems = new ArrayList<>();
    FileItem item = new DiskFileItemFactory().createItem("id_file", "", false, "xsl");
    item.getOutputStream().write("<?xml version='1.0'?><a/>".getBytes());
    fileItems.add(item);
    multipartFiles.put("id_file", fileItems);

    _instance.init(request, XslExportJspBean.RIGHT_MANAGE_XSL_EXPORT);
    try {
        _instance.doCreateXslExport(new MultipartHttpServletRequest(request, multipartFiles, parameters));
        fail("Should have thrown");
    } catch (AccessDeniedException ade) {
        assertFalse(XslExportHome.getList().stream().anyMatch(e -> randomName.equals(e.getTitle())
                && randomName.equals(e.getDescription()) && randomName.equals(e.getExtension())));
    } finally {
        XslExportHome.getList().stream().filter(e -> randomName.equals(e.getTitle()))
                .forEach(e -> XslExportHome.remove(e.getIdXslExport()));
    }
}

From source file:fr.paris.lutece.portal.web.xsl.XslExportJspBeanTest.java

public void testDoCreateXslExportInvalidToken() throws AccessDeniedException, IOException {
    MockHttpServletRequest request = new MockHttpServletRequest();
    AdminUser user = new AdminUser();
    user.setRoles(//from   ww  w  . j a va  2  s  .c  om
            AdminRoleHome.findAll().stream().collect(Collectors.toMap(AdminRole::getKey, Function.identity())));
    Utils.registerAdminUserWithRigth(request, user, XslExportJspBean.RIGHT_MANAGE_XSL_EXPORT);
    String randomName = getRandomName();
    Map<String, String[]> parameters = new HashMap<>();
    parameters.put("title", new String[] { randomName });
    parameters.put("description", new String[] { randomName });
    parameters.put("extension", new String[] { randomName });
    parameters.put(SecurityTokenService.PARAMETER_TOKEN, new String[] {
            SecurityTokenService.getInstance().getToken(request, "admin/xsl/create_xsl_export.html") + "b" });
    Map<String, List<FileItem>> multipartFiles = new HashMap<>();
    List<FileItem> fileItems = new ArrayList<>();
    FileItem item = new DiskFileItemFactory().createItem("id_file", "", false, "xsl");
    item.getOutputStream().write("<?xml version='1.0'?><a/>".getBytes());
    fileItems.add(item);
    multipartFiles.put("id_file", fileItems);

    _instance.init(request, XslExportJspBean.RIGHT_MANAGE_XSL_EXPORT);
    try {
        _instance.doCreateXslExport(new MultipartHttpServletRequest(request, multipartFiles, parameters));
        fail("Should have thrown");
    } catch (AccessDeniedException ade) {
        assertFalse(XslExportHome.getList().stream().anyMatch(e -> randomName.equals(e.getTitle())
                && randomName.equals(e.getDescription()) && randomName.equals(e.getExtension())));
    } finally {
        XslExportHome.getList().stream().filter(e -> randomName.equals(e.getTitle()))
                .forEach(e -> XslExportHome.remove(e.getIdXslExport()));
    }
}

From source file:fr.paris.lutece.portal.web.style.PageTemplatesJspBeanTest.java

public void testDoCreatePageTemplateNoToken() throws AccessDeniedException, IOException {
    final String desc = getRandomName();
    Map<String, String[]> parameters = new HashMap<String, String[]>();
    parameters.put(Parameters.PAGE_TEMPLATE_DESCRIPTION, new String[] { desc });
    DiskFileItemFactory fileItemFactory = new DiskFileItemFactory();
    Map<String, List<FileItem>> files = new HashMap<>();
    List<FileItem> pageTemplateFiles = new ArrayList<>();
    FileItem pageTemplateFile = fileItemFactory.createItem("page_template_file", "text/html", false,
            "junit.html");
    pageTemplateFile.getOutputStream().write(new byte[1]);
    pageTemplateFiles.add(pageTemplateFile);
    files.put("page_template_file", pageTemplateFiles);
    List<FileItem> pageTemplatePictures = new ArrayList<>();
    FileItem pageTemplatePicture = fileItemFactory.createItem("page_template_picture", "image/jpg", false,
            "junit.jpg");
    pageTemplatePicture.getOutputStream().write(new byte[1]);
    pageTemplatePictures.add(pageTemplatePicture);
    files.put("page_template_picture", pageTemplatePictures);
    MultipartHttpServletRequest multipartRequest = new MultipartHttpServletRequest(request, files, parameters);
    try {/*from ww  w.  j  av a  2s  .c om*/
        instance.doCreatePageTemplate(multipartRequest);
        fail("Should have thrown");
    } catch (AccessDeniedException e) {
        assertFalse(PageTemplateHome.getPageTemplatesList().stream()
                .anyMatch(t -> t.getDescription().equals(desc)));
    } finally {
        PageTemplateHome.getPageTemplatesList().stream().filter(t -> t.getDescription().equals(desc))
                .forEach(t -> PageTemplateHome.remove(t.getId()));
    }
}

From source file:fr.paris.lutece.portal.web.style.PageTemplatesJspBeanTest.java

/**
 * Test of doCreatePageTemplate method, of fr.paris.lutece.portal.web.style.PageTemplatesJspBean.
 * /*w  w  w  . j  a va 2 s  .  co m*/
 * @throws AccessDeniedException
 * @throws IOException
 */
public void testDoCreatePageTemplate() throws AccessDeniedException, IOException {
    final String desc = getRandomName();
    Map<String, String[]> parameters = new HashMap<String, String[]>();
    parameters.put(Parameters.PAGE_TEMPLATE_DESCRIPTION, new String[] { desc });
    parameters.put(SecurityTokenService.PARAMETER_TOKEN, new String[] {
            SecurityTokenService.getInstance().getToken(request, "admin/style/create_page_template.html") });
    DiskFileItemFactory fileItemFactory = new DiskFileItemFactory();
    Map<String, List<FileItem>> files = new HashMap<>();
    List<FileItem> pageTemplateFiles = new ArrayList<>();
    FileItem pageTemplateFile = fileItemFactory.createItem("page_template_file", "text/html", false,
            "junit.html");
    pageTemplateFile.getOutputStream().write(new byte[1]);
    pageTemplateFiles.add(pageTemplateFile);
    files.put("page_template_file", pageTemplateFiles);
    List<FileItem> pageTemplatePictures = new ArrayList<>();
    FileItem pageTemplatePicture = fileItemFactory.createItem("page_template_picture", "image/jpg", false,
            "junit.jpg");
    pageTemplatePicture.getOutputStream().write(new byte[1]);
    pageTemplatePictures.add(pageTemplatePicture);
    files.put("page_template_picture", pageTemplatePictures);
    MultipartHttpServletRequest multipartRequest = new MultipartHttpServletRequest(request, files, parameters);
    try {
        instance.doCreatePageTemplate(multipartRequest);
        assertTrue(PageTemplateHome.getPageTemplatesList().stream()
                .anyMatch(t -> t.getDescription().equals(desc)));
    } finally {
        PageTemplateHome.getPageTemplatesList().stream().filter(t -> t.getDescription().equals(desc))
                .forEach(t -> PageTemplateHome.remove(t.getId()));
    }
}

From source file:fr.paris.lutece.portal.web.style.PageTemplatesJspBeanTest.java

public void testDoCreatePageTemplateInvalidToken() throws AccessDeniedException, IOException {
    final String desc = getRandomName();
    Map<String, String[]> parameters = new HashMap<String, String[]>();
    parameters.put(Parameters.PAGE_TEMPLATE_DESCRIPTION, new String[] { desc });
    parameters.put(SecurityTokenService.PARAMETER_TOKEN, new String[] {
            SecurityTokenService.getInstance().getToken(request, "admin/style/create_page_template.html")
                    + "b" });
    DiskFileItemFactory fileItemFactory = new DiskFileItemFactory();
    Map<String, List<FileItem>> files = new HashMap<>();
    List<FileItem> pageTemplateFiles = new ArrayList<>();
    FileItem pageTemplateFile = fileItemFactory.createItem("page_template_file", "text/html", false,
            "junit.html");
    pageTemplateFile.getOutputStream().write(new byte[1]);
    pageTemplateFiles.add(pageTemplateFile);
    files.put("page_template_file", pageTemplateFiles);
    List<FileItem> pageTemplatePictures = new ArrayList<>();
    FileItem pageTemplatePicture = fileItemFactory.createItem("page_template_picture", "image/jpg", false,
            "junit.jpg");
    pageTemplatePicture.getOutputStream().write(new byte[1]);
    pageTemplatePictures.add(pageTemplatePicture);
    files.put("page_template_picture", pageTemplatePictures);
    MultipartHttpServletRequest multipartRequest = new MultipartHttpServletRequest(request, files, parameters);
    try {/* w  w w.  ja v a2 s .c  om*/
        instance.doCreatePageTemplate(multipartRequest);
        fail("Should have thrown");
    } catch (AccessDeniedException e) {
        assertFalse(PageTemplateHome.getPageTemplatesList().stream()
                .anyMatch(t -> t.getDescription().equals(desc)));
    } finally {
        PageTemplateHome.getPageTemplatesList().stream().filter(t -> t.getDescription().equals(desc))
                .forEach(t -> PageTemplateHome.remove(t.getId()));
    }
}

From source file:fr.paris.lutece.portal.web.stylesheet.StyleSheetJspBeanTest.java

public void testDoCreateStyleSheetNoToken() throws IOException, AccessDeniedException {
    MockHttpServletRequest request = new MockHttpServletRequest();
    Map<String, String[]> parameters = new HashMap<>();
    final String randomName = getRandomName();
    parameters.put(Parameters.STYLESHEET_NAME, new String[] { randomName });
    parameters.put(Parameters.STYLES, new String[] { Integer.toString(style.getId()) });
    parameters.put(Parameters.MODE_STYLESHEET, new String[] { "0" });
    Map<String, List<FileItem>> multipartFiles = new HashMap<>();
    List<FileItem> items = new ArrayList<>();
    FileItem source = new DiskFileItemFactory().createItem(Parameters.STYLESHEET_SOURCE, "application/xml",
            true, randomName);/*from   w ww.  j av a2s  .c o m*/
    source.getOutputStream().write("<a/>".getBytes());
    items.add(source);
    multipartFiles.put(Parameters.STYLESHEET_SOURCE, items);
    MultipartHttpServletRequest multipart = new MultipartHttpServletRequest(request, multipartFiles,
            parameters);
    try {
        instance.doCreateStyleSheet(multipart);
        fail("Should have thrown");
    } catch (AccessDeniedException e) {
        assertTrue(StyleSheetHome.getStyleSheetList(0).stream()
                .noneMatch(stylesheet -> stylesheet.getDescription().equals(randomName)));
    } finally {
        StyleSheetHome.getStyleSheetList(0).stream()
                .filter(stylesheet -> stylesheet.getDescription().equals(randomName))
                .forEach(stylesheet -> StyleSheetHome.remove(stylesheet.getId()));
    }
}

From source file:fr.paris.lutece.portal.web.stylesheet.StyleSheetJspBeanTest.java

/**
 * Test of doCreateStyleSheet method, of class
 * fr.paris.lutece.portal.web.stylesheet.StyleSheetJspBean.
 * /*from   w w w  .j ava 2 s  .c o m*/
 * @throws IOException
 * @throws AccessDeniedException
 */
public void testDoCreateStyleSheet() throws IOException, AccessDeniedException {
    MockHttpServletRequest request = new MockHttpServletRequest();
    Map<String, String[]> parameters = new HashMap<>();
    final String randomName = getRandomName();
    parameters.put(Parameters.STYLESHEET_NAME, new String[] { randomName });
    parameters.put(Parameters.STYLES, new String[] { Integer.toString(style.getId()) });
    parameters.put(Parameters.MODE_STYLESHEET, new String[] { "0" });
    parameters.put(SecurityTokenService.PARAMETER_TOKEN, new String[] {
            SecurityTokenService.getInstance().getToken(request, "admin/stylesheet/create_stylesheet.html") });
    Map<String, List<FileItem>> multipartFiles = new HashMap<>();
    List<FileItem> items = new ArrayList<>();
    FileItem source = new DiskFileItemFactory().createItem(Parameters.STYLESHEET_SOURCE, "application/xml",
            true, randomName);
    source.getOutputStream().write("<a/>".getBytes());
    items.add(source);
    multipartFiles.put(Parameters.STYLESHEET_SOURCE, items);
    MultipartHttpServletRequest multipart = new MultipartHttpServletRequest(request, multipartFiles,
            parameters);
    try {
        instance.doCreateStyleSheet(multipart);
        assertTrue(StyleSheetHome.getStyleSheetList(0).stream()
                .anyMatch(stylesheet -> stylesheet.getDescription().equals(randomName)));
    } finally {
        StyleSheetHome.getStyleSheetList(0).stream()
                .filter(stylesheet -> stylesheet.getDescription().equals(randomName))
                .forEach(stylesheet -> StyleSheetHome.remove(stylesheet.getId()));
    }
}

From source file:fr.paris.lutece.portal.web.stylesheet.StyleSheetJspBeanTest.java

public void testDoCreateStyleSheetInvalidToken() throws IOException, AccessDeniedException {
    MockHttpServletRequest request = new MockHttpServletRequest();
    Map<String, String[]> parameters = new HashMap<>();
    final String randomName = getRandomName();
    parameters.put(Parameters.STYLESHEET_NAME, new String[] { randomName });
    parameters.put(Parameters.STYLES, new String[] { Integer.toString(style.getId()) });
    parameters.put(Parameters.MODE_STYLESHEET, new String[] { "0" });
    parameters.put(SecurityTokenService.PARAMETER_TOKEN, new String[] {
            SecurityTokenService.getInstance().getToken(request, "admin/stylesheet/create_stylesheet.html")
                    + "b" });
    Map<String, List<FileItem>> multipartFiles = new HashMap<>();
    List<FileItem> items = new ArrayList<>();
    FileItem source = new DiskFileItemFactory().createItem(Parameters.STYLESHEET_SOURCE, "application/xml",
            true, randomName);/*from  www. j  av  a  2 s.  c om*/
    source.getOutputStream().write("<a/>".getBytes());
    items.add(source);
    multipartFiles.put(Parameters.STYLESHEET_SOURCE, items);
    MultipartHttpServletRequest multipart = new MultipartHttpServletRequest(request, multipartFiles,
            parameters);
    try {
        instance.doCreateStyleSheet(multipart);
        fail("Should have thrown");
    } catch (AccessDeniedException e) {
        assertTrue(StyleSheetHome.getStyleSheetList(0).stream()
                .noneMatch(stylesheet -> stylesheet.getDescription().equals(randomName)));
    } finally {
        StyleSheetHome.getStyleSheetList(0).stream()
                .filter(stylesheet -> stylesheet.getDescription().equals(randomName))
                .forEach(stylesheet -> StyleSheetHome.remove(stylesheet.getId()));
    }
}