All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.bremersee.web.reactive.ApiExceptionHandler Maven / Gradle / Ivy

There is a newer version: 2.3.3
Show newest version
/*
 * Copyright 2018 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.bremersee.web.reactive;

import java.time.OffsetDateTime;
import java.time.ZoneId;
import javax.validation.constraints.NotNull;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.bremersee.exception.RestApiExceptionMapper;
import org.bremersee.exception.RestApiExceptionUtils;
import org.bremersee.exception.model.RestApiException;
import org.bremersee.http.MediaTypeHelper;
import org.springframework.boot.autoconfigure.web.ResourceProperties;
import org.springframework.boot.autoconfigure.web.reactive.error.AbstractErrorWebExceptionHandler;
import org.springframework.boot.web.reactive.error.ErrorAttributes;
import org.springframework.context.ApplicationContext;
import org.springframework.http.MediaType;
import org.springframework.http.codec.ServerCodecConfigurer;
import org.springframework.lang.Nullable;
import org.springframework.util.AntPathMatcher;
import org.springframework.util.PathMatcher;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.RouterFunctions;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Mono;

/**
 * The reactive api exception handler.
 *
 * @author Christian Bremer
 */
@Validated
@Slf4j
public class ApiExceptionHandler extends AbstractErrorWebExceptionHandler {

  @Getter(AccessLevel.PROTECTED)
  @Setter
  @NotNull
  private PathMatcher pathMatcher = new AntPathMatcher();

  @Getter(AccessLevel.PROTECTED)
  @NotNull
  private final RestApiExceptionMapper restApiExceptionMapper;

  /**
   * Instantiates a new api exception handler.
   *
   * @param errorAttributes        the error attributes
   * @param resourceProperties     the resource properties
   * @param applicationContext     the application context
   * @param serverCodecConfigurer  the server codec configurer
   * @param restApiExceptionMapper the rest api exception mapper
   */
  @SuppressWarnings("WeakerAccess")
  public ApiExceptionHandler(
      @NotNull final ErrorAttributes errorAttributes,
      @NotNull final ResourceProperties resourceProperties,
      @NotNull final ApplicationContext applicationContext,
      @Nullable final ServerCodecConfigurer serverCodecConfigurer,
      @NotNull final RestApiExceptionMapper restApiExceptionMapper) {

    super(errorAttributes, resourceProperties, applicationContext);
    if (serverCodecConfigurer != null) {
      setMessageReaders(serverCodecConfigurer.getReaders());
      setMessageWriters(serverCodecConfigurer.getWriters());
    }
    this.restApiExceptionMapper = restApiExceptionMapper;
  }

  @Override
  protected RouterFunction getRoutingFunction(
      final ErrorAttributes errorAttributes) {

    return RouterFunctions.route(this::isResponsibleExceptionHandler, this::renderErrorResponse);
  }

  /**
   * Is this exception handler responsible.
   *
   * @param request the request
   * @return {@code true} if it is responsible, otherwise {@code false}
   */
  @SuppressWarnings("WeakerAccess")
  protected boolean isResponsibleExceptionHandler(final ServerRequest request) {
    return restApiExceptionMapper.getApiPaths().stream().anyMatch(
        s -> pathMatcher.match(s, request.path()));
  }

  /**
   * Render error response.
   *
   * @param request the request
   * @return the server response
   */
  @SuppressWarnings("WeakerAccess")
  protected Mono renderErrorResponse(final ServerRequest request) {

    final RestApiException response = restApiExceptionMapper
        .build(getError(request), request.path(), null);
    final String accepts = MediaTypeHelper.toString(request.headers().accept());
    if (MediaTypeHelper.canContentTypeBeJson(accepts)) {
      return ServerResponse
          .status(restApiExceptionMapper.detectHttpStatus(getError(request), null))
          .contentType(MediaType.APPLICATION_JSON)
          .body(BodyInserters.fromObject(response));
    } else if (MediaTypeHelper.canContentTypeBeXml(accepts)) {
      return ServerResponse
          .status(restApiExceptionMapper.detectHttpStatus(getError(request), null))
          .contentType(MediaType.APPLICATION_XML)
          .body(BodyInserters.fromObject(response));
    } else {
      final String id = StringUtils.hasText(response.getId())
          ? response.getId()
          : RestApiExceptionUtils.NO_ID_VALUE;
      final String timestamp = response.getTimestamp() != null
          ? response.getTimestamp().format(RestApiExceptionUtils.TIMESTAMP_FORMATTER)
          : OffsetDateTime.now(ZoneId.of("UTC")).format(RestApiExceptionUtils.TIMESTAMP_FORMATTER);
      final String msg = StringUtils.hasText(response.getMessage())
          ? response.getMessage()
          : RestApiExceptionUtils.NO_MESSAGE_VALUE;
      final String code = StringUtils.hasText(response.getErrorCode())
          ? response.getErrorCode()
          : RestApiExceptionUtils.NO_ERROR_CODE_VALUE;
      final String cls = StringUtils.hasText(response.getClassName())
          ? response.getClassName()
          : RestApiExceptionUtils.NO_CLASS_VALUE;
      return ServerResponse
          .status(restApiExceptionMapper.detectHttpStatus(getError(request), null))
          .header(RestApiExceptionUtils.ID_HEADER_NAME, id)
          .header(RestApiExceptionUtils.TIMESTAMP_HEADER_NAME, timestamp)
          .header(RestApiExceptionUtils.MESSAGE_HEADER_NAME, msg)
          .header(RestApiExceptionUtils.CODE_HEADER_NAME, code)
          .header(RestApiExceptionUtils.CLASS_HEADER_NAME, cls)
          .contentType(MediaTypeHelper.findContentType(
              request.headers().accept(), MediaType.TEXT_PLAIN))
          .body(BodyInserters.empty());
    }
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy