Search in sources :

Example 21 with SafeRuntimeException

use of com.palantir.logsafe.exceptions.SafeRuntimeException in project conjure-java-runtime by palantir.

the class Pkcs1PrivateKeyReader method readRsaKey.

public RSAPrivateKeySpec readRsaKey() {
    byte tag = derBytes.get();
    if (tag != SEQUENCE) {
        throw new SafeRuntimeException("Expected SEQUENCE byte (0x30) at the beginning of RSA private key");
    }
    int length = readLength();
    // cap the read length
    derBytes.limit(derBytes.position() + length);
    BigInteger version = readNumber();
    if (version.intValue() == 1) {
        throw new SafeRuntimeException("Only version 0 (two-prime) RSA keys are supported");
    }
    BigInteger modulus = readNumber();
    BigInteger publicExponent = readNumber();
    BigInteger privateExponent = readNumber();
    BigInteger primeP = readNumber();
    BigInteger primeQ = readNumber();
    BigInteger primeExponentP = readNumber();
    BigInteger primeExponentQ = readNumber();
    BigInteger crtCoefficient = readNumber();
    if (publicExponent.signum() == 0 || primeExponentP.signum() == 0 || primeExponentQ.signum() == 0 || primeP.signum() == 0 || primeQ.signum() == 0 || crtCoefficient.signum() == 0) {
        return new RSAPrivateKeySpec(modulus, privateExponent);
    } else {
        return new RSAPrivateCrtKeySpec(modulus, publicExponent, privateExponent, primeP, primeQ, primeExponentP, primeExponentQ, crtCoefficient);
    }
}
Also used : RSAPrivateCrtKeySpec(java.security.spec.RSAPrivateCrtKeySpec) SafeRuntimeException(com.palantir.logsafe.exceptions.SafeRuntimeException) RSAPrivateKeySpec(java.security.spec.RSAPrivateKeySpec) BigInteger(java.math.BigInteger)

Example 22 with SafeRuntimeException

use of com.palantir.logsafe.exceptions.SafeRuntimeException in project dialogue by palantir.

the class MyServiceIntegrationTest method testCustomResponse.

private void testCustomResponse(int code) {
    undertowHandler = exchange -> {
        exchange.assertMethod(HttpMethod.PUT);
        exchange.assertPath("/custom/request1");
        exchange.assertAccept().isEqualTo("*/*");
        exchange.assertContentType().isNull();
        exchange.assertNoBody();
        exchange.exchange.setStatusCode(code);
        exchange.exchange.getResponseHeaders().add(HttpString.tryFromString("My-Custom-Header"), "my-custom-header-value");
        exchange.setContentType("text/csv");
        exchange.writeStringBody("Custom Body");
    };
    try (Response response = myServiceDialogue.customResponse()) {
        assertThat(response.code()).isEqualTo(code);
        assertThat(CharStreams.toString(new InputStreamReader(response.body(), StandardCharsets.UTF_8))).isEqualTo("Custom Body");
        assertThat(response.headers().get("My-Custom-Header")).containsExactly("my-custom-header-value");
    } catch (IOException e) {
        throw new SafeRuntimeException(e);
    }
}
Also used : Response(com.palantir.dialogue.Response) InputStreamReader(java.io.InputStreamReader) SafeRuntimeException(com.palantir.logsafe.exceptions.SafeRuntimeException) IOException(java.io.IOException)

Example 23 with SafeRuntimeException

use of com.palantir.logsafe.exceptions.SafeRuntimeException in project dialogue by palantir.

the class CustomStringDeserializer method deserialize.

@Override
public String deserialize(Response response) {
    try (InputStream is = response.body()) {
        String csv = new String(ByteStreams.toByteArray(is), StandardCharsets.UTF_8);
        List<String> fields = SPLITTER.splitToList(csv);
        Preconditions.checkState(fields.size() == 2);
        Preconditions.checkState("mystring".equals(fields.get(0)));
        Preconditions.checkState(!Strings.isNullOrEmpty(fields.get(1)));
        return fields.get(1);
    } catch (IOException e) {
        throw new SafeRuntimeException("Failed to serialize payload", e);
    }
}
Also used : SafeRuntimeException(com.palantir.logsafe.exceptions.SafeRuntimeException) InputStream(java.io.InputStream) IOException(java.io.IOException)

Example 24 with SafeRuntimeException

use of com.palantir.logsafe.exceptions.SafeRuntimeException in project dialogue by palantir.

the class BlockingChannelAdapterTest method testFailure.

@Test
public void testFailure() {
    Channel channel = BlockingChannelAdapter.of((_endpoint, _request) -> {
        throw new SafeRuntimeException("expected");
    }, executor);
    ListenableFuture<Response> result = channel.execute(TestEndpoint.POST, Request.builder().build());
    Awaitility.waitAtMost(Duration.ofSeconds(3)).until(result::isDone);
    assertThatThrownBy(result::get).isInstanceOf(ExecutionException.class).hasCauseExactlyInstanceOf(SafeRuntimeException.class).hasRootCauseMessage("expected");
}
Also used : TestResponse(com.palantir.dialogue.TestResponse) Response(com.palantir.dialogue.Response) SafeRuntimeException(com.palantir.logsafe.exceptions.SafeRuntimeException) Channel(com.palantir.dialogue.Channel) Test(org.junit.jupiter.api.Test)

Example 25 with SafeRuntimeException

use of com.palantir.logsafe.exceptions.SafeRuntimeException in project dialogue by palantir.

the class ChannelCache method createEmptyCache.

static ChannelCache createEmptyCache() {
    ChannelCache newCache = new ChannelCache();
    LIVE_INSTANCES.add(newCache);
    int numLiveInstances = LIVE_INSTANCES.size();
    if ((numLiveInstances > 5 && log.isInfoEnabled()) || log.isDebugEnabled()) {
        if (numLiveInstances >= 10) {
            log.info("Created ChannelCache instance #{} ({} alive): {}", SafeArg.of("instanceNumber", newCache.instanceNumber), SafeArg.of("totalAliveNow", numLiveInstances), SafeArg.of("newCache", newCache), new SafeRuntimeException("ChannelCache constructed here"));
        } else {
            log.info("Created ChannelCache instance #{} ({} alive): {}", SafeArg.of("instanceNumber", newCache.instanceNumber), SafeArg.of("totalAliveNow", numLiveInstances), SafeArg.of("newCache", newCache));
        }
    }
    return newCache;
}
Also used : SafeRuntimeException(com.palantir.logsafe.exceptions.SafeRuntimeException)

Aggregations

SafeRuntimeException (com.palantir.logsafe.exceptions.SafeRuntimeException)43 IOException (java.io.IOException)13 Test (org.junit.jupiter.api.Test)7 Path (java.nio.file.Path)6 Response (com.palantir.dialogue.Response)5 ImmutableMap (com.google.common.collect.ImmutableMap)3 TestResponse (com.palantir.dialogue.TestResponse)3 DefaultInvocationContext (com.palantir.tritium.event.DefaultInvocationContext)3 InvocationContext (com.palantir.tritium.event.InvocationContext)3 File (java.io.File)3 InputStream (java.io.InputStream)3 ArrayList (java.util.ArrayList)3 Test (org.junit.Test)3 ImmutableList (com.google.common.collect.ImmutableList)2 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)2 Value (com.palantir.atlasdb.keyvalue.api.Value)2 DisableNamespacesResponse (com.palantir.atlasdb.timelock.api.DisableNamespacesResponse)2 SuccessfulDisableNamespacesResponse (com.palantir.atlasdb.timelock.api.SuccessfulDisableNamespacesResponse)2 UnsuccessfulDisableNamespacesResponse (com.palantir.atlasdb.timelock.api.UnsuccessfulDisableNamespacesResponse)2 Channel (com.palantir.dialogue.Channel)2