use of java.io.UncheckedIOException in project elasticsearch by elastic.
the class StoredScriptSource method parse.
/**
* This will parse XContent into a {@link StoredScriptSource}. The following formats can be parsed:
*
* The simple script format with no compiler options or user-defined params:
*
* Example:
* {@code
* {"script": "return Math.log(doc.popularity) * 100;"}
* }
*
* The above format requires the lang to be specified using the deprecated stored script namespace
* (as a url parameter during a put request). See {@link ScriptMetaData} for more information about
* the stored script namespaces.
*
* The complex script format using the new stored script namespace
* where lang and code are required but options is optional:
*
* {@code
* {
* "script" : {
* "lang" : "<lang>",
* "code" : "<code>",
* "options" : {
* "option0" : "<option0>",
* "option1" : "<option1>",
* ...
* }
* }
* }
* }
*
* Example:
* {@code
* {
* "script": {
* "lang" : "painless",
* "code" : "return Math.log(doc.popularity) * params.multiplier"
* }
* }
* }
*
* The simple template format:
*
* {@code
* {
* "query" : ...
* }
* }
*
* The complex template format:
*
* {@code
* {
* "template": {
* "query" : ...
* }
* }
* }
*
* Note that templates can be handled as both strings and complex JSON objects.
* Also templates may be part of the 'code' parameter in a script. The Parser
* can handle this case as well.
*
* @param lang An optional parameter to allow for use of the deprecated stored
* script namespace. This will be used to specify the language
* coming in as a url parameter from a request or for stored templates.
* @param content The content from the request to be parsed as described above.
* @return The parsed {@link StoredScriptSource}.
*/
public static StoredScriptSource parse(String lang, BytesReference content, XContentType xContentType) {
try (XContentParser parser = xContentType.xContent().createParser(NamedXContentRegistry.EMPTY, content)) {
Token token = parser.nextToken();
if (token != Token.START_OBJECT) {
throw new ParsingException(parser.getTokenLocation(), "unexpected token [" + token + "], expected [{]");
}
token = parser.nextToken();
if (token != Token.FIELD_NAME) {
throw new ParsingException(parser.getTokenLocation(), "unexpected token [" + token + ", expected [" + SCRIPT_PARSE_FIELD.getPreferredName() + ", " + TEMPLATE_PARSE_FIELD.getPreferredName());
}
String name = parser.currentName();
if (SCRIPT_PARSE_FIELD.getPreferredName().equals(name)) {
token = parser.nextToken();
if (token == Token.VALUE_STRING) {
if (lang == null) {
throw new IllegalArgumentException("must specify lang as a url parameter when using the deprecated stored script namespace");
}
return new StoredScriptSource(lang, parser.text(), Collections.emptyMap());
} else if (token == Token.START_OBJECT) {
if (lang == null) {
return PARSER.apply(parser, null).build();
} else {
//this is really for search templates, that need to be converted to json format
try (XContentBuilder builder = XContentFactory.jsonBuilder()) {
builder.copyCurrentStructure(parser);
return new StoredScriptSource(lang, builder.string(), Collections.singletonMap(Script.CONTENT_TYPE_OPTION, XContentType.JSON.mediaType()));
}
}
} else {
throw new ParsingException(parser.getTokenLocation(), "unexpected token [" + token + "], expected [{, <code>]");
}
} else {
if (lang == null) {
throw new IllegalArgumentException("unexpected stored script format");
}
if (TEMPLATE_PARSE_FIELD.getPreferredName().equals(name)) {
token = parser.nextToken();
if (token == Token.VALUE_STRING) {
return new StoredScriptSource(lang, parser.text(), Collections.singletonMap(Script.CONTENT_TYPE_OPTION, XContentType.JSON.mediaType()));
}
}
try (XContentBuilder builder = XContentFactory.jsonBuilder()) {
if (token != Token.START_OBJECT) {
builder.startObject();
builder.copyCurrentStructure(parser);
builder.endObject();
} else {
builder.copyCurrentStructure(parser);
}
return new StoredScriptSource(lang, builder.string(), Collections.singletonMap(Script.CONTENT_TYPE_OPTION, XContentType.JSON.mediaType()));
}
}
} catch (IOException ioe) {
throw new UncheckedIOException(ioe);
}
}
use of java.io.UncheckedIOException in project disunity by ata4.
the class BundleUnpack method runFile.
@Override
protected void runFile(Path file) {
try (BundleReader bundleReader = new BundleReader(file)) {
Bundle bundle = bundleReader.read();
AtomicInteger done = new AtomicInteger();
long total = bundle.entryInfos().size();
// define output directory, if not yet defined
if (outputDir == null) {
// with sub-directories
if (bundle.entryInfos().size() == 1) {
outputDir = file.getParent();
if (outputDir == null) {
// Passed a filename only. Use the current directory.
outputDir = Paths.get(".");
}
} else {
String fileName = PathUtils.getBaseName(file);
outputDir = file.resolveSibling(fileName);
}
}
try {
bundle.entries().stream().filter(entry -> filename == null || entry.name().equals(filename)).forEach(uncheck(entry -> {
progress.update(Optional.of(entry.name()), done.getAndIncrement() / (double) total);
Path entryFile = outputDir.resolve(entry.name());
Files.createDirectories(entryFile.getParent());
Files.copy(entry.inputStream(), entryFile, REPLACE_EXISTING);
if (done.get() == total) {
progress.update(Optional.empty(), 1);
}
}));
} catch (UncheckedIOException ex) {
throw ex.getCause();
}
if (writeProp && filename == null) {
String bundleName = outputDir.getFileName().toString();
Path propsFile = outputDir.getParent().resolve(bundleName + ".json");
BundleProps.write(propsFile, bundle);
}
} catch (IOException ex) {
L.log(Level.WARNING, "Can't unpack asset bundle " + file, ex);
}
}
use of java.io.UncheckedIOException in project disunity by ata4.
the class AssetCommand method runFileRecursive.
@Override
protected void runFileRecursive(Path file) {
if (BundleUtils.isBundle(file)) {
// file is a bundle, load serialized files from it
try (BundleReader bundleReader = new BundleReader(file)) {
Bundle bundle = bundleReader.read();
bundle.entries().stream().filter(not(BundleEntry::isLibrary)).filter(not(BundleEntry::isResource)).forEach(uncheck(entry -> {
try (SerializedFileReader reader = new SerializedFileReader(BundleUtils.dataReaderForEntry(entry))) {
SerializedFile serialized = reader.read();
runSerializedFile(file.resolve(entry.name()), serialized);
}
}));
} catch (UncheckedIOException | IOException ex) {
L.log(Level.WARNING, "Can't open asset bundle " + file, ex);
}
} else {
// load file directly
try (SerializedFileReader reader = new SerializedFileReader(file)) {
SerializedFile serialized = reader.read();
runSerializedFile(file, serialized);
} catch (IOException ex) {
L.log(Level.WARNING, "Can't open asset file " + file, ex);
}
}
}
use of java.io.UncheckedIOException in project elasticsearch by elastic.
the class InternalEngineTests method testHandleDocumentFailure.
public void testHandleDocumentFailure() throws Exception {
try (Store store = createStore()) {
final ParsedDocument doc1 = testParsedDocument("1", "test", null, testDocumentWithTextField(), B_1, null);
final ParsedDocument doc2 = testParsedDocument("2", "test", null, testDocumentWithTextField(), B_1, null);
final ParsedDocument doc3 = testParsedDocument("3", "test", null, testDocumentWithTextField(), B_1, null);
AtomicReference<ThrowingIndexWriter> throwingIndexWriter = new AtomicReference<>();
try (Engine engine = createEngine(defaultSettings, store, createTempDir(), NoMergePolicy.INSTANCE, (directory, iwc) -> {
throwingIndexWriter.set(new ThrowingIndexWriter(directory, iwc));
return throwingIndexWriter.get();
})) {
// test document failure while indexing
if (randomBoolean()) {
throwingIndexWriter.get().setThrowFailure(() -> new IOException("simulated"));
} else {
throwingIndexWriter.get().setThrowFailure(() -> new IllegalArgumentException("simulated max token length"));
}
Engine.IndexResult indexResult = engine.index(indexForDoc(doc1));
assertNotNull(indexResult.getFailure());
throwingIndexWriter.get().clearFailure();
indexResult = engine.index(indexForDoc(doc1));
assertNull(indexResult.getFailure());
engine.index(indexForDoc(doc2));
// all these simulated exceptions are not fatal to the IW so we treat them as document failures
if (randomBoolean()) {
throwingIndexWriter.get().setThrowFailure(() -> new IOException("simulated"));
expectThrows(IOException.class, () -> engine.delete(new Engine.Delete("test", "1", newUid(doc1))));
} else {
throwingIndexWriter.get().setThrowFailure(() -> new IllegalArgumentException("simulated max token length"));
expectThrows(IllegalArgumentException.class, () -> engine.delete(new Engine.Delete("test", "1", newUid(doc1))));
}
// test non document level failure is thrown
if (randomBoolean()) {
// simulate close by corruption
throwingIndexWriter.get().setThrowFailure(null);
UncheckedIOException uncheckedIOException = expectThrows(UncheckedIOException.class, () -> {
Engine.Index index = indexForDoc(doc3);
index.parsedDoc().rootDoc().add(new StoredField("foo", "bar") {
// this is a hack to add a failure during store document which triggers a tragic event
// and in turn fails the engine
@Override
public BytesRef binaryValue() {
throw new UncheckedIOException(new MockDirectoryWrapper.FakeIOException());
}
});
engine.index(index);
});
assertTrue(uncheckedIOException.getCause() instanceof MockDirectoryWrapper.FakeIOException);
} else {
// normal close
engine.close();
}
// now the engine is closed check we respond correctly
try {
if (randomBoolean()) {
engine.index(indexForDoc(doc1));
} else {
engine.delete(new Engine.Delete("test", "", newUid(doc1)));
}
fail("engine should be closed");
} catch (Exception e) {
assertThat(e, instanceOf(AlreadyClosedException.class));
}
}
}
}
use of java.io.UncheckedIOException in project elasticsearch by elastic.
the class Ec2DiscoveryPluginTests method testNodeAttributesBogusUrl.
public void testNodeAttributesBogusUrl() {
UncheckedIOException e = expectThrows(UncheckedIOException.class, () -> getNodeAttributes(Settings.EMPTY, "bogus"));
assertNotNull(e.getCause());
String msg = e.getCause().getMessage();
assertTrue(msg, msg.contains("no protocol: bogus"));
}
Aggregations