use of com.linkedin.restli.server.RestLiServiceException in project incubator-gobblin by apache.
the class PoliciesResource method get.
@Override
public Policy get(String resourceId) {
try {
ThrottlingPolicy throttlingPolicy = (ThrottlingPolicy) this.broker.getSharedResource(new ThrottlingPolicyFactory(), new SharedLimiterKey(resourceId));
Policy restliPolicy = new Policy();
restliPolicy.setPolicyName(throttlingPolicy.getClass().getSimpleName());
restliPolicy.setResource(resourceId);
restliPolicy.setParameters(new StringMap(throttlingPolicy.getParameters()));
restliPolicy.setPolicyDetails(throttlingPolicy.getDescription());
MetricContext resourceContext = (MetricContext) broker.getSharedResource(new MetricContextFactory(), new SubTaggedMetricContextKey(resourceId, ImmutableMap.of(RESOURCE_ID_TAG, resourceId)));
StringMap metrics = new StringMap();
for (Map.Entry<String, Meter> meter : resourceContext.getMeters().entrySet()) {
metrics.put(meter.getKey(), Double.toString(meter.getValue().getOneMinuteRate()));
}
restliPolicy.setMetrics(metrics);
return restliPolicy;
} catch (NotConfiguredException nce) {
throw new RestLiServiceException(HttpStatus.S_404_NOT_FOUND, "Policy not found for resource " + resourceId);
}
}
use of com.linkedin.restli.server.RestLiServiceException in project incubator-gobblin by apache.
the class TestFailover method test.
@Test
public void test() throws Exception {
try (Closer closer = Closer.create()) {
Map<String, String> configMap = Maps.newHashMap();
TestingServer zkTestingServer = closer.register(new TestingServer(-1));
configMap.put(ThrottlingGuiceServletConfig.ZK_STRING_KEY, zkTestingServer.getConnectString());
configMap.put(ThrottlingGuiceServletConfig.HA_CLUSTER_NAME, TestFailover.class.getSimpleName() + "_cluster");
Config config = ConfigFactory.parseMap(configMap);
ThrottlingGuiceServletConfig server2001 = createServerAtPort(config, 2001);
PermitAllocation allocation = sendRequestToServer(server2001, 10);
Assert.assertTrue(allocation.getPermits() >= 1);
ThrottlingGuiceServletConfig server2002 = createServerAtPort(config, 2002);
allocation = sendRequestToServer(server2001, 10);
Assert.assertTrue(allocation.getPermits() >= 1);
try {
sendRequestToServer(server2002, 10);
Assert.fail();
} catch (RestLiServiceException exc) {
Assert.assertTrue(exc.hasErrorDetails());
Assert.assertTrue(exc.getErrorDetails().containsKey(LimiterServerResource.LOCATION_301));
Assert.assertEquals(new URI(exc.getErrorDetails().get(LimiterServerResource.LOCATION_301).toString()).getPort(), 2001);
}
server2001.close();
allocation = sendRequestToServer(server2002, 10);
Assert.assertTrue(allocation.getPermits() >= 1);
}
}
use of com.linkedin.restli.server.RestLiServiceException in project rest.li by linkedin.
the class AutomaticValidationDemoResource method batchGet.
@RestMethod.BatchGet
public BatchResult<Integer, ValidationDemo> batchGet(Set<Integer> ids) {
Map<Integer, ValidationDemo> resultMap = new HashMap<>();
Map<Integer, RestLiServiceException> errorMap = new HashMap<>();
// Generate entities that are missing a required field
for (Integer id : ids) {
if (id == 0) {
errorMap.put(id, new RestLiServiceException(HttpStatus.S_400_BAD_REQUEST));
} else if (id == 1) {
ValidationDemo.UnionFieldWithInlineRecord union = new ValidationDemo.UnionFieldWithInlineRecord();
union.setMyRecord(new myRecord().setFoo1(100).setFoo2(200));
resultMap.put(id, new ValidationDemo().setStringA("a").setStringB("b").setUnionFieldWithInlineRecord(union));
} else {
ValidationDemo.UnionFieldWithInlineRecord union = new ValidationDemo.UnionFieldWithInlineRecord();
union.setMyRecord(new myRecord());
ValidationDemo validationDemo = new ValidationDemo().setStringA("a").setStringB("b").setUnionFieldWithInlineRecord(union);
resultMap.put(id, validationDemo);
}
}
;
return new BatchResult<>(resultMap, errorMap);
}
use of com.linkedin.restli.server.RestLiServiceException in project rest.li by linkedin.
the class ComplexKeysDataProvider method batchGet.
public BatchResult<ComplexResourceKey<TwoPartKey, TwoPartKey>, Message> batchGet(Set<ComplexResourceKey<TwoPartKey, TwoPartKey>> keys) {
Map<ComplexResourceKey<TwoPartKey, TwoPartKey>, Message> data = new HashMap<>();
Map<ComplexResourceKey<TwoPartKey, TwoPartKey>, RestLiServiceException> errors = new HashMap<>();
for (ComplexResourceKey<TwoPartKey, TwoPartKey> key : keys) {
String stringKey = keyToString(key.getKey());
if (_db.containsKey(stringKey)) {
data.put(key, _db.get(stringKey));
} else {
errors.put(key, new RestLiServiceException(HttpStatus.S_404_NOT_FOUND));
}
}
return new BatchResult<>(data, errors);
}
use of com.linkedin.restli.server.RestLiServiceException in project rest.li by linkedin.
the class AutomaticValidationDemoResource method batchUpdate.
@RestMethod.BatchPartialUpdate
public BatchUpdateResult<Integer, ValidationDemo> batchUpdate(final BatchPatchRequest<Integer, ValidationDemo> entityUpdates) {
Map<Integer, UpdateResponse> results = new HashMap<>();
Map<Integer, RestLiServiceException> errors = new HashMap<>();
for (Map.Entry<Integer, PatchRequest<ValidationDemo>> entry : entityUpdates.getData().entrySet()) {
Integer key = entry.getKey();
PatchRequest<ValidationDemo> patch = entry.getValue();
results.put(key, new UpdateResponse(HttpStatus.S_204_NO_CONTENT));
}
return new BatchUpdateResult<>(results, errors);
}
Aggregations