@Test public void test() { ThrottlingGuiceServletConfig guiceServletConfig = new ThrottlingGuiceServletConfig(); ThrottlingPolicyFactory factory = new ThrottlingPolicyFactory(); SharedLimiterKey res1key = new SharedLimiterKey("res1"); Map<String, String> configMap = avro.shaded.com.google.common.collect.ImmutableMap.<String, String>builder() .put(BrokerConfigurationKeyGenerator.generateKey(factory, res1key, null, ThrottlingPolicyFactory.POLICY_KEY), CountBasedPolicy.FACTORY_ALIAS) .put(BrokerConfigurationKeyGenerator.generateKey(factory, res1key, null, CountBasedPolicy.COUNT_KEY), "100") .build(); guiceServletConfig.initialize(ConfigFactory.parseMap(configMap)); Injector injector = guiceServletConfig.getInjector(); PoliciesResource policiesResource = injector.getInstance(PoliciesResource.class); Policy policy = policiesResource.get("res1"); Assert.assertEquals(policy.getPolicyName(), CountBasedPolicy.class.getSimpleName()); Assert.assertEquals(policy.getResource(), "res1"); Assert.assertEquals(policy.getParameters().get("maxPermits"), "100"); guiceServletConfig.close(); }
@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); } }
PoliciesResource extends CollectionResourceTemplate<String, Policy> { @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); } } }
PoliciesResource extends CollectionResourceTemplate<String, Policy> { @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); } }  }
PoliciesResource extends CollectionResourceTemplate<String, Policy> { @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); } }  @Override Policy get(String resourceId); }
PoliciesResource extends CollectionResourceTemplate<String, Policy> { @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); } }  @Override Policy get(String resourceId);  }