001/*
002 * Copyright 2011-2016 UnboundID Corp.
003 *
004 * This program is free software; you can redistribute it and/or modify
005 * it under the terms of the GNU General Public License (GPLv2 only)
006 * or the terms of the GNU Lesser General Public License (LGPLv2.1 only)
007 * as published by the Free Software Foundation.
008 *
009 * This program is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
012 * GNU General Public License for more details.
013 *
014 * You should have received a copy of the GNU General Public License
015 * along with this program; if not, see <http://www.gnu.org/licenses>.
016 */
017
018package com.unboundid.scim.sdk;
019
020import com.unboundid.scim.marshal.Marshaller;
021
022import java.io.OutputStream;
023
024/**
025 * This class is the base class for all custom checked exceptions defined in
026 * the SCIM SDK.
027 */
028public class SCIMException extends Exception implements SCIMResponse
029{
030  /**
031   * The serial version UID required for this serializable class.
032   */
033  private static final long serialVersionUID = -7530770599624725752L;
034
035  /**
036   * The HTTP status code for this SCIM exception.
037   */
038  private final int statusCode;
039
040
041
042  /**
043   * Create a new SCIM exception from the provided informatuon.
044   *
045   * @param statusCode    The HTTP status code for this SCIM exception.
046   * @param errorMessage  The error message for this SCIM exception.
047   */
048  protected SCIMException(final int statusCode, final String errorMessage)
049  {
050    super(errorMessage);
051
052    this.statusCode = statusCode;
053  }
054
055
056
057  /**
058   * Create a new SCIM exception from the provided informatuon.
059   *
060   * @param statusCode    The HTTP status code for this SCIM exception.
061   * @param errorMessage  The error message for this SCIM exception.
062   * @param cause         The cause (which is saved for later retrieval by the
063   *                      {@link #getCause()} method).  (A <tt>null</tt> value
064   *                      is permitted, and indicates that the cause is
065   *                      nonexistent or unknown.)
066   */
067  protected SCIMException(final int statusCode, final String errorMessage,
068                          final Throwable cause)
069  {
070    super(errorMessage, cause);
071
072    this.statusCode = statusCode;
073  }
074
075
076
077  /**
078   * Retrieve the HTTP status code for this SCIM exception.
079   *
080   * @return  The HTTP status code for this SCIM exception.
081   */
082  public int getStatusCode()
083  {
084    return statusCode;
085  }
086
087  /**
088   * {@inheritDoc}
089   */
090  public final void marshal(final Marshaller marshaller,
091                            final OutputStream outputStream)
092      throws Exception {
093    marshaller.marshal(this, outputStream);
094  }
095
096  /**
097   * Create the appropriate SCIMException from the provided information.
098   *
099   * @param statusCode    The HTTP status code for this SCIM exception.
100   * @param errorMessage  The error message for this SCIM exception.
101   * @return The appropriate SCIMException from the provided information.
102   */
103  public static SCIMException createException(final int statusCode,
104                                              final String errorMessage)
105  {
106    return createException(statusCode, errorMessage, null);
107  }
108
109  /**
110   * Create the appropriate SCIMException from the provided information.
111   *
112   * @param statusCode    The HTTP status code for this SCIM exception.
113   * @param errorMessage  The error message for this SCIM exception.
114   * @param cause         The cause (which is saved for later retrieval by the
115   *                      {@link #getCause()} method).  (A <tt>null</tt> value
116   *                      is permitted, and indicates that the cause is
117   *                      nonexistent or unknown.)
118   * @return The appropriate SCIMException from the provided information.
119   */
120  public static SCIMException createException(final int statusCode,
121                                              final String errorMessage,
122                                              final Exception cause)
123  {
124    switch(statusCode)
125    {
126      case -1  : return new ConnectException(errorMessage);
127      case 304 : return new NotModifiedException(errorMessage);
128      case 400 : return new InvalidResourceException(errorMessage);
129      case 401 : return new UnauthorizedException(errorMessage);
130      case 403 : return new ForbiddenException(errorMessage);
131      case 404 : return new ResourceNotFoundException(errorMessage);
132      case 409 : return new ResourceConflictException(errorMessage);
133      case 412 : return new PreconditionFailedException(errorMessage);
134      case 413 : return new RequestEntityTooLargeException(errorMessage);
135      case 500 : return new ServerErrorException(errorMessage);
136      case 501 : return new UnsupportedOperationException(errorMessage);
137      default : return new SCIMException(statusCode, errorMessage, cause);
138    }
139  }
140}