View Javadoc
1   /*
2    * This file is part of Waarp Project (named also Waarp or GG).
3    *
4    *  Copyright (c) 2019, Waarp SAS, and individual contributors by the @author
5    *  tags. See the COPYRIGHT.txt in the distribution for a full listing of
6    * individual contributors.
7    *
8    *  All Waarp Project is free software: you can redistribute it and/or
9    * modify it under the terms of the GNU General Public License as published by
10   * the Free Software Foundation, either version 3 of the License, or (at your
11   * option) any later version.
12   *
13   * Waarp is distributed in the hope that it will be useful, but WITHOUT ANY
14   * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15   * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16   *
17   *  You should have received a copy of the GNU General Public License along with
18   * Waarp . If not, see <http://www.gnu.org/licenses/>.
19   */
20  
21  package org.waarp.openr66.protocol.http.restv2.resthandlers;
22  
23  import io.cdap.http.ExceptionHandler;
24  import io.cdap.http.HttpResponder;
25  import io.netty.handler.codec.http.DefaultHttpHeaders;
26  import io.netty.handler.codec.http.HttpRequest;
27  import org.waarp.common.logging.WaarpLogger;
28  import org.waarp.common.logging.WaarpLoggerFactory;
29  import org.waarp.openr66.protocol.http.restv2.errors.RestErrorException;
30  import org.waarp.openr66.protocol.http.restv2.utils.JsonUtils;
31  import org.waarp.openr66.protocol.http.restv2.utils.RestUtils;
32  
33  import javax.ws.rs.InternalServerErrorException;
34  import javax.ws.rs.NotSupportedException;
35  import java.util.Locale;
36  
37  import static io.netty.handler.codec.http.HttpResponseStatus.*;
38  import static javax.ws.rs.core.HttpHeaders.*;
39  
40  /**
41   * Handles all exceptions thrown by handlers during the processing of an HTTP
42   * request.
43   */
44  public class RestExceptionHandler extends ExceptionHandler {
45  
46    /**
47     * The logger for all events.
48     */
49    private static final WaarpLogger logger =
50        WaarpLoggerFactory.getLogger(RestExceptionHandler.class);
51  
52    /**
53     * Method called when an exception is thrown during the processing of a
54     * request.
55     *
56     * @param t the exception thrown during execution
57     * @param request the HttpRequest that failed
58     * @param responder the HttpResponder for the request
59     */
60    @Override
61    public void handle(final Throwable t, final HttpRequest request,
62                       final HttpResponder responder) {
63      if (t instanceof RestErrorException) {
64        final RestErrorException userErrors = (RestErrorException) t;
65        try {
66          final Locale lang = RestUtils.getLocale(request);
67          final String errorText =
68              JsonUtils.nodeToString(userErrors.makeNode(lang));
69          responder.sendJson(BAD_REQUEST, errorText);
70        } catch (final InternalServerErrorException e) {
71          logger.error(e);
72          responder.sendStatus(INTERNAL_SERVER_ERROR);
73        }
74      } else if (t instanceof NotSupportedException) {
75        final DefaultHttpHeaders headers = new DefaultHttpHeaders();
76        headers.add(ACCEPT, t.getMessage());
77        responder.sendStatus(UNSUPPORTED_MEDIA_TYPE, headers);
78      } else {
79        logger.error(t);
80        responder.sendStatus(INTERNAL_SERVER_ERROR);
81      }
82    }
83  }