Example usage for org.apache.commons.io FileUtils writeByteArrayToFile

List of usage examples for org.apache.commons.io FileUtils writeByteArrayToFile

Introduction

In this page you can find the example usage for org.apache.commons.io FileUtils writeByteArrayToFile.

Prototype

public static void writeByteArrayToFile(File file, byte[] data) throws IOException 

Source Link

Document

Writes a byte array to a file creating the file if it does not exist.

Usage

From source file:org.apache.maven.plugins.site.AuthAsyncProxyServlet.java

/** {@inheritDoc} */
public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
    final HttpServletRequest request = (HttpServletRequest) req;
    final HttpServletResponse response = (HttpServletResponse) res;

    log.info("handle ");

    if (this.authentications != null && !this.authentications.isEmpty()) {
        String proxyAuthorization = request.getHeader("Proxy-Authorization");
        if (proxyAuthorization != null && proxyAuthorization.startsWith("Basic ")) {
            String proxyAuth = proxyAuthorization.substring(6);
            String authorization = B64Code.decode(proxyAuth);
            String[] authTokens = authorization.split(":");
            String user = authTokens[0];
            String password = authTokens[1];

            if (this.authentications.get(user) == null) {
                throw new IllegalArgumentException(user + " not found in the map!");
            }//w ww. j a  v  a2 s  .c  o m

            if (sleepTime > 0) {
                try {
                    Thread.sleep(sleepTime);
                } catch (InterruptedException e) {
                    // nop
                }
            }
            String authPass = this.authentications.get(user);
            if (password.equals(authPass)) {
                String targetPath = request.getServletPath();

                HttpRequest rq = new HttpRequest();
                rq.method = request.getMethod();
                rq.path = targetPath;

                @SuppressWarnings("rawtypes")
                Enumeration headerNames = request.getHeaderNames();
                while (headerNames.hasMoreElements()) {
                    String name = (String) headerNames.nextElement();
                    rq.headers.put(name, request.getHeader(name));
                }

                httpRequests.add(rq);

                if (request.getMethod().equalsIgnoreCase("PUT") && targetPath != null) {
                    File targetFile = new File(siteTargetPath, targetPath);
                    log.info("writing file " + targetFile.getPath());
                    FileUtils.writeByteArrayToFile(targetFile, IOUtils.toByteArray(request.getInputStream()));
                }

                //PrintWriter writer = response.getWriter();

                response.setStatus(HttpServletResponse.SC_OK);
                return;
            }
        }

        // Proxy-Authenticate Basic realm="CCProxy Authorization"
        response.addHeader("Proxy-Authenticate", "Basic realm=\"Jetty Proxy Authorization\"");
        response.setStatus(HttpServletResponse.SC_PROXY_AUTHENTICATION_REQUIRED);
        return;
    }

    super.service(req, res);
}

From source file:org.apache.maven.plugins.site.deploy.SimpleDavServerHandler.java

public SimpleDavServerHandler(final File targetPath) throws Exception {
    this.siteTargetPath = targetPath;
    Handler repoHandler = new AbstractHandler() {
        public void handle(String target, HttpServletRequest request, HttpServletResponse response,
                int dispatch) throws IOException, ServletException {
            String targetPath = request.getPathInfo();

            HttpRequest rq = new HttpRequest();
            rq.method = request.getMethod();
            rq.path = targetPath;//from   w  w w  . jav  a  2 s . c  om

            @SuppressWarnings("rawtypes")
            Enumeration headerNames = request.getHeaderNames();
            while (headerNames.hasMoreElements()) {
                String name = (String) headerNames.nextElement();
                rq.headers.put(name, request.getHeader(name));
            }

            httpRequests.add(rq);

            if (request.getMethod().equalsIgnoreCase("PUT")) {
                File targetFile = new File(siteTargetPath, targetPath);
                log.info("writing file " + targetFile.getPath());
                FileUtils.writeByteArrayToFile(targetFile, IOUtils.toByteArray(request.getInputStream()));
            }

            //PrintWriter writer = response.getWriter();

            response.setStatus(HttpServletResponse.SC_OK);

            ((Request) request).setHandled(true);
        }
    };
    server = new Server(0);
    server.setHandler(repoHandler);
    server.start();

}

From source file:org.apache.nutch.indexwriter.cloudsearch.CloudSearchIndexWriter.java

@Override
public void commit() throws IOException {

    // nothing to do
    if (numDocsInBatch == 0) {
        return;// w  ww . ja  v a2 s.  c o  m
    }

    // close the array
    buffer.append(']');

    LOG.info("Sending {} docs to CloudSearch", numDocsInBatch);

    byte[] bb = buffer.toString().getBytes(StandardCharsets.UTF_8);

    if (dumpBatchFilesToTemp) {
        try {
            File temp = File.createTempFile("CloudSearch_", ".json");
            FileUtils.writeByteArrayToFile(temp, bb);
            LOG.info("Wrote batch file {}", temp.getName());
        } catch (IOException e1) {
            LOG.error("Exception while generating batch file", e1);
        } finally {
            // reset buffer and doc counter
            buffer = new StringBuffer(MAX_SIZE_BATCH_BYTES).append('[');
            numDocsInBatch = 0;
        }
        return;
    }
    // not in debug mode
    try (InputStream inputStream = new ByteArrayInputStream(bb)) {
        UploadDocumentsRequest batch = new UploadDocumentsRequest();
        batch.setContentLength((long) bb.length);
        batch.setContentType(ContentType.Applicationjson);
        batch.setDocuments(inputStream);
        UploadDocumentsResult result = client.uploadDocuments(batch);
    } catch (Exception e) {
        LOG.error("Exception while sending batch", e);
        LOG.error(buffer.toString());
    } finally {
        // reset buffer and doc counter
        buffer = new StringBuffer(MAX_SIZE_BATCH_BYTES).append('[');
        numDocsInBatch = 0;
    }
}

From source file:org.apache.oodt.config.distributed.DistributedConfigurationManager.java

private void saveFile(String path, byte[] data) throws IOException {
    String localFilePath = ConfigUtils.fixForComponentHome(component, path);
    File localFile = new File(localFilePath);
    if (localFile.exists() && localFile.delete()) {
        logger.warn("Deleted already existing file at {} before writing new content", localFilePath);
    }/* w  ww. ja  v a2 s. c  o  m*/

    logger.debug("Storing configuration in file: {}", localFilePath);
    FileUtils.writeByteArrayToFile(localFile, data);
    logger.info("File from ZNode at {} saved to {}", path, localFilePath);
    savedFiles.add(localFilePath);
}

From source file:org.apache.solr.cloud.ZkCLI.java

/**
 * Allows you to perform a variety of zookeeper related tasks, such as:
 * //from w  w w .  j  av a 2  s  .  c  o  m
 * Bootstrap the current configs for all collections in solr.xml.
 * 
 * Upload a named config set from a given directory.
 * 
 * Link a named config set explicity to a collection.
 * 
 * Clear ZooKeeper info.
 * 
 * If you also pass a solrPort, it will be used to start an embedded zk useful
 * for single machine, multi node tests.
 */
public static void main(String[] args) throws InterruptedException, TimeoutException, IOException,
        ParserConfigurationException, SAXException, KeeperException {

    CommandLineParser parser = new PosixParser();
    Options options = new Options();

    options.addOption(OptionBuilder.hasArg(true)
            .withDescription("cmd to run: " + BOOTSTRAP + ", " + UPCONFIG + ", " + DOWNCONFIG + ", "
                    + LINKCONFIG + ", " + MAKEPATH + ", " + PUT + ", " + PUT_FILE + "," + GET + "," + GET_FILE
                    + ", " + LIST + ", " + CLEAR + ", " + UPDATEACLS)
            .create(CMD));

    Option zkHostOption = new Option("z", ZKHOST, true, "ZooKeeper host address");
    options.addOption(zkHostOption);
    Option solrHomeOption = new Option("s", SOLRHOME, true,
            "for " + BOOTSTRAP + ", " + RUNZK + ": solrhome location");
    options.addOption(zkHostOption);
    options.addOption(solrHomeOption);

    options.addOption("d", CONFDIR, true, "for " + UPCONFIG + ": a directory of configuration files");
    options.addOption("n", CONFNAME, true, "for " + UPCONFIG + ", " + LINKCONFIG + ": name of the config set");

    options.addOption("c", COLLECTION, true, "for " + LINKCONFIG + ": name of the collection");

    options.addOption(EXCLUDE_REGEX_SHORT, EXCLUDE_REGEX, true,
            "for " + UPCONFIG + ": files matching this regular expression won't be uploaded");

    options.addOption("r", RUNZK, true,
            "run zk internally by passing the solr run port - only for clusters on one machine (tests, dev)");

    options.addOption("h", HELP, false, "bring up this help page");
    options.addOption(NAME, true, "name of the cluster property to set");
    options.addOption(VALUE_LONG, true, "value of the cluster to set");

    try {
        // parse the command line arguments
        CommandLine line = parser.parse(options, args);

        if (line.hasOption(HELP) || !line.hasOption(ZKHOST) || !line.hasOption(CMD)) {
            // automatically generate the help statement
            HelpFormatter formatter = new HelpFormatter();
            formatter.printHelp(ZK_CLI_NAME, options);
            System.out.println("Examples:");
            System.out.println(
                    "zkcli.sh -zkhost localhost:9983 -cmd " + BOOTSTRAP + " -" + SOLRHOME + " /opt/solr");
            System.out.println("zkcli.sh -zkhost localhost:9983 -cmd " + UPCONFIG + " -" + CONFDIR
                    + " /opt/solr/collection1/conf" + " -" + CONFNAME + " myconf");
            System.out.println("zkcli.sh -zkhost localhost:9983 -cmd " + DOWNCONFIG + " -" + CONFDIR
                    + " /opt/solr/collection1/conf" + " -" + CONFNAME + " myconf");
            System.out.println("zkcli.sh -zkhost localhost:9983 -cmd " + LINKCONFIG + " -" + COLLECTION
                    + " collection1" + " -" + CONFNAME + " myconf");
            System.out.println("zkcli.sh -zkhost localhost:9983 -cmd " + MAKEPATH + " /apache/solr");
            System.out.println("zkcli.sh -zkhost localhost:9983 -cmd " + PUT + " /solr.conf 'conf data'");
            System.out.println("zkcli.sh -zkhost localhost:9983 -cmd " + PUT_FILE
                    + " /solr.xml /User/myuser/solr/solr.xml");
            System.out.println("zkcli.sh -zkhost localhost:9983 -cmd " + GET + " /solr.xml");
            System.out.println("zkcli.sh -zkhost localhost:9983 -cmd " + GET_FILE + " /solr.xml solr.xml.file");
            System.out.println("zkcli.sh -zkhost localhost:9983 -cmd " + CLEAR + " /solr");
            System.out.println("zkcli.sh -zkhost localhost:9983 -cmd " + LIST);
            System.out.println("zkcli.sh -zkhost localhost:9983 -cmd " + CLUSTERPROP + " -" + NAME
                    + " urlScheme -" + VALUE_LONG + " https");
            System.out.println("zkcli.sh -zkhost localhost:9983 -cmd " + UPDATEACLS + " /solr");
            return;
        }

        // start up a tmp zk server first
        String zkServerAddress = line.getOptionValue(ZKHOST);
        String solrHome = line.getOptionValue(SOLRHOME);

        String solrPort = null;
        if (line.hasOption(RUNZK)) {
            if (!line.hasOption(SOLRHOME)) {
                System.out.println("-" + SOLRHOME + " is required for " + RUNZK);
                System.exit(1);
            }
            solrPort = line.getOptionValue(RUNZK);
        }

        SolrZkServer zkServer = null;
        if (solrPort != null) {
            zkServer = new SolrZkServer("true", null, solrHome + "/zoo_data", solrHome,
                    Integer.parseInt(solrPort));
            zkServer.parseConfig();
            zkServer.start();
        }
        SolrZkClient zkClient = null;
        try {
            zkClient = new SolrZkClient(zkServerAddress, 30000, 30000, () -> {
            });

            if (line.getOptionValue(CMD).equalsIgnoreCase(BOOTSTRAP)) {
                if (!line.hasOption(SOLRHOME)) {
                    System.out.println("-" + SOLRHOME + " is required for " + BOOTSTRAP);
                    System.exit(1);
                }

                CoreContainer cc = new CoreContainer(solrHome);

                if (!ZkController.checkChrootPath(zkServerAddress, true)) {
                    System.out.println("A chroot was specified in zkHost but the znode doesn't exist. ");
                    System.exit(1);
                }

                ZkController.bootstrapConf(zkClient, cc, solrHome);

                // No need to close the CoreContainer, as it wasn't started
                // up in the first place...

            } else if (line.getOptionValue(CMD).equalsIgnoreCase(UPCONFIG)) {
                if (!line.hasOption(CONFDIR) || !line.hasOption(CONFNAME)) {
                    System.out.println("-" + CONFDIR + " and -" + CONFNAME + " are required for " + UPCONFIG);
                    System.exit(1);
                }
                String confDir = line.getOptionValue(CONFDIR);
                String confName = line.getOptionValue(CONFNAME);
                final String excludeExpr = line.getOptionValue(EXCLUDE_REGEX, EXCLUDE_REGEX_DEFAULT);

                if (!ZkController.checkChrootPath(zkServerAddress, true)) {
                    System.out.println("A chroot was specified in zkHost but the znode doesn't exist. ");
                    System.exit(1);
                }
                ZkConfigManager configManager = new ZkConfigManager(zkClient);
                final Pattern excludePattern = Pattern.compile(excludeExpr);
                configManager.uploadConfigDir(Paths.get(confDir), confName, excludePattern);
            } else if (line.getOptionValue(CMD).equalsIgnoreCase(DOWNCONFIG)) {
                if (!line.hasOption(CONFDIR) || !line.hasOption(CONFNAME)) {
                    System.out.println("-" + CONFDIR + " and -" + CONFNAME + " are required for " + DOWNCONFIG);
                    System.exit(1);
                }
                String confDir = line.getOptionValue(CONFDIR);
                String confName = line.getOptionValue(CONFNAME);
                ZkConfigManager configManager = new ZkConfigManager(zkClient);
                configManager.downloadConfigDir(confName, Paths.get(confDir));
            } else if (line.getOptionValue(CMD).equalsIgnoreCase(LINKCONFIG)) {
                if (!line.hasOption(COLLECTION) || !line.hasOption(CONFNAME)) {
                    System.out.println(
                            "-" + COLLECTION + " and -" + CONFNAME + " are required for " + LINKCONFIG);
                    System.exit(1);
                }
                String collection = line.getOptionValue(COLLECTION);
                String confName = line.getOptionValue(CONFNAME);

                ZkController.linkConfSet(zkClient, collection, confName);
            } else if (line.getOptionValue(CMD).equalsIgnoreCase(LIST)) {
                zkClient.printLayoutToStdOut();
            } else if (line.getOptionValue(CMD).equalsIgnoreCase(CLEAR)) {
                List arglist = line.getArgList();
                if (arglist.size() != 1) {
                    System.out.println("-" + CLEAR + " requires one arg - the path to clear");
                    System.exit(1);
                }
                zkClient.clean(arglist.get(0).toString());
            } else if (line.getOptionValue(CMD).equalsIgnoreCase(MAKEPATH)) {
                List arglist = line.getArgList();
                if (arglist.size() != 1) {
                    System.out.println("-" + MAKEPATH + " requires one arg - the path to make");
                    System.exit(1);
                }
                zkClient.makePath(arglist.get(0).toString(), true);
            } else if (line.getOptionValue(CMD).equalsIgnoreCase(PUT)) {
                List arglist = line.getArgList();
                if (arglist.size() != 2) {
                    System.out
                            .println("-" + PUT + " requires two args - the path to create and the data string");
                    System.exit(1);
                }
                String path = arglist.get(0).toString();
                if (zkClient.exists(path, true)) {
                    zkClient.setData(path, arglist.get(1).toString().getBytes(StandardCharsets.UTF_8), true);
                } else {
                    zkClient.create(path, arglist.get(1).toString().getBytes(StandardCharsets.UTF_8),
                            CreateMode.PERSISTENT, true);
                }
            } else if (line.getOptionValue(CMD).equalsIgnoreCase(PUT_FILE)) {
                List arglist = line.getArgList();
                if (arglist.size() != 2) {
                    System.out.println("-" + PUT_FILE
                            + " requires two args - the path to create in ZK and the path to the local file");
                    System.exit(1);
                }

                String path = arglist.get(0).toString();
                InputStream is = new FileInputStream(arglist.get(1).toString());
                try {
                    if (zkClient.exists(path, true)) {
                        zkClient.setData(path, IOUtils.toByteArray(is), true);
                    } else {
                        zkClient.create(path, IOUtils.toByteArray(is), CreateMode.PERSISTENT, true);
                    }
                } finally {
                    IOUtils.closeQuietly(is);
                }

            } else if (line.getOptionValue(CMD).equalsIgnoreCase(GET)) {
                List arglist = line.getArgList();
                if (arglist.size() != 1) {
                    System.out.println("-" + GET + " requires one arg - the path to get");
                    System.exit(1);
                }
                byte[] data = zkClient.getData(arglist.get(0).toString(), null, null, true);
                System.out.println(new String(data, StandardCharsets.UTF_8));
            } else if (line.getOptionValue(CMD).equalsIgnoreCase(GET_FILE)) {
                List arglist = line.getArgList();
                if (arglist.size() != 2) {
                    System.out.println(
                            "-" + GET_FILE + "requires two args - the path to get and the file to save it to");
                    System.exit(1);
                }
                byte[] data = zkClient.getData(arglist.get(0).toString(), null, null, true);
                FileUtils.writeByteArrayToFile(new File(arglist.get(1).toString()), data);
            } else if (line.getOptionValue(CMD).equals(UPDATEACLS)) {
                List arglist = line.getArgList();
                if (arglist.size() != 1) {
                    System.out.println("-" + UPDATEACLS + " requires one arg - the path to update");
                    System.exit(1);
                }
                zkClient.updateACLs(arglist.get(0).toString());
            } else if (line.getOptionValue(CMD).equalsIgnoreCase(CLUSTERPROP)) {
                if (!line.hasOption(NAME)) {
                    System.out.println("-" + NAME + " is required for " + CLUSTERPROP);
                }
                String propertyName = line.getOptionValue(NAME);
                //If -val option is missing, we will use the null value. This is required to maintain
                //compatibility with Collections API.
                String propertyValue = line.getOptionValue(VALUE_LONG);
                ClusterProperties props = new ClusterProperties(zkClient);
                try {
                    props.setClusterProperty(propertyName, propertyValue);
                } catch (IOException ex) {
                    System.out.println("Unable to set the cluster property due to following error : "
                            + ex.getLocalizedMessage());
                    System.exit(1);
                }
            } else {
                // If not cmd matches
                System.out.println("Unknown command " + line.getOptionValue(CMD) + ". Use -h to get help.");
                System.exit(1);
            }
        } finally {
            if (solrPort != null) {
                zkServer.stop();
            }
            if (zkClient != null) {
                zkClient.close();
            }
        }
    } catch (ParseException exp) {
        System.out.println("Unexpected exception:" + exp.getMessage());
    }

}

From source file:org.apache.solr.handler.admin.EditFileRequestHandler.java

private boolean testReloadSuccess(SolrQueryRequest req, SolrQueryResponse rsp) {
    // Try writing the config to a temporary core and reloading to see that we don't allow people to shoot themselves
    // in the foot.
    File home = null;/* w  w  w.  j  a v a 2 s. c  o  m*/
    try {
        home = new File(FileUtils.getTempDirectory(), "SOLR_5459"); // Unlikely to name a core or collection this!
        FileUtils.writeStringToFile(new File(home, "solr.xml"), "<solr></solr>", "UTF-8"); // Use auto-discovery
        File coll = new File(home, "SOLR_5459");

        SolrCore core = req.getCore();
        CoreDescriptor desc = core.getCoreDescriptor();
        CoreContainer coreContainer = desc.getCoreContainer();

        if (coreContainer.isZooKeeperAware()) {
            try {
                String confPath = ((ZkSolrResourceLoader) core.getResourceLoader()).getCollectionZkPath();

                ZkController.downloadConfigDir(coreContainer.getZkController().getZkClient(), confPath,
                        new File(coll, "conf"));
            } catch (Exception ex) {
                log.error("Error when attempting to download conf from ZooKeeper: " + ex.getMessage());
                rsp.setException(new SolrException(ErrorCode.BAD_REQUEST,
                        "Error when attempting to download conf from ZooKeeper" + ex.getMessage()));
                return false;
            }
        } else {
            FileUtils.copyDirectory(new File(desc.getInstanceDir(), "conf"), new File(coll, "conf"));
        }

        FileUtils.writeStringToFile(new File(coll, "core.properties"), "name=SOLR_5459", "UTF-8");

        FileUtils.writeByteArrayToFile(new File(new File(coll, "conf"), req.getParams().get("file", null)),
                data);

        return tryReloading(rsp, home);

    } catch (IOException ex) {
        log.warn("Caught IO exception when trying to verify configs. " + ex.getMessage());
        rsp.setException(new SolrException(ErrorCode.SERVER_ERROR,
                "Caught IO exception when trying to verify configs. " + ex.getMessage()));
        return false;
    }
}

From source file:org.apache.spark.io.GenericFileInputStreamSuite.java

@Before
public void setUp() throws IOException {
    // Create a byte array of size 2 MB with random bytes
    randomBytes = RandomUtils.nextBytes(2 * 1024 * 1024);
    inputFile = File.createTempFile("temp-file", ".tmp");
    FileUtils.writeByteArrayToFile(inputFile, randomBytes);
}

From source file:org.apache.storm.daemon.nimbus.NimbusUtils.java

@ClojureClass(className = "backtype.storm.daemon.nimbus#setup-storm-code")
public static void setupStormCode(Map<Object, Object> conf, String topologyId, String tmpJarLocation,
        Map<Object, Object> stormConf, StormTopology topology) throws IOException {
    String stormroot = ConfigUtil.masterStormdistRoot(conf, topologyId);
    FileUtils.forceMkdir(new File(stormroot));
    FileUtils.cleanDirectory(new File(stormroot));
    setupJar(conf, tmpJarLocation, stormroot);
    FileUtils.writeByteArrayToFile(new File(ConfigUtil.masterStormcodePath(stormroot)),
            Utils.serialize(topology));/*  ww  w . ja  v a  2 s  . c  o  m*/
    FileUtils.writeByteArrayToFile(new File(ConfigUtil.masterStormconfPath(stormroot)),
            Utils.serialize(stormConf));
}

From source file:org.apache.storm.utils.LocalState.java

private void persistInternal(Map<String, ThriftSerializedObject> serialized, TSerializer ser, boolean cleanup) {
    try {//from w w w  .  j  a  v a 2s  .  c  om
        if (ser == null) {
            ser = new TSerializer();
        }
        byte[] toWrite = ser.serialize(new LocalStateData(serialized));

        String newPath = _vs.createVersion();
        File file = new File(newPath);
        FileUtils.writeByteArrayToFile(file, toWrite);
        if (toWrite.length != file.length()) {
            throw new IOException("Tried to serialize " + toWrite.length + " bytes to "
                    + file.getCanonicalPath() + ", but " + file.length() + " bytes were written.");
        }
        _vs.succeedVersion(newPath);
        if (cleanup)
            _vs.cleanup(4);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:org.apache.taverna.scufl2.ucfpackage.TestUCFPackage.java

@Test
public void doubleSaveOutputStream() throws Exception {
    ByteArrayOutputStream outStream = new ByteArrayOutputStream();
    UCFPackage container = new UCFPackage();
    container.setPackageMediaType(UCFPackage.MIME_WORKFLOW_BUNDLE);
    container.addResource("Hello there ", "helloworld.txt", "text/plain");
    container.save(outStream);//  ww w . j  a va 2s  .co m
    container.addResource("Hello again", "again.txt", "text/plain");
    outStream.close();

    outStream = new ByteArrayOutputStream();
    container.save(outStream);
    outStream.close();

    FileUtils.writeByteArrayToFile(tmpFile, outStream.toByteArray());
    ZipFile zipFile = new ZipFile(tmpFile);
    assertNotNull(zipFile.getEntry("helloworld.txt"));
    assertNotNull(zipFile.getEntry("again.txt"));
}