View Javadoc
1   /*
2    * Copyright 2019 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.bremersee.ldaptive;
18  
19  import java.io.Serial;
20  import lombok.EqualsAndHashCode;
21  import org.bremersee.exception.AbstractServiceExceptionBuilder;
22  import org.bremersee.exception.ErrorCodeAware;
23  import org.bremersee.exception.HttpStatusAware;
24  import org.bremersee.exception.ServiceException;
25  import org.bremersee.exception.ServiceExceptionBuilder;
26  import org.ldaptive.LdapException;
27  import org.ldaptive.ResultCode;
28  
29  /**
30   * The ldaptive exception.
31   *
32   * @author Christian Bremer
33   */
34  @EqualsAndHashCode(callSuper = true)
35  public class LdaptiveException extends ServiceException implements HttpStatusAware, ErrorCodeAware {
36  
37    @Serial
38    private static final long serialVersionUID = 1L;
39  
40    /**
41     * Instantiates a new ldaptive exception.
42     *
43     * @param httpStatus the http status
44     * @param errorCode the error code
45     * @param reason the reason
46     * @param cause the cause
47     */
48    protected LdaptiveException(
49        int httpStatus,
50        String errorCode,
51        String reason,
52        Throwable cause) {
53      super(httpStatus, errorCode, reason, cause);
54    }
55  
56    /**
57     * Gets ldap exception (can be {@code null}).
58     *
59     * @return the ldap exception
60     */
61    public LdapException getLdapException() {
62      if (getCause() instanceof LdapException le) {
63        return le;
64      }
65      return null;
66    }
67  
68    /**
69     * Gets result code (can be {@code null}).
70     *
71     * @return the result code
72     */
73    public ResultCode getResultCode() {
74      LdapException ldapException = getLdapException();
75      return ldapException != null ? ldapException.getResultCode() : null;
76    }
77  
78    /**
79     * Creates a new service exception builder.
80     *
81     * @return the service exception builder
82     */
83    public static ServiceExceptionBuilder<LdaptiveException> builder() {
84  
85      return new AbstractServiceExceptionBuilder<>() {
86  
87        @Serial
88        private static final long serialVersionUID = 2L;
89  
90        @Override
91        protected LdaptiveException buildWith(int httpStatus, String errorCode, String reason,
92            Throwable cause) {
93          return new LdaptiveException(httpStatus, errorCode, reason, cause);
94        }
95      };
96    }
97  
98  }