use of org.eclipse.hono.client.ClientErrorException in project hono by eclipse.
the class BaseRegistrationService method processDeregisterRequest.
private Future<EventBusMessage> processDeregisterRequest(final EventBusMessage request) {
final String tenantId = request.getTenant();
final String deviceId = request.getDeviceId();
if (tenantId == null || deviceId == null) {
return Future.failedFuture(new ClientErrorException(HttpURLConnection.HTTP_BAD_REQUEST));
} else {
log.debug("deregistering device [{}] of tenant [{}]", deviceId, tenantId);
final Future<RegistrationResult> result = Future.future();
removeDevice(tenantId, deviceId, result.completer());
return result.map(res -> {
return request.getResponse(res.getStatus()).setDeviceId(deviceId).setCacheDirective(res.getCacheDirective());
});
}
}
use of org.eclipse.hono.client.ClientErrorException in project hono by eclipse.
the class BaseTenantService method processAddRequest.
private Future<EventBusMessage> processAddRequest(final EventBusMessage request) {
final String tenantId = request.getTenant();
final JsonObject payload = getRequestPayload(request.getJsonPayload());
if (tenantId == null) {
log.debug("request does not contain mandatory property [{}]", MessageHelper.APP_PROPERTY_TENANT_ID);
return Future.failedFuture(new ClientErrorException(HttpURLConnection.HTTP_BAD_REQUEST));
} else if (isValidRequestPayload(payload)) {
log.debug("creating tenant [{}]", tenantId);
final Future<TenantResult<JsonObject>> addResult = Future.future();
addNotPresentFieldsWithDefaultValuesForTenant(payload);
add(tenantId, payload, addResult.completer());
return addResult.map(tr -> {
return request.getResponse(tr.getStatus()).setJsonPayload(tr.getPayload()).setCacheDirective(tr.getCacheDirective());
});
} else {
log.debug("request contains malformed payload");
return Future.failedFuture(new ClientErrorException(HttpURLConnection.HTTP_BAD_REQUEST));
}
}
use of org.eclipse.hono.client.ClientErrorException in project hono by eclipse.
the class BaseTenantService method processGetRequest.
private Future<EventBusMessage> processGetRequest(final EventBusMessage request) {
final String tenantId = request.getTenant();
final JsonObject payload = request.getJsonPayload();
if (tenantId == null && payload == null) {
log.debug("request does not contain any query parameters");
return Future.failedFuture(new ClientErrorException(HttpURLConnection.HTTP_BAD_REQUEST));
} else if (payload != null) {
final String tenantIdFromPayload = getTypesafeValueForField(payload, TenantConstants.FIELD_PAYLOAD_TENANT_ID);
if (tenantIdFromPayload == null) {
log.debug("payload does not contain any query parameters");
return Future.failedFuture(new ClientErrorException(HttpURLConnection.HTTP_BAD_REQUEST));
} else {
log.debug("retrieving tenant [id: {}]", tenantIdFromPayload);
final Future<TenantResult<JsonObject>> getResult = Future.future();
get(tenantIdFromPayload, getResult.completer());
return getResult.map(tr -> {
return request.getResponse(tr.getStatus()).setJsonPayload(tr.getPayload()).setTenant(tenantIdFromPayload).setCacheDirective(tr.getCacheDirective());
});
}
} else {
// deprecated API
log.debug("retrieving tenant [{}] using deprecated variant of get tenant request", tenantId);
final Future<TenantResult<JsonObject>> getResult = Future.future();
get(tenantId, getResult.completer());
return getResult.map(tr -> {
return request.getResponse(tr.getStatus()).setJsonPayload(tr.getPayload()).setTenant(tenantId).setCacheDirective(tr.getCacheDirective());
});
}
}
use of org.eclipse.hono.client.ClientErrorException in project hono by eclipse.
the class CredentialsApiAuthProviderTest method testAuthenticateFailsWith401ForMalformedCredentials.
/**
* Verifies that the auth provider fails an authentication request with a 401
* {@code ClientErrorException} if the credentials cannot be parsed.
*
* @param ctx The vert.x test context.
*/
@Test
public void testAuthenticateFailsWith401ForMalformedCredentials(final TestContext ctx) {
// WHEN trying to authenticate using malformed credentials
// that do not contain a tenant
final JsonObject authInfo = new JsonObject().put("username", "no-tenant").put("password", "secret");
provider.authenticate(authInfo, ctx.asyncAssertFailure(t -> {
// THEN authentication fails with a 401 client error
ctx.assertEquals(HttpURLConnection.HTTP_UNAUTHORIZED, ((ClientErrorException) t).getErrorCode());
}));
}
use of org.eclipse.hono.client.ClientErrorException in project hono by eclipse.
the class CredentialsApiAuthProviderTest method testAuthenticateFailsWith401ForNonExistingAuthId.
/**
* Verifies that the auth provider fails an authentication request with a 401
* {@code ClientErrorException} if the auth-id is unknown.
*
* @param ctx The vert.x test context.
*/
@Test
public void testAuthenticateFailsWith401ForNonExistingAuthId(final TestContext ctx) {
// WHEN trying to authenticate using an auth-id that is not known
final JsonObject authInfo = new JsonObject().put("username", "unknown@TENANT").put("password", "secret");
when(credentialsClient.isOpen()).thenReturn(Boolean.TRUE);
when(credentialsClient.get(anyString(), eq("unknown"))).thenReturn(Future.failedFuture(new ClientErrorException(HttpURLConnection.HTTP_NOT_FOUND)));
provider.authenticate(authInfo, ctx.asyncAssertFailure(t -> {
// THEN authentication fails with a 401 client error
ctx.assertEquals(HttpURLConnection.HTTP_UNAUTHORIZED, ((ClientErrorException) t).getErrorCode());
}));
}
Aggregations