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.data.ldaptive;
18  
19  import lombok.EqualsAndHashCode;
20  import org.bremersee.exception.AbstractServiceExceptionBuilder;
21  import org.bremersee.exception.ErrorCodeAware;
22  import org.bremersee.exception.HttpStatusAware;
23  import org.bremersee.exception.ServiceException;
24  import org.bremersee.exception.ServiceExceptionBuilder;
25  import org.ldaptive.LdapException;
26  import org.ldaptive.ResultCode;
27  
28  /**
29   * The ldaptive exception.
30   *
31   * @author Christian Bremer
32   */
33  @EqualsAndHashCode(callSuper = true)
34  public class LdaptiveException extends ServiceException implements HttpStatusAware, ErrorCodeAware {
35  
36    private static final long serialVersionUID = 1L;
37  
38    /**
39     * Instantiates a new ldaptive exception.
40     *
41     * @param httpStatus the http status
42     * @param errorCode the error code
43     */
44    protected LdaptiveException(final int httpStatus, final String errorCode) {
45      super(httpStatus, errorCode);
46    }
47  
48    /**
49     * Instantiates a new ldaptive exception.
50     *
51     * @param httpStatus the http status
52     * @param errorCode the error code
53     * @param reason the reason
54     */
55    protected LdaptiveException(final int httpStatus, final String errorCode, final String reason) {
56      super(httpStatus, errorCode, reason);
57    }
58  
59    /**
60     * Instantiates a new ldaptive exception.
61     *
62     * @param httpStatus the http status
63     * @param errorCode the error code
64     * @param cause the cause
65     */
66    protected LdaptiveException(final int httpStatus, final String errorCode, final Throwable cause) {
67      super(httpStatus, errorCode, cause);
68    }
69  
70    /**
71     * Instantiates a new ldaptive exception.
72     *
73     * @param httpStatus the http status
74     * @param errorCode the error code
75     * @param reason the reason
76     * @param cause the cause
77     */
78    protected LdaptiveException(
79        final int httpStatus,
80        final String errorCode,
81        final String reason,
82        final Throwable cause) {
83      super(httpStatus, errorCode, reason, cause);
84    }
85  
86  
87    /**
88     * Gets ldap exception (can be {@code null}).
89     *
90     * @return the ldap exception
91     */
92    public LdapException getLdapException() {
93      return getCause() instanceof LdapException ? (LdapException) getCause() : null;
94    }
95  
96    /**
97     * Gets result code (can be {@code null}).
98     *
99     * @return the result code
100    */
101   @SuppressWarnings("unused")
102   public ResultCode getResultCode() {
103     final LdapException ldapException = getLdapException();
104     return ldapException != null ? ldapException.getResultCode() : null;
105   }
106 
107   /**
108    * Creates a new service exception builder.
109    *
110    * @return the service exception builder
111    */
112   public static ServiceExceptionBuilder<LdaptiveException> builder() {
113     return new AbstractServiceExceptionBuilder<>() {
114 
115       private static final long serialVersionUID = 2L;
116 
117       @Override
118       protected LdaptiveException buildWith(int httpStatus, String errorCode) {
119         return new LdaptiveException(httpStatus, errorCode);
120       }
121 
122       @Override
123       protected LdaptiveException buildWith(int httpStatus, String errorCode, String reason) {
124         return new LdaptiveException(httpStatus, errorCode, reason);
125       }
126 
127       @Override
128       protected LdaptiveException buildWith(int httpStatus, String errorCode, Throwable cause) {
129         return new LdaptiveException(httpStatus, errorCode, cause);
130       }
131 
132       @Override
133       protected LdaptiveException buildWith(int httpStatus, String errorCode, String reason,
134           Throwable cause) {
135         return new LdaptiveException(httpStatus, errorCode, reason, cause);
136       }
137     };
138   }
139 
140 }