Search in sources :

Example 1 with FormattedRuntimeException

use of com.b2international.commons.exceptions.FormattedRuntimeException in project snow-owl by b2ihealthcare.

the class EsDocumentSearcher method fromSearchAfterToken.

private Object[] fromSearchAfterToken(final String searchAfterToken) throws BadRequestException {
    if (Strings.isNullOrEmpty(searchAfterToken)) {
        return null;
    }
    final byte[] decodedToken = Base64.getUrlDecoder().decode(searchAfterToken);
    try (final DataInputStream dis = new DataInputStream(new ByteArrayInputStream(decodedToken))) {
        JavaBinCodec codec = new JavaBinCodec();
        List<?> obj = (List<?>) codec.unmarshal(dis);
        codec.close();
        return obj.toArray();
    } catch (final IOException e) {
        throw new FormattedRuntimeException("Couldn't decode searchAfter token.", e);
    } catch (final IllegalArgumentException e) {
        throw new BadRequestException("Invalid 'searchAfter' parameter value '%s'", searchAfterToken);
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) FormattedRuntimeException(com.b2international.commons.exceptions.FormattedRuntimeException) BadRequestException(com.b2international.commons.exceptions.BadRequestException) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream) JavaBinCodec(org.apache.solr.common.util.JavaBinCodec)

Example 2 with FormattedRuntimeException

use of com.b2international.commons.exceptions.FormattedRuntimeException in project snow-owl by b2ihealthcare.

the class SnomedNamespaceAndModuleAssigner method create.

/**
 * Instantiate a namespace and module assigner instance based on the given type.
 *
 * @param context
 * @param assignerType
 * @param moduleId
 * @param namespace
 * @return
 */
static SnomedNamespaceAndModuleAssigner create(ServiceProvider context, String assignerType, String moduleId, String namespace) {
    SnomedNamespaceAndModuleAssigner assigner = context.service(ClassPathScanner.class).getComponentsByInterface(SnomedNamespaceAndModuleAssigner.class).stream().filter(a -> assignerType.equals(a.getName())).findFirst().orElseThrow(() -> new FormattedRuntimeException("Couldn't find namespace and module assigner '%s'.", assignerType));
    assigner.init(namespace, moduleId);
    return assigner;
}
Also used : FormattedRuntimeException(com.b2international.commons.exceptions.FormattedRuntimeException) ClassPathScanner(com.b2international.snowowl.core.plugin.ClassPathScanner)

Example 3 with FormattedRuntimeException

use of com.b2international.commons.exceptions.FormattedRuntimeException in project snow-owl by b2ihealthcare.

the class EsDocumentSearcher method toSearchAfterToken.

private String toSearchAfterToken(final Object[] searchAfter) {
    if (searchAfter == null) {
        return null;
    }
    try (final ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
        final JavaBinCodec codec = new JavaBinCodec();
        codec.marshal(searchAfter, baos);
        codec.close();
        final byte[] tokenBytes = baos.toByteArray();
        return Base64.getUrlEncoder().encodeToString(tokenBytes);
    } catch (IOException e) {
        throw new FormattedRuntimeException("Couldn't encode searchAfter parameters to a token.", e);
    }
}
Also used : FormattedRuntimeException(com.b2international.commons.exceptions.FormattedRuntimeException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) JavaBinCodec(org.apache.solr.common.util.JavaBinCodec)

Aggregations

FormattedRuntimeException (com.b2international.commons.exceptions.FormattedRuntimeException)3 IOException (java.io.IOException)2 JavaBinCodec (org.apache.solr.common.util.JavaBinCodec)2 BadRequestException (com.b2international.commons.exceptions.BadRequestException)1 ClassPathScanner (com.b2international.snowowl.core.plugin.ClassPathScanner)1 Lists.newArrayList (com.google.common.collect.Lists.newArrayList)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 DataInputStream (java.io.DataInputStream)1