2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 package org.onap.aai.schemaservice.interceptors.post;
22 import org.onap.aai.exceptions.AAIException;
23 import org.onap.aai.logging.ErrorLogHelper;
24 import org.onap.aai.schemaservice.interceptors.AAIContainerFilter;
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;
37 @Priority(AAIResponseFilterPriority.INVALID_RESPONSE_STATUS)
38 public class InvalidResponseStatus extends AAIContainerFilter implements ContainerResponseFilter {
41 public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext)
44 String contentType = responseContext.getHeaderString("Content-Type");
45 ArrayList<String> templateVars = new ArrayList<>();
46 List<MediaType> mediaTypeList = new ArrayList<>();
50 if (responseContext.getStatus() == 405) {
52 // add the accept type error msg here as well.
54 responseContext.setStatus(400);
55 e = new AAIException("AAI_3012");
57 if (contentType == null) {
58 mediaTypeList.add(MediaType.APPLICATION_XML_TYPE);
60 mediaTypeList.add(MediaType.valueOf(contentType));
63 message = ErrorLogHelper.getRESTAPIErrorResponse(mediaTypeList, e, templateVars);
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);
77 mediaTypeList.add(MediaType.valueOf(contentType));
78 e = new AAIException("AAI_3019", contentType);
80 message = ErrorLogHelper.getRESTAPIErrorResponse(mediaTypeList, e, templateVars);
81 responseContext.setEntity(message);