Search in sources :

Example 1 with DeviceNotificationException

use of io.gravitee.am.authdevice.notifier.api.exception.DeviceNotificationException in project gravitee-access-management by gravitee-io.

the class HttpAuthenticationDeviceNotifierProvider method notify.

@Override
public Single<ADNotificationResponse> notify(ADNotificationRequest request) {
    final MultiMap formData = MultiMap.caseInsensitiveMultiMap();
    formData.set(TRANSACTION_ID, request.getTransactionId());
    formData.set(STATE, request.getState());
    formData.set(PARAM_SUBJECT, request.getSubject());
    formData.set(PARAM_SCOPE, request.getScopes());
    formData.set(PARAM_EXPIRE, Integer.toString(request.getExpiresIn()));
    if (!CollectionUtils.isEmpty(request.getAcrValues())) {
        formData.set(PARAM_ACR, request.getAcrValues());
    }
    if (!isEmpty(request.getMessage())) {
        formData.set(PARAM_MESSAGE, request.getMessage());
    }
    final HttpRequest<Buffer> notificationRequest = this.client.requestAbs(HttpMethod.POST, this.configuration.getEndpoint());
    if (!StringUtils.isEmpty(this.configuration.getHeaderValue())) {
        notificationRequest.putHeader(this.configuration.getHeaderName(), this.configuration.getHeaderValue());
    }
    return notificationRequest.rxSendForm(formData).doOnError((error) -> LOGGER.warn("Unexpected error during device notification : {}", error.getMessage(), error)).onErrorResumeNext(Single.error(new DeviceNotificationException("Unexpected error during device notification"))).flatMap(response -> {
        if (response.statusCode() != HttpStatusCode.OK_200) {
            LOGGER.info("Device notification fails for tid '{}' with status '{}'", request.getTransactionId(), response.statusCode());
            return Single.error(new DeviceNotificationException("Device notification fails"));
        }
        final JsonObject result = response.bodyAsJsonObject();
        if (!request.getTransactionId().equals(result.getString(TRANSACTION_ID)) || !request.getState().equals(result.getString(STATE))) {
            LOGGER.warn("Device notification response contains invalid tid or state", request.getTransactionId(), response.statusCode());
            return Single.error(new DeviceNotificationException("Invalid device notification response"));
        }
        final ADNotificationResponse notificationResponse = new ADNotificationResponse(request.getTransactionId());
        final JsonObject extraData = result.getJsonObject(RESPONSE_ATTR_DATA);
        if (extraData != null) {
            notificationResponse.setExtraData(extraData.getMap());
        }
        return Single.just(notificationResponse);
    });
}
Also used : Buffer(io.vertx.reactivex.core.buffer.Buffer) MultiMap(io.vertx.reactivex.core.MultiMap) DeviceNotificationException(io.gravitee.am.authdevice.notifier.api.exception.DeviceNotificationException) JsonObject(io.vertx.core.json.JsonObject) ADNotificationResponse(io.gravitee.am.authdevice.notifier.api.model.ADNotificationResponse)

Aggregations

DeviceNotificationException (io.gravitee.am.authdevice.notifier.api.exception.DeviceNotificationException)1 ADNotificationResponse (io.gravitee.am.authdevice.notifier.api.model.ADNotificationResponse)1 JsonObject (io.vertx.core.json.JsonObject)1 MultiMap (io.vertx.reactivex.core.MultiMap)1 Buffer (io.vertx.reactivex.core.buffer.Buffer)1