ec83fef0e1fac26fabe0a975e2ca53b565525c37
[sdc.git] /
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 package org.openecomp.sdc.action.errors;
21
22 import static org.openecomp.sdc.action.ActionConstants.WWW_AUTHENTICATE_HEADER_PARAM;
23 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_ARTIFACT_ALREADY_EXISTS_CODE;
24 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_ARTIFACT_CHECKSUM_ERROR_CODE;
25 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_ARTIFACT_DELETE_READ_ONLY;
26 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_ARTIFACT_DEL_LOCKED_OTHER_USER_CODE;
27 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_ARTIFACT_ENTITY_NOT_EXIST_CODE;
28 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_ARTIFACT_INVALID_NAME_CODE;
29 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_ARTIFACT_INVALID_PROTECTION_CODE;
30 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_ARTIFACT_TOO_BIG_ERROR_CODE;
31 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_ARTIFACT_UPDATE_READ_ONLY;
32 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_AUTHENTICATION_ERR_CODE;
33 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_AUTHORIZATION_ERR_CODE;
34 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_CHECKIN_ON_ENTITY_LOCKED_BY_OTHER_USER;
35 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_CHECKIN_ON_UNLOCKED_ENTITY;
36 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_CHECKOUT_ON_LOCKED_ENTITY;
37 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_CHECKOUT_ON_LOCKED_ENTITY_OTHER_USER;
38 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_DELETE_ON_LOCKED_ENTITY_CODE;
39 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_EDIT_ON_ENTITY_LOCKED_BY_OTHER_USER;
40 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_ENTITY_NOT_EXIST_CODE;
41 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_ENTITY_UNIQUE_VALUE_ERROR;
42 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_INTERNAL_SERVER_ERR_CODE;
43 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_INVALID_INSTANCE_ID_CODE;
44 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_INVALID_PARAM_CODE;
45 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_INVALID_REQUEST_BODY_CODE;
46 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_INVALID_REQUEST_ID_CODE;
47 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_INVALID_SEARCH_CRITERIA;
48 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_MULT_SEARCH_CRITERIA;
49 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_NOT_LOCKED_CODE;
50 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_REQUEST_AUTHORIZATION_HEADER_INVALID;
51 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_REQUEST_INVALID_GENERIC_CODE;
52 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_SUBMIT_FINALIZED_ENTITY_NOT_ALLOWED;
53 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_SUBMIT_LOCKED_ENTITY_NOT_ALLOWED;
54 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_UNDO_CHECKOUT_ON_ENTITY_LOCKED_BY_OTHER_USER;
55 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_UNDO_CHECKOUT_ON_UNLOCKED_ENTITY;
56 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_UPDATE_INVALID_VERSION;
57 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_UPDATE_NOT_ALLOWED_CODE;
58 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_UPDATE_NOT_ALLOWED_CODE_NAME;
59 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_UPDATE_NOT_ALLOWED_FOR_NAME;
60 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_UPDATE_ON_UNLOCKED_ENTITY;
61
62 import javax.ws.rs.core.MediaType;
63 import javax.ws.rs.core.Response;
64 import javax.ws.rs.ext.ExceptionMapper;
65
66 /**
67  * Mapper class to map Action Library exceptions to corresponding HTTP Response objects.
68  */
69 public class ActionExceptionMapper implements ExceptionMapper<ActionException> {
70
71     @Override
72     public Response toResponse(ActionException exception) {
73         Response response;
74         String errorCode = exception.getErrorCode();
75         switch (errorCode) {
76             case ACTION_REQUEST_INVALID_GENERIC_CODE:
77             case ACTION_INVALID_INSTANCE_ID_CODE:
78             case ACTION_INVALID_REQUEST_ID_CODE:
79             case ACTION_INVALID_REQUEST_BODY_CODE:
80             case ACTION_INVALID_PARAM_CODE:
81             case ACTION_UPDATE_NOT_ALLOWED_FOR_NAME:
82             case ACTION_CHECKOUT_ON_LOCKED_ENTITY:
83             case ACTION_ENTITY_UNIQUE_VALUE_ERROR:
84             case ACTION_INVALID_SEARCH_CRITERIA:
85             case ACTION_MULT_SEARCH_CRITERIA:
86             case ACTION_UPDATE_ON_UNLOCKED_ENTITY:
87             case ACTION_UPDATE_INVALID_VERSION:
88             case ACTION_UPDATE_NOT_ALLOWED_CODE:
89             case ACTION_CHECKIN_ON_UNLOCKED_ENTITY:
90             case ACTION_SUBMIT_FINALIZED_ENTITY_NOT_ALLOWED:
91             case ACTION_SUBMIT_LOCKED_ENTITY_NOT_ALLOWED:
92             case ACTION_UNDO_CHECKOUT_ON_UNLOCKED_ENTITY:
93             case ACTION_UPDATE_NOT_ALLOWED_CODE_NAME:
94             case ACTION_ARTIFACT_CHECKSUM_ERROR_CODE:
95             case ACTION_ARTIFACT_ALREADY_EXISTS_CODE:
96             case ACTION_ARTIFACT_INVALID_NAME_CODE:
97             case ACTION_ARTIFACT_TOO_BIG_ERROR_CODE:
98             case ACTION_ARTIFACT_INVALID_PROTECTION_CODE:
99             case ACTION_ARTIFACT_DELETE_READ_ONLY:
100             case ACTION_NOT_LOCKED_CODE:
101                 response = Response.status(Response.Status.BAD_REQUEST)
102                     .entity(new ActionExceptionResponse(errorCode, Response.Status.BAD_REQUEST.getReasonPhrase(), exception.getDescription()))
103                     .type(MediaType.APPLICATION_JSON).build();
104                 break;
105             case ACTION_AUTHENTICATION_ERR_CODE:
106                 response = Response.status(Response.Status.UNAUTHORIZED)
107                     .header(WWW_AUTHENTICATE_HEADER_PARAM, ACTION_REQUEST_AUTHORIZATION_HEADER_INVALID)
108                     .entity(new ActionExceptionResponse(errorCode, Response.Status.UNAUTHORIZED.getReasonPhrase(), exception.getDescription()))
109                     .type(MediaType.APPLICATION_JSON).build();
110                 break;
111             case ACTION_AUTHORIZATION_ERR_CODE:
112             case ACTION_EDIT_ON_ENTITY_LOCKED_BY_OTHER_USER:
113             case ACTION_CHECKIN_ON_ENTITY_LOCKED_BY_OTHER_USER:
114             case ACTION_CHECKOUT_ON_LOCKED_ENTITY_OTHER_USER:
115             case ACTION_UNDO_CHECKOUT_ON_ENTITY_LOCKED_BY_OTHER_USER:
116             case ACTION_DELETE_ON_LOCKED_ENTITY_CODE:
117             case ACTION_ARTIFACT_DEL_LOCKED_OTHER_USER_CODE:
118             case ACTION_ARTIFACT_UPDATE_READ_ONLY:
119                 response = Response.status(Response.Status.FORBIDDEN)
120                     .entity(new ActionExceptionResponse(errorCode, Response.Status.FORBIDDEN.getReasonPhrase(), exception.getDescription()))
121                     .type(MediaType.APPLICATION_JSON).build();
122                 break;
123             case ACTION_ENTITY_NOT_EXIST_CODE:
124             case ACTION_ARTIFACT_ENTITY_NOT_EXIST_CODE:
125                 response = Response.status(Response.Status.NOT_FOUND)
126                     .entity(new ActionExceptionResponse(errorCode, Response.Status.NOT_FOUND.getReasonPhrase(), exception.getDescription()))
127                     .type(MediaType.APPLICATION_JSON).build();
128                 break;
129             case ACTION_INTERNAL_SERVER_ERR_CODE:
130             default:
131                 response = Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(
132                     new ActionExceptionResponse(errorCode, Response.Status.INTERNAL_SERVER_ERROR.getReasonPhrase(), exception.getDescription()))
133                     .type(MediaType.APPLICATION_JSON).build();
134         }
135         return response;
136     }
137 }