Search in sources :

Example 6 with GetOption

use of com.github.ambry.protocol.GetOption in project ambry by linkedin.

the class RestUtils method getGetOption.

/**
 * Gets the {@link GetOption} required by the request.
 * @param restRequest the representation of the request.
 * @return the required {@link GetOption}. Defaults to {@link GetOption#None}.
 * @throws RestServiceException if the {@link RestUtils.Headers#GET_OPTION} is present but not recognized.
 */
public static GetOption getGetOption(RestRequest restRequest) throws RestServiceException {
    GetOption options = GetOption.None;
    Map<String, Object> args = restRequest.getArgs();
    Object value = args.get(RestUtils.Headers.GET_OPTION);
    if (value != null) {
        String str = (String) value;
        boolean foundMatch = false;
        for (GetOption getOption : GetOption.values()) {
            if (str.equalsIgnoreCase(getOption.name())) {
                options = getOption;
                foundMatch = true;
                break;
            }
        }
        if (!foundMatch) {
            throw new RestServiceException("Unrecognized value for [" + RestUtils.Headers.GET_OPTION + "]: " + str, RestServiceErrorCode.InvalidArgs);
        }
    }
    return options;
}
Also used : GetOption(com.github.ambry.protocol.GetOption)

Example 7 with GetOption

use of com.github.ambry.protocol.GetOption in project ambry by linkedin.

the class RestUtils method getGetOption.

/**
 * Gets the {@link GetOption} required by the request.
 * @param restRequest the representation of the request.
 * @param defaultGetOption the {@link GetOption} to use if the {@code restRequest} doesn't have one. Can be
 * {@code null}.
 * @return the required {@link GetOption}. Defaults to {@link GetOption#None}.
 * @throws RestServiceException if the {@link RestUtils.Headers#GET_OPTION} is present but not recognized.
 */
public static GetOption getGetOption(RestRequest restRequest, GetOption defaultGetOption) throws RestServiceException {
    GetOption option = defaultGetOption == null ? GetOption.None : defaultGetOption;
    Object value = restRequest.getArgs().get(RestUtils.Headers.GET_OPTION);
    if (value != null) {
        String str = (String) value;
        boolean foundMatch = false;
        for (GetOption getOption : GetOption.values()) {
            if (str.equalsIgnoreCase(getOption.name())) {
                option = getOption;
                foundMatch = true;
                break;
            }
        }
        if (!foundMatch) {
            throw new RestServiceException("Unrecognized value for [" + RestUtils.Headers.GET_OPTION + "]: " + str, RestServiceErrorCode.InvalidArgs);
        }
    }
    return option;
}
Also used : GetOption(com.github.ambry.protocol.GetOption)

Example 8 with GetOption

use of com.github.ambry.protocol.GetOption in project ambry by linkedin.

the class RestUtilsTest method getGetOptionTest.

/**
 * Tests {@link RestUtils#getGetOption(RestRequest, GetOption)}.
 * @throws Exception
 */
@Test
public void getGetOptionTest() throws Exception {
    for (GetOption option : GetOption.values()) {
        JSONObject headers = new JSONObject();
        headers.put(RestUtils.Headers.GET_OPTION, option.toString().toLowerCase());
        RestRequest restRequest = createRestRequest(RestMethod.GET, "/", headers);
        assertEquals("Option returned not as expected", option, RestUtils.getGetOption(restRequest, null));
        assertEquals("Option returned not as expected", option, RestUtils.getGetOption(restRequest, option));
        assertEquals("Option returned not as expected", option, RestUtils.getGetOption(restRequest, GetOption.None));
    }
    // no value defined
    RestRequest restRequest = createRestRequest(RestMethod.GET, "/", null);
    assertEquals("Option returned not as expected", GetOption.None, RestUtils.getGetOption(restRequest, null));
    for (GetOption option : GetOption.values()) {
        assertEquals("Option returned not as expected", option, RestUtils.getGetOption(restRequest, option));
    }
    // bad value
    JSONObject headers = new JSONObject();
    headers.put(RestUtils.Headers.GET_OPTION, "non_existent_option");
    restRequest = createRestRequest(RestMethod.GET, "/", headers);
    try {
        RestUtils.getGetOption(restRequest, GetOption.None);
        fail("Should have failed to get GetOption because value of header is invalid");
    } catch (RestServiceException e) {
        assertEquals("Unexpected RestServiceErrorCode", RestServiceErrorCode.InvalidArgs, e.getErrorCode());
    }
}
Also used : JSONObject(org.json.JSONObject) GetOption(com.github.ambry.protocol.GetOption) Test(org.junit.Test)

Example 9 with GetOption

use of com.github.ambry.protocol.GetOption in project ambry by linkedin.

the class GetBlobOptionsTest method testToStringEqualsAndHashcode.

/**
 * Test toString, equals, and hashCode methods.
 */
@Test
public void testToStringEqualsAndHashcode() {
    ByteRange byteRange = ByteRanges.fromLastNBytes(4);
    GetOption getOption = GetOption.None;
    GetBlobOptions.OperationType type = GetBlobOptions.OperationType.Data;
    GetBlobOptions a = new GetBlobOptionsBuilder().operationType(type).getOption(getOption).range(byteRange).build();
    GetBlobOptions b = new GetBlobOptionsBuilder().operationType(type).getOption(getOption).range(byteRange).build();
    assertEquals("GetBlobOptions should be equal", a, b);
    assertEquals("GetBlobOptions hashcodes should be equal", a.hashCode(), b.hashCode());
    assertEquals("GetBlobOptions toString should be equal", a.toString(), b.toString());
    // Change OperationType
    b = new GetBlobOptionsBuilder().operationType(GetBlobOptions.OperationType.All).getOption(getOption).range(byteRange).build();
    assertOptionsAreDistinct(a, b);
    // Change GetOption
    b = new GetBlobOptionsBuilder().operationType(type).getOption(GetOption.Include_All).range(byteRange).build();
    assertOptionsAreDistinct(a, b);
    // Change range
    b = new GetBlobOptionsBuilder().operationType(type).getOption(getOption).range(ByteRanges.fromOffsetRange(2, 7)).build();
    assertOptionsAreDistinct(a, b);
    // Change rawMode (need to omit range)
    a = new GetBlobOptionsBuilder().operationType(GetBlobOptions.OperationType.All).getOption(getOption).build();
    b = new GetBlobOptionsBuilder().operationType(GetBlobOptions.OperationType.All).getOption(getOption).rawMode(true).build();
    assertOptionsAreDistinct(a, b);
}
Also used : GetOption(com.github.ambry.protocol.GetOption) Test(org.junit.Test)

Example 10 with GetOption

use of com.github.ambry.protocol.GetOption in project ambry by linkedin.

the class FrontendTestUrlSigningServiceFactory method verifyGetWithDefaultOptions.

/**
 * Verifies GET blob, blobinfo and HEAD with the given {@code defaultOptionsToTest} set as the default {
 * @link GetOption} (one by one)
 * @param blobId the id of the blob to fetch
 * @param expectedHeaders the headers expected in the responses
 * @param expectedContent the content expected for the blob
 * @param defaultOptionsToTest the {@link GetOption}s to check as defaults
 * @throws Exception
 */
private void verifyGetWithDefaultOptions(String blobId, JSONObject expectedHeaders, ByteBuffer expectedContent, EnumSet<GetOption> defaultOptionsToTest) throws Exception {
    for (GetOption option : defaultOptionsToTest) {
        restartFrontendRestRequestServiceWithDefaultGetOption(option);
        getBlobInfoAndVerify(blobId, null, expectedHeaders, refAccount, refContainer);
        getHeadAndVerify(blobId, null, null, expectedHeaders, refAccount, refContainer);
        getBlobAndVerify(blobId, null, null, expectedHeaders, expectedContent, refAccount, refContainer);
    }
}
Also used : GetOption(com.github.ambry.protocol.GetOption)

Aggregations

GetOption (com.github.ambry.protocol.GetOption)13 RestRequest (com.github.ambry.rest.RestRequest)4 Test (org.junit.Test)4 Utils (com.github.ambry.utils.Utils)3 AccountService (com.github.ambry.account.AccountService)2 AccountStatsStore (com.github.ambry.accountstats.AccountStatsStore)2 BlobId (com.github.ambry.commons.BlobId)2 ByteBufferReadableStreamChannel (com.github.ambry.commons.ByteBufferReadableStreamChannel)2 VerifiableProperties (com.github.ambry.config.VerifiableProperties)2 MessageFormatRecord (com.github.ambry.messageformat.MessageFormatRecord)2 TestUtils (com.github.ambry.utils.TestUtils)2 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)2 DefaultHttpHeaders (io.netty.handler.codec.http.DefaultHttpHeaders)2 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)2 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)2 ArrayList (java.util.ArrayList)2 Arrays (java.util.Arrays)2 List (java.util.List)2 Map (java.util.Map)2 Properties (java.util.Properties)2