use of org.eclipse.hono.client.util.AnnotatedCacheKey in project hono by eclipse.
the class ProtonBasedTenantClientTest method addResultToCache.
private Future<AnnotatedCacheKey<?>> addResultToCache(final String tenantId) {
final TenantResult<TenantObject> tenantResult = TenantResult.from(HttpURLConnection.HTTP_OK, new TenantObject(tenantId, false));
@SuppressWarnings("unchecked") final ArgumentCaptor<AnnotatedCacheKey<?>> responseCacheKey = ArgumentCaptor.forClass(AnnotatedCacheKey.class);
when(cache.getIfPresent(responseCacheKey.capture())).thenReturn(tenantResult);
return client.get(tenantId, null).map(tenantObject -> {
final AnnotatedCacheKey<?> cacheKey = responseCacheKey.getValue();
cacheKey.putAttribute("tenant-id", tenantId);
cacheBackingMap.put(cacheKey, tenantResult);
return cacheKey;
});
}
use of org.eclipse.hono.client.util.AnnotatedCacheKey in project hono by eclipse.
the class ProtonBasedDeviceRegistrationClient method assertRegistration.
@Override
public Future<RegistrationAssertion> assertRegistration(final String tenantId, final String deviceId, final String gatewayId, final SpanContext context) {
Objects.requireNonNull(tenantId);
Objects.requireNonNull(deviceId);
final AnnotatedCacheKey<CacheKey> responseCacheKey = new AnnotatedCacheKey<>(new CacheKey(tenantId, deviceId, gatewayId));
final Span span = newChildSpan(context, "assert Device Registration");
TracingHelper.setDeviceTags(span, tenantId, deviceId);
TracingHelper.TAG_GATEWAY_ID.set(span, gatewayId);
return getResponseFromCache(responseCacheKey, span).recover(t -> getOrCreateClient(tenantId).compose(client -> {
final Map<String, Object> properties = createDeviceIdProperties(deviceId);
if (gatewayId != null) {
properties.put(MessageHelper.APP_PROPERTY_GATEWAY_ID, gatewayId);
}
return client.createAndSendRequest(RegistrationConstants.ACTION_ASSERT, properties, null, RegistrationConstants.CONTENT_TYPE_APPLICATION_JSON, this::getRequestResponseResult, span);
}).map(registrationResult -> {
addToCache(responseCacheKey, registrationResult);
return registrationResult;
})).recover(t -> {
Tags.HTTP_STATUS.set(span, ServiceInvocationException.extractStatusCode(t));
TracingHelper.logError(span, t);
return Future.failedFuture(t);
}).map(registrationResult -> {
Tags.HTTP_STATUS.set(span, registrationResult.getStatus());
if (registrationResult.isError()) {
Tags.ERROR.set(span, Boolean.TRUE);
}
switch(registrationResult.getStatus()) {
case HttpURLConnection.HTTP_OK:
final JsonObject payload = registrationResult.getPayload();
try {
return payload.mapTo(RegistrationAssertion.class);
} catch (final DecodeException e) {
if (log.isDebugEnabled()) {
log.debug("registration service returned invalid response:{}{}", System.lineSeparator(), payload.encodePrettily());
}
TracingHelper.logError(span, "registration service returned invalid response", e);
throw new ServerErrorException(HttpURLConnection.HTTP_INTERNAL_ERROR, "registration service returned invalid response");
}
case HttpURLConnection.HTTP_NOT_FOUND:
throw new ClientErrorException(registrationResult.getStatus(), "device unknown or disabled");
case HttpURLConnection.HTTP_FORBIDDEN:
throw new ClientErrorException(registrationResult.getStatus(), "gateway unknown, disabled or not authorized to act on behalf of device");
default:
throw StatusCodeMapper.from(registrationResult);
}
}).onComplete(o -> span.finish());
}
use of org.eclipse.hono.client.util.AnnotatedCacheKey in project hono by eclipse.
the class ProtonBasedDeviceRegistrationClient method removeResultsForDeviceFromCache.
@SuppressWarnings("unchecked")
private void removeResultsForDeviceFromCache(final String tenantId, final String deviceId) {
removeFromCacheByPattern(key -> {
final CacheKey cacheKey = ((AnnotatedCacheKey<CacheKey>) key).getKey();
final boolean tenantMatches = cacheKey.tenantId.equals(tenantId);
final boolean deviceOrGatewayMatches = cacheKey.deviceId.equals(deviceId) || Objects.equals(cacheKey.gatewayId, deviceId);
return tenantMatches && deviceOrGatewayMatches;
});
}
use of org.eclipse.hono.client.util.AnnotatedCacheKey in project hono by eclipse.
the class ProtonBasedTenantClient method get.
@Override
public Future<TenantObject> get(final String tenantId, final SpanContext parent) {
Objects.requireNonNull(tenantId);
final AnnotatedCacheKey<String> responseCacheKey = new AnnotatedCacheKey<>(tenantId);
final Span span = newChildSpan(parent, "get Tenant by ID");
span.setTag(MessageHelper.APP_PROPERTY_TENANT_ID, tenantId);
return get(responseCacheKey, () -> new JsonObject().put(TenantConstants.FIELD_PAYLOAD_TENANT_ID, tenantId), span);
}
use of org.eclipse.hono.client.util.AnnotatedCacheKey in project hono by eclipse.
the class ProtonBasedTenantClient method get.
@Override
public Future<TenantObject> get(final X500Principal subjectDn, final SpanContext parent) {
Objects.requireNonNull(subjectDn);
final String subjectDnRfc2253 = subjectDn.getName(X500Principal.RFC2253);
final AnnotatedCacheKey<X500Principal> responseCacheKey = new AnnotatedCacheKey<>(subjectDn);
final Span span = newChildSpan(parent, "get Tenant by subject DN");
TAG_SUBJECT_DN.set(span, subjectDnRfc2253);
return get(responseCacheKey, () -> new JsonObject().put(TenantConstants.FIELD_PAYLOAD_SUBJECT_DN, subjectDnRfc2253), span);
}
Aggregations