content: /*
   * Copyright 2018-2024 adorsys GmbH & Co KG
   *
   * This program is free software: you can redistribute it and/or modify it
   * under the terms of the GNU Affero General Public License as published
   * by the Free Software Foundation, either version 3 of the License, or (at
   * your option) any later version. This program is distributed in the hope that
   * it will be useful, but WITHOUT ANY WARRANTY; without even the implied
   * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
   * See the GNU Affero General Public License for more details.
   *
   * You should have received a copy of the GNU Affero General Public License
   * along with this program. If not, see https://www.gnu.org/licenses/.
   *
   * This project is also available under a separate commercial license. You can
   * contact us at sales@adorsys.com.
   */
  
  package de.adorsys.psd2.xs2a.web.advice;
  
  import de.adorsys.psd2.xs2a.web.Xs2aEndpointChecker;
  import de.adorsys.psd2.xs2a.web.error.TppErrorMessageWriter;
  import de.adorsys.psd2.xs2a.web.filter.TppErrorMessage;
  import lombok.RequiredArgsConstructor;
  import lombok.extern.slf4j.Slf4j;
  import org.springframework.stereotype.Component;
  import org.springframework.web.HttpMediaTypeNotAcceptableException;
  import org.springframework.web.HttpRequestMethodNotSupportedException;
  import org.springframework.web.servlet.ModelAndView;
  import org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver;
  
  import jakarta.servlet.http.HttpServletRequest;
  import jakarta.servlet.http.HttpServletResponse;
  import java.io.IOException;
  
  import static de.adorsys.psd2.xs2a.core.domain.MessageCategory.ERROR;
  import static de.adorsys.psd2.xs2a.core.error.MessageErrorCode.REQUESTED_FORMATS_INVALID;
  import static de.adorsys.psd2.xs2a.core.error.MessageErrorCode.SERVICE_INVALID_405_METHOD_NOT_SUPPORTED;
  
  @Slf4j
  @Component
  @RequiredArgsConstructor
  public class Xs2aRestExceptionHandler extends DefaultHandlerExceptionResolver {
      private final TppErrorMessageWriter tppErrorMessageWriter;
      private final Xs2aEndpointChecker xs2aEndpointChecker;
  
      @Override
      protected ModelAndView doResolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
          if (xs2aEndpointChecker.isXs2aEndpoint(request)) {
              return super.doResolveException(request, response, handler, ex);
          }
          return null;
      }
  
      @Override
      protected ModelAndView handleHttpRequestMethodNotSupported(HttpRequestMethodNotSupportedException ex, HttpServletRequest request, HttpServletResponse response, Object handler) throws IOException {
          tppErrorMessageWriter.writeError(response, new TppErrorMessage(ERROR, SERVICE_INVALID_405_METHOD_NOT_SUPPORTED, ex.getMethod()));
          return new ModelAndView();
      }
  
      @Override
      protected ModelAndView handleHttpMediaTypeNotAcceptable(HttpMediaTypeNotAcceptableException ex, HttpServletRequest request, HttpServletResponse response, Object handler) throws IOException {
          tppErrorMessageWriter.writeError(response, new TppErrorMessage(ERROR, REQUESTED_FORMATS_INVALID));
          return new ModelAndView();
      }
  }
  