Skip to main content
summaryrefslogtreecommitdiffstats
blob: 3a802ea97914a5137ea05d2898b0a32490a1880c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*******************************************************************************
 * Copyright (c) 2014 Boeing.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Boeing - initial API and implementation
 *******************************************************************************/
package org.eclipse.osee.jaxrs.server.internal.exceptions;

import static org.eclipse.osee.jaxrs.server.internal.JaxRsUtils.newStatusType;
import java.util.ArrayList;
import java.util.List;
import javax.ws.rs.ForbiddenException;
import javax.ws.rs.NotAcceptableException;
import javax.ws.rs.NotAllowedException;
import javax.ws.rs.NotAuthorizedException;
import javax.ws.rs.NotFoundException;
import javax.ws.rs.NotSupportedException;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import javax.ws.rs.core.Response.Status.Family;
import javax.ws.rs.core.Response.StatusType;
import javax.ws.rs.ext.Provider;
import org.eclipse.osee.framework.jdk.core.util.Strings;
import org.eclipse.osee.jaxrs.ErrorResponse;
import org.eclipse.osee.jaxrs.OseeWebApplicationException;
import org.eclipse.osee.logger.Log;

/**
 * @author Roberto E. Escobar
 */
public final class JaxRsExceptions {

   private JaxRsExceptions() {
      // Utility class
   }

   /**
    * Create exception handling providers
    */
   public static List<?> newExceptionProviders(Log logger) {
      List<Object> providers = new ArrayList<Object>();
      providers.add(new ErrorResponseMessageBodyWriter());
      providers.add(new GenericExceptionMapper(logger));
      providers.add(new ForbiddenExceptionMapper(logger));
      providers.add(new NotAcceptableExceptionMapper(logger));
      providers.add(new NotAllowedExceptionMapper(logger));
      providers.add(new NotAuthorizedExceptionMapper(logger));
      providers.add(new NotFoundExceptionMapper(logger));
      providers.add(new NotSupportedExceptionMapper(logger));
      return providers;
   }

   @Provider
   public static class JaxRsExceptionMapper<T extends WebApplicationException> extends AbstractExceptionMapper<T> {

      private static final String APPLICATION_EXCEPTION_TYPE = "Web Application Exception";

      public JaxRsExceptionMapper(Log logger) {
         super(logger);
      }

      protected String getMessage(T ex) {
         return ex.getMessage();
      }

      @Override
      public OseeWebApplicationException asWebAppException(T ex) {
         Response response = ex.getResponse();
         String message = getMessage(ex);
         StatusType status = response.getStatusInfo();
         OseeWebApplicationException exception = new OseeWebApplicationException(ex.getCause(), status, message);
         getLogger().info(ex, "%s - [%s]", APPLICATION_EXCEPTION_TYPE, exception.getErrorResponse());
         return exception;
      }
   }

   @Provider
   public static class ForbiddenExceptionMapper extends JaxRsExceptionMapper<ForbiddenException> {
      public ForbiddenExceptionMapper(Log logger) {
         super(logger);
      }
   }

   @Provider
   public static class NotAcceptableExceptionMapper extends JaxRsExceptionMapper<NotAcceptableException> {
      public NotAcceptableExceptionMapper(Log logger) {
         super(logger);
      }
   }

   @Provider
   public static class NotAllowedExceptionMapper extends JaxRsExceptionMapper<NotAllowedException> {
      public NotAllowedExceptionMapper(Log logger) {
         super(logger);
      }
   }
   @Provider
   public static class NotAuthorizedExceptionMapper extends JaxRsExceptionMapper<NotAuthorizedException> {
      public NotAuthorizedExceptionMapper(Log logger) {
         super(logger);
      }
   }

   @Provider
   public static class NotFoundExceptionMapper extends JaxRsExceptionMapper<NotFoundException> {
      public NotFoundExceptionMapper(Log logger) {
         super(logger);
      }

      @Override
      protected String getMessage(NotFoundException ex) {
         String baseMessage = super.getMessage(ex);
         String message =
            String.format("%sUnable to find resource at [%s]", Strings.isValid(baseMessage) ? baseMessage + " - " : "",
               getUriInfo().getRequestUri().toASCIIString());
         return message;
      }
   }

   @Provider
   public static class NotSupportedExceptionMapper extends JaxRsExceptionMapper<NotSupportedException> {
      public NotSupportedExceptionMapper(Log logger) {
         super(logger);
      }
   }

   @Provider
   public static class GenericExceptionMapper extends AbstractExceptionMapper<Throwable> {

      private static final String SEE_HTTP_STATUS_CODES = "See HTTP Status codes";
      private static final String INTERNAL_SERVER_ERROR_TYPE = "Internal Server Error";
      private static final String APPLICATION_EXCEPTION_TYPE = "Web Application Exception";
      private static final String OSEE_APPLICATION_EXCEPTION_TYPE = "Osee Web Application Exception";

      public GenericExceptionMapper(Log logger) {
         super(logger);
      }

      @Override
      public OseeWebApplicationException asWebAppException(Throwable throwable) {
         boolean isError = false;
         String logMessage;

         OseeWebApplicationException exception;
         if (throwable instanceof OseeWebApplicationException) {
            logMessage = OSEE_APPLICATION_EXCEPTION_TYPE;
            exception = ((OseeWebApplicationException) throwable);
         } else if (throwable instanceof WebApplicationException) {
            logMessage = APPLICATION_EXCEPTION_TYPE;
            WebApplicationException webAppException = ((WebApplicationException) throwable);
            Response response = webAppException.getResponse();
            int statusCode = response.getStatus();
            String message = webAppException.getMessage();

            StatusType status = Status.fromStatusCode(statusCode);
            if (status == null) {
               status = newStatusType(statusCode, Family.SERVER_ERROR, SEE_HTTP_STATUS_CODES);
            }
            exception = new OseeWebApplicationException(throwable, status, message);
         } else {
            isError = true;
            logMessage = INTERNAL_SERVER_ERROR_TYPE;
            exception = new OseeWebApplicationException(throwable, Status.INTERNAL_SERVER_ERROR);
         }

         ErrorResponse errorResponse = exception.getErrorResponse();
         if (isError) {
            getLogger().error(throwable, "%s - [%s]", logMessage, errorResponse);
         } else {
            getLogger().info(throwable, "%s - [%s]", logMessage, errorResponse);
         }
         return exception;
      }
   }
}

Back to the top