Search in sources :

Example 26 with RestServiceException

use of com.github.ambry.rest.RestServiceException in project ambry by linkedin.

the class TailoredPeersClusterMap method doBadArgsTest.

// badArgsTest() helpers.
/**
 * Does the test where bad args are provided in the request to {@link GetPeersHandler}.
 * @param name the name of the host whose peers are required. Can be {@code null} if this param should be omitted.
 * @param port the port of the host whose peers are required. Can be {@code null} if this param should be omitted.
 * @param expectedErrorCode the {@link RestServiceErrorCode} expected in response.
 * @throws Exception
 */
private void doBadArgsTest(String name, String port, RestServiceErrorCode expectedErrorCode) throws Exception {
    StringBuilder uri = new StringBuilder(Operations.GET_PEERS + "?");
    if (name != null) {
        uri.append(GetPeersHandler.NAME_QUERY_PARAM).append("=").append(name);
    }
    if (port != null) {
        if (name != null) {
            uri.append("&");
        }
        uri.append(GetPeersHandler.PORT_QUERY_PARAM).append("=").append(port);
    }
    JSONObject data = new JSONObject();
    data.put(MockRestRequest.REST_METHOD_KEY, RestMethod.GET);
    data.put(MockRestRequest.URI_KEY, uri);
    RestRequest restRequest = new MockRestRequest(data, null);
    try {
        sendRequestGetResponse(restRequest, new MockRestResponseChannel());
        fail("Request should have failed");
    } catch (RestServiceException e) {
        assertEquals("Unexpected RestServiceErrorCode", expectedErrorCode, e.getErrorCode());
    }
}
Also used : RestServiceException(com.github.ambry.rest.RestServiceException) MockRestRequest(com.github.ambry.rest.MockRestRequest) RestRequest(com.github.ambry.rest.RestRequest) JSONObject(org.json.JSONObject) MockRestRequest(com.github.ambry.rest.MockRestRequest) MockRestResponseChannel(com.github.ambry.rest.MockRestResponseChannel)

Example 27 with RestServiceException

use of com.github.ambry.rest.RestServiceException in project ambry by linkedin.

the class GetReplicasHandlerTest method getReplicasWithBadInputTest.

/**
 * Tests reactions of the {@link GetReplicasHandler#getReplicas(String, RestResponseChannel)} operation to bad input -
 * specifically if we do not include required parameters.
 * @throws Exception
 */
@Test
public void getReplicasWithBadInputTest() throws Exception {
    // bad input - invalid blob id.
    try {
        getReplicasHandler.getReplicas("12345", new MockRestResponseChannel());
        fail("Exception should have been thrown because the blobid is invalid");
    } catch (RestServiceException e) {
        assertEquals("Unexpected RestServiceErrorCode", RestServiceErrorCode.NotFound, e.getErrorCode());
    }
    // bad input - invalid blob id for this cluster map.
    String blobId = "AAEAAQAAAAAAAADFAAAAJDMyYWZiOTJmLTBkNDYtNDQyNS1iYzU0LWEwMWQ1Yzg3OTJkZQ.gif";
    try {
        getReplicasHandler.getReplicas(blobId, new MockRestResponseChannel());
        fail("Exception should have been thrown because the blobid is invalid");
    } catch (RestServiceException e) {
        assertEquals("Unexpected RestServiceErrorCode", RestServiceErrorCode.NotFound, e.getErrorCode());
    }
}
Also used : RestServiceException(com.github.ambry.rest.RestServiceException) MockRestResponseChannel(com.github.ambry.rest.MockRestResponseChannel) Test(org.junit.Test)

Example 28 with RestServiceException

use of com.github.ambry.rest.RestServiceException in project ambry by linkedin.

the class GetSignedUrlHandlerTest method urlSigningServiceFailureTest.

/**
 * Tests where {@link UrlSigningService} fails.
 * @throws Exception
 */
@Test
public void urlSigningServiceFailureTest() throws Exception {
    String msg = "@@expected";
    urlSigningServiceFactory.getSignedUrlException = new RestServiceException(msg, RestServiceErrorCode.InternalServerError);
    verifyFailureWithMsg(msg);
}
Also used : RestServiceException(com.github.ambry.rest.RestServiceException) UtilsTest(com.github.ambry.utils.UtilsTest) Test(org.junit.Test)

Example 29 with RestServiceException

use of com.github.ambry.rest.RestServiceException in project ambry by linkedin.

the class AccountAndContainerInjector method injectAccountAndContainerForPostRequest.

/**
 * Injects target {@link Account} and {@link Container} for PUT requests. This method also ensures required headers
 * are present for the PUT requests that use serviceId as the account name, and the PUT requests that carry both the
 * {@code x-ambry-target-account} and {@code x-ambry-target-container} headers.
 * @param restRequest The Put {@link RestRequest}.
 * @throws RestServiceException
 */
public void injectAccountAndContainerForPostRequest(RestRequest restRequest) throws RestServiceException {
    accountAndContainerSanityCheck(restRequest);
    if (getHeader(restRequest.getArgs(), Headers.TARGET_ACCOUNT_NAME, false) != null || getHeader(restRequest.getArgs(), Headers.TARGET_CONTAINER_NAME, false) != null) {
        ensureRequiredHeadersOrThrow(restRequest, requiredAmbryHeadersForPutWithAccountAndContainerName);
        frontendMetrics.putWithAccountAndContainerHeaderRate.mark();
        injectAccountAndContainerUsingAccountAndContainerHeaders(restRequest);
    } else if (frontendConfig.frontendAllowServiceIdBasedPostRequest) {
        ensureRequiredHeadersOrThrow(restRequest, requiredAmbryHeadersForPutWithServiceId);
        frontendMetrics.putWithServiceIdForAccountNameRate.mark();
        String serviceId = getHeader(restRequest.getArgs(), Headers.SERVICE_ID, true);
        boolean isPrivate = isPrivate(restRequest.getArgs());
        injectAccountAndContainerUsingServiceId(restRequest, serviceId, isPrivate);
    } else {
        throw new RestServiceException("Missing either " + Headers.TARGET_ACCOUNT_NAME + " or " + Headers.TARGET_CONTAINER_NAME + " header", RestServiceErrorCode.BadRequest);
    }
}
Also used : RestServiceException(com.github.ambry.rest.RestServiceException)

Example 30 with RestServiceException

use of com.github.ambry.rest.RestServiceException in project ambry by linkedin.

the class AmbryBlobStorageService method submitResponse.

/**
 * Submits the response and {@code responseBody} (and any {@code exception})for the {@code restRequest} to the
 * {@code responseHandler}.
 * @param restRequest the {@link RestRequest} for which a response is ready.
 * @param restResponseChannel the {@link RestResponseChannel} over which the response can be sent.
 * @param responseBody the body of the response in the form of a {@link ReadableStreamChannel}.
 * @param exception any {@link Exception} that occurred during the handling of {@code restRequest}.
 */
void submitResponse(RestRequest restRequest, RestResponseChannel restResponseChannel, ReadableStreamChannel responseBody, Exception exception) {
    try {
        if (exception != null && exception instanceof RouterException) {
            exception = new RestServiceException(exception, RestServiceErrorCode.getRestServiceErrorCode(((RouterException) exception).getErrorCode()));
        }
        responseHandler.handleResponse(restRequest, restResponseChannel, responseBody, exception);
    } catch (Exception e) {
        frontendMetrics.responseSubmissionError.inc();
        if (exception != null) {
            logger.error("Error submitting response to response handler", e);
        } else {
            exception = e;
        }
        logger.error("Handling of request {} failed", restRequest.getUri(), exception);
        restResponseChannel.onResponseComplete(exception);
        if (responseBody != null) {
            try {
                responseBody.close();
            } catch (IOException ioe) {
                frontendMetrics.resourceReleaseError.inc();
                logger.error("Error closing ReadableStreamChannel", e);
            }
        }
    }
}
Also used : RestServiceException(com.github.ambry.rest.RestServiceException) RouterException(com.github.ambry.router.RouterException) IOException(java.io.IOException) IOException(java.io.IOException) RouterException(com.github.ambry.router.RouterException) ExecutionException(java.util.concurrent.ExecutionException) RestServiceException(com.github.ambry.rest.RestServiceException)

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