View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.syncope.common.keymaster.client.self;
20  
21  import java.security.AccessControlException;
22  import javax.ws.rs.BadRequestException;
23  import javax.ws.rs.ForbiddenException;
24  import javax.ws.rs.NotFoundException;
25  import javax.ws.rs.core.Response;
26  import javax.ws.rs.ext.Provider;
27  import javax.xml.ws.WebServiceException;
28  import org.apache.commons.lang3.StringUtils;
29  import org.apache.cxf.jaxrs.client.ResponseExceptionMapper;
30  import org.apache.syncope.common.keymaster.client.api.KeymasterException;
31  import org.slf4j.Logger;
32  import org.slf4j.LoggerFactory;
33  
34  @Provider
35  public class SelfKeymasterClientExceptionMapper implements ResponseExceptionMapper<Exception> {
36  
37      private static final Logger LOG = LoggerFactory.getLogger(SelfKeymasterClientExceptionMapper.class);
38  
39      @Override
40      public Exception fromResponse(final Response response) {
41          int statusCode = response.getStatus();
42          String message = response.readEntity(String.class);
43  
44          Exception ex;
45          if (statusCode == Response.Status.UNAUTHORIZED.getStatusCode()) {
46              // 1. Map SC_UNAUTHORIZED
47              ex = new AccessControlException(StringUtils.isBlank(message)
48                      ? "Remote unauthorized exception"
49                      : message);
50          } else if (statusCode == Response.Status.FORBIDDEN.getStatusCode()) {
51              // 2. Map SC_FORBIDDEN
52              ex = new ForbiddenException(StringUtils.isBlank(message)
53                      ? "Remote forbidden exception"
54                      : message);
55          } else if (statusCode == Response.Status.NOT_FOUND.getStatusCode()) {
56              // 3. Map SC_NOT_FOUND
57              ex = StringUtils.isBlank(message)
58                      ? new NotFoundException()
59                      : new NotFoundException(message);
60          } else if (statusCode == Response.Status.BAD_REQUEST.getStatusCode()) {
61              // 4. Map SC_BAD_REQUEST
62              ex = StringUtils.isBlank(message)
63                      ? new BadRequestException()
64                      : message.contains(KeymasterException.class.getSimpleName())
65                      ? new KeymasterException(message)
66                      : new BadRequestException(message);
67          } else {
68              // 5. All other codes are mapped to runtime exception with HTTP code information
69              ex = new WebServiceException(String.format("Remote exception with status code: %s",
70                      Response.Status.fromStatusCode(statusCode).name()));
71          }
72          LOG.error("Exception thrown", ex);
73          return ex;
74      }
75  }