001 /*
002 * Copyright 2011-2012 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
018 package com.unboundid.scim.sdk;
019
020 import com.unboundid.scim.marshal.Marshaller;
021
022 import java.io.OutputStream;
023
024 /**
025 * This class is the base class for all custom checked exceptions defined in
026 * the SCIM server.
027 */
028 public 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 switch(statusCode)
107 {
108 case 400 : return new InvalidResourceException(errorMessage);
109 case 401 : return new UnauthorizedException(errorMessage);
110 case 403 : return new UnsupportedOperationException(errorMessage);
111 case 404 : return new ResourceNotFoundException(errorMessage);
112 case 409 : return new ResourceConflictException(errorMessage);
113 case 500 : return new ServerErrorException(errorMessage);
114 default : return new SCIMException(statusCode, errorMessage);
115 }
116 }
117 }