Search in sources :

Example 36 with UncheckedIOException

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);
    }
}
Also used : ParsingException(org.elasticsearch.common.ParsingException) Token(org.elasticsearch.common.xcontent.XContentParser.Token) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) XContentParser(org.elasticsearch.common.xcontent.XContentParser) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Example 37 with UncheckedIOException

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);
    }
}
Also used : LogUtils(info.ata4.log.LogUtils) Parameters(com.beust.jcommander.Parameters) IOConsumer.uncheck(info.ata4.util.function.IOConsumer.uncheck) Parameter(com.beust.jcommander.Parameter) Files(java.nio.file.Files) IOException(java.io.IOException) Logger(java.util.logging.Logger) Level(java.util.logging.Level) UncheckedIOException(java.io.UncheckedIOException) FileCommand(info.ata4.disunity.cli.command.FileCommand) PathConverter(info.ata4.disunity.cli.converters.PathConverter) Bundle(info.ata4.junity.bundle.Bundle) Paths(java.nio.file.Paths) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) PathUtils(info.ata4.io.util.PathUtils) Optional(java.util.Optional) BundleReader(info.ata4.junity.bundle.BundleReader) Path(java.nio.file.Path) REPLACE_EXISTING(java.nio.file.StandardCopyOption.REPLACE_EXISTING) Path(java.nio.file.Path) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Bundle(info.ata4.junity.bundle.Bundle) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) BundleReader(info.ata4.junity.bundle.BundleReader)

Example 38 with UncheckedIOException

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);
        }
    }
}
Also used : RecursiveFileCommand(info.ata4.disunity.cli.command.RecursiveFileCommand) LogUtils(info.ata4.log.LogUtils) IOConsumer.uncheck(info.ata4.util.function.IOConsumer.uncheck) IOException(java.io.IOException) Logger(java.util.logging.Logger) BundleUtils(info.ata4.junity.bundle.BundleUtils) SerializedFile(info.ata4.junity.serialize.SerializedFile) Predicates.not(info.ata4.util.function.Predicates.not) Level(java.util.logging.Level) BundleEntry(info.ata4.junity.bundle.BundleEntry) UncheckedIOException(java.io.UncheckedIOException) Bundle(info.ata4.junity.bundle.Bundle) SerializedFileReader(info.ata4.junity.serialize.SerializedFileReader) BundleReader(info.ata4.junity.bundle.BundleReader) Path(java.nio.file.Path) SerializedFileReader(info.ata4.junity.serialize.SerializedFileReader) SerializedFile(info.ata4.junity.serialize.SerializedFile) Bundle(info.ata4.junity.bundle.Bundle) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) BundleEntry(info.ata4.junity.bundle.BundleEntry) BundleReader(info.ata4.junity.bundle.BundleReader)

Example 39 with UncheckedIOException

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));
            }
        }
    }
}
Also used : MockDirectoryWrapper(org.apache.lucene.store.MockDirectoryWrapper) Store(org.elasticsearch.index.store.Store) AtomicReference(java.util.concurrent.atomic.AtomicReference) UncheckedIOException(java.io.UncheckedIOException) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) AlreadyClosedException(org.apache.lucene.store.AlreadyClosedException) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) BrokenBarrierException(java.util.concurrent.BrokenBarrierException) ElasticsearchException(org.elasticsearch.ElasticsearchException) StoredField(org.apache.lucene.document.StoredField) ParsedDocument(org.elasticsearch.index.mapper.ParsedDocument) BytesRef(org.apache.lucene.util.BytesRef)

Example 40 with UncheckedIOException

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"));
}
Also used : UncheckedIOException(java.io.UncheckedIOException)

Aggregations

UncheckedIOException (java.io.UncheckedIOException)76 IOException (java.io.IOException)72 Path (java.nio.file.Path)13 InputStream (java.io.InputStream)7 ArrayList (java.util.ArrayList)7 Test (org.junit.Test)7 File (java.io.File)6 Arrays (java.util.Arrays)6 HashMap (java.util.HashMap)6 Map (java.util.Map)6 List (java.util.List)5 Collectors (java.util.stream.Collectors)5 BufferedReader (java.io.BufferedReader)4 URL (java.net.URL)4 IntStream (java.util.stream.IntStream)4 Protein (de.bioforscher.jstructure.model.structure.Protein)3 InterruptedIOException (java.io.InterruptedIOException)3 Random (java.util.Random)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3