Search in sources :

Example 11 with MantisServerSentEvent

use of io.mantisrx.common.MantisServerSentEvent in project mantis by Netflix.

the class CompressionUtils method decompressAndBase64Decode_old.

public static List<MantisServerSentEvent> decompressAndBase64Decode_old(String encodedString, boolean isCompressedBinary) {
    encodedString = encodedString.trim();
    // System.out.println("Inside client decompress Current thread -->" + Thread.currentThread().getName());
    if (!encodedString.isEmpty() && isCompressedBinary && !encodedString.startsWith("ping") && !encodedString.startsWith("{")) {
        if (logger.isDebugEnabled()) {
            logger.debug("decoding " + encodedString);
        }
        byte[] decoded = Base64.getDecoder().decode(encodedString);
        GZIPInputStream gis;
        try {
            gis = new GZIPInputStream(new ByteArrayInputStream(decoded));
            BufferedReader bf = new BufferedReader(new InputStreamReader(gis, "UTF-8"));
            String outStr = "";
            String line;
            while ((line = bf.readLine()) != null) {
                outStr += line;
            }
            String[] toks = outStr.split("\\$\\$\\$");
            List<MantisServerSentEvent> msseList = new ArrayList<>();
            for (String tok : toks) {
                msseList.add(new MantisServerSentEvent(tok));
            }
            // return Arrays.asList(new MantisServerSentEvent(toks));
            return msseList;
        } catch (IOException e) {
            logger.error(e.getMessage());
        }
        return new ArrayList<MantisServerSentEvent>();
    } else {
        List<MantisServerSentEvent> s = new ArrayList<MantisServerSentEvent>();
        s.add(new MantisServerSentEvent(encodedString));
        return s;
    }
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) InputStreamReader(java.io.InputStreamReader) MantisServerSentEvent(io.mantisrx.common.MantisServerSentEvent) ByteArrayInputStream(java.io.ByteArrayInputStream) BufferedReader(java.io.BufferedReader) ArrayList(java.util.ArrayList) IOException(java.io.IOException)

Example 12 with MantisServerSentEvent

use of io.mantisrx.common.MantisServerSentEvent in project mantis by Netflix.

the class CompressionUtils method tokenize_2.

static List<MantisServerSentEvent> tokenize_2(BufferedReader bf) throws IOException {
    StringBuilder sb = new StringBuilder();
    String line;
    List<MantisServerSentEvent> msseList = new ArrayList<>();
    String outStr = "";
    while ((line = bf.readLine()) != null) {
        sb.append(line);
    }
    outStr = sb.toString();
    String[] toks = outStr.split("\\$\\$\\$");
    for (String tok : toks) {
        msseList.add(new MantisServerSentEvent(tok));
    }
    return msseList;
}
Also used : MantisServerSentEvent(io.mantisrx.common.MantisServerSentEvent) ArrayList(java.util.ArrayList)

Example 13 with MantisServerSentEvent

use of io.mantisrx.common.MantisServerSentEvent in project mantis by Netflix.

the class CompressionUtils method decompressAndBase64Decode.

public static List<MantisServerSentEvent> decompressAndBase64Decode(String encodedString, boolean isCompressedBinary, boolean useSnappy, String delimiter) {
    encodedString = encodedString.trim();
    // System.out.println("Inside client decompress Current thread -->" + Thread.currentThread().getName());
    if (!encodedString.isEmpty() && isCompressedBinary && !encodedString.startsWith("ping") && !encodedString.startsWith("{")) {
        if (logger.isDebugEnabled()) {
            logger.debug("decoding " + encodedString);
        }
        byte[] decoded = Base64.getDecoder().decode(encodedString);
        try {
            if (useSnappy) {
                return delimiter == null ? tokenize(snappyDecompress(decoded)) : tokenize(snappyDecompress(decoded), delimiter);
            } else {
                return delimiter == null ? tokenize(gzipDecompress(decoded)) : tokenize(gzipDecompress(decoded), delimiter);
            }
        } catch (IOException e) {
            logger.error(e.getMessage());
        }
        return new ArrayList<MantisServerSentEvent>();
    } else {
        List<MantisServerSentEvent> s = new ArrayList<MantisServerSentEvent>();
        s.add(new MantisServerSentEvent(encodedString));
        return s;
    }
}
Also used : MantisServerSentEvent(io.mantisrx.common.MantisServerSentEvent) ArrayList(java.util.ArrayList) IOException(java.io.IOException)

Example 14 with MantisServerSentEvent

use of io.mantisrx.common.MantisServerSentEvent in project mantis by Netflix.

the class CompressionUtils method tokenize.

static List<MantisServerSentEvent> tokenize(BufferedReader bf, String delimiter) throws IOException {
    StringBuilder sb = new StringBuilder();
    String line;
    List<MantisServerSentEvent> msseList = new ArrayList<>();
    final int delimiterLength = delimiter.length();
    char[] delimiterArray = delimiter.toCharArray();
    int delimiterCount = 0;
    while ((line = bf.readLine()) != null) {
        // Consider replacing this whole thing with just String.indexOf
        for (int i = 0; i < line.length(); i++) {
            if (line.charAt(i) != delimiterArray[delimiterCount]) {
                if (delimiterCount > 0) {
                    for (int j = 0; j < delimiterCount; ++j) {
                        sb.append(delimiterArray[j]);
                    }
                    delimiterCount = 0;
                }
                if (line.charAt(i) != delimiterArray[delimiterCount]) {
                    sb.append(line.charAt(i));
                } else {
                    delimiterCount++;
                }
            } else {
                delimiterCount++;
            }
            if (delimiterCount == delimiterLength) {
                msseList.add(new MantisServerSentEvent(sb.toString()));
                delimiterCount = 0;
                sb = new StringBuilder();
            }
        }
    }
    // We have a trailing event.
    if (sb.length() > 0) {
        // We had a partial delimiter match which was not in the builder.
        if (delimiterCount > 0) {
            for (int j = 0; j < delimiterCount; ++j) {
                sb.append(delimiter.charAt(j));
            }
        }
        msseList.add(new MantisServerSentEvent(sb.toString()));
    }
    return msseList;
}
Also used : MantisServerSentEvent(io.mantisrx.common.MantisServerSentEvent) ArrayList(java.util.ArrayList)

Example 15 with MantisServerSentEvent

use of io.mantisrx.common.MantisServerSentEvent in project mantis by Netflix.

the class CompressionUtilsTest method shouldTokenizeWithEventsContainingPartialDelimiterMatches.

@Test
public void shouldTokenizeWithEventsContainingPartialDelimiterMatches() {
    String testInput = "ab$cdef$$$ghi$jkl$$$lmno$$pqrst$";
    try (BufferedReader reader = new BufferedReader(new StringReader(testInput))) {
        List<MantisServerSentEvent> result = CompressionUtils.tokenize(reader);
        assertEquals(result.size(), 3);
        assertEquals(result.get(0).getEventAsString(), "ab$cdef");
        assertEquals(result.get(1).getEventAsString(), "ghi$jkl");
        assertEquals(result.get(2).getEventAsString(), "lmno$$pqrst$");
    } catch (IOException ex) {
        Assert.fail("Tokenization threw an IO exception that was unexpected");
    }
}
Also used : MantisServerSentEvent(io.mantisrx.common.MantisServerSentEvent) BufferedReader(java.io.BufferedReader) StringReader(java.io.StringReader) IOException(java.io.IOException) Test(org.junit.Test)

Aggregations

MantisServerSentEvent (io.mantisrx.common.MantisServerSentEvent)17 IOException (java.io.IOException)9 CountDownLatch (java.util.concurrent.CountDownLatch)8 Action1 (rx.functions.Action1)7 ArrayList (java.util.ArrayList)6 Test (org.junit.Test)6 Observable (rx.Observable)6 Action0 (rx.functions.Action0)6 Subscription (rx.Subscription)5 MantisSSEJob (io.mantisrx.client.MantisSSEJob)4 FileInputStream (java.io.FileInputStream)4 InputStream (java.io.InputStream)4 Properties (java.util.Properties)4 BufferedReader (java.io.BufferedReader)3 JobSla (io.mantisrx.runtime.JobSla)2 StringReader (java.io.StringReader)2 DefaultRegistry (com.netflix.spectator.api.DefaultRegistry)1 SinkConnectionsStatus (io.mantisrx.client.SinkConnectionsStatus)1 Counter (io.mantisrx.common.metrics.Counter)1 Metrics (io.mantisrx.common.metrics.Metrics)1