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