PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/java/com/atlassian/crowd/integration/rest/entity/ErrorEntity.java

https://bitbucket.org/atlassian/crowd-rest-client
Java | 218 lines | 176 code | 11 blank | 31 comment | 43 complexity | 4aff986852cb7a71b9bb2086fd5d0ce9 MD5 | raw file
  1. /*
  2. * Copyright © 2010 - 2015 Atlassian Corporation Pty Ltd.
  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. * https://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. package com.atlassian.crowd.integration.rest.entity;
  17. import com.atlassian.crowd.event.EventTokenExpiredException;
  18. import com.atlassian.crowd.event.IncrementalSynchronisationNotAvailableException;
  19. import com.atlassian.crowd.exception.ApplicationAccessDeniedException;
  20. import com.atlassian.crowd.exception.ApplicationNotFoundException;
  21. import com.atlassian.crowd.exception.ApplicationPermissionException;
  22. import com.atlassian.crowd.exception.ExpiredCredentialException;
  23. import com.atlassian.crowd.exception.GroupNotFoundException;
  24. import com.atlassian.crowd.exception.InactiveAccountException;
  25. import com.atlassian.crowd.exception.InvalidAuthenticationException;
  26. import com.atlassian.crowd.exception.InvalidCredentialException;
  27. import com.atlassian.crowd.exception.InvalidEmailAddressException;
  28. import com.atlassian.crowd.exception.InvalidGroupException;
  29. import com.atlassian.crowd.exception.InvalidMembershipException;
  30. import com.atlassian.crowd.exception.InvalidTokenException;
  31. import com.atlassian.crowd.exception.InvalidUserException;
  32. import com.atlassian.crowd.exception.MembershipAlreadyExistsException;
  33. import com.atlassian.crowd.exception.MembershipNotFoundException;
  34. import com.atlassian.crowd.exception.NestedGroupsNotSupportedException;
  35. import com.atlassian.crowd.exception.UserNotFoundException;
  36. import com.atlassian.crowd.exception.WebhookNotFoundException;
  37. import javax.xml.bind.annotation.XmlAccessType;
  38. import javax.xml.bind.annotation.XmlAccessorType;
  39. import javax.xml.bind.annotation.XmlElement;
  40. import javax.xml.bind.annotation.XmlEnum;
  41. import javax.xml.bind.annotation.XmlRootElement;
  42. /**
  43. * Represents an error. All errors returned from REST resources should have the error entity in the response.
  44. *
  45. * @since v2.1
  46. */
  47. @XmlRootElement (name = "error")
  48. @XmlAccessorType (XmlAccessType.FIELD)
  49. public class ErrorEntity
  50. {
  51. // previously the enum constants were annotated with @XmlEnumValue, however this proved problematic because when
  52. // serialising to JSON, the Jackson JSON serialiser ignored the JAXB @XmlEnumValue value and used Enum#name.
  53. @XmlEnum
  54. public enum ErrorReason
  55. {
  56. APPLICATION_ACCESS_DENIED,
  57. APPLICATION_PERMISSION_DENIED,
  58. EXPIRED_CREDENTIAL,
  59. GROUP_NOT_FOUND,
  60. ILLEGAL_ARGUMENT,
  61. INACTIVE_ACCOUNT,
  62. INVALID_USER_AUTHENTICATION,
  63. INVALID_CREDENTIAL,
  64. INVALID_EMAIL,
  65. INVALID_GROUP,
  66. INVALID_SSO_TOKEN,
  67. INVALID_USER,
  68. MEMBERSHIP_NOT_FOUND,
  69. MEMBERSHIP_ALREADY_EXISTS,
  70. NESTED_GROUPS_NOT_SUPPORTED,
  71. APPLICATION_NOT_FOUND,
  72. UNSUPPORTED_OPERATION,
  73. USER_NOT_FOUND,
  74. OPERATION_FAILED,
  75. EVENT_TOKEN_EXPIRED,
  76. INCREMENTAL_SYNC_NOT_AVAILABLE,
  77. WEBHOOK_NOT_FOUND,
  78. PERMISSION_DENIED,
  79. INVALID_MEMBERSHIP;
  80. public static ErrorReason of(Exception e)
  81. {
  82. if (e instanceof ApplicationAccessDeniedException)
  83. {
  84. return ErrorReason.APPLICATION_ACCESS_DENIED;
  85. }
  86. else if (e instanceof ApplicationNotFoundException)
  87. {
  88. return ErrorReason.APPLICATION_NOT_FOUND;
  89. }
  90. else if (e instanceof ApplicationPermissionException)
  91. {
  92. return ErrorReason.APPLICATION_PERMISSION_DENIED;
  93. }
  94. else if (e instanceof ExpiredCredentialException)
  95. {
  96. return ErrorReason.EXPIRED_CREDENTIAL;
  97. }
  98. else if (e instanceof GroupNotFoundException)
  99. {
  100. return ErrorReason.GROUP_NOT_FOUND;
  101. }
  102. else if (e instanceof IllegalArgumentException)
  103. {
  104. return ErrorReason.ILLEGAL_ARGUMENT;
  105. }
  106. else if (e instanceof InactiveAccountException)
  107. {
  108. return ErrorReason.INACTIVE_ACCOUNT;
  109. }
  110. else if (e instanceof InvalidAuthenticationException)
  111. {
  112. return ErrorReason.INVALID_USER_AUTHENTICATION;
  113. }
  114. else if (e instanceof InvalidCredentialException)
  115. {
  116. return ErrorReason.INVALID_CREDENTIAL;
  117. }
  118. else if (e instanceof InvalidEmailAddressException)
  119. {
  120. return ErrorReason.INVALID_EMAIL;
  121. }
  122. else if (e instanceof InvalidGroupException)
  123. {
  124. return ErrorReason.INVALID_GROUP;
  125. }
  126. else if (e instanceof InvalidTokenException)
  127. {
  128. return ErrorReason.INVALID_SSO_TOKEN;
  129. }
  130. else if (e instanceof InvalidUserException)
  131. {
  132. return ErrorReason.INVALID_USER;
  133. }
  134. else if (e instanceof MembershipNotFoundException)
  135. {
  136. return ErrorReason.MEMBERSHIP_NOT_FOUND;
  137. }
  138. else if (e instanceof MembershipAlreadyExistsException)
  139. {
  140. return ErrorReason.MEMBERSHIP_ALREADY_EXISTS;
  141. }
  142. else if (e instanceof NestedGroupsNotSupportedException)
  143. {
  144. return ErrorReason.NESTED_GROUPS_NOT_SUPPORTED;
  145. }
  146. else if (e instanceof UnsupportedOperationException)
  147. {
  148. return ErrorReason.UNSUPPORTED_OPERATION;
  149. }
  150. else if (e instanceof UserNotFoundException)
  151. {
  152. return ErrorReason.USER_NOT_FOUND;
  153. }
  154. else if (e instanceof EventTokenExpiredException)
  155. {
  156. return ErrorReason.EVENT_TOKEN_EXPIRED;
  157. }
  158. else if (e instanceof IncrementalSynchronisationNotAvailableException)
  159. {
  160. return ErrorReason.INCREMENTAL_SYNC_NOT_AVAILABLE;
  161. }
  162. else if (e instanceof WebhookNotFoundException)
  163. {
  164. return ErrorReason.WEBHOOK_NOT_FOUND;
  165. }
  166. else if (e instanceof InvalidMembershipException)
  167. {
  168. return ErrorReason.INVALID_MEMBERSHIP;
  169. }
  170. else
  171. {
  172. return ErrorReason.OPERATION_FAILED;
  173. }
  174. }
  175. }
  176. @XmlElement (name = "reason")
  177. private final ErrorReason reason;
  178. @XmlElement (name = "message")
  179. private final String message;
  180. /**
  181. * JAXB requires a no-arg constructor.
  182. */
  183. private ErrorEntity()
  184. {
  185. reason = null;
  186. message = null;
  187. }
  188. /**
  189. * Constructs an error entity.
  190. *
  191. * @param reason reason for the error.
  192. * @param message message
  193. */
  194. public ErrorEntity(final ErrorReason reason, final String message)
  195. {
  196. this.reason = reason;
  197. this.message = message;
  198. }
  199. public ErrorReason getReason()
  200. {
  201. return reason;
  202. }
  203. public String getMessage()
  204. {
  205. return message;
  206. }
  207. }