Search in sources :

Example 11 with RestServiceException

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));
}
Also used : RestServiceException(com.github.ambry.rest.RestServiceException) RestRequestMetrics(com.github.ambry.rest.RestRequestMetrics) RestMethod(com.github.ambry.rest.RestMethod)

Example 12 with RestServiceException

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());
    }
}
Also used : RestServiceException(com.github.ambry.rest.RestServiceException) MockRestRequest(com.github.ambry.rest.MockRestRequest) RestRequest(com.github.ambry.rest.RestRequest) MockRestResponseChannel(com.github.ambry.rest.MockRestResponseChannel) RestUtilsTest(com.github.ambry.rest.RestUtilsTest) UtilsTest(com.github.ambry.utils.UtilsTest) Test(org.junit.Test)

Example 13 with RestServiceException

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());
    }
}
Also used : RestServiceException(com.github.ambry.rest.RestServiceException) MockRestResponseChannel(com.github.ambry.rest.MockRestResponseChannel)

Example 14 with RestServiceException

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;
}
Also used : RestServiceException(com.github.ambry.rest.RestServiceException) MockRestRequest(com.github.ambry.rest.MockRestRequest) RestRequest(com.github.ambry.rest.RestRequest) JSONObject(org.json.JSONObject) MockRestResponseChannel(com.github.ambry.rest.MockRestResponseChannel) ByteBuffer(java.nio.ByteBuffer) LinkedList(java.util.LinkedList)

Example 15 with RestServiceException

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();
    }
}
Also used : RestServiceException(com.github.ambry.rest.RestServiceException) MockRestResponseChannel(com.github.ambry.rest.MockRestResponseChannel)

Aggregations

RestServiceException (com.github.ambry.rest.RestServiceException)35 MockRestResponseChannel (com.github.ambry.rest.MockRestResponseChannel)15 Test (org.junit.Test)12 MockRestRequest (com.github.ambry.rest.MockRestRequest)11 RestRequest (com.github.ambry.rest.RestRequest)10 UtilsTest (com.github.ambry.utils.UtilsTest)8 JSONObject (org.json.JSONObject)8 RestMethod (com.github.ambry.rest.RestMethod)7 RestUtilsTest (com.github.ambry.rest.RestUtilsTest)6 Account (com.github.ambry.account.Account)5 Container (com.github.ambry.account.Container)5 IOException (java.io.IOException)4 ByteBuffer (java.nio.ByteBuffer)4 ExecutionException (java.util.concurrent.ExecutionException)4 BlobId (com.github.ambry.commons.BlobId)3 RestUtils (com.github.ambry.rest.RestUtils)3 MetricRegistry (com.codahale.metrics.MetricRegistry)2 PartitionId (com.github.ambry.clustermap.PartitionId)2 VerifiableProperties (com.github.ambry.config.VerifiableProperties)2 RestResponseChannel (com.github.ambry.rest.RestResponseChannel)2