use of com.github.ambry.rest.RestServiceException in project ambry by linkedin.
the class GetSignedUrlHandler method handle.
/**
* Handles a request for getting signed URLs.
* @param restRequest the {@link RestRequest} that contains the request parameters.
* @param restResponseChannel the {@link RestResponseChannel} where headers should be set.
* @param callback the {@link Callback} to invoke when the response is ready (or if there is an exception).
* @throws RestServiceException if required parameters are not found or are invalid
*/
void handle(RestRequest restRequest, RestResponseChannel restResponseChannel, Callback<ReadableStreamChannel> callback) throws RestServiceException {
RestRequestMetrics requestMetrics = restRequest.getSSLSession() != null ? metrics.getSignedUrlSSLMetrics : metrics.getSignedUrlMetrics;
restRequest.getMetricsTracker().injectMetrics(requestMetrics);
String restMethodInSignedUrlStr = RestUtils.getHeader(restRequest.getArgs(), RestUtils.Headers.URL_TYPE, true);
RestMethod restMethodInUrl;
try {
restMethodInUrl = RestMethod.valueOf(restMethodInSignedUrlStr);
} catch (IllegalArgumentException e) {
throw new RestServiceException("Unrecognized RestMethod: " + restMethodInSignedUrlStr, RestServiceErrorCode.InvalidArgs);
}
securityService.processRequest(restRequest, new SecurityProcessRequestCallback(restRequest, restMethodInUrl, restResponseChannel, callback));
}
use of com.github.ambry.rest.RestServiceException in project ambry by linkedin.
the class FrontendTestUrlSigningServiceFactory method putFailureTest.
/**
* Checks reactions of PUT methods in {@link AmbryBlobStorageService}
* @throws Exception
*/
@Test
public void putFailureTest() throws Exception {
RestRequest restRequest = createRestRequest(RestMethod.PUT, "/", null, null);
MockRestResponseChannel restResponseChannel = new MockRestResponseChannel();
try {
doOperation(restRequest, restResponseChannel);
fail("PUT should have failed because Ambry does not support it");
} catch (RestServiceException e) {
assertEquals("PUT is an unsupported method", RestServiceErrorCode.UnsupportedHttpMethod, e.getErrorCode());
}
}
use of com.github.ambry.rest.RestServiceException in project ambry by linkedin.
the class FrontendTestUrlSigningServiceFactory method verifyGone.
/**
* Verifies that a blob is GONE after it is deleted.
* @param restRequest the {@link RestRequest} to send to {@link AmbryBlobStorageService}.
*/
private void verifyGone(RestRequest restRequest) throws Exception {
MockRestResponseChannel restResponseChannel = new MockRestResponseChannel();
try {
doOperation(restRequest, restResponseChannel);
fail("Operation should have failed because blob is deleted");
} catch (RestServiceException e) {
assertEquals("AmbryBlobStorageService should have thrown a Deleted exception", RestServiceErrorCode.Deleted, e.getErrorCode());
}
}
use of com.github.ambry.rest.RestServiceException in project ambry by linkedin.
the class FrontendTestUrlSigningServiceFactory method postBlobAndVerifyWithAccountAndContainer.
/**
* Posts a blob and verifies the injected {@link Account} and {@link Container} into the {@link RestRequest}.
* @param accountName The accountName to send as the header of the request.
* @param containerName The containerName to send as the header of the request.
* @param serviceId The serviceId to send as the header of the request.
* @param isPrivate The isPrivate flag for the blob.
* @param expectedAccount The expected {@link Account} that would be injected into the {@link RestRequest}.
* @param expectedContainer The expected {@link Container} that would be injected into the {@link RestRequest}.
* @param expectedRestErrorCode The expected {@link RestServiceErrorCode} after the put operation.
* @return The blobId string if the put operation is successful, {@link null} otherwise.
* @throws Exception
*/
private String postBlobAndVerifyWithAccountAndContainer(String accountName, String containerName, String serviceId, boolean isPrivate, Account expectedAccount, Container expectedContainer, RestServiceErrorCode expectedRestErrorCode) throws Exception {
int CONTENT_LENGTH = 1024;
ByteBuffer content = ByteBuffer.wrap(TestUtils.getRandomBytes(CONTENT_LENGTH));
List<ByteBuffer> contents = new LinkedList<>();
contents.add(content);
contents.add(null);
String contentType = "application/octet-stream";
String ownerId = "postGetHeadDeleteOwnerID";
JSONObject headers = new JSONObject();
setAmbryHeadersForPut(headers, 7200, isPrivate, serviceId, contentType, ownerId, accountName, containerName);
RestRequest restRequest = createRestRequest(RestMethod.POST, "/", headers, contents);
MockRestResponseChannel restResponseChannel = new MockRestResponseChannel();
try {
doOperation(restRequest, restResponseChannel);
if (expectedRestErrorCode != null) {
fail("Should have thrown");
}
} catch (RestServiceException e) {
assertEquals("Wrong RestServiceErrorCode", expectedRestErrorCode, e.getErrorCode());
}
assertEquals("Wrong account object in RestRequest's args", expectedAccount, restRequest.getArgs().get(RestUtils.InternalKeys.TARGET_ACCOUNT_KEY));
assertEquals("Wrong container object in RestRequest's args", expectedContainer, restRequest.getArgs().get(RestUtils.InternalKeys.TARGET_CONTAINER_KEY));
return expectedRestErrorCode == null ? restResponseChannel.getHeader(RestUtils.Headers.LOCATION) : null;
}
use of com.github.ambry.rest.RestServiceException in project ambry by linkedin.
the class FrontendTestUrlSigningServiceFactory method checkRouterExceptionPipeline.
/**
* Checks that the exception received by submitting {@code restRequest} to {@link AmbryBlobStorageService} matches
* what was expected.
* @param expectedExceptionMsg the expected exception message.
* @param restRequest the {@link RestRequest} to submit to {@link AmbryBlobStorageService}.
* @throws Exception
*/
private void checkRouterExceptionPipeline(String expectedExceptionMsg, RestRequest restRequest) throws Exception {
try {
doOperation(restRequest, new MockRestResponseChannel());
fail("Operation " + restRequest.getRestMethod() + " should have failed because an external service would have thrown an exception");
} catch (RestServiceException | RuntimeException e) {
// catching RestServiceException because RouterException should have been converted.
// RuntimeException might get bubbled up as is.
assertEquals("Unexpected exception message", expectedExceptionMsg, Utils.getRootCause(e).getMessage());
// Nothing should be closed.
assertTrue("RestRequest channel is not open", restRequest.isOpen());
restRequest.close();
}
}
Aggregations