4fd322052b3d8429aa4210133651bb85708f6f20
[aai/schema-service.git] / aai-schema-service / src / main / java / org / onap / aai / schemaservice / interceptors / post / InvalidResponseStatus.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 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.onap.aai.schemaservice.interceptors.post;
21
22 import org.onap.aai.exceptions.AAIException;
23 import org.onap.aai.logging.ErrorLogHelper;
24 import org.onap.aai.schemaservice.interceptors.AAIContainerFilter;
25
26 import javax.annotation.Priority;
27 import javax.ws.rs.container.ContainerRequestContext;
28 import javax.ws.rs.container.ContainerResponseContext;
29 import javax.ws.rs.container.ContainerResponseFilter;
30 import javax.ws.rs.core.MediaType;
31 import java.io.IOException;
32 import java.util.ArrayList;
33 import java.util.List;
34
35 @Priority(AAIResponseFilterPriority.INVALID_RESPONSE_STATUS)
36 public class InvalidResponseStatus extends AAIContainerFilter implements ContainerResponseFilter {
37
38     @Override
39     public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext)
40         throws IOException {
41
42         if (responseContext.getStatus() == 405) {
43
44             responseContext.setStatus(400);
45             AAIException e = new AAIException("AAI_3012");
46             ArrayList<String> templateVars = new ArrayList<>();
47
48             List<MediaType> mediaTypeList = new ArrayList<>();
49
50             String contentType = responseContext.getHeaderString("Content-Type");
51
52             if (contentType == null) {
53                 mediaTypeList.add(MediaType.APPLICATION_XML_TYPE);
54             } else {
55                 mediaTypeList.add(MediaType.valueOf(contentType));
56             }
57
58             String message = ErrorLogHelper.getRESTAPIErrorResponse(mediaTypeList, e, templateVars);
59
60             responseContext.setEntity(message);
61         }
62
63     }
64
65 }