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();
}
}
}
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();
}
Aggregations