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