/** * @return the scroll search hit in the original raw JSON format */ public InputStream getHitContents() { return new FastByteArrayInputStream(hit); } }
/** * @return serialized bulk entry in byte array format */ public InputStream getEntryContents() { return new FastByteArrayInputStream(contents); }
@Override protected InputStream openRead() throws FileNotFoundException { return new FastByteArrayInputStream(fileBytes); }
static PartitionDefinition readSerializablePartition(BytesArray bytes) throws IOException, ClassNotFoundException { FastByteArrayInputStream in = new FastByteArrayInputStream(bytes); ObjectInputStream ois = new ObjectInputStream(in); return (PartitionDefinition) ois.readObject(); }
@SuppressWarnings("unchecked") public static <T extends Serializable> T deserializeFromBase64(String data) { if (!StringUtils.hasLength(data)) { return null; } byte[] rawData = DatatypeConverter.parseBase64Binary(data); ObjectInputStream ois = null; try { ois = new ObjectInputStream(new FastByteArrayInputStream(rawData)); Object o = ois.readObject(); return (T) o; } catch (ClassNotFoundException ex) { throw new EsHadoopIllegalStateException("cannot deserialize object", ex); } catch (IOException ex) { throw new EsHadoopSerializationException("cannot deserialize object", ex); } finally { close(ois); } }
public Scroll read(InputStream content) throws IOException { Assert.notNull(content); //copy content BytesArray copy = IOUtils.asBytes(content); content = new FastByteArrayInputStream(copy); if (log.isTraceEnabled()) { log.trace("About to parse scroll content " + copy); } Parser parser = new JacksonJsonParser(content); try { return read(parser, copy); } finally { parser.close(); } }
@Before public void setup() { BytesArray all = new BytesArray(new byte[]{1, 2, 3, 4, 5, 6}); BytesArray middleFour = new BytesArray(all.bytes, 1, 4); whole = new FastByteArrayInputStream(all); middle = new FastByteArrayInputStream(middleFour); }
static PartitionDefinition readWritablePartition(BytesArray bytes) throws IOException { FastByteArrayInputStream in = new FastByteArrayInputStream(bytes); try { DataInputStream di = new DataInputStream(in); return new PartitionDefinition(di); } finally { in.close(); } }
@Before public void before() throws Exception { InputStream in = new FastByteArrayInputStream(IOUtils.asBytes(getClass().getResourceAsStream("parser-test-nested.json"))); parser = new JacksonJsonParser(in); }
public static Map<String, Object> asMap(InputStream inputStream) { Map<String, Object> map; try { // create parser manually to lower Jackson requirements JsonParser jsonParser = MAPPER.getJsonFactory().createJsonParser(inputStream); map = MAPPER.readValue(jsonParser, Map.class); } catch (IOException ex) { throw new EsHadoopParsingException(ex); } return map; }
@Test public void testEmptyArrayReadWrite() throws Exception { ArrayWritable array = new WritableArrayWritable(Text.class); FastByteArrayOutputStream out = new FastByteArrayOutputStream(); DataOutputStream da = new DataOutputStream(out); array.write(da); da.close(); WritableArrayWritable waw = new WritableArrayWritable(NullWritable.class); waw.readFields(new DataInputStream(new FastByteArrayInputStream(out.bytes()))); assertSame(array.getValueClass(), waw.getValueClass()); } }
@Test public void testMapWithArrayReadWrite() throws Exception { LinkedMapWritable written = new LinkedMapWritable(); ArrayWritable array = new WritableArrayWritable(Text.class); array.set(new Writable[] { new Text("one") , new Text("two"), new Text("three")} ); written.put(new Text("foo"), array); FastByteArrayOutputStream out = new FastByteArrayOutputStream(); DataOutputStream da = new DataOutputStream(out); written.write(da); da.close(); LinkedMapWritable read = new LinkedMapWritable(); read.readFields(new DataInputStream(new FastByteArrayInputStream(out.bytes()))); assertThat(read.size(), is(written.size())); assertThat(read.toString(), is(written.toString())); }
@Test public void testPostTypelessDocumentSuccess() throws Exception { String index = "index"; Settings settings = new TestSettings(); settings.setInternalVersion(EsMajorVersion.V_7_X); settings.setResourceWrite(index); Resource writeResource = new Resource(settings, false); BytesArray document = new BytesArray("{\"field\":\"value\"}"); SimpleRequest request = new SimpleRequest(Request.Method.POST, null, index + "/_doc", null, document); String response = "{\n" + " \"_index\": \"index\",\n" + " \"_type\": \"_doc\",\n" + " \"_id\": \"AbcDefGhiJklMnoPqrS_\",\n" + " \"_version\": 1,\n" + " \"result\": \"created\",\n" + " \"_shards\": {\n" + " \"total\": 2,\n" + " \"successful\": 1,\n" + " \"failed\": 0\n" + " },\n" + " \"_seq_no\": 0,\n" + " \"_primary_term\": 1\n" + "}"; NetworkClient mock = Mockito.mock(NetworkClient.class); Mockito.when(mock.execute(Mockito.eq(request))).thenReturn(new SimpleResponse(201, new FastByteArrayInputStream(new BytesArray(response)), "localhost:9200")); RestClient client = new RestClient(new TestSettings(), mock); String id = client.postDocument(writeResource, document); assertEquals("AbcDefGhiJklMnoPqrS_", id); }
@Test(expected = EsHadoopInvalidRequest.class) public void testPostTypelessDocumentWeirdness() throws Exception { String index = "index"; Settings settings = new TestSettings(); settings.setInternalVersion(EsMajorVersion.V_7_X); settings.setResourceWrite(index); Resource writeResource = new Resource(settings, false); BytesArray document = new BytesArray("{\"field\":\"value\"}"); SimpleRequest request = new SimpleRequest(Request.Method.POST, null, index + "/_doc", null, document); String response = "{\n" + " \"_index\": \"index\",\n" + " \"_type\": \"_doc\",\n" + " \"definitely_not_an_id\": \"AbcDefGhiJklMnoPqrS_\",\n" + // Make the ID go away " \"_version\": 1,\n" + " \"result\": \"created\",\n" + " \"_shards\": {\n" + " \"total\": 2,\n" + " \"successful\": 1,\n" + " \"failed\": 0\n" + " },\n" + " \"_seq_no\": 0,\n" + " \"_primary_term\": 1\n" + "}"; NetworkClient mock = Mockito.mock(NetworkClient.class); Mockito.when(mock.execute(Mockito.eq(request))).thenReturn(new SimpleResponse(201, new FastByteArrayInputStream(new BytesArray(response)), "localhost:9200")); RestClient client = new RestClient(new TestSettings(), mock); String id = client.postDocument(writeResource, document); assertEquals("AbcDefGhiJklMnoPqrS_", id); }
@Test public void addKeyStdIn() throws Exception { BytesArray storeData = new BytesArray(128); KeystoreWrapper ks = KeystoreWrapper.newStore().build(); ks.saveKeystore(new FastByteArrayOutputStream(storeData)); TestPrompt console = new TestPrompt(); console.addInput("blahh"); KeytoolHarness keytool = new KeytoolHarness(console, Keytool.Command.ADD, true, storeData); assertThat(keytool.run("test.password.1", true, false), equalTo(0)); assertThat(console.getOutput(), equalTo("")); assertThat(console.hasInputLeft(), is(false)); assertThat(keytool.ksExists(), is(true)); assertThat(keytool.fileBytes.length(), is(not(0))); ks = KeystoreWrapper.loadStore(new FastByteArrayInputStream(keytool.fileBytes)).build(); assertThat(ks.getSecureSetting("test.password.1"), equalTo("blahh")); }
@Test public void removeKey() throws Exception { BytesArray storeData = new BytesArray(128); KeystoreWrapper ks = KeystoreWrapper.newStore().build(); ks.setSecureSetting("test.password.1", "bar"); ks.setSecureSetting("test.password.2", "foo"); ks.saveKeystore(new FastByteArrayOutputStream(storeData)); TestPrompt console = new TestPrompt(); KeytoolHarness keytool = new KeytoolHarness(console, Keytool.Command.REMOVE, true, storeData); assertThat(keytool.run("test.password.1", false, false), equalTo(0)); assertThat(console.getOutput(), equalTo("")); assertThat(console.hasInputLeft(), is(false)); assertThat(keytool.ksExists(), is(true)); assertThat(keytool.fileBytes.length(), is(not(0))); ks = KeystoreWrapper.loadStore(new FastByteArrayInputStream(keytool.fileBytes)).build(); assertThat(ks.containsEntry("test.password.1"), is(false)); assertThat(ks.getSecureSetting("test.password.2"), equalTo("foo")); }
@Test public void addKey() throws Exception { BytesArray storeData = new BytesArray(128); KeystoreWrapper ks = KeystoreWrapper.newStore().build(); ks.saveKeystore(new FastByteArrayOutputStream(storeData)); TestPrompt console = new TestPrompt(); console.addInput("blahh"); KeytoolHarness keytool = new KeytoolHarness(console, Keytool.Command.ADD, true, storeData); assertThat(keytool.run("test.password.1", false, false), equalTo(0)); assertThat(console.getOutput(), equalTo("")); assertThat(console.hasInputLeft(), is(false)); assertThat(keytool.ksExists(), is(true)); assertThat(keytool.fileBytes.length(), is(not(0))); ks = KeystoreWrapper.loadStore(new FastByteArrayInputStream(keytool.fileBytes)).build(); assertThat(ks.getSecureSetting("test.password.1"), equalTo("blahh")); }
@Test public void addExistingKeyForce() throws Exception { BytesArray storeData = new BytesArray(128); KeystoreWrapper ks = KeystoreWrapper.newStore().build(); ks.setSecureSetting("test.password.1", "blah"); ks.saveKeystore(new FastByteArrayOutputStream(storeData)); TestPrompt console = new TestPrompt(); console.addInput("blerb"); KeytoolHarness keytool = new KeytoolHarness(console, Keytool.Command.ADD, true, storeData); assertThat(keytool.run("test.password.1", false, true), equalTo(0)); assertThat(console.getOutput(), equalTo("")); assertThat(console.hasInputLeft(), is(false)); assertThat(keytool.ksExists(), is(true)); assertThat(keytool.fileBytes.length(), is(not(0))); ks = KeystoreWrapper.loadStore(new FastByteArrayInputStream(keytool.fileBytes)).build(); assertThat(ks.getSecureSetting("test.password.1"), equalTo("blerb")); }
@Test(expected = EsHadoopInvalidRequest.class) public void testPostDocumentWeirdness() throws Exception { String index = "index/type"; Settings settings = new TestSettings(); settings.setInternalVersion(EsMajorVersion.V_6_X); settings.setResourceWrite(index); Resource writeResource = new Resource(settings, false); BytesArray document = new BytesArray("{\"field\":\"value\"}"); SimpleRequest request = new SimpleRequest(Request.Method.POST, null, index, null, document); String response = "{\n" + " \"_index\": \"index\",\n" + " \"_type\": \"type\",\n" + " \"definitely_not_an_id\": \"AbcDefGhiJklMnoPqrS_\",\n" + // Make the ID go away " \"_version\": 1,\n" + " \"result\": \"created\",\n" + " \"_shards\": {\n" + " \"total\": 2,\n" + " \"successful\": 1,\n" + " \"failed\": 0\n" + " },\n" + " \"_seq_no\": 0,\n" + " \"_primary_term\": 1\n" + "}"; NetworkClient mock = Mockito.mock(NetworkClient.class); Mockito.when(mock.execute(Mockito.eq(request))).thenReturn(new SimpleResponse(201, new FastByteArrayInputStream(new BytesArray(response)), "localhost:9200")); RestClient client = new RestClient(new TestSettings(), mock); String id = client.postDocument(writeResource, document); assertEquals("AbcDefGhiJklMnoPqrS_", id); }
@Test public void addExistingKeyOverwrite() throws Exception { BytesArray storeData = new BytesArray(128); KeystoreWrapper ks = KeystoreWrapper.newStore().build(); ks.setSecureSetting("test.password.1", "blah"); ks.saveKeystore(new FastByteArrayOutputStream(storeData)); TestPrompt console = new TestPrompt(); console.addInput("y").addInput("blerb"); KeytoolHarness keytool = new KeytoolHarness(console, Keytool.Command.ADD, true, storeData); assertThat(keytool.run("test.password.1", false, false), equalTo(0)); assertThat(console.getOutput(), equalTo("")); assertThat(console.hasInputLeft(), is(false)); assertThat(keytool.ksExists(), is(true)); assertThat(keytool.fileBytes.length(), is(not(0))); ks = KeystoreWrapper.loadStore(new FastByteArrayInputStream(keytool.fileBytes)).build(); assertThat(ks.getSecureSetting("test.password.1"), equalTo("blerb")); }