Merge "[AAI] Fix doc config files"
[aai/aai-common.git] / aai-aaf-auth / src / main / java / org / onap / aai / aaf / auth / ResponseFormatter.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.aaf.auth;
22
23 import java.io.IOException;
24 import java.util.ArrayList;
25 import java.util.Collections;
26
27 import javax.servlet.http.HttpServletRequest;
28 import javax.servlet.http.HttpServletResponse;
29 import javax.ws.rs.core.MediaType;
30
31 import org.onap.aai.exceptions.AAIException;
32 import org.onap.aai.logging.ErrorLogHelper;
33
34 public class ResponseFormatter {
35
36     private static final String ACCEPT_HEADER = "accept";
37     private static final String CONTENT_TYPE_HEADER = "Content-Type";
38
39     private ResponseFormatter() {
40         throw new IllegalStateException("Utility class");
41     }
42
43     public static void errorResponse(HttpServletRequest request, HttpServletResponse response) throws IOException {
44         errorResponse(new AAIException("AAI_3300"), request, response);
45     }
46
47     public static void errorResponse(AAIException exception, HttpServletRequest request, HttpServletResponse response)
48             throws IOException {
49
50         if (response.isCommitted()) {
51             return;
52         }
53
54         String accept = request.getHeader(ACCEPT_HEADER);
55         switch (accept != null ? accept : MediaType.APPLICATION_XML) {
56             case MediaType.APPLICATION_JSON:
57                 response.setHeader(CONTENT_TYPE_HEADER, MediaType.APPLICATION_JSON);
58                 break;
59             case MediaType.APPLICATION_XML:
60             default:
61                 response.setHeader(CONTENT_TYPE_HEADER, MediaType.APPLICATION_XML);
62         }
63
64         response.setStatus(exception.getErrorObject().getHTTPResponseCode().getStatusCode());
65         response.resetBuffer();
66
67         String resp = ErrorLogHelper.getRESTAPIErrorResponse(Collections.singletonList(MediaType.valueOf(accept)),
68                 exception, new ArrayList<>());
69         response.getOutputStream().print(resp);
70         response.flushBuffer();
71     }
72 }