Example usage for org.apache.commons.lang3 StringUtils repeat

List of usage examples for org.apache.commons.lang3 StringUtils repeat

Introduction

In this page you can find the example usage for org.apache.commons.lang3 StringUtils repeat.

Prototype

public static String repeat(final char ch, final int repeat) 

Source Link

Document

<p>Returns padding using the specified delimiter repeated to a given length.</p> <pre> StringUtils.repeat('e', 0) = "" StringUtils.repeat('e', 3) = "eee" StringUtils.repeat('e', -2) = "" </pre> <p>Note: this method doesn't not support padding with <a href="http://www.unicode.org/glossary/#supplementary_character">Unicode Supplementary Characters</a> as they require a pair of char s to be represented.

Usage

From source file:org.apache.metron.solr.integration.SolrUpdateIntegrationTest.java

@Test
public void testHugeErrorFields() throws Exception {
    String hugeString = StringUtils.repeat("test ", 1_000_000);
    String hugeStringTwo = hugeString + "-2";

    Map<String, Object> documentMap = new HashMap<>();
    documentMap.put("guid", "error_guid");
    // Needs to be over 32kb
    documentMap.put("raw_message", hugeString);
    documentMap.put("raw_message_1", hugeStringTwo);
    Document errorDoc = new Document(documentMap, "error", "error", 0L);
    getDao().update(errorDoc, Optional.of("error"));

    // Ensure that the huge string is returned when not a string field
    Document latest = getDao().getLatest("error_guid", "error");
    @SuppressWarnings("unchecked")
    String actual = (String) latest.getDocument().get("raw_message");
    assertEquals(actual, hugeString);//w  w  w  .  j  a  va  2s  .com
    String actualTwo = (String) latest.getDocument().get("raw_message_1");
    assertEquals(actualTwo, hugeStringTwo);

    // Validate that error occurs for string fields.
    documentMap.put("error_hash", hugeString);
    errorDoc = new Document(documentMap, "error", "error", 0L);

    exception.expect(IOException.class);
    exception.expectMessage("Document contains at least one immense term in field=\"error_hash\"");
    getDao().update(errorDoc, Optional.of("error"));
}

From source file:org.apache.metron.stellar.common.utils.hashing.DefaultHasher.java

/**
 * {@inheritDoc}/* w  w w .j a v a2 s . c  o  m*/
 *
 * Returns a hash which has been encoded using the supplied encoder. If input is null then a string
 * containing all '0' will be returned. The length of the string is determined by the hashing algorithm
 * used.
 * @param toHash The value to hash.
 * @return A hash of {@code toHash} that has been encoded.
 * @throws EncoderException If unable to encode the hash then this exception occurs.
 * @throws NoSuchAlgorithmException If the supplied algorithm is not known.
 */
@Override
public String getHash(final Object toHash) throws EncoderException, NoSuchAlgorithmException {
    final MessageDigest messageDigest = MessageDigest.getInstance(algorithm);

    if (toHash == null) {
        return StringUtils.repeat("00", messageDigest.getDigestLength());
    } else if (toHash instanceof String) {
        return getHash(messageDigest, toHash.toString().getBytes(charset));
    } else if (toHash instanceof Serializable) {
        final byte[] serialized = SerializationUtils.serialize((Serializable) toHash);
        return getHash(messageDigest, serialized);
    }

    return null;
}

From source file:org.apache.metron.stellar.common.utils.hashing.DefaultHasherTest.java

@Test
public void getHashAsHexOfNullValueReturnsPadded00() throws Exception {
    assertEquals(StringUtils.repeat("00", 16), new DefaultHasher("md5", encoder).getHash(null));
    assertEquals(StringUtils.repeat("00", 32), new DefaultHasher("sha-256", encoder).getHash(null));
}

From source file:org.apache.metron.stellar.dsl.functions.HashFunctionsTest.java

@Test
public void nullInputForValueToHashShouldReturnHashedEncodedValueOf0x00() throws Exception {
    assertEquals(StringUtils.repeat('0', 32), hash.apply(Arrays.asList(null, "md5")));
}

From source file:org.apache.metron.stellar.dsl.functions.HashFunctionsTest.java

@Test
public void nullInputForValueToHashShouldReturnHashedEncodedValueOf0x00InDirectStellarCall() throws Exception {
    final String algorithm = "'md5'";
    final Map<String, Object> variables = new HashMap<>();
    variables.put("toHash", null);

    assertEquals(StringUtils.repeat('0', 32), run("HASH(toHash, " + algorithm + ")", variables));
}

From source file:org.apache.nifi.processors.standard.LogAttribute.java

protected String processFlowFile(final ComponentLog logger, final DebugLevels logLevel, final FlowFile flowFile,
        final ProcessSession session, final ProcessContext context) {
    final Set<String> attributeKeys = getAttributesToLog(flowFile.getAttributes().keySet(), context);
    final ComponentLog LOG = getLogger();
    final String dashedLine;

    String logPrefix = context.getProperty(LOG_PREFIX).evaluateAttributeExpressions(flowFile).getValue();

    if (StringUtil.isBlank(logPrefix)) {
        dashedLine = StringUtils.repeat('-', 50);
    } else {/*from  ww w  .  j  a v a2  s.c o m*/
        // abbreviate long lines
        logPrefix = StringUtils.abbreviate(logPrefix, 40);
        // center the logPrefix and pad with dashes
        logPrefix = StringUtils.center(logPrefix, 40, '-');
        // five dashes on the left and right side, plus the dashed logPrefix
        dashedLine = StringUtils.repeat('-', 5) + logPrefix + StringUtils.repeat('-', 5);
    }

    // Pretty print metadata
    final StringBuilder message = new StringBuilder();
    message.append("logging for flow file ").append(flowFile);
    message.append("\n");
    message.append(dashedLine);
    message.append("\nStandard FlowFile Attributes");
    message.append(
            String.format("\nKey: '%1$s'\n\tValue: '%2$s'", "entryDate", new Date(flowFile.getEntryDate())));
    message.append(String.format("\nKey: '%1$s'\n\tValue: '%2$s'", "lineageStartDate",
            new Date(flowFile.getLineageStartDate())));
    message.append(String.format("\nKey: '%1$s'\n\tValue: '%2$s'", "fileSize", flowFile.getSize()));
    message.append("\nFlowFile Attribute Map Content");
    for (final String key : attributeKeys) {
        message.append(String.format("\nKey: '%1$s'\n\tValue: '%2$s'", key, flowFile.getAttribute(key)));
    }
    message.append("\n");
    message.append(dashedLine);

    // The user can request to log the payload
    final boolean logPayload = context.getProperty(LOG_PAYLOAD).asBoolean();
    if (logPayload) {
        message.append("\n");
        if (flowFile.getSize() < ONE_MB) {
            final FlowFilePayloadCallback callback = new FlowFilePayloadCallback();
            session.read(flowFile, callback);
            message.append(callback.getContents());
        } else {
            message.append("\n Not including payload since it is larger than one mb.");
        }
    }
    final String outputMessage = message.toString().trim();
    // Uses optional property to specify logging level
    switch (logLevel) {
    case info:
        LOG.info(outputMessage);
        break;
    case debug:
        LOG.debug(outputMessage);
        break;
    case warn:
        LOG.warn(outputMessage);
        break;
    case trace:
        LOG.trace(outputMessage);
        break;
    case error:
        LOG.error(outputMessage);
        break;
    default:
        LOG.debug(outputMessage);
    }

    return outputMessage;

}

From source file:org.apache.nifi.processors.standard.TestPostHTTP.java

@Test
public void testSendWithCompressionServerAcceptGzip() throws Exception {
    setup(null);/*from  w ww.j a v  a  2s  .  com*/

    final String suppliedMimeType = "text/plain";
    runner.setProperty(PostHTTP.URL, server.getUrl());
    runner.setProperty(PostHTTP.CONTENT_TYPE, suppliedMimeType);
    runner.setProperty(PostHTTP.COMPRESSION_LEVEL, "9");

    final Map<String, String> attrs = new HashMap<>();
    attrs.put(CoreAttributes.MIME_TYPE.key(), "text/plain");

    runner.enqueue(StringUtils.repeat("Lines of sample text.", 100).getBytes(), attrs);

    runner.run(1);
    runner.assertAllFlowFilesTransferred(PostHTTP.REL_SUCCESS);

    Map<String, String> lastPostHeaders = servlet.getLastPostHeaders();
    Assert.assertEquals(suppliedMimeType, lastPostHeaders.get(PostHTTP.CONTENT_TYPE_HEADER));
    // Ensure that a 'Content-Encoding' header was set with a 'gzip' value
    Assert.assertEquals(PostHTTP.CONTENT_ENCODING_GZIP_VALUE,
            lastPostHeaders.get(PostHTTP.CONTENT_ENCODING_HEADER));
    Assert.assertNull(lastPostHeaders.get("Content-Length"));
}

From source file:org.apache.nifi.processors.standard.TestPostHTTP.java

@Test
public void testSendWithoutCompressionServerAcceptGzip() throws Exception {
    setup(null);//from www  .  j av  a 2 s . c o m

    final String suppliedMimeType = "text/plain";
    runner.setProperty(PostHTTP.URL, server.getUrl());
    runner.setProperty(PostHTTP.CONTENT_TYPE, suppliedMimeType);
    runner.setProperty(PostHTTP.COMPRESSION_LEVEL, "0");
    runner.setProperty(PostHTTP.CHUNKED_ENCODING, "false");

    final Map<String, String> attrs = new HashMap<>();
    attrs.put(CoreAttributes.MIME_TYPE.key(), "text/plain");

    runner.enqueue(StringUtils.repeat("Lines of sample text.", 100).getBytes(), attrs);

    runner.run(1);
    runner.assertAllFlowFilesTransferred(PostHTTP.REL_SUCCESS);

    Map<String, String> lastPostHeaders = servlet.getLastPostHeaders();
    Assert.assertEquals(suppliedMimeType, lastPostHeaders.get(PostHTTP.CONTENT_TYPE_HEADER));
    // Ensure that the request was not sent with a 'Content-Encoding' header
    Assert.assertNull(lastPostHeaders.get(PostHTTP.CONTENT_ENCODING_HEADER));
    Assert.assertEquals("2100", lastPostHeaders.get("Content-Length"));
}

From source file:org.apache.nifi.processors.standard.TestPostHTTP.java

@Test
public void testSendWithCompressionServerNotAcceptGzip() throws Exception {
    setup(null);//from   ww  w.  j a  v  a 2s .c  o m

    final String suppliedMimeType = "text/plain";
    // Specify a property to the URL to have the CaptureServlet specify it doesn't accept gzip
    runner.setProperty(PostHTTP.URL, server.getUrl() + "?acceptGzip=false");
    runner.setProperty(PostHTTP.CONTENT_TYPE, suppliedMimeType);
    runner.setProperty(PostHTTP.COMPRESSION_LEVEL, "9");

    final Map<String, String> attrs = new HashMap<>();
    attrs.put(CoreAttributes.MIME_TYPE.key(), "text/plain");

    runner.enqueue(StringUtils.repeat("Lines of sample text.", 100).getBytes(), attrs);

    runner.run(1);
    runner.assertAllFlowFilesTransferred(PostHTTP.REL_SUCCESS);

    Map<String, String> lastPostHeaders = servlet.getLastPostHeaders();
    Assert.assertEquals(suppliedMimeType, lastPostHeaders.get(PostHTTP.CONTENT_TYPE_HEADER));
    // Ensure that the request was not sent with a 'Content-Encoding' header
    Assert.assertNull(lastPostHeaders.get(PostHTTP.CONTENT_ENCODING_HEADER));
}

From source file:org.apache.nifi.processors.standard.TestPostHTTP.java

@Test
public void testSendChunked() throws Exception {
    setup(null);//from www  . j  a  v  a  2  s  .  c o  m

    final String suppliedMimeType = "text/plain";
    runner.setProperty(PostHTTP.URL, server.getUrl());
    runner.setProperty(PostHTTP.CONTENT_TYPE, suppliedMimeType);
    runner.setProperty(PostHTTP.CHUNKED_ENCODING, "true");

    final Map<String, String> attrs = new HashMap<>();
    attrs.put(CoreAttributes.MIME_TYPE.key(), "text/plain");

    runner.enqueue(StringUtils.repeat("Lines of sample text.", 100).getBytes(), attrs);

    runner.run(1);
    runner.assertAllFlowFilesTransferred(PostHTTP.REL_SUCCESS);

    byte[] postValue = servlet.getLastPost();
    Assert.assertArrayEquals(StringUtils.repeat("Lines of sample text.", 100).getBytes(), postValue);

    Map<String, String> lastPostHeaders = servlet.getLastPostHeaders();
    Assert.assertEquals(suppliedMimeType, lastPostHeaders.get(PostHTTP.CONTENT_TYPE_HEADER));
    Assert.assertNull(lastPostHeaders.get("Content-Length"));
    Assert.assertEquals("chunked", lastPostHeaders.get("Transfer-Encoding"));
}