d9871713f5f5c1d6a18c79d11a6b8ee0a780b60b
[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.print.attribute.standard.Media;
28 import javax.ws.rs.container.ContainerRequestContext;
29 import javax.ws.rs.container.ContainerResponseContext;
30 import javax.ws.rs.container.ContainerResponseFilter;
31 import javax.ws.rs.core.MediaType;
32 import javax.ws.rs.core.UriInfo;
33 import java.io.IOException;
34 import java.util.ArrayList;
35 import java.util.List;
36
37 @Priority(AAIResponseFilterPriority.INVALID_RESPONSE_STATUS)
38 public class InvalidResponseStatus extends AAIContainerFilter implements ContainerResponseFilter {
39
40     @Override
41     public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext)
42         throws IOException {
43
44         String contentType = responseContext.getHeaderString("Content-Type");
45         ArrayList<String> templateVars = new ArrayList<>();
46         List<MediaType> mediaTypeList = new ArrayList<>();
47         AAIException e;
48         String message = "";
49
50         if (responseContext.getStatus() == 405) {
51
52             // add the accept type error msg here as well.
53
54             responseContext.setStatus(400);
55             e = new AAIException("AAI_3012");
56
57             if (contentType == null) {
58                 mediaTypeList.add(MediaType.APPLICATION_XML_TYPE);
59             } else {
60                 mediaTypeList.add(MediaType.valueOf(contentType));
61             }
62
63             message = ErrorLogHelper.getRESTAPIErrorResponse(mediaTypeList, e, templateVars);
64
65             responseContext.setEntity(message);
66         } else if (responseContext.getStatus() == 406) {
67             responseContext.setStatus(406);
68             mediaTypeList.add(MediaType.valueOf(contentType));
69             if (contentType == null) {
70                 mediaTypeList.add(MediaType.APPLICATION_XML_TYPE);
71                 e = new AAIException("AAI_3019", "null");
72             } else if (contentType.equals(MediaType.APPLICATION_XML)) {
73                 e = new AAIException("AAI_3019", MediaType.APPLICATION_XML);
74             } else if (contentType.equals(MediaType.APPLICATION_JSON)) {
75                 e = new AAIException("AAI_3019", MediaType.APPLICATION_JSON);
76             } else {
77                 mediaTypeList.add(MediaType.valueOf(contentType));
78                 e = new AAIException("AAI_3019", contentType);
79             }
80             message = ErrorLogHelper.getRESTAPIErrorResponse(mediaTypeList, e, templateVars);
81             responseContext.setEntity(message);
82         }
83     }
84 }