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;
}
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;
}
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());
}
}
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);
}
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);
}
}
Aggregations