@Override public Map<String, String> unpackageFlowFile(final InputStream in, final OutputStream out) throws IOException { final byte[] header = (nextHeader == null) ? readHeader(in) : nextHeader; if (!Arrays.equals(header, FlowFilePackagerV3.MAGIC_HEADER)) { throw new IOException("Not in FlowFile-v3 format"); } final Map<String, String> attributes = readAttributes(in); final long expectedNumBytes = readLong(in); // read length of payload copy(in, out, expectedNumBytes); // read payload nextHeader = readHeader(in); haveReadSomething = true; return attributes; }
protected String readString(final InputStream in) throws IOException { final Integer numBytes = readFieldLength(in); if (numBytes == null) { throw new EOFException(); } final byte[] bytes = new byte[numBytes]; fillBuffer(in, bytes, numBytes); return new String(bytes, "UTF-8"); }
protected Map<String, String> readAttributes(final InputStream in) throws IOException { final Map<String, String> attributes = new HashMap<>(); final Integer numAttributes = readFieldLength(in); //read number of attributes if (numAttributes == null) { return null; } if (numAttributes == 0) { throw new IOException("flow files cannot have zero attributes"); } for (int i = 0; i < numAttributes; i++) { //read each attribute key/value pair final String key = readString(in); final String value = readString(in); attributes.put(key, value); } return attributes; }
private void checkBatch(TestServer server, CaptureServlet servlet, Set<String> actualContent, int expectedCount) throws Exception { FlowFileUnpackagerV3 unpacker = new FlowFileUnpackagerV3(); Set<String> actualFFContent = new HashSet<>(); Set<String> actualPostContent = new HashSet<>(); ByteArrayOutputStream baos = new ByteArrayOutputStream()) { for (int i = 0; i < expectedCount; i++) { Map<String, String> receivedAttrs = unpacker.unpackageFlowFile(bais, baos); String receivedContent = new String(baos.toByteArray()); actualPostContent.add(receivedContent); assertEquals(server.getUrl(), receivedAttrs.get("url")); assertTrue(unpacker.hasMoreData() || i == (expectedCount - 1)); baos.reset();
public static FlowFileUnpackager createUnpackager(final String version) { switch (version) { case "1": return new FlowFileUnpackagerV1(); case "2": return new FlowFileUnpackagerV2(); case "3": return new FlowFileUnpackagerV3(); default: System.out.println("ERROR: Invalid version: " + version + "; must be 1, 2, or 3"); return null; } } }
protected long readLong(final InputStream in) throws IOException { fillBuffer(in, readBuffer, 8); return (((long) readBuffer[0] << 56) + ((long) (readBuffer[1] & 255) << 48) + ((long) (readBuffer[2] & 255) << 40) + ((long) (readBuffer[3] & 255) << 32) + ((long) (readBuffer[4] & 255) << 24) + ((readBuffer[5] & 255) << 16) + ((readBuffer[6] & 255) << 8) + ((readBuffer[7] & 255))); }
final ByteArrayInputStream bais = new ByteArrayInputStream(lastPost); FlowFileUnpackagerV3 unpacker = new FlowFileUnpackagerV3(); Map<String, String> receivedAttrs = unpacker.unpackageFlowFile(bais, baos); byte[] contentReceived = baos.toByteArray(); assertEquals("Hello", new String(contentReceived)); assertEquals("cba", receivedAttrs.get("abc")); assertTrue(unpacker.hasMoreData()); receivedAttrs = unpacker.unpackageFlowFile(bais, baos); contentReceived = baos.toByteArray();
final FlowFileUnpackager unpackager; if (APPLICATION_FLOW_FILE_V3.equals(contentType)) { unpackager = new FlowFileUnpackagerV3(); } else if (APPLICATION_FLOW_FILE_V2.equals(contentType)) { unpackager = new FlowFileUnpackagerV2();
final ByteArrayInputStream bais = new ByteArrayInputStream(lastPost); FlowFileUnpackagerV3 unpacker = new FlowFileUnpackagerV3(); Map<String, String> receivedAttrs = unpacker.unpackageFlowFile(bais, baos); byte[] contentReceived = baos.toByteArray(); assertEquals("Hello", new String(contentReceived)); assertEquals("cba", receivedAttrs.get("abc")); assertTrue(unpacker.hasMoreData()); receivedAttrs = unpacker.unpackageFlowFile(bais, baos); contentReceived = baos.toByteArray();
break; case FLOWFILE_STREAM_FORMAT_V3: unpacker = new FlowFileStreamUnpacker(new FlowFileUnpackagerV3()); addFragmentAttrs = false; break;
final FlowFileUnpackager unpackager; if (APPLICATION_FLOW_FILE_V3.equals(contentType)) { unpackager = new FlowFileUnpackagerV3(); } else if (APPLICATION_FLOW_FILE_V2.equals(contentType)) { unpackager = new FlowFileUnpackagerV2();
break; case FLOWFILE_STREAM_FORMAT_V3: unpacker = new FlowFileStreamUnpacker(new FlowFileUnpackagerV3()); addFragmentAttrs = false; break;