[SDC-29] Amdocs OnBoard 1707 initial commit.
[sdc.git] / openecomp-be / lib / openecomp-sdc-action-lib / openecomp-sdc-action-api / src / main / java / org / openecomp / sdc / action / errors / ActionExceptionMapper.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdc.action.errors;
22
23 import static org.openecomp.sdc.action.ActionConstants.WWW_AUTHENTICATE_HEADER_PARAM;
24 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_ARTIFACT_ALREADY_EXISTS_CODE;
25 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_ARTIFACT_CHECKSUM_ERROR_CODE;
26 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_ARTIFACT_DELETE_READ_ONLY;
27 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_ARTIFACT_DEL_LOCKED_OTHER_USER_CODE;
28 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_ARTIFACT_ENTITY_NOT_EXIST_CODE;
29 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_ARTIFACT_INVALID_NAME_CODE;
30 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_ARTIFACT_INVALID_PROTECTION_CODE;
31 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_ARTIFACT_TOO_BIG_ERROR_CODE;
32 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_ARTIFACT_UPDATE_READ_ONLY;
33 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_AUTHENTICATION_ERR_CODE;
34 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_AUTHORIZATION_ERR_CODE;
35 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_CHECKIN_ON_ENTITY_LOCKED_BY_OTHER_USER;
36 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_CHECKIN_ON_UNLOCKED_ENTITY;
37 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_CHECKOUT_ON_LOCKED_ENTITY;
38 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_CHECKOUT_ON_LOCKED_ENTITY_OTHER_USER;
39 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_DELETE_ON_LOCKED_ENTITY_CODE;
40 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_EDIT_ON_ENTITY_LOCKED_BY_OTHER_USER;
41 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_ENTITY_NOT_EXIST_CODE;
42 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_ENTITY_UNIQUE_VALUE_ERROR;
43 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_INTERNAL_SERVER_ERR_CODE;
44 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_INVALID_INSTANCE_ID_CODE;
45 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_INVALID_PARAM_CODE;
46 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_INVALID_REQUEST_BODY_CODE;
47 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_INVALID_REQUEST_ID_CODE;
48 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_INVALID_SEARCH_CRITERIA;
49 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_MULT_SEARCH_CRITERIA;
50 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_NOT_LOCKED_CODE;
51 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_REQUEST_AUTHORIZATION_HEADER_INVALID;
52 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_REQUEST_INVALID_GENERIC_CODE;
53 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_SUBMIT_FINALIZED_ENTITY_NOT_ALLOWED;
54 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_SUBMIT_LOCKED_ENTITY_NOT_ALLOWED;
55 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_UNDO_CHECKOUT_ON_ENTITY_LOCKED_BY_OTHER_USER;
56 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_UNDO_CHECKOUT_ON_UNLOCKED_ENTITY;
57 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_UPDATE_INVALID_VERSION;
58 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_UPDATE_NOT_ALLOWED_CODE;
59 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_UPDATE_NOT_ALLOWED_CODE_NAME;
60 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_UPDATE_NOT_ALLOWED_FOR_NAME;
61 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_UPDATE_ON_UNLOCKED_ENTITY;
62
63 import javax.ws.rs.core.MediaType;
64 import javax.ws.rs.core.Response;
65 import javax.ws.rs.ext.ExceptionMapper;
66
67 /**
68  * Mapper class to map Action Library exceptions to corresponding HTTP Response objects.
69  */
70 public class ActionExceptionMapper implements ExceptionMapper<ActionException> {
71
72   @Override
73   public Response toResponse(ActionException exception) {
74     Response response;
75     String errorCode = exception.getErrorCode();
76     switch (errorCode) {
77       case ACTION_REQUEST_INVALID_GENERIC_CODE:
78       case ACTION_INVALID_INSTANCE_ID_CODE:
79       case ACTION_INVALID_REQUEST_ID_CODE:
80       case ACTION_INVALID_REQUEST_BODY_CODE:
81       case ACTION_INVALID_PARAM_CODE:
82       case ACTION_UPDATE_NOT_ALLOWED_FOR_NAME:
83       case ACTION_CHECKOUT_ON_LOCKED_ENTITY:
84       case ACTION_ENTITY_UNIQUE_VALUE_ERROR:
85       case ACTION_INVALID_SEARCH_CRITERIA:
86       case ACTION_MULT_SEARCH_CRITERIA:
87       case ACTION_UPDATE_ON_UNLOCKED_ENTITY:
88       case ACTION_UPDATE_INVALID_VERSION:
89       case ACTION_UPDATE_NOT_ALLOWED_CODE:
90       case ACTION_CHECKIN_ON_UNLOCKED_ENTITY:
91       case ACTION_SUBMIT_FINALIZED_ENTITY_NOT_ALLOWED:
92       case ACTION_SUBMIT_LOCKED_ENTITY_NOT_ALLOWED:
93       case ACTION_UNDO_CHECKOUT_ON_UNLOCKED_ENTITY:
94       case ACTION_UPDATE_NOT_ALLOWED_CODE_NAME:
95       case ACTION_ARTIFACT_CHECKSUM_ERROR_CODE:
96       case ACTION_ARTIFACT_ALREADY_EXISTS_CODE:
97       case ACTION_ARTIFACT_INVALID_NAME_CODE:
98       case ACTION_ARTIFACT_TOO_BIG_ERROR_CODE:
99       case ACTION_ARTIFACT_INVALID_PROTECTION_CODE:
100       case ACTION_ARTIFACT_DELETE_READ_ONLY:
101       case ACTION_NOT_LOCKED_CODE:
102         response = Response
103             .status(Response.Status.BAD_REQUEST)
104             .entity(new ActionExceptionResponse(errorCode,
105                 Response.Status.BAD_REQUEST.getReasonPhrase(), exception.getDescription()))
106             .type(MediaType.APPLICATION_JSON)
107             .build();
108         break;
109       case ACTION_AUTHENTICATION_ERR_CODE:
110         response = Response
111             .status(Response.Status.UNAUTHORIZED)
112             .header(WWW_AUTHENTICATE_HEADER_PARAM, ACTION_REQUEST_AUTHORIZATION_HEADER_INVALID)
113             .entity(new ActionExceptionResponse(errorCode,
114                 Response.Status.UNAUTHORIZED.getReasonPhrase(), exception.getDescription()))
115             .type(MediaType.APPLICATION_JSON)
116             .build();
117         break;
118       case ACTION_AUTHORIZATION_ERR_CODE:
119       case ACTION_EDIT_ON_ENTITY_LOCKED_BY_OTHER_USER:
120       case ACTION_CHECKIN_ON_ENTITY_LOCKED_BY_OTHER_USER:
121       case ACTION_CHECKOUT_ON_LOCKED_ENTITY_OTHER_USER:
122       case ACTION_UNDO_CHECKOUT_ON_ENTITY_LOCKED_BY_OTHER_USER:
123       case ACTION_DELETE_ON_LOCKED_ENTITY_CODE:
124       case ACTION_ARTIFACT_DEL_LOCKED_OTHER_USER_CODE:
125       case ACTION_ARTIFACT_UPDATE_READ_ONLY:
126         response = Response
127             .status(Response.Status.FORBIDDEN)
128             .entity(
129                 new ActionExceptionResponse(errorCode, Response.Status.FORBIDDEN.getReasonPhrase(),
130                     exception.getDescription())).type(MediaType.APPLICATION_JSON)
131             .build();
132         break;
133       case ACTION_ENTITY_NOT_EXIST_CODE:
134       case ACTION_ARTIFACT_ENTITY_NOT_EXIST_CODE:
135         response = Response
136             .status(Response.Status.NOT_FOUND)
137             .entity(
138                 new ActionExceptionResponse(errorCode, Response.Status.NOT_FOUND.getReasonPhrase(),
139                     exception.getDescription())).type(MediaType.APPLICATION_JSON)
140             .build();
141         break;
142       case ACTION_INTERNAL_SERVER_ERR_CODE:
143       default:
144         response = Response
145             .status(Response.Status.INTERNAL_SERVER_ERROR)
146             .entity(new ActionExceptionResponse(errorCode,
147                 Response.Status.INTERNAL_SERVER_ERROR.getReasonPhrase(),
148                 exception.getDescription()))
149             .type(MediaType.APPLICATION_JSON)
150             .build();
151     }
152     return response;
153   }
154 }