Search in sources :

Example 91 with UncheckedExecutionException

use of com.google.common.util.concurrent.UncheckedExecutionException in project nifi-minifi by apache.

the class ConfigService method getConfig.

@GET
public Response getConfig(@Context HttpServletRequest request, @Context HttpHeaders httpHeaders, @Context UriInfo uriInfo) {
    try {
        authorizer.authorize(SecurityContextHolder.getContext().getAuthentication(), uriInfo);
    } catch (AuthorizationException e) {
        logger.warn(HttpRequestUtil.getClientString(request) + " not authorized to access " + uriInfo, e);
        return Response.status(403).build();
    }
    Map<String, List<String>> parameters = new HashMap<>();
    for (Map.Entry<String, List<String>> entry : uriInfo.getQueryParameters().entrySet()) {
        parameters.put(entry.getKey(), entry.getValue());
    }
    List<MediaType> acceptValues = httpHeaders.getAcceptableMediaTypes();
    boolean defaultAccept = false;
    if (acceptValues.size() == 0) {
        acceptValues = Arrays.asList(MediaType.WILDCARD_TYPE);
        defaultAccept = true;
    }
    if (logger.isDebugEnabled()) {
        StringBuilder builder = new StringBuilder("Handling request from ").append(HttpRequestUtil.getClientString(request)).append(" with parameters ").append(parameters).append(" and Accept");
        if (defaultAccept) {
            builder = builder.append(" default value");
        }
        builder = builder.append(": ").append(acceptValues.stream().map(Object::toString).collect(Collectors.joining(", ")));
        logger.debug(builder.toString());
    }
    try {
        ConfigurationProviderValue configurationProviderValue = configurationCache.get(new ConfigurationProviderKey(acceptValues, parameters));
        Configuration configuration = configurationProviderValue.getConfiguration();
        Response.ResponseBuilder ok = Response.ok();
        ok = ok.header("X-Content-Version", configuration.getVersion());
        ok = ok.type(configurationProviderValue.getMediaType());
        byte[] buffer = new byte[1024];
        int read;
        try (InputStream inputStream = configuration.getInputStream();
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            MessageDigest sha256 = MessageDigest.getInstance("SHA-256");
            while ((read = inputStream.read(buffer)) >= 0) {
                outputStream.write(buffer, 0, read);
                md5.update(buffer, 0, read);
                sha256.update(buffer, 0, read);
            }
            ok = ok.header("Content-MD5", bytesToHex(md5.digest()));
            ok = ok.header("X-Content-SHA-256", bytesToHex(sha256.digest()));
            ok = ok.entity(outputStream.toByteArray());
        } catch (ConfigurationProviderException | IOException | NoSuchAlgorithmException e) {
            logger.error("Error reading or checksumming configuration file", e);
            throw new WebApplicationException(500);
        }
        return ok.build();
    } catch (AuthorizationException e) {
        logger.warn(HttpRequestUtil.getClientString(request) + " not authorized to access " + uriInfo, e);
        return Response.status(403).build();
    } catch (InvalidParameterException e) {
        logger.info(HttpRequestUtil.getClientString(request) + " made invalid request with " + HttpRequestUtil.getQueryString(request), e);
        return Response.status(400).entity("Invalid request.").build();
    } catch (ConfigurationProviderException e) {
        logger.warn("Unable to get configuration.", e);
        return Response.status(500).build();
    } catch (ExecutionException | UncheckedExecutionException e) {
        Throwable cause = e.getCause();
        if (cause instanceof WebApplicationException) {
            throw (WebApplicationException) cause;
        }
        logger.error(HttpRequestUtil.getClientString(request) + " made request with " + HttpRequestUtil.getQueryString(request) + " that caused error.", cause);
        return Response.status(500).entity("Internal error").build();
    }
}
Also used : Configuration(org.apache.nifi.minifi.c2.api.Configuration) WebApplicationException(javax.ws.rs.WebApplicationException) AuthorizationException(org.apache.nifi.minifi.c2.api.security.authorization.AuthorizationException) HashMap(java.util.HashMap) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) ConfigurationProviderException(org.apache.nifi.minifi.c2.api.ConfigurationProviderException) InvalidParameterException(org.apache.nifi.minifi.c2.api.InvalidParameterException) MediaType(javax.ws.rs.core.MediaType) ArrayList(java.util.ArrayList) List(java.util.List) MessageDigest(java.security.MessageDigest) UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException) ExecutionException(java.util.concurrent.ExecutionException) UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) Response(javax.ws.rs.core.Response) HashMap(java.util.HashMap) Map(java.util.Map) GET(javax.ws.rs.GET)

Example 92 with UncheckedExecutionException

use of com.google.common.util.concurrent.UncheckedExecutionException in project mycore by MyCoRe-Org.

the class MCRFileSystemProvider method resolvePath.

static MCRFilesystemNode resolvePath(MCRPath path) throws IOException {
    try {
        String ifsid = nodeCache.getUnchecked(path);
        MCRFilesystemNode node = MCRFilesystemNode.getNode(ifsid);
        if (node != null) {
            return node;
        }
        nodeCache.invalidate(path);
        return resolvePath(path);
    } catch (UncheckedExecutionException e) {
        Throwable cause = e.getCause();
        if (cause instanceof NoSuchFileException) {
            throw (NoSuchFileException) cause;
        }
        if (cause instanceof NotDirectoryException) {
            throw (NotDirectoryException) cause;
        }
        if (cause instanceof IOException) {
            throw (IOException) cause;
        }
        throw e;
    }
}
Also used : NotDirectoryException(java.nio.file.NotDirectoryException) MCRFilesystemNode(org.mycore.datamodel.ifs.MCRFilesystemNode) UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException) NoSuchFileException(java.nio.file.NoSuchFileException) IOException(java.io.IOException)

Example 93 with UncheckedExecutionException

use of com.google.common.util.concurrent.UncheckedExecutionException in project java-driver by datastax.

the class CodecRegistry method lookupCodec.

@SuppressWarnings("unchecked")
private <T> TypeCodec<T> lookupCodec(DataType cqlType, TypeToken<T> javaType) {
    checkNotNull(cqlType, "Parameter cqlType cannot be null");
    TypeCodec<?> codec = BUILT_IN_CODECS_MAP.get(cqlType.getName());
    if (codec != null && (javaType == null || codec.accepts(javaType))) {
        logger.trace("Returning built-in codec {}", codec);
        return (TypeCodec<T>) codec;
    }
    if (logger.isTraceEnabled())
        logger.trace("Querying cache for codec [{} <-> {}]", toString(cqlType), toString(javaType));
    try {
        CacheKey cacheKey = new CacheKey(cqlType, javaType);
        codec = cache.get(cacheKey);
    } catch (UncheckedExecutionException e) {
        if (e.getCause() instanceof CodecNotFoundException) {
            throw (CodecNotFoundException) e.getCause();
        }
        throw new CodecNotFoundException(e.getCause(), cqlType, javaType);
    } catch (RuntimeException e) {
        throw new CodecNotFoundException(e.getCause(), cqlType, javaType);
    } catch (ExecutionException e) {
        throw new CodecNotFoundException(e.getCause(), cqlType, javaType);
    }
    logger.trace("Returning cached codec {}", codec);
    return (TypeCodec<T>) codec;
}
Also used : UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException) CodecNotFoundException(com.datastax.driver.core.exceptions.CodecNotFoundException) ExecutionException(java.util.concurrent.ExecutionException) UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException)

Example 94 with UncheckedExecutionException

use of com.google.common.util.concurrent.UncheckedExecutionException in project presto by prestodb.

the class BuiltInTypeAndFunctionNamespaceManager method getWindowFunctionImplementation.

public WindowFunctionSupplier getWindowFunctionImplementation(FunctionHandle functionHandle) {
    checkArgument(functionHandle instanceof BuiltInFunctionHandle, "Expect BuiltInFunctionHandle");
    Signature signature = ((BuiltInFunctionHandle) functionHandle).getSignature();
    checkArgument(signature.getKind() == WINDOW || signature.getKind() == AGGREGATE, "%s is not a window function", signature);
    checkArgument(signature.getTypeVariableConstraints().isEmpty(), "%s has unbound type parameters", signature);
    try {
        return specializedWindowCache.getUnchecked(getSpecializedFunctionKey(signature));
    } catch (UncheckedExecutionException e) {
        throwIfInstanceOf(e.getCause(), PrestoException.class);
        throw e;
    }
}
Also used : UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException) TypeSignature.parseTypeSignature(com.facebook.presto.common.type.TypeSignature.parseTypeSignature) Signature(com.facebook.presto.spi.function.Signature) TypeSignature(com.facebook.presto.common.type.TypeSignature) PrestoException(com.facebook.presto.spi.PrestoException)

Example 95 with UncheckedExecutionException

use of com.google.common.util.concurrent.UncheckedExecutionException in project cassandra by apache.

the class CodecRegistry method lookupCodec.

@SuppressWarnings("unchecked")
private <T> TypeCodec<T> lookupCodec(DataType cqlType, TypeToken<T> javaType) {
    checkNotNull(cqlType, "Parameter cqlType cannot be null");
    TypeCodec<?> codec = BUILT_IN_CODECS_MAP.get(cqlType.getName());
    if (codec != null && (javaType == null || codec.accepts(javaType))) {
        logger.trace("Returning built-in codec {}", codec);
        return (TypeCodec<T>) codec;
    }
    if (logger.isTraceEnabled())
        logger.trace("Querying cache for codec [{} <-> {}]", toString(cqlType), toString(javaType));
    try {
        CacheKey cacheKey = new CacheKey(cqlType, javaType);
        codec = cache.get(cacheKey);
    } catch (UncheckedExecutionException e) {
        if (e.getCause() instanceof CodecNotFoundException) {
            throw (CodecNotFoundException) e.getCause();
        }
        throw new CodecNotFoundException(e.getCause());
    } catch (RuntimeException | ExecutionException e) {
        throw new CodecNotFoundException(e.getCause());
    }
    logger.trace("Returning cached codec {}", codec);
    return (TypeCodec<T>) codec;
}
Also used : UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException) CodecNotFoundException(org.apache.cassandra.cql3.functions.types.exceptions.CodecNotFoundException) ExecutionException(java.util.concurrent.ExecutionException) UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException)

Aggregations

UncheckedExecutionException (com.google.common.util.concurrent.UncheckedExecutionException)101 ExecutionException (java.util.concurrent.ExecutionException)60 IOException (java.io.IOException)31 InvalidCacheLoadException (com.google.common.cache.CacheLoader.InvalidCacheLoadException)22 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)10 Test (org.junit.Test)9 Map (java.util.Map)8 ArrayList (java.util.ArrayList)7 File (java.io.File)6 TimeoutException (java.util.concurrent.TimeoutException)6 Stopwatch (com.google.common.base.Stopwatch)5 ImmutableMap (com.google.common.collect.ImmutableMap)5 HashMap (java.util.HashMap)5 List (java.util.List)5 NoSuchElementException (java.util.NoSuchElementException)5 CountDownLatch (java.util.concurrent.CountDownLatch)5 InputStream (java.io.InputStream)4 Callable (java.util.concurrent.Callable)4 SirixIOException (org.sirix.exception.SirixIOException)4 TypeSignature (com.facebook.presto.common.type.TypeSignature)3