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