use of com.linkedin.restli.examples.greetings.api.Greeting in project rest.li by linkedin.
the class SimpleResourceUnderCollectionResource method update.
/**
* Updates the greeting.
*/
@Override
public UpdateResponse update(PatchRequest<Greeting> patchRequest) {
Long key = this.getContext().getPathKeys().get("subgreetingsId");
if (TONES.containsKey(key)) {
try {
Greeting patched = new Greeting();
PatchApplier.applyPatch(patched, patchRequest);
TONES.put(key, patched.getTone());
return new UpdateResponse(HttpStatus.S_204_NO_CONTENT);
} catch (DataProcessingException e) {
return new UpdateResponse((HttpStatus.S_400_BAD_REQUEST));
}
} else {
return new UpdateResponse(HttpStatus.S_404_NOT_FOUND);
}
}
use of com.linkedin.restli.examples.greetings.api.Greeting in project rest.li by linkedin.
the class StreamingGreetings method get.
@Override
public void get(Long key, @CallbackParam Callback<Greeting> callback) {
if (getContext().responseAttachmentsSupported()) {
final GreetingWriter greetingWriter = new GreetingWriter(ByteString.copy(greetingBytes));
final RestLiResponseAttachments streamingAttachments = new RestLiResponseAttachments.Builder().appendSingleAttachment(greetingWriter).build();
getContext().setResponseAttachments(streamingAttachments);
final String headerValue = getContext().getRequestHeaders().get("getHeader");
getContext().setResponseHeader("getHeader", headerValue);
callback.onSuccess(new Greeting().setMessage("Your greeting has an attachment since you were kind and " + "decided you wanted to read it!").setId(key));
}
callback.onError(new RestLiServiceException(HttpStatus.S_400_BAD_REQUEST, "You must be able to receive attachments!"));
}
use of com.linkedin.restli.examples.greetings.api.Greeting in project rest.li by linkedin.
the class TestScatterGather method testSendGetKVSGRequests.
private static void testSendGetKVSGRequests(ScatterGatherBuilder<Greeting> sg, Long[] requestIds) throws ServiceUnavailableException, InterruptedException {
Collection<ScatterGatherBuilder.KVRequestInfo<Long, Greeting>> scatterGatherRequests = buildScatterGatherGetKVRequests(sg, requestIds);
final Map<Long, Greeting> results = new ConcurrentHashMap<Long, Greeting>();
final CountDownLatch latch = new CountDownLatch(scatterGatherRequests.size());
final List<Throwable> errors = new ArrayList<Throwable>();
final List<BatchKVResponse<Long, Greeting>> responses = new ArrayList<BatchKVResponse<Long, Greeting>>();
for (ScatterGatherBuilder.KVRequestInfo<Long, Greeting> requestInfo : scatterGatherRequests) {
Callback<Response<BatchKVResponse<Long, Greeting>>> cb = new Callback<Response<BatchKVResponse<Long, Greeting>>>() {
@Override
public void onSuccess(Response<BatchKVResponse<Long, Greeting>> response) {
results.putAll(response.getEntity().getResults());
synchronized (responses) {
responses.add(response.getEntity());
}
latch.countDown();
}
@Override
public void onError(Throwable e) {
synchronized (errors) {
errors.add(e);
}
latch.countDown();
}
};
REST_CLIENT.sendRequest(requestInfo.getRequest(), requestInfo.getRequestContext(), cb);
}
latch.await();
if (!errors.isEmpty()) {
Assert.fail("Errors in scatter/gather: " + errors.toString());
}
Assert.assertEquals(results.values().size(), requestIds.length);
Set<Set<Long>> responseIdSets = new HashSet<Set<Long>>();
Set<Long> responseIds = new HashSet<Long>();
for (BatchKVResponse<Long, Greeting> response : responses) {
Set<Long> theseIds = response.getResults().keySet();
//no duplicate requests
Assert.assertFalse(responseIdSets.contains(theseIds));
for (Long id : theseIds) {
//no duplicate ids
Assert.assertFalse(responseIds.contains(id));
responseIds.add(id);
}
responseIdSets.add(theseIds);
}
Assert.assertTrue(responseIds.containsAll(Arrays.asList(requestIds)));
Assert.assertEquals(responseIds.size(), requestIds.length);
}
use of com.linkedin.restli.examples.greetings.api.Greeting in project rest.li by linkedin.
the class TestScatterGather method generateCreate.
private static List<Greeting> generateCreate(int num) {
List<Greeting> creates = new ArrayList<Greeting>();
for (int i = 0; i < num; ++i) {
Greeting greeting = new Greeting();
greeting.setMessage("create message").setTone(Tone.FRIENDLY);
creates.add(greeting);
}
return creates;
}
use of com.linkedin.restli.examples.greetings.api.Greeting in project rest.li by linkedin.
the class TestScatterGather method testBuildSGGetEntityRequests.
private static void testBuildSGGetEntityRequests(int numEndpoints, ScatterGatherBuilder<Greeting> sg, Long[] ids) throws ServiceUnavailableException {
Collection<ScatterGatherBuilder.KVRequestInfo<Long, EntityResponse<Greeting>>> requests = buildScatterGatherGetEntityRequests(sg, ids);
Assert.assertEquals(requests.size(), numEndpoints);
Set<Set<String>> requestIdSets = new HashSet<Set<String>>();
Set<Long> requestIds = new HashSet<Long>();
for (ScatterGatherBuilder.KVRequestInfo<Long, EntityResponse<Greeting>> requestInfo : requests) {
//URI will be something like "greetings/?ids=21&ids=4&ids=53&ids=60&ids=66&ids=88&ids=93&foo=bar"
BatchRequest<BatchKVResponse<Long, EntityResponse<Greeting>>> request = requestInfo.getRequest();
Set<String> expectedParams = new HashSet<String>();
expectedParams.add(RestConstants.QUERY_BATCH_IDS_PARAM);
expectedParams.add("foo");
expectedParams.add(RestConstants.FIELDS_PARAM);
Set<PathSpec> expectedFields = Collections.singleton(new PathSpec("message"));
testGetEntityRequest(request, expectedParams, expectedFields, null, requestIdSets, requestIds);
}
Assert.assertTrue(requestIds.containsAll(Arrays.asList(ids)));
Assert.assertEquals(requestIds.size(), ids.length);
}
Aggregations