use of com.github.ambry.router.ReadableStreamChannel in project ambry by linkedin.
the class MockRouter method getBlob.
@Override
public Future<GetBlobResult> getBlob(String blobId, GetBlobOptions options, Callback<GetBlobResult> callback, QuotaChargeCallback quotaChargeCallback) {
lock.lock();
try {
BlobInfoAndData blob = allBlobs.get(blobId);
FutureResult<GetBlobResult> future = new FutureResult<>();
if (blob == null) {
Exception e = new RouterException("NotFound", RouterErrorCode.BlobDoesNotExist);
future.done(null, e);
if (callback != null) {
callback.onCompletion(null, e);
}
return future;
}
// Discard the options and only return the BlobAll.
ReadableStreamChannel channel = new ByteBufferReadableStreamChannel(ByteBuffer.wrap(blob.getData()));
GetBlobResult result = new GetBlobResult(blob.getBlobInfo(), channel);
future.done(result, null);
if (callback != null) {
callback.onCompletion(result, null);
}
return future;
} finally {
lock.unlock();
}
}
use of com.github.ambry.router.ReadableStreamChannel in project ambry by linkedin.
the class MockHeadCallback method shouldProceed.
/**
* Handles argument pre-checks and examines the URL to see if any custom operations need to be performed (which might
* involve throwing exceptions).
* <p/>
* Also blocks if required.
* @param restRequest the {@link RestRequest} that needs to be handled.
* @param restResponseChannel the {@link RestResponseChannel} that can be used to set headers.
* @return {@code true} if the pre-checks decided it is OK to continue. Otherwise {@code false}.
*/
private boolean shouldProceed(RestRequest restRequest, RestResponseChannel restResponseChannel) {
if (blocking) {
try {
blockLatch.await();
} catch (InterruptedException e) {
throw new IllegalStateException(e);
}
}
boolean shouldProceed = canHonorRequest(restRequest, restResponseChannel);
if (shouldProceed) {
String uri = restRequest.getUri();
ReadableStreamChannel response = null;
Exception exception = null;
if (uri.startsWith(ECHO_REST_METHOD)) {
String responseStr = restRequest.getRestMethod().toString() + uri.substring(ECHO_REST_METHOD.length());
ByteBuffer buffer = ByteBuffer.wrap(responseStr.getBytes());
response = new ByteBufferRSC(buffer);
shouldProceed = false;
} else if (THROW_RUNTIME_EXCEPTION.equals(uri)) {
throw new RuntimeException(THROW_RUNTIME_EXCEPTION);
} else if (SEND_RESPONSE_RUNTIME_EXCEPTION.equals(uri)) {
shouldProceed = false;
exception = new RuntimeException(SEND_RESPONSE_RUNTIME_EXCEPTION);
} else if (SEND_RESPONSE_REST_SERVICE_EXCEPTION.equals(uri)) {
shouldProceed = false;
RestServiceErrorCode errorCode = RestServiceErrorCode.InternalServerError;
try {
errorCode = RestServiceErrorCode.valueOf(verifiableProperties.getString(REST_ERROR_CODE));
} catch (IllegalArgumentException e) {
// it's alright.
}
exception = new RestServiceException(SEND_RESPONSE_REST_SERVICE_EXCEPTION, errorCode);
}
if (!shouldProceed) {
try {
if (exception == null) {
restResponseChannel.setStatus(ResponseStatus.Ok);
}
} catch (RestServiceException e) {
exception = e;
} finally {
handleResponse(restRequest, restResponseChannel, response, exception);
}
}
}
return shouldProceed;
}
use of com.github.ambry.router.ReadableStreamChannel in project ambry by linkedin.
the class NoSizeRSC method readPartByPartTest.
/**
* Tests reading {@link ReadableStreamChannelInputStream} part by part.
* @param in the data that the {@link ReadableStreamChannelInputStream} should contain.
* @throws Exception
*/
private void readPartByPartTest(byte[] in) throws Exception {
// channel with size and one piece of content.
ReadableStreamChannel channel = new ByteBufferReadableStreamChannel(ByteBuffer.wrap(in));
InputStream stream = new ReadableStreamChannelInputStream(channel);
doReadPartByPartTest(stream, in);
stream.close();
// channel with no size but one piece of content.
channel = new NoSizeRSC(ByteBuffer.wrap(in));
stream = new ReadableStreamChannelInputStream(channel);
doReadPartByPartTest(stream, in);
stream.close();
// channel with no size and multiple pieces of content.
List<ByteBuffer> contents = splitContent(in, CONTENT_SPLIT_PART_COUNT);
contents.add(null);
channel = new MockRestRequest(MockRestRequest.DUMMY_DATA, contents);
stream = new ReadableStreamChannelInputStream(channel);
doReadPartByPartTest(stream, in);
stream.close();
}
use of com.github.ambry.router.ReadableStreamChannel in project ambry by linkedin.
the class NoSizeRSC method readErrorCasesTest.
/**
* Tests cases where read() methods get incorrect input.
* @throws IOException
*/
@Test
public void readErrorCasesTest() throws IOException {
byte[] in = new byte[1024];
new Random().nextBytes(in);
ReadableStreamChannel channel = new ByteBufferReadableStreamChannel(ByteBuffer.wrap(in));
InputStream dstInputStream = new ReadableStreamChannelInputStream(channel);
try {
dstInputStream.read(null, 0, in.length);
fail("The read should have failed");
} catch (NullPointerException e) {
// expected. nothing to do.
}
byte[] out = new byte[in.length];
try {
dstInputStream.read(out, -1, out.length);
fail("The read should have failed");
} catch (IndexOutOfBoundsException e) {
// expected. nothing to do.
}
try {
dstInputStream.read(out, 0, -1);
fail("The read should have failed");
} catch (IndexOutOfBoundsException e) {
// expected. nothing to do.
}
try {
dstInputStream.read(out, 0, out.length + 1);
fail("The read should have failed");
} catch (IndexOutOfBoundsException e) {
// expected. nothing to do.
}
assertEquals("Bytes read should have been 0 because passed len was 0", 0, dstInputStream.read(out, 0, 0));
}
use of com.github.ambry.router.ReadableStreamChannel in project ambry by linkedin.
the class NoSizeRSC method incompleteReadsTest.
/**
* Tests for the case when reads are incomplete either because exceptions were thrown or the read simply did not
* complete.
* @throws IOException
*/
@Test
public void incompleteReadsTest() throws IOException {
// Exception during read
String exceptionMsg = "@@randomMsg@@";
Exception exceptionToThrow = new Exception(exceptionMsg);
ReadableStreamChannel channel = new IncompleteReadReadableStreamChannel(exceptionToThrow);
InputStream inputStream = new ReadableStreamChannelInputStream(channel);
try {
inputStream.read();
fail("The read should have failed");
} catch (Exception e) {
while (e.getCause() != null) {
e = (Exception) e.getCause();
}
assertEquals("Exception messages do not match", exceptionMsg, e.getMessage());
}
// incomplete read
channel = new IncompleteReadReadableStreamChannel(null);
inputStream = new ReadableStreamChannelInputStream(channel);
try {
inputStream.read();
fail("The read should have failed");
} catch (IllegalStateException e) {
// expected. Nothing to do.
}
}
Aggregations