Search in sources :

Example 1 with ServerErrorException

use of javax.ws.rs.ServerErrorException in project pulsar by yahoo.

the class PersistentTopicsImpl method getMessageFromHttpResponse.

private Message getMessageFromHttpResponse(Response response) throws Exception {
    if (response.getStatus() != Status.OK.getStatusCode()) {
        if (response.getStatus() >= 500) {
            throw new ServerErrorException(response);
        } else if (response.getStatus() >= 400) {
            throw new ClientErrorException(response);
        } else {
            throw new WebApplicationException(response);
        }
    }
    String msgId = response.getHeaderString("X-Pulsar-Message-ID");
    InputStream stream = null;
    try {
        stream = (InputStream) response.getEntity();
        byte[] data = new byte[stream.available()];
        stream.read(data);
        Map<String, String> properties = Maps.newTreeMap();
        MultivaluedMap<String, Object> headers = response.getHeaders();
        Object publishTime = headers.getFirst("X-Pulsar-publish-time");
        if (publishTime != null) {
            properties.put("publish-time", (String) publishTime);
        }
        for (Entry<String, List<Object>> entry : headers.entrySet()) {
            String header = entry.getKey();
            if (header.contains("X-Pulsar-PROPERTY-")) {
                String keyName = header.substring(header.indexOf("X-Pulsar-PROPERTY-") + 1, header.length());
                properties.put(keyName, (String) entry.getValue().get(0));
            }
        }
        return new MessageImpl(msgId, properties, data);
    } finally {
        if (stream != null) {
            stream.close();
        }
    }
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) InputStream(java.io.InputStream) ClientErrorException(javax.ws.rs.ClientErrorException) List(java.util.List) ServerErrorException(javax.ws.rs.ServerErrorException) MessageImpl(com.yahoo.pulsar.client.impl.MessageImpl)

Example 2 with ServerErrorException

use of javax.ws.rs.ServerErrorException in project pulsar by yahoo.

the class PulsarBrokerStatsClientTest method testServiceException.

@Test
public void testServiceException() throws Exception {
    URL url = new URL("http://localhost:15000");
    PulsarAdmin admin = new PulsarAdmin(url, (Authentication) null);
    BrokerStatsImpl client = (BrokerStatsImpl) spy(admin.brokerStats());
    try {
        client.getLoadReport();
    } catch (PulsarAdminException e) {
    // Ok
    }
    try {
        client.getPendingBookieOpsStats();
    } catch (PulsarAdminException e) {
    // Ok
    }
    try {
        client.getBrokerResourceAvailability("prop", "cluster", "ns");
    } catch (PulsarAdminException e) {
    // Ok
    }
    assertTrue(client.getApiException(new ClientErrorException(403)) instanceof NotAuthorizedException);
    assertTrue(client.getApiException(new ClientErrorException(404)) instanceof NotFoundException);
    assertTrue(client.getApiException(new ClientErrorException(409)) instanceof ConflictException);
    assertTrue(client.getApiException(new ClientErrorException(412)) instanceof PreconditionFailedException);
    assertTrue(client.getApiException(new ClientErrorException(400)) instanceof PulsarAdminException);
    assertTrue(client.getApiException(new ServerErrorException(500)) instanceof ServerSideErrorException);
    assertTrue(client.getApiException(new ServerErrorException(503)) instanceof PulsarAdminException);
    log.info("Client: ", client);
    admin.close();
}
Also used : PulsarAdmin(com.yahoo.pulsar.client.admin.PulsarAdmin) BrokerStatsImpl(com.yahoo.pulsar.client.admin.internal.BrokerStatsImpl) ConflictException(com.yahoo.pulsar.client.admin.PulsarAdminException.ConflictException) ServerSideErrorException(com.yahoo.pulsar.client.admin.PulsarAdminException.ServerSideErrorException) ClientErrorException(javax.ws.rs.ClientErrorException) NotFoundException(com.yahoo.pulsar.client.admin.PulsarAdminException.NotFoundException) PreconditionFailedException(com.yahoo.pulsar.client.admin.PulsarAdminException.PreconditionFailedException) PulsarAdminException(com.yahoo.pulsar.client.admin.PulsarAdminException) NotAuthorizedException(com.yahoo.pulsar.client.admin.PulsarAdminException.NotAuthorizedException) ServerErrorException(javax.ws.rs.ServerErrorException) URL(java.net.URL) Test(org.testng.annotations.Test)

Aggregations

ClientErrorException (javax.ws.rs.ClientErrorException)2 ServerErrorException (javax.ws.rs.ServerErrorException)2 PulsarAdmin (com.yahoo.pulsar.client.admin.PulsarAdmin)1 PulsarAdminException (com.yahoo.pulsar.client.admin.PulsarAdminException)1 ConflictException (com.yahoo.pulsar.client.admin.PulsarAdminException.ConflictException)1 NotAuthorizedException (com.yahoo.pulsar.client.admin.PulsarAdminException.NotAuthorizedException)1 NotFoundException (com.yahoo.pulsar.client.admin.PulsarAdminException.NotFoundException)1 PreconditionFailedException (com.yahoo.pulsar.client.admin.PulsarAdminException.PreconditionFailedException)1 ServerSideErrorException (com.yahoo.pulsar.client.admin.PulsarAdminException.ServerSideErrorException)1 BrokerStatsImpl (com.yahoo.pulsar.client.admin.internal.BrokerStatsImpl)1 MessageImpl (com.yahoo.pulsar.client.impl.MessageImpl)1 InputStream (java.io.InputStream)1 URL (java.net.URL)1 List (java.util.List)1 WebApplicationException (javax.ws.rs.WebApplicationException)1 Test (org.testng.annotations.Test)1