Example usage for org.apache.commons.io Charsets ISO_8859_1

List of usage examples for org.apache.commons.io Charsets ISO_8859_1

Introduction

In this page you can find the example usage for org.apache.commons.io Charsets ISO_8859_1.

Prototype

Charset ISO_8859_1

To view the source code for org.apache.commons.io Charsets ISO_8859_1.

Click Source Link

Document

CharEncodingISO Latin Alphabet No.

Usage

From source file:de.hanbei.httpserver.MockHttpServerTest.java

@Test
public void getContentUTF8Encoding() throws IOException {
    HttpGet httpget = new HttpGet("http://localhost:7001/testUtf8");
    httpget.setHeader("Accept-Charset", Charsets.ISO_8859_1.name());
    HttpResponse response = httpclient.execute(httpget);
    assertEquals(200, response.getStatusLine().getStatusCode());
    assertEquals("text/plain; charset=iso-8859-1", response.getFirstHeader("Content-Type").getValue());
    HttpEntity entity = response.getEntity();
    String content = IOUtils.toString(entity.getContent(), Charsets.ISO_8859_1);
    assertEquals("Celo", content.trim());
}

From source file:com.streamsets.pipeline.stage.origin.tcp.TestTCPServerSource.java

@Test
public void initMethod() throws Exception {

    final TCPServerSourceConfig configBean = createConfigBean(Charsets.ISO_8859_1);

    initSourceAndValidateIssues(configBean);

    // empty ports
    configBean.ports = new LinkedList<>();
    initSourceAndValidateIssues(configBean, Errors.TCP_02);

    // invalid ports
    // too large//w  w w  . ja  v  a 2 s.  c om
    configBean.ports = Arrays.asList("123456789");
    initSourceAndValidateIssues(configBean, Errors.TCP_03);

    // not a number
    configBean.ports = Arrays.asList("abcd");
    initSourceAndValidateIssues(configBean, Errors.TCP_03);

    // start TLS config tests
    configBean.ports = randomSinglePort();
    configBean.tlsConfigBean.tlsEnabled = true;
    configBean.tlsConfigBean.keyStoreFilePath = "non-existent-file-path";
    initSourceAndValidateIssues(configBean, TlsConfigErrors.TLS_01);

    File blankTempFile = File.createTempFile("blank", "txt");
    blankTempFile.deleteOnExit();
    configBean.tlsConfigBean.keyStoreFilePath = blankTempFile.getAbsolutePath();
    initSourceAndValidateIssues(configBean, TlsConfigErrors.TLS_21);

    // now, try with real keystore
    String hostname = TLSTestUtils.getHostname();
    File testDir = new File("target", UUID.randomUUID().toString()).getAbsoluteFile();
    testDir.deleteOnExit();
    final File keyStore = new File(testDir, "keystore.jks");
    keyStore.deleteOnExit();
    Assert.assertTrue(testDir.mkdirs());
    final String keyStorePassword = "keystore";
    KeyPair keyPair = TLSTestUtils.generateKeyPair();
    Certificate cert = TLSTestUtils.generateCertificate("CN=" + hostname, keyPair, 30);
    TLSTestUtils.createKeyStore(keyStore.toString(), keyStorePassword, "web", keyPair.getPrivate(), cert);

    configBean.tlsConfigBean.keyStoreFilePath = keyStore.getAbsolutePath();
    configBean.tlsConfigBean.keyStorePassword = () -> "invalid-password";

    initSourceAndValidateIssues(configBean, TlsConfigErrors.TLS_21);

    // finally, a valid certificate/config
    configBean.tlsConfigBean.keyStorePassword = () -> keyStorePassword;
    initSourceAndValidateIssues(configBean);

    // ack ELs
    configBean.recordProcessedAckMessage = "${invalid EL)";
    initSourceAndValidateIssues(configBean, Errors.TCP_30);
    configBean.recordProcessedAckMessage = "${time:now()}";
    configBean.batchCompletedAckMessage = "${another invalid EL]";
    initSourceAndValidateIssues(configBean, Errors.TCP_31);
    configBean.batchCompletedAckMessage = "${record:value('/first')}";

    // syslog mode
    configBean.tcpMode = TCPMode.SYSLOG;
    configBean.syslogFramingMode = SyslogFramingMode.NON_TRANSPARENT_FRAMING;
    configBean.nonTransparentFramingSeparatorCharStr = "";
    initSourceAndValidateIssues(configBean, Errors.TCP_40);
    configBean.syslogFramingMode = SyslogFramingMode.OCTET_COUNTING;
    initSourceAndValidateIssues(configBean);

    // separated records
    configBean.tcpMode = TCPMode.DELIMITED_RECORDS;
    configBean.dataFormatConfig.charset = Charsets.UTF_8.name();
    initSourceAndValidateIssues(configBean, Errors.TCP_41);
    configBean.recordSeparatorStr = "";
    initSourceAndValidateIssues(configBean, Errors.TCP_40);
    configBean.recordSeparatorStr = "x";
    initSourceAndValidateIssues(configBean, DataFormatErrors.DATA_FORMAT_12);
    configBean.dataFormat = DataFormat.TEXT;
    initSourceAndValidateIssues(configBean);

}

From source file:com.streamsets.pipeline.stage.origin.tcp.TestTCPServerSource.java

@Test
public void runTextRecordsWithAck()
        throws StageException, IOException, ExecutionException, InterruptedException {

    final String recordSeparatorStr = "\n";
    final String[] expectedRecords = TEN_DELIMITED_RECORDS.split(recordSeparatorStr);
    final int batchSize = expectedRecords.length;

    final Charset charset = Charsets.ISO_8859_1;
    final TCPServerSourceConfig configBean = createConfigBean(charset);
    configBean.dataFormat = DataFormat.TEXT;
    configBean.tcpMode = TCPMode.DELIMITED_RECORDS;
    configBean.recordSeparatorStr = recordSeparatorStr;
    configBean.ports = NetworkUtils.getRandomPorts(1);
    configBean.recordProcessedAckMessage = "record_ack_${record:id()}";
    configBean.batchCompletedAckMessage = "batch_ack_${batchSize}";
    configBean.batchSize = batchSize;//  ww  w  .jav a  2  s .  com

    final TCPServerSource source = new TCPServerSource(configBean);
    final String outputLane = "lane";
    final PushSourceRunner runner = new PushSourceRunner.Builder(TCPServerDSource.class, source)
            .addOutputLane(outputLane).build();

    final List<Record> records = new LinkedList<>();
    runner.runInit();

    EventLoopGroup workerGroup = new NioEventLoopGroup();

    ChannelFuture channelFuture = startTcpClient(configBean, workerGroup,
            TEN_DELIMITED_RECORDS.getBytes(charset), true);

    runner.runProduce(new HashMap<>(), batchSize, output -> {
        records.addAll(output.getRecords().get(outputLane));
        runner.setStop();
    });
    runner.waitOnProduce();

    // Wait until the connection is closed.
    final Channel channel = channelFuture.channel();
    TCPServerSourceClientHandler clientHandler = channel.pipeline().get(TCPServerSourceClientHandler.class);

    final List<String> responses = new LinkedList<>();
    for (int i = 0; i < batchSize + 1; i++) {
        // one for each record, plus one for the batch
        responses.add(clientHandler.getResponse());
    }

    channel.close();

    workerGroup.shutdownGracefully();

    assertThat(records, hasSize(batchSize));

    final List<String> expectedAcks = new LinkedList<>();
    for (int i = 0; i < records.size(); i++) {
        // validate the output record value
        assertThat(records.get(i).get("/text").getValueAsString(), equalTo(expectedRecords[i]));
        // validate the record-level ack
        expectedAcks.add(String.format("record_ack_%s", records.get(i).getHeader().getSourceId()));
    }
    // validate the batch-level ack
    expectedAcks.add(String.format("batch_ack_%d", batchSize));

    // because of the vagaries of TCP, we can't be sure that a single ack is returned in each discrete read
    // this is due to the fact that the server can choose to flush the buffer in different ways, and the client
    // can choose if/how to buffer on its side when reading from the channel
    // therefore, we will simply combine all acks in the expected order into a single String and assert at that
    // level, rather than at an individual read/expected ack level
    final String combinedAcks = StringUtils.join(responses, "");
    assertThat(combinedAcks, startsWith(StringUtils.join(expectedAcks, "")));
}

From source file:com.norconex.commons.lang.io.FileUtil.java

/**
 * Returns the specified number of lines starting from the beginning
 * of a text file.//w w w.  j  av  a2s.  c o m
 * @param file the file to read lines from
 * @param numberOfLinesToRead the number of lines to read
 * @return array of file lines
 * @throws IOException i/o problem
 */
public static String[] head(File file, int numberOfLinesToRead) throws IOException {
    return head(file, Charsets.ISO_8859_1.toString(), numberOfLinesToRead);
}

From source file:com.streamsets.pipeline.stage.origin.tcp.TestTCPServerSource.java

@Test
public void errorHandling() throws StageException, IOException, ExecutionException, InterruptedException {

    final Charset charset = Charsets.ISO_8859_1;
    final TCPServerSourceConfig configBean = createConfigBean(charset);
    configBean.dataFormat = DataFormat.JSON;
    configBean.tcpMode = TCPMode.DELIMITED_RECORDS;
    configBean.recordSeparatorStr = "\n";
    configBean.ports = NetworkUtils.getRandomPorts(1);

    final TCPServerSource source = new TCPServerSource(configBean);
    final String outputLane = "lane";
    final PushSourceRunner toErrorRunner = new PushSourceRunner.Builder(TCPServerDSource.class, source)
            .addOutputLane(outputLane).setOnRecordError(OnRecordError.TO_ERROR).build();

    final List<Record> records = new LinkedList<>();
    final List<Record> errorRecords = new LinkedList<>();
    runAndCollectRecords(toErrorRunner, configBean, records, errorRecords, 1, outputLane,
            "{\"invalid_json\": yes}\n".getBytes(charset), true, false);

    assertThat(records, empty());/*from www .j  av  a2s  .co  m*/
    assertThat(errorRecords, hasSize(1));
    assertThat(errorRecords.get(0).getHeader().getErrorCode(),
            equalTo(com.streamsets.pipeline.lib.parser.Errors.DATA_PARSER_04.getCode()));

    final PushSourceRunner discardRunner = new PushSourceRunner.Builder(TCPServerDSource.class, source)
            .addOutputLane(outputLane).setOnRecordError(OnRecordError.DISCARD).build();
    records.clear();
    errorRecords.clear();

    configBean.ports = NetworkUtils.getRandomPorts(1);
    runAndCollectRecords(discardRunner, configBean, records, errorRecords, 1, outputLane,
            "{\"invalid_json\": yes}\n".getBytes(charset), true, false);
    assertThat(records, empty());
    assertThat(errorRecords, empty());

    configBean.ports = NetworkUtils.getRandomPorts(1);
    final PushSourceRunner stopPipelineRunner = new PushSourceRunner.Builder(TCPServerDSource.class, source)
            .addOutputLane(outputLane).setOnRecordError(OnRecordError.STOP_PIPELINE).build();
    records.clear();
    errorRecords.clear();
    try {
        runAndCollectRecords(stopPipelineRunner, configBean, records, errorRecords, 1, outputLane,
                "{\"invalid_json\": yes}\n".getBytes(charset), true, true);
        Assert.fail("ExecutionException should have been thrown");
    } catch (ExecutionException e) {
        assertThat(e.getCause(), instanceOf(RuntimeException.class));
        final RuntimeException runtimeException = (RuntimeException) e.getCause();
        assertThat(runtimeException.getCause(), instanceOf(StageException.class));
        final StageException stageException = (StageException) runtimeException.getCause();
        assertThat(stageException.getErrorCode().getCode(), equalTo(Errors.TCP_06.getCode()));
    }
}

From source file:com.norconex.commons.lang.io.FileUtil.java

/**
 * Returns the specified number of lines starting from the end
 * of a text file.//from  w  w  w .  ja v  a2  s.c  o  m
 * @param file the file to read lines from
 * @param numberOfLinesToRead the number of lines to read
 * @return array of file lines
 * @throws IOException i/o problem
 */
public static String[] tail(File file, int numberOfLinesToRead) throws IOException {
    return tail(file, Charsets.ISO_8859_1.toString(), numberOfLinesToRead);
}

From source file:org.gvsig.framework.web.service.impl.OGCInfoServiceImpl.java

public CSWResult getCswRecords(String cswUrl, CSWCriteria cswCriteria)
        throws NotSupportedVersionException, URISyntaxException, NullPointerException {
    // Check if services gvSIG are initialized
    if (!librariesInitialized) {
        LibrariesInitializer libs = new DefaultLibrariesInitializer();
        libs.fullInitialize();//  w  w w . ja v  a2s  .  c o  m
        librariesInitialized = true;
    }

    URI uri = null;
    CSWResult cswResults = new CSWResult();
    uri = new URI(cswUrl);
    // Creates driver "CSW/ISO 19115"
    CSWISO19115LangCatalogServiceDriver iCatalogServiceDriver = new CSWISO19115LangCatalogServiceDriver();
    @SuppressWarnings("unused")
    DiscoveryServiceCapabilities getCapabilities = iCatalogServiceDriver.getCapabilities(uri, "spa");

    CatalogClient catalogClient = new CatalogClient(cswUrl, null, iCatalogServiceDriver);

    // Query
    CatalogQuery query = catalogClient.createNewQuery();
    String title = cswCriteria.getTitle();
    if (title != null && !title.isEmpty()) {
        query.setTitle(title);
    }
    String abstract_ = cswCriteria.getAbstract_();
    if (abstract_ != null && !abstract_.isEmpty()) {
        query.setAbstract(abstract_);
    }
    // The dates must have the following format: "yyyy-MM-dd"
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

    Calendar dateFrom = cswCriteria.getDateFrom();
    if (dateFrom != null) {
        String dateFromString = sdf.format(dateFrom.getTime());
        query.setDateFrom(dateFromString);
    }
    Calendar dateTo = cswCriteria.getDateTo();
    if (dateTo != null) {
        String dateToString = sdf.format(dateTo.getTime());
        query.setDateTo(dateToString);
    }

    // Executed query
    int startAt;
    if (cswCriteria.getStartAt() <= 0) {
        startAt = 1;
    } else {
        startAt = cswCriteria.getStartAt();
    }
    GetRecordsReply records = iCatalogServiceDriver.getRecords(uri, query, startAt);

    // Returned results.
    // Returned records. Maximum 10.
    int retrievedRecords = records.getRetrievedRecordsNumber();
    cswResults.setRetrievedRecords(retrievedRecords);
    // Total found records.
    cswResults.setTotalRecords(records.getRecordsNumber());
    logger.debug(cswResults.toString());

    // For every result returns title, abstract, image and first wms url in
    // UTF-8.
    int index = 0;
    if (retrievedRecords != 0) {
        List<CSWSingleResult> cswSingleRecordList = cswResults.getResults();
        while (index < retrievedRecords) {
            Record record = records.getRecordAt(index);
            CSWSingleResult cswSingleResult = new CSWSingleResult();

            // Returned title
            String returnedTitle = record.getTitle();
            if (returnedTitle != null) {
                String titleUtf8 = new String(returnedTitle.getBytes(Charsets.ISO_8859_1), Charsets.UTF_8);
                cswSingleResult.setTitle(titleUtf8);
            }

            // Returned abstract
            String returnedAbstract = record.getAbstract_();
            if (returnedAbstract != null) {
                String abstract_Utf8 = new String(returnedAbstract.getBytes(Charsets.ISO_8859_1),
                        Charsets.UTF_8);
                cswSingleResult.setAbstract_(abstract_Utf8);
            }

            XMLNode node = record.getNode();

            // Returned file identifier
            XMLNode fileIdentifierNode = node.searchNode("fileIdentifier->CharacterString");
            if (fileIdentifierNode != null && fileIdentifierNode.getText() != null) {
                String fileIdentifierUtf8 = new String(
                        fileIdentifierNode.getText().getBytes(Charsets.ISO_8859_1), Charsets.UTF_8);
                cswSingleResult.setFileIdentifier(fileIdentifierUtf8);
            }

            // Returned image
            XMLNode imageNode = node.searchNode(
                    "identificationInfo->SV_ServiceIdentification->graphicOverview->MD_BrowseGraphic->fileName->CharacterString");
            if (imageNode != null && imageNode.getText() != null) {
                String imageUtf8 = new String(imageNode.getText().getBytes(Charsets.ISO_8859_1),
                        Charsets.UTF_8);
                cswSingleResult.setImage(imageUtf8);
            } else {
                imageNode = node.searchNode(
                        "identificationInfo->MD_DataIdentification->graphicOverview->MD_BrowseGraphic->fileName->CharacterString");
                if (imageNode != null && imageNode.getText() != null) {
                    String imageUtf8 = new String(imageNode.getText().getBytes(Charsets.ISO_8859_1),
                            Charsets.UTF_8);
                    cswSingleResult.setImage(imageUtf8);
                }
            }

            // Returned first wms or wmts url
            XMLNode[] urlNodes = node.searchMultipleNode(
                    "distributionInfo->MD_Distribution->transferOptions->MD_DigitalTransferOptions->onLine->CI_OnlineResource");
            boolean containsServiceUrl = false;
            int i = 0;
            while (i < urlNodes.length && !containsServiceUrl) {
                XMLNode xmlNode = urlNodes[i];
                i = i + 1;
                XMLNode protocolNode = xmlNode.searchNode("protocol->CharacterString");
                XMLNode urlNode = xmlNode.searchNode("linkage->URL");
                String urlUtf8 = null;
                if (urlNode != null && urlNode.getText() != null) {
                    urlUtf8 = new String(urlNode.getText().getBytes(Charsets.ISO_8859_1), Charsets.UTF_8);
                }
                if (protocolNode != null && protocolNode.getText() != null) {
                    String protocolUtf8 = new String(protocolNode.getText().getBytes(Charsets.ISO_8859_1),
                            Charsets.UTF_8);
                    if ((protocolUtf8.toLowerCase()).contains("ogc:wms")) {
                        containsServiceUrl = true;
                        cswSingleResult.setServiceUrl(urlUtf8);
                        cswSingleResult.setServiceType("wms");
                    }
                } else if (urlUtf8 != null && (urlUtf8.toLowerCase()).contains("service=wms")) {
                    containsServiceUrl = true;
                    String[] parts = urlUtf8.split("\\?");
                    String part1 = parts[0];
                    if (part1 != null && !part1.isEmpty()) {
                        cswSingleResult.setServiceUrl(part1);
                    } else {
                        cswSingleResult.setServiceUrl(urlUtf8);
                    }
                    cswSingleResult.setServiceType("wms");
                } else if (urlUtf8 != null && (urlUtf8.toLowerCase()).contains("service=wmts")) {
                    containsServiceUrl = true;
                    String[] parts = urlUtf8.split("\\?");
                    String part1 = parts[0];
                    if (part1 != null && !part1.isEmpty()) {
                        cswSingleResult.setServiceUrl(part1);
                    } else {
                        cswSingleResult.setServiceUrl(urlUtf8);
                    }
                    cswSingleResult.setServiceType("wmts");
                }
            }

            cswSingleRecordList.add(cswSingleResult);
            logger.debug("Data " + index + ":: " + cswSingleResult.toString());

            index = index + 1;
        }
    }
    return cswResults;
}

From source file:org.hobbit.core.rabbit.RabbitMQUtilsTest.java

@Test
public void testByteArrays() {
    performByteArraysTest(new byte[][] { new byte[0], new byte[0], new byte[0] });
    performByteArraysTest(new byte[][] { new byte[0], new byte[0], new byte[100] });
    performByteArraysTest(new byte[][] { "test".getBytes(Charsets.UTF_8), new byte[0],
            "one more test".getBytes(Charsets.ISO_8859_1) });
}

From source file:org.jahia.modules.external.modules.ModulesDataSource.java

private void saveCndResourceBundle(ExternalData data, String key) throws RepositoryException {
    String resourceBundleName = module.getResourceBundleName();
    if (resourceBundleName == null) {
        resourceBundleName = "resources." + module.getId();
    }/*from  w w  w.ja  v  a2 s .c o  m*/
    String rbBasePath = "/src/main/resources/resources/"
            + StringUtils.substringAfterLast(resourceBundleName, ".");
    Map<String, Map<String, String[]>> i18nProperties = data.getI18nProperties();
    if (i18nProperties != null) {
        List<File> newFiles = new ArrayList<File>();
        for (Map.Entry<String, Map<String, String[]>> entry : i18nProperties.entrySet()) {
            String lang = entry.getKey();
            Map<String, String[]> properties = entry.getValue();

            String[] values = properties.get(Constants.JCR_TITLE);
            String title = ArrayUtils.isEmpty(values) ? null : values[0];

            values = properties.get(Constants.JCR_DESCRIPTION);
            String description = ArrayUtils.isEmpty(values) ? null : values[0];

            String rbPath = rbBasePath + "_" + lang + PROPERTIES_EXTENSION;
            InputStream is = null;
            InputStreamReader isr = null;
            OutputStream os = null;
            OutputStreamWriter osw = null;
            try {
                FileObject file = getFile(rbPath);
                FileContent content = file.getContent();
                Properties p = new SortedProperties();
                if (file.exists()) {
                    is = content.getInputStream();
                    isr = new InputStreamReader(is, Charsets.ISO_8859_1);
                    p.load(isr);
                    isr.close();
                    is.close();
                } else if (StringUtils.isBlank(title) && StringUtils.isBlank(description)) {
                    continue;
                } else {
                    newFiles.add(new File(file.getName().getPath()));
                }
                if (!StringUtils.isEmpty(title)) {
                    p.setProperty(key, title);
                }
                if (!StringUtils.isEmpty(description)) {
                    p.setProperty(key + "_description", description);
                }
                os = content.getOutputStream();
                osw = new OutputStreamWriter(os, Charsets.ISO_8859_1);
                p.store(osw, rbPath);
                ResourceBundle.clearCache();
            } catch (FileSystemException e) {
                logger.error("Failed to save resourceBundle", e);
                throw new RepositoryException("Failed to save resourceBundle", e);
            } catch (IOException e) {
                logger.error("Failed to save resourceBundle", e);
                throw new RepositoryException("Failed to save resourceBundle", e);
            } finally {
                IOUtils.closeQuietly(is);
                IOUtils.closeQuietly(isr);
                IOUtils.closeQuietly(os);
                IOUtils.closeQuietly(osw);
            }
        }
        SourceControlManagement sourceControl = module.getSourceControl();
        if (sourceControl != null) {
            try {
                sourceControl.add(newFiles);
            } catch (IOException e) {
                logger.error("Failed to add files to source control", e);
                throw new RepositoryException("Failed to add new files to source control: " + newFiles, e);
            }
        }
    }
}

From source file:org.jahia.modules.external.modules.ModulesDataSource.java

@Override
public String[] getPropertyValues(String path, String propertyName) throws PathNotFoundException {
    if (SOURCE_CODE.equals(propertyName)) {
        InputStream is = null;//from   w w  w  .  j  a v  a  2 s.com
        try {
            is = getFile(path).getContent().getInputStream();
            java.nio.charset.Charset c = path.toLowerCase().endsWith(".properties") ? Charsets.ISO_8859_1
                    : Charsets.UTF_8;
            return new String[] { IOUtils.toString(is, c) };
        } catch (Exception e) {
            logger.error("Failed to read source code", e);
        } finally {
            IOUtils.closeQuietly(is);
        }
    }
    throw new PathNotFoundException(path + "/" + propertyName);
}