aboutsummaryrefslogtreecommitdiffstats
path: root/openecomp-be/lib/openecomp-sdc-action-lib/openecomp-sdc-action-api/src/main/java/org/openecomp/sdc/action/errors/ActionExceptionMapper.java
blob: 98e1c6938aaf5fb336e4ca186ce2ca8ff2639896 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*-
 * ============LICENSE_START=======================================================
 * SDC
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.openecomp.sdc.action.errors;

import static org.openecomp.sdc.action.ActionConstants.WWW_AUTHENTICATE_HEADER_PARAM;
import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_ARTIFACT_ALREADY_EXISTS_CODE;
import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_ARTIFACT_CHECKSUM_ERROR_CODE;
import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_ARTIFACT_DELETE_READ_ONLY;
import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_ARTIFACT_DEL_LOCKED_OTHER_USER_CODE;
import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_ARTIFACT_ENTITY_NOT_EXIST_CODE;
import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_ARTIFACT_INVALID_NAME_CODE;
import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_ARTIFACT_INVALID_PROTECTION_CODE;
import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_ARTIFACT_TOO_BIG_ERROR_CODE;
import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_ARTIFACT_UPDATE_READ_ONLY;
import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_AUTHENTICATION_ERR_CODE;
import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_AUTHORIZATION_ERR_CODE;
import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_CHECKIN_ON_ENTITY_LOCKED_BY_OTHER_USER;
import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_CHECKIN_ON_UNLOCKED_ENTITY;
import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_CHECKOUT_ON_LOCKED_ENTITY;
import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_CHECKOUT_ON_LOCKED_ENTITY_OTHER_USER;
import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_DELETE_ON_LOCKED_ENTITY_CODE;
import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_EDIT_ON_ENTITY_LOCKED_BY_OTHER_USER;
import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_ENTITY_NOT_EXIST_CODE;
import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_ENTITY_UNIQUE_VALUE_ERROR;
import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_INTERNAL_SERVER_ERR_CODE;
import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_INVALID_INSTANCE_ID_CODE;
import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_INVALID_PARAM_CODE;
import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_INVALID_REQUEST_BODY_CODE;
import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_INVALID_REQUEST_ID_CODE;
import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_INVALID_SEARCH_CRITERIA;
import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_MULT_SEARCH_CRITERIA;
import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_NOT_LOCKED_CODE;
import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_REQUEST_AUTHORIZATION_HEADER_INVALID;
import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_REQUEST_INVALID_GENERIC_CODE;
import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_SUBMIT_FINALIZED_ENTITY_NOT_ALLOWED;
import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_SUBMIT_LOCKED_ENTITY_NOT_ALLOWED;
import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_UNDO_CHECKOUT_ON_ENTITY_LOCKED_BY_OTHER_USER;
import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_UNDO_CHECKOUT_ON_UNLOCKED_ENTITY;
import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_UPDATE_INVALID_VERSION;
import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_UPDATE_NOT_ALLOWED_CODE;
import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_UPDATE_NOT_ALLOWED_CODE_NAME;
import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_UPDATE_NOT_ALLOWED_FOR_NAME;
import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_UPDATE_ON_UNLOCKED_ENTITY;

import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;

/**
 * Mapper class to map Action Library exceptions to corresponding HTTP Response objects.
 */
public class ActionExceptionMapper implements ExceptionMapper<ActionException> {

  @Override
  public Response toResponse(ActionException exception) {
    Response response;
    String errorCode = exception.getErrorCode();
    switch (errorCode) {
      case ACTION_REQUEST_INVALID_GENERIC_CODE:
      case ACTION_INVALID_INSTANCE_ID_CODE:
      case ACTION_INVALID_REQUEST_ID_CODE:
      case ACTION_INVALID_REQUEST_BODY_CODE:
      case ACTION_INVALID_PARAM_CODE:
      case ACTION_UPDATE_NOT_ALLOWED_FOR_NAME:
      case ACTION_CHECKOUT_ON_LOCKED_ENTITY:
      case ACTION_ENTITY_UNIQUE_VALUE_ERROR:
      case ACTION_INVALID_SEARCH_CRITERIA:
      case ACTION_MULT_SEARCH_CRITERIA:
      case ACTION_UPDATE_ON_UNLOCKED_ENTITY:
      case ACTION_UPDATE_INVALID_VERSION:
      case ACTION_UPDATE_NOT_ALLOWED_CODE:
      case ACTION_CHECKIN_ON_UNLOCKED_ENTITY:
      case ACTION_SUBMIT_FINALIZED_ENTITY_NOT_ALLOWED:
      case ACTION_SUBMIT_LOCKED_ENTITY_NOT_ALLOWED:
      case ACTION_UNDO_CHECKOUT_ON_UNLOCKED_ENTITY:
      case ACTION_UPDATE_NOT_ALLOWED_CODE_NAME:
      case ACTION_ARTIFACT_CHECKSUM_ERROR_CODE:
      case ACTION_ARTIFACT_ALREADY_EXISTS_CODE:
      case ACTION_ARTIFACT_INVALID_NAME_CODE:
      case ACTION_ARTIFACT_TOO_BIG_ERROR_CODE:
      case ACTION_ARTIFACT_INVALID_PROTECTION_CODE:
      case ACTION_ARTIFACT_DELETE_READ_ONLY:
      case ACTION_NOT_LOCKED_CODE:
        response = Response
            .status(Response.Status.BAD_REQUEST)
            .entity(new ActionExceptionResponse(errorCode,
                Response.Status.BAD_REQUEST.getReasonPhrase(), exception.getDescription()))
            .type(MediaType.APPLICATION_JSON)
            .build();
        break;
      case ACTION_AUTHENTICATION_ERR_CODE:
        response = Response
            .status(Response.Status.UNAUTHORIZED)
            .header(WWW_AUTHENTICATE_HEADER_PARAM, ACTION_REQUEST_AUTHORIZATION_HEADER_INVALID)
            .entity(new ActionExceptionResponse(errorCode,
                Response.Status.UNAUTHORIZED.getReasonPhrase(), exception.getDescription()))
            .type(MediaType.APPLICATION_JSON)
            .build();
        break;
      case ACTION_AUTHORIZATION_ERR_CODE:
      case ACTION_EDIT_ON_ENTITY_LOCKED_BY_OTHER_USER:
      case ACTION_CHECKIN_ON_ENTITY_LOCKED_BY_OTHER_USER:
      case ACTION_CHECKOUT_ON_LOCKED_ENTITY_OTHER_USER:
      case ACTION_UNDO_CHECKOUT_ON_ENTITY_LOCKED_BY_OTHER_USER:
      case ACTION_DELETE_ON_LOCKED_ENTITY_CODE:
      case ACTION_ARTIFACT_DEL_LOCKED_OTHER_USER_CODE:
      case ACTION_ARTIFACT_UPDATE_READ_ONLY:
        response = Response
            .status(Response.Status.FORBIDDEN)
            .entity(
                new ActionExceptionResponse(errorCode, Response.Status.FORBIDDEN.getReasonPhrase(),
                    exception.getDescription())).type(MediaType.APPLICATION_JSON)
            .build();
        break;
      case ACTION_ENTITY_NOT_EXIST_CODE:
      case ACTION_ARTIFACT_ENTITY_NOT_EXIST_CODE:
        response = Response
            .status(Response.Status.NOT_FOUND)
            .entity(
                new ActionExceptionResponse(errorCode, Response.Status.NOT_FOUND.getReasonPhrase(),
                    exception.getDescription())).type(MediaType.APPLICATION_JSON)
            .build();
        break;
      case ACTION_INTERNAL_SERVER_ERR_CODE:
      default:
        response = Response
            .status(Response.Status.INTERNAL_SERVER_ERROR)
            .entity(new ActionExceptionResponse(errorCode,
                Response.Status.INTERNAL_SERVER_ERROR.getReasonPhrase(),
                exception.getDescription()))
            .type(MediaType.APPLICATION_JSON)
            .build();
    }
    return response;
  }
}