Example usage for org.apache.commons.io IOUtils write

List of usage examples for org.apache.commons.io IOUtils write

Introduction

In this page you can find the example usage for org.apache.commons.io IOUtils write.

Prototype

public static void write(StringBuffer data, OutputStream output, String encoding) throws IOException 

Source Link

Document

Writes chars from a StringBuffer to bytes on an OutputStream using the specified character encoding.

Usage

From source file:de.tu.darmstadt.lt.ner.writer.SentenceToCRFTestFileWriter.java

@Override
public void process(JCas jcas) throws AnalysisEngineProcessException {
    try {//from   ww w.  j a v a 2  s  .c  om
        StringBuilder sb = new StringBuilder();
        int index = 0;

        for (String l : sentences) {
            Sentence sentence = new Sentence(jcas, index, l.length() + index);
            sentence.addToIndexes();
            index = index + l.length() + 1;
            sb.append(l + "\n");
        }

        jcas.setDocumentText(sb.toString().trim());

        //TODO replace this segmenter with open source segmenter, if applications like NoD is
        // to use this. OR
        // use the pre-gscl release
        /*    AnalysisEngine pipeline = createEngine(OpenNlpSegmenter.class,
            OpenNlpSegmenter.PARAM_LANGUAGE, crfTestFileLanguage, OpenNlpSegmenter.PARAM_WRITE_SENTENCE,
            false);
            pipeline.process(jcas);*/

        // get the token from jcas and convert it to CRF test file format. one token per line,
        // with
        // out gold.
        StringBuilder sbCRF = new StringBuilder();

        Map<Sentence, Collection<Token>> sentencesTokens = JCasUtil.indexCovered(jcas, Sentence.class,
                Token.class);
        List<Sentence> sentences = new ArrayList<Sentence>(sentencesTokens.keySet());
        // sort sentences by sentence
        Collections.sort(sentences, new Comparator<Sentence>() {
            @Override
            public int compare(Sentence arg0, Sentence arg1) {
                return arg0.getBegin() - arg1.getBegin();
            }
        });

        for (Sentence sentence : sentences) {
            for (Token token : sentencesTokens.get(sentence)) {
                sbCRF.append(token.getCoveredText() + LF);
            }
            sbCRF.append(LF);
        }

        IOUtils.write(sbCRF.toString(), new FileOutputStream(crfFileName), "UTF-8");
    } catch (Exception e) {
        System.out.println(e);
    }
}

From source file:dk.itst.oiosaml.sp.service.util.HttpSOAPClient.java

public Envelope wsCall(String location, String username, String password, boolean ignoreCertPath, String xml,
        String soapAction) throws IOException, SOAPException {
    URI serviceLocation;//from w w  w  . j a va 2  s  .com
    try {
        serviceLocation = new URI(location);
    } catch (URISyntaxException e) {
        throw new IOException("Invalid uri for artifact resolve: " + location);
    }
    if (log.isDebugEnabled())
        log.debug("serviceLocation..:" + serviceLocation);
    if (log.isDebugEnabled())
        log.debug("SOAP Request: " + xml);

    HttpURLConnection c = (HttpURLConnection) serviceLocation.toURL().openConnection();
    if (c instanceof HttpsURLConnection) {
        HttpsURLConnection sc = (HttpsURLConnection) c;

        if (ignoreCertPath) {
            sc.setSSLSocketFactory(new DummySSLSocketFactory());
            sc.setHostnameVerifier(new HostnameVerifier() {
                public boolean verify(String hostname, SSLSession session) {
                    return true;
                }
            });
        }
    }
    c.setAllowUserInteraction(false);
    c.setDoInput(true);
    c.setDoOutput(true);
    c.setFixedLengthStreamingMode(xml.getBytes("UTF-8").length);
    c.setRequestMethod("POST");
    c.setReadTimeout(20000);
    c.setConnectTimeout(30000);

    addContentTypeHeader(xml, c);
    c.addRequestProperty("SOAPAction", "\"" + (soapAction == null ? "" : soapAction) + "\"");

    if (username != null && password != null) {
        c.addRequestProperty("Authorization",
                "Basic " + Base64.encodeBytes((username + ":" + password).getBytes(), Base64.DONT_BREAK_LINES));
    }
    OutputStream outputStream = c.getOutputStream();
    IOUtils.write(xml, outputStream, "UTF-8");
    outputStream.flush();
    outputStream.close();

    if (c.getResponseCode() == 200) {
        InputStream inputStream = c.getInputStream();
        String result = IOUtils.toString(inputStream, "UTF-8");
        inputStream.close();

        if (log.isDebugEnabled())
            log.debug("Server SOAP response: " + result);
        XMLObject res = SAMLUtil.unmarshallElementFromString(result);

        Envelope envelope = (Envelope) res;
        if (SAMLUtil.getFirstElement(envelope.getBody(), Fault.class) != null) {
            log.warn(
                    "Result has soap11:Fault, but server returned 200 OK. Treating as error, please fix the server");
            throw new SOAPException(c.getResponseCode(), result);
        }
        return envelope;
    } else {
        log.debug("Response code: " + c.getResponseCode());

        InputStream inputStream = c.getErrorStream();
        String result = IOUtils.toString(inputStream, "UTF-8");
        inputStream.close();
        if (log.isDebugEnabled())
            log.debug("Server SOAP fault: " + result);

        throw new SOAPException(c.getResponseCode(), result);
    }
}

From source file:de.hybris.platform.atddimpex.keywords.ImpexKeywordLibrary.java

private OutputStream getImpExLogOutputStream(final String resourceName) throws IOException {
    if (impExLogOutputStream == null) {
        final RobotTest robotTest = robotTestContext.getCurrentRobotTest();

        if (robotTest == null) {
            throw new IllegalStateException(
                    "KeywordMethods must only be called within a valid RobotTestContext");
        } else {//from  w w w .j  a  v  a 2  s  .c o m
            final String testSuiteName = robotTest.getTestSuite().getName().replaceAll(Pattern.quote(" "), "_");

            final String impExLogPath = String.format("%s/%s/%s-data.impex", robotTestContext.getProjectName(),
                    testSuiteName, robotTest.getName());
            final File impExLogFile = new File(Config.getParameter(PROP_REPORT_PATH), impExLogPath);

            impExLogOutputStream = FileUtils.openOutputStream(impExLogFile);
        }
    } else {
        IOUtils.write(IMPEX_LOG_LINE_SEPERATOR, impExLogOutputStream, DEFAULT_ENCODING);
    }

    IOUtils.write(IMPEX_LOG_RESOURCE_BANNER, impExLogOutputStream, DEFAULT_ENCODING);
    IOUtils.write(String.format("# Import from %s %s", resourceName, IMPEX_LOG_LINE_SEPERATOR),
            impExLogOutputStream, DEFAULT_ENCODING);
    IOUtils.write(IMPEX_LOG_RESOURCE_BANNER, impExLogOutputStream, DEFAULT_ENCODING);

    impExLogOutputStream.flush();

    return impExLogOutputStream;
}

From source file:com.migo.utils.GenUtils.java

/**
 * ??//from w  ww  .  jav a2  s .c  om
 */
public static void generatorCode(Map<String, String> table, List<Map<String, String>> columns,
        ZipOutputStream zip) {
    //??
    Configuration config = getConfig();

    //?
    TableEntity tableEntity = new TableEntity();
    tableEntity.setTableName(table.get("tableName"));
    tableEntity.setComments(table.get("tableComment"));
    //????Java??
    String className = tableToJava(tableEntity.getTableName(), config.getString("tablePrefix"));
    tableEntity.setClassName(className);
    tableEntity.setClassname(StringUtils.uncapitalize(className));

    //?
    List<ColumnEntity> columsList = new ArrayList<>();
    for (Map<String, String> column : columns) {
        ColumnEntity columnEntity = new ColumnEntity();
        columnEntity.setColumnName(column.get("columnName"));
        columnEntity.setDataType(column.get("dataType"));
        columnEntity.setComments(column.get("columnComment"));
        columnEntity.setExtra(column.get("extra"));

        //????Java??
        String attrName = columnToJava(columnEntity.getColumnName());
        columnEntity.setAttrName(attrName);
        columnEntity.setAttrname(StringUtils.uncapitalize(attrName));

        //???Java
        String attrType = config.getString(columnEntity.getDataType(), "unknowType");
        columnEntity.setAttrType(attrType);

        //?
        if ("PRI".equalsIgnoreCase(column.get("columnKey")) && tableEntity.getPk() == null) {
            tableEntity.setPk(columnEntity);
        }

        columsList.add(columnEntity);
    }
    tableEntity.setColumns(columsList);

    //
    if (tableEntity.getPk() == null) {
        tableEntity.setPk(tableEntity.getColumns().get(0));
    }

    //velocity?
    Properties prop = new Properties();
    prop.put("file.resource.loader.class",
            "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
    Velocity.init(prop);

    //???
    Map<String, Object> map = new HashMap<>();
    map.put("tableName", tableEntity.getTableName());
    map.put("comments", tableEntity.getComments());
    map.put("pk", tableEntity.getPk());
    map.put("className", tableEntity.getClassName());
    map.put("classname", tableEntity.getClassname());
    map.put("pathName", tableEntity.getClassname().toLowerCase());
    map.put("columns", tableEntity.getColumns());
    map.put("package", config.getString("package"));
    map.put("author", config.getString("author"));
    map.put("email", config.getString("email"));
    map.put("datetime", DateUtils.format(new Date(), DateUtils.DATE_TIME_PATTERN));
    VelocityContext context = new VelocityContext(map);

    //??
    List<String> templates = getTemplates();
    for (String template : templates) {
        //?
        StringWriter sw = new StringWriter();
        Template tpl = Velocity.getTemplate(template, "UTF-8");
        tpl.merge(context, sw);

        try {
            //zip
            zip.putNextEntry(new ZipEntry(
                    getFileName(template, tableEntity.getClassName(), config.getString("package"))));
            IOUtils.write(sw.toString(), zip, "UTF-8");
            IOUtils.closeQuietly(sw);
            zip.closeEntry();
        } catch (IOException e) {
            throw new RRException("???" + tableEntity.getTableName(), e);
        }
    }
}

From source file:com.scaleunlimited.cascading.DatumCompilerTest.java

@Test
public void testUUID() throws Exception {
    CompiledDatum result = DatumCompiler.generate(MyUUIDDatumTemplate.class);

    File baseDir = new File("build/test/DatumCompilerTest/testUUID/");
    FileUtils.deleteDirectory(baseDir);//w w w .j a va 2s .c  o m
    File srcDir = new File(baseDir, result.getPackageName().replaceAll("\\.", "/"));
    assertTrue(srcDir.mkdirs());

    File codeFile = new File(srcDir, result.getClassName() + ".java");
    OutputStream os = new FileOutputStream(codeFile);
    IOUtils.write(result.getClassCode(), os, "UTF-8");
    os.close();

    // Compile with Janino, give it a try. We have Janino since
    // it's a cascading dependency, but probably want to add a test
    // dependency on it.

    ClassLoader cl = new JavaSourceClassLoader(this.getClass().getClassLoader(), // parentClassLoader
            new File[] { baseDir }, // optionalSourcePath
            (String) null // optionalCharacterEncoding
    );

    // WARNING - we have to use xxxDatumTemplate as the base name, so that the code returned
    // by the compiler is for type xxxDatum. Otherwise when we try to load the class here,
    // we'll likely get the base (template) class, which will mask our generated class.
    Class clazz = cl.loadClass(result.getPackageName() + "." + result.getClassName());
    assertEquals("MyUUIDDatum", clazz.getSimpleName());

    Constructor c = clazz.getConstructor(UUID.class);
    BaseDatum datum = (BaseDatum) c.newInstance(UUID.randomUUID());

    // Verify that it can be serialized with Hadoop.
    // TODO figure out why Hadoop serializations aren't available???
    /*
    BasePlatform testPlatform = new HadoopPlatform(DatumCompilerTest.class);
    Tap tap = testPlatform.makeTap( testPlatform.makeBinaryScheme(datum.getFields()), 
                testPlatform.makePath("build/test/DatumCompilerTest/testSimpleSchema/"));
    TupleEntryCollector writer = tap.openForWrite(testPlatform.makeFlowProcess());
    writer.add(datum.getTuple());
    writer.close();
            
    TupleEntryIterator iter = tap.openForRead(testPlatform.makeFlowProcess());
    TupleEntry te = iter.next();
            
    // TODO how to test round-trip?
     */
}

From source file:com.seajas.search.contender.service.modifier.ModifierFilterProcessorTest.java

/**
 * Test the modifier with asExpression = true, when applied to actual sample content.
 * //from  w  w w .j  a  va2 s .  c  om
 * @throws IOException
 */
@Test
public void testExpressionFromFile() throws IOException {
    String fileContent = IOUtils.toString(new FileInputStream("support/examples/yomiuri.html"), "Shift_JIS");

    ModifierFilterProcessor processor = new ModifierFilterProcessor();
    ModifierFilter modifierFilter = new ModifierFilter();

    modifierFilter.setId(-1);
    modifierFilter.setModifierId(-1);
    modifierFilter.setFragmentStart("<body [^>]*>");
    modifierFilter.setFragmentEnd("<!--// article_start //-->");
    modifierFilter.setIsExpression(true);

    Reader result = processor.process(modifierFilter, new StringReader(fileContent));

    StringBuffer buffer = new StringBuffer();

    for (int c; (c = result.read()) != -1;)
        buffer.append((char) c);

    // Expected result

    IOUtils.write(buffer, new FileOutputStream("support/examples/yomiuri.actual.html"), "Shift_JIS");

    assertEquals(buffer.toString(),
            IOUtils.toString(new FileInputStream("support/examples/yomiuri.expected.html"), "Shift_JIS"));
}

From source file:edu.cornell.library.scholars.webapp.controller.api.distribute.file.SelectingFileDistributor.java

@Override
public void writeOutput(OutputStream output) throws DataDistributorException {
    try {/*from   w  ww  . ja v  a 2 s  .c o m*/
        File file = fileFinder.find();
        if (file != null && file.isFile()) {
            IOUtils.copy(new FileInputStream(file), output);
            return;
        } else {
            IOUtils.write(emptyResponse, output, "UTF-8");
        }
    } catch (IOException e) {
        throw new DataDistributorException(e);
    }
}

From source file:de.micromata.genome.gwiki.tools.PatchVersion.java

public void writeFile(String fname, String content) {
    try {/*from  ww  w .j  a  v  a 2 s. c  o m*/
        FileOutputStream fout = new FileOutputStream(new File(fname));
        IOUtils.write(content, fout, "UTF-8");
        IOUtils.closeQuietly(fout);
    } catch (IOException ex) {
        throw new RuntimeIOException(ex);
    }
}

From source file:io.inkstand.jcr.util.JCRContentLoaderTest.java

/**
 * This tests implements an exploit to the XML External Entity Attack {@see http://www.ws-attacks.org/index.php/XML_Entity_Reference_Attack}.
 * The attack targets a file in the filesystem containing a secret, i.e. a password, configurations, etc.
 * The attacking file defines an entity that resolves
 * to the file containing the secret. The entity (&amp;xxe;) is used in the xml file and will be resolved
 * to provide the title of the test node. If the code is not vulnerable, the attack will fail.
 *
 * @throws Throwable//  w w w.j  a va2  s. com
 */
@Test(expected = InkstandRuntimeException.class)
public void testLoadContent_ExternalitEntityAttack_notVulnerable() throws Throwable {
    //prepare
    //the attacked file containing the secret
    final File attackedFile = folder.newFile("attackedFile.txt");
    try (FileOutputStream fos = new FileOutputStream(attackedFile)) {
        //the lead-padding of 4-chars is ignored for some mysterious reasons...
        IOUtils.write("    secretContent", fos, Charset.forName("UTF-8"));
    }
    //as attacker file we use a template and replacing a %s placeholder with the url of the attacked file
    //in a real-world attack we would use a valuable target such as /etc/passwd
    final File attackerFile = folder.newFile("attackerFile.xml");

    //load the template file from the classpath
    try (InputStream is = getClass().getResourceAsStream("test01_inkstandJcrImport_v1-0_xxe-attack.xml");
            FileOutputStream fos = new FileOutputStream(attackerFile)) {

        final String attackerContent = prepareAttackerContent(is, attackedFile);
        IOUtils.write(attackerContent, fos);
    }
    final Session actSession = repository.login("admin", "admin");

    //act
    //when the code is not vulnerable, the following call will cause a runtime exception
    //as the dtd processing of external entities is not allowed.
    subject.loadContent(actSession, attackerFile.toURI().toURL());

    //assert
    //the content from the attacked file is inserted as test title and in the repository
    //if the code would be vulnerable, the following lines would succeed
    final Session verifySession = repository.getRepository().login();
    final Node root = verifySession.getNode("/root");
    assertStringPropertyEquals(root, "jcr:title", "secretContent");
    throw new AssertionError("Code is vulnerable to XXE attack");
}

From source file:com.digitalpebble.stormcrawler.elasticsearch.util.URLExtractor.java

private final void queryES() throws IOException {
    int maxBufferSize = 100;

    SearchResponse scrollResp = client.prepareSearch(this.indexName).setTypes(this.docType)
            .setScroll(new TimeValue(60000)).setQuery(QueryBuilders.matchAllQuery()).setSize(maxBufferSize)
            .execute().actionGet();/*from  w  w w .jav  a 2 s.c  o m*/

    long total = scrollResp.getHits().getTotalHits();

    LOG.info("Total hits found {}", total);

    // Scroll until no hits are returned
    while (true) {
        SearchHits hits = scrollResp.getHits();
        LOG.info("Processing {} documents - {} out of {}", hits.getHits().length, cumulated, total);
        for (SearchHit hit : hits) {
            String url = null;

            Map<String, Object> sourceMap = hit.getSource();
            if (sourceMap == null) {
                hit.getFields().get("url");
            } else {
                url = sourceMap.get("url").toString();
            }

            if (StringUtils.isBlank(url)) {
                LOG.error("Can't retrieve URL for hit {}", hit);
                continue;
            }

            StringBuilder line = new StringBuilder(url);

            if (boltType.equalsIgnoreCase("status")) {
                sourceMap = (Map<String, Object>) sourceMap.get("metadata");
                if (sourceMap != null) {
                    Iterator<Entry<String, Object>> iter = sourceMap.entrySet().iterator();
                    while (iter.hasNext()) {
                        Entry<String, Object> e = iter.next();
                        Object o = e.getValue();
                        if (o == null) {
                            continue;
                        }
                        if (o instanceof String) {
                            line.append("\t").append(e.getKey()).append("=").append(o);
                        }
                        if (o instanceof List) {
                            for (Object val : (List) o) {
                                line.append("\t").append(e.getKey()).append("=").append(val.toString());
                            }
                        }
                    }
                }
            }

            line.append("\n");
            IOUtils.write(line.toString(), output, "UTF-8");
            cumulated++;
        }
        scrollResp = client.prepareSearchScroll(scrollResp.getScrollId()).setScroll(new TimeValue(600000))
                .execute().actionGet();
        // Break condition: No hits are returned
        if (scrollResp.getHits().getHits().length == 0) {
            break;
        }
    }
}