@Override public InputStream loadResource(String location) { // delegate to good ol' TCCL return IOUtils.open(location); }
public static String[] getFilters(Settings settings) { return IOUtils.deserializeFromBase64(settings.getProperty(InternalConfigurationOptions.INTERNAL_ES_QUERY_FILTERS)); }
@Override public void checkSchema(ResourceSchema s) throws IOException { Properties props = getUDFProperties(); // save schema to back-end for JSON translation if (!StringUtils.hasText(props.getProperty(ResourceSchema.class.getName()))) { // save the schema as String (used JDK serialization since toString() screws up the signature - see the testcase) props.setProperty(ResourceSchema.class.getName(), IOUtils.serializeToBase64(s)); } }
public static BytesArray asBytes(InputStream in) throws IOException { BytesArray ba = unwrapStreamBuffer(in); if (ba != null) { return ba; } return asBytes(new BytesArray(in.available()), in); }
private KeyStore loadKeyStore(String location, char[] pass) throws GeneralSecurityException, IOException { KeyStore keyStore = KeyStore.getInstance(keyStoreType); InputStream in = null; try { if (LOG.isDebugEnabled()) { LOG.debug("Loading keystore located at [" + location + "]"); } try { in = IOUtils.open(location); if (in == null) { throw new EsHadoopIllegalArgumentException(String.format("Could not locate [%s] on classpath", location)); } } catch (Exception e) { throw new EsHadoopIllegalArgumentException(String.format("Expected to find keystore file at [%s] but " + "was unable to. Make sure that it is available on the classpath, or if not, that you have " + "specified a valid URI.", location)); } keyStore.load(in, pass); } finally { IOUtils.close(in); } return keyStore; }
@Test public void testSchemaSerializationPlusBase64() throws Exception { Schema schemaFromString = Utils.getSchemaFromString("name:bytearray,links:{(missing:chararray)}"); Schema schemaSaved = IOUtils.deserializeFromBase64(IOUtils.serializeToBase64(schemaFromString)); assertEquals(schemaFromString.toString(), schemaSaved.toString()); }
@Override public String getRawEvent(DeserializationFailure event) throws IOException { return IOUtils.asString(event.getHitContents()); } }
public static String asStringAlways(InputStream in) { if (in == null) { return StringUtils.EMPTY; } try { return asBytes(in).toString(); } catch (IOException ex) { return StringUtils.EMPTY; } }
public static String serializeToBase64(Serializable object) { if (object == null) { return StringUtils.EMPTY; } FastByteArrayOutputStream baos = new FastByteArrayOutputStream(); ObjectOutputStream oos = null; try { oos = new ObjectOutputStream(baos); oos.writeObject(object); } catch (IOException ex) { throw new EsHadoopSerializationException("Cannot serialize object " + object, ex); } finally { close(oos); } return DatatypeConverter.printBase64Binary(baos.bytes().bytes()); }
private static BytesArray unwrapStreamBuffer(InputStream in) { if (in instanceof FastByteArrayInputStream) { return ((FastByteArrayInputStream) in).data; } if (in instanceof ByteArrayInputStream) { return new BytesArray(byteArrayInputStreamInternalBuffer((ByteArrayInputStream) in)); } return null; }
public Settings load(String source) { Properties copy = IOUtils.propsFromString(source); merge(copy); return this; }
public String save() { Properties copy = asProperties(); return IOUtils.propsToString(copy); }
private void checkResponse(Request request, Response response) { if (response.hasFailed()) { // check error first String msg = null; // try to parse the answer try { EsHadoopException ex = errorExtractor.extractError(this.<Map> parseContent(response.body(), null)); msg = (ex != null) ? ex.toString() : null; if (response.isClientError()) { msg = msg + "\n" + request.body(); } else { msg = errorExtractor.prettify(msg, request.body()); } } catch (Exception ex) { // can't parse message, move on } if (!StringUtils.hasText(msg)) { msg = String.format("[%s] on [%s] failed; server[%s] returned [%s|%s:%s]", request.method().name(), request.path(), response.uri(), response.status(), response.statusDescription(), IOUtils.asStringAlways(response.body())); } throw new EsHadoopInvalidRequest(msg); } }
/** * Visible for testing */ @Override public String getRawEvent(BulkWriteFailure event) throws IOException { return IOUtils.asString(event.getEntryContents()); } }
public static BytesArray asBytes(InputStream in) throws IOException { BytesArray ba = unwrapStreamBuffer(in); if (ba != null) { return ba; } return asBytes(new BytesArray(in.available()), in); }
public static String asString(InputStream in) throws IOException { return asBytes(in).toString(); }
@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); } }
private static BytesArray unwrapStreamBuffer(InputStream in) { if (in instanceof FastByteArrayInputStream) { return ((FastByteArrayInputStream) in).data; } if (in instanceof ByteArrayInputStream) { return new BytesArray(byteArrayInputStreamInternalBuffer((ByteArrayInputStream) in)); } return null; }
public Settings load(String source) { Properties copy = IOUtils.propsFromString(source); merge(copy); return this; }