Example usage for org.apache.commons.fileupload FileItemFactory createItem

List of usage examples for org.apache.commons.fileupload FileItemFactory createItem

Introduction

In this page you can find the example usage for org.apache.commons.fileupload FileItemFactory createItem.

Prototype

FileItem createItem(String fieldName, String contentType, boolean isFormField, String fileName);

Source Link

Document

Create a new FileItem instance from the supplied parameters and any local factory configuration.

Usage

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

@Test
@Category(UnitTest.class)
public void TestserializeUploadedFiles() throws Exception {
    //homeFolder + "/upload/" + jobId + "/" + relPath;
    // Create dummy FGDB

    String jobId = "123-456-789-testosm";
    String wkdirpath = homeFolder + "/upload/" + jobId;
    File workingDir = new File(wkdirpath);
    FileUtils.forceMkdir(workingDir);/*w ww.  j av  a2 s  .  c o m*/
    org.junit.Assert.assertTrue(workingDir.exists());

    List<FileItem> fileItemsList = new ArrayList<FileItem>();

    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();

    OutputStream os = item.getOutputStream();
    os.write(testFieldValueBytes);
    os.close();

    File out = new File(wkdirpath + "/buffer.tmp");
    item.write(out);
    fileItemsList.add(item);
    org.junit.Assert.assertTrue(out.exists());

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

    _ms._serializeUploadedFiles(fileItemsList, jobId, uploadedFiles, uploadedFilesPaths, wkdirpath);

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

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

    FileUtils.forceDelete(workingDir);
}

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

@Test
@Category(UnitTest.class)
public void TestserializeFGDB() throws Exception {
    String jobId = "123-456-789";
    String wkdirpath = homeFolder + "/upload/" + jobId;
    File workingDir = new File(wkdirpath);
    FileUtils.forceMkdir(workingDir);/*from  w w  w.  ja v a  2 s  .c  om*/
    org.junit.Assert.assertTrue(workingDir.exists());

    List<FileItem> fileItemsList = new ArrayList<FileItem>();

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

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

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

    OutputStream os = item.getOutputStream();
    os.write(testFieldValueBytes);
    os.close();

    File out = new File(wkdirpath + "/buffer.tmp");
    item.write(out);
    fileItemsList.add(item);
    org.junit.Assert.assertTrue(out.exists());

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

    _ms._serializeFGDB(fileItemsList, jobId, uploadedFiles, uploadedFilesPaths);

    org.junit.Assert.assertEquals("GDB", uploadedFiles.get("fgdbTest"));
    org.junit.Assert.assertEquals("fgdbTest.gdb", uploadedFilesPaths.get("fgdbTest"));

    File fgdbpath = new File(wkdirpath + "/fgdbTest.gdb");
    org.junit.Assert.assertTrue(fgdbpath.exists());

    File content = new File(wkdirpath + "/fgdbTest.gdb/dummy1.gdbtable");
    org.junit.Assert.assertTrue(content.exists());

    FileUtils.forceDelete(workingDir);
}

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

@Ignore
@Test//from   w w  w  . ja va 2  s . co  m
@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:com.github.davidcarboni.encryptedfileupload.EncryptedFileItemSerializeTest.java

/**
 * Create a FileItem with the specfied content bytes and repository.
 *///ww  w .j av  a 2s . c  o  m
private FileItem createFileItem(byte[] contentBytes, File repository) {
    FileItemFactory factory = new EncryptedFileItemFactory(threshold, repository);
    String textFieldName = "textField";

    FileItem item = factory.createItem(textFieldName, textContentType, true, "My File Name");
    try {
        OutputStream os = item.getOutputStream();
        os.write(contentBytes);
        os.close();
    } catch (IOException e) {
        fail("Unexpected IOException" + e);
    }

    return item;

}

From source file:com.carolinarollergirls.scoreboard.jetty.MediaServlet.java

protected void processZipFileItem(FileItemFactory factory, FileItem zip, List<FileItem> fileItems)
        throws IOException {
    ZipInputStream ziS = new ZipInputStream(zip.getInputStream());
    ZipEntry zE;/*w  w w  . j a v  a 2  s  .c  om*/
    try {
        while (null != (zE = ziS.getNextEntry())) {
            if (zE.isDirectory() || !uploadFileNameFilter.accept(null, zE.getName()))
                continue;
            FileItem item = factory.createItem(null, null, false, zE.getName());
            OutputStream oS = item.getOutputStream();
            IOUtils.copyLarge(ziS, oS);
            oS.close();
            fileItems.add(item);
        }
    } finally {
        ziS.close();
    }
}

From source file:com.hp.application.automation.tools.octane.CIJenkinsServicesImpl.java

private List<ParameterValue> createParameters(AbstractProject project, JSONArray paramsJSON) {
    List<ParameterValue> result = new ArrayList<>();
    boolean parameterHandled;
    ParameterValue tmpValue;/*from  w  ww. ja  va  2 s . c  om*/
    ParametersDefinitionProperty paramsDefProperty = (ParametersDefinitionProperty) project
            .getProperty(ParametersDefinitionProperty.class);
    if (paramsDefProperty != null) {
        for (ParameterDefinition paramDef : paramsDefProperty.getParameterDefinitions()) {
            parameterHandled = false;
            for (int i = 0; i < paramsJSON.size(); i++) {
                JSONObject paramJSON = paramsJSON.getJSONObject(i);
                if (paramJSON.has("name") && paramJSON.get("name") != null
                        && paramJSON.get("name").equals(paramDef.getName())) {
                    tmpValue = null;
                    switch (CIParameterType.fromValue(paramJSON.getString("type"))) {
                    case FILE:
                        try {
                            FileItemFactory fif = new DiskFileItemFactory();
                            FileItem fi = fif.createItem(paramJSON.getString("name"), "text/plain", false,
                                    paramJSON.getString("file"));
                            fi.getOutputStream()
                                    .write(DatatypeConverter.parseBase64Binary(paramJSON.getString("value")));
                            tmpValue = new FileParameterValue(paramJSON.getString("name"), fi);
                        } catch (IOException ioe) {
                            logger.warn("failed to process file parameter", ioe);
                        }
                        break;
                    case NUMBER:
                        tmpValue = new StringParameterValue(paramJSON.getString("name"),
                                paramJSON.get("value").toString());
                        break;
                    case STRING:
                        tmpValue = new StringParameterValue(paramJSON.getString("name"),
                                paramJSON.getString("value"));
                        break;
                    case BOOLEAN:
                        tmpValue = new BooleanParameterValue(paramJSON.getString("name"),
                                paramJSON.getBoolean("value"));
                        break;
                    case PASSWORD:
                        tmpValue = new PasswordParameterValue(paramJSON.getString("name"),
                                paramJSON.getString("value"));
                        break;
                    default:
                        break;
                    }
                    if (tmpValue != null) {
                        result.add(tmpValue);
                        parameterHandled = true;
                    }
                    break;
                }
            }
            if (!parameterHandled) {
                if (paramDef instanceof FileParameterDefinition) {
                    FileItemFactory fif = new DiskFileItemFactory();
                    FileItem fi = fif.createItem(paramDef.getName(), "text/plain", false, "");
                    try {
                        fi.getOutputStream().write(new byte[0]);
                    } catch (IOException ioe) {
                        logger.error("failed to create default value for file parameter '" + paramDef.getName()
                                + "'", ioe);
                    }
                    tmpValue = new FileParameterValue(paramDef.getName(), fi);
                    result.add(tmpValue);
                } else {
                    result.add(paramDef.getDefaultParameterValue());
                }
            }
        }
    }
    return result;
}

From source file:com.microfocus.application.automation.tools.octane.CIJenkinsServicesImpl.java

private List<ParameterValue> createParameters(Job project, CIParameters ciParameters) {
    List<ParameterValue> result = new ArrayList<>();
    boolean parameterHandled;
    ParameterValue tmpValue;//from   w w  w .j a v  a 2  s .  c o m
    ParametersDefinitionProperty paramsDefProperty = (ParametersDefinitionProperty) project
            .getProperty(ParametersDefinitionProperty.class);
    if (paramsDefProperty != null) {
        Map<String, CIParameter> ciParametersMap = ciParameters.getParameters().stream()
                .collect(Collectors.toMap(CIParameter::getName, Function.identity()));
        for (ParameterDefinition paramDef : paramsDefProperty.getParameterDefinitions()) {
            parameterHandled = false;
            CIParameter ciParameter = ciParametersMap.remove(paramDef.getName());
            if (ciParameter != null) {
                tmpValue = null;
                switch (ciParameter.getType()) {
                case FILE:
                    try {
                        FileItemFactory fif = new DiskFileItemFactory();
                        FileItem fi = fif.createItem(ciParameter.getName(), "text/plain", false,
                                UUID.randomUUID().toString());
                        fi.getOutputStream()
                                .write(DatatypeConverter.parseBase64Binary(ciParameter.getValue().toString()));
                        tmpValue = new FileParameterValue(ciParameter.getName(), fi);
                    } catch (IOException ioe) {
                        logger.warn("failed to process file parameter", ioe);
                    }
                    break;
                case NUMBER:
                    tmpValue = new StringParameterValue(ciParameter.getName(),
                            ciParameter.getValue().toString());
                    break;
                case STRING:
                    tmpValue = new StringParameterValue(ciParameter.getName(),
                            ciParameter.getValue().toString());
                    break;
                case BOOLEAN:
                    tmpValue = new BooleanParameterValue(ciParameter.getName(),
                            Boolean.parseBoolean(ciParameter.getValue().toString()));
                    break;
                case PASSWORD:
                    tmpValue = new PasswordParameterValue(ciParameter.getName(),
                            ciParameter.getValue().toString());
                    break;
                default:
                    break;
                }
                if (tmpValue != null) {
                    result.add(tmpValue);
                    parameterHandled = true;
                }
            }
            if (!parameterHandled) {
                if (paramDef instanceof FileParameterDefinition) {
                    FileItemFactory fif = new DiskFileItemFactory();
                    FileItem fi = fif.createItem(paramDef.getName(), "text/plain", false, "");
                    try {
                        fi.getOutputStream().write(new byte[0]);
                    } catch (IOException ioe) {
                        logger.error("failed to create default value for file parameter '" + paramDef.getName()
                                + "'", ioe);
                    }
                    tmpValue = new FileParameterValue(paramDef.getName(), fi);
                    result.add(tmpValue);
                } else {
                    result.add(paramDef.getDefaultParameterValue());
                }
            }
        }

        //add parameters that are not defined in job
        for (CIParameter notDefinedParameter : ciParametersMap.values()) {
            tmpValue = new StringParameterValue(notDefinedParameter.getName(),
                    notDefinedParameter.getValue().toString());
            result.add(tmpValue);
        }
    }
    return result;
}

From source file:hoot.services.controllers.ingest.FileUploadResourceTest.java

@Ignore
@Test/* www .jav a 2 s . c o m*/
@Category(UnitTest.class)
public void TestserializeUploadedFiles() throws Exception {
    FileUploadResource res = new FileUploadResource();

    //homeFolder + "/upload/" + jobId + "/" + relPath;
    // Create dummy FGDB

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

    List<FileItem> fileItemsList = new ArrayList<FileItem>();

    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();

    OutputStream os = item.getOutputStream();
    os.write(testFieldValueBytes);
    os.close();

    File out = new File(wkdirpath + "/buffer.tmp");
    item.write(out);
    fileItemsList.add(item);
    org.junit.Assert.assertTrue(out.exists());

    /*
        Map<String,String> uploadedFiles = new HashMap<String, String>();
        Map<String,String> uploadedFilesPaths = new HashMap<String, String>();
            
          res._serializeUploadedFiles(fileItemsList, jobId,
    uploadedFiles, uploadedFilesPaths, wkdirpath);
            
          org.junit.Assert.assertEquals("OSM", uploadedFiles.get("dummy1"));
          org.junit.Assert.assertEquals("dummy1.osm", uploadedFilesPaths.get("dummy1"));
            
            
            
          File content = new File(wkdirpath + "/dummy1.osm");
          org.junit.Assert.assertTrue(content.exists());
            
          FileUtils.forceDelete(workingDir);*/
}

From source file:hoot.services.controllers.ingest.FileUploadResourceTest.java

@Ignore
@Test/*w ww. j a v  a 2  s  .com*/
@Category(UnitTest.class)
public void TestserializeFGDB() throws Exception {
    FileUploadResource res = new FileUploadResource();

    //homeFolder + "/upload/" + jobId + "/" + relPath;
    // Create dummy FGDB

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

    List<FileItem> fileItemsList = new ArrayList<FileItem>();

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

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

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

    OutputStream os = item.getOutputStream();
    os.write(testFieldValueBytes);
    os.close();

    File out = new File(wkdirpath + "/buffer.tmp");
    item.write(out);
    fileItemsList.add(item);
    org.junit.Assert.assertTrue(out.exists());

    /*
        Map<String,String> uploadedFiles = new HashMap<String, String>();
        Map<String,String> uploadedFilesPaths = new HashMap<String, String>();
            
          res._serializeFGDB(fileItemsList, jobId,
    uploadedFiles, uploadedFilesPaths );
            
          org.junit.Assert.assertEquals("GDB", uploadedFiles.get("fgdbTest"));
          org.junit.Assert.assertEquals("fgdbTest.gdb", uploadedFilesPaths.get("fgdbTest"));
            
          File fgdbpath = new File(wkdirpath + "/fgdbTest.gdb");
          org.junit.Assert.assertTrue(fgdbpath.exists());
            
          File content = new File(wkdirpath + "/fgdbTest.gdb/dummy1.gdbtable");
          org.junit.Assert.assertTrue(content.exists());
            
          FileUtils.forceDelete(workingDir);*/
}

From source file:org.apache.hupa.server.preferences.InImapUserPreferencesStorage.java

/**
 * Right now, using the same approach present in upload attachments to create the attachment
 *///from  www .  ja  va2  s.  c om
private static FileItem createPreferencesFileItem(InputStream is, String filename, String contentType)
        throws IOException {
    FileItemFactory f = new DiskFileItemFactory();
    FileItem item = f.createItem(filename, contentType, false, filename);
    UploadServlet.copyFromInputStreamToOutputStream(is, item.getOutputStream());
    return item;
}