1 /*******************************************************************************
2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017 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 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 * ============LICENSE_END=========================================================
19 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21 *******************************************************************************/
22 package org.onap.dmaap.messagerouter.msgrtr.nsa.cambria.utils;
24 import java.io.ByteArrayInputStream;
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.io.OutputStream;
28 import java.io.PrintWriter;
29 import java.io.Writer;
31 import javax.servlet.http.HttpServletResponse;
32 import com.att.eelf.configuration.EELFLogger;
33 import com.att.eelf.configuration.EELFManager;
34 import org.json.JSONException;
35 import org.json.JSONObject;
36 import org.onap.dmaap.messagerouter.msgrtr.nsa.cambria.beans.DMaaPContext;
39 * class is used to create response object which is given to user
45 public class DMaaPResponseBuilder {
47 //private static Logger log = Logger.getLogger(DMaaPResponseBuilder.class);
48 private static final EELFLogger log = EELFManager.getInstance().getLogger(DMaaPResponseBuilder.class);
49 protected static final int kBufferLength = 4096;
51 public static void setNoCacheHeadings(DMaaPContext ctx) {
52 HttpServletResponse response = ctx.getResponse();
53 response.addHeader("Cache-Control", "no-store, no-cache, must-revalidate");
54 response.addHeader("Pragma", "no-cache");
55 response.addHeader("Expires", "0");
59 * static method is used to create response object associated with
64 * @throws JSONException
67 public static void respondOk(DMaaPContext ctx, JSONObject result) throws JSONException, IOException {
69 respondOkWithStream(ctx, "application/json", new ByteArrayInputStream(result.toString(4).getBytes()));
74 * method used to set staus to 204
78 public static void respondOkNoContent(DMaaPContext ctx) {
80 ctx.getResponse().setStatus(204);
81 } catch (Exception excp) {
82 log.error(excp.getMessage(), excp);
87 * static method is used to create response object associated with html
92 public static void respondOkWithHtml(DMaaPContext ctx, String html) {
94 respondOkWithStream(ctx, "text/html", new ByteArrayInputStream(html.toString().getBytes()));
95 } catch (Exception excp) {
96 log.error(excp.getMessage(), excp);
101 * method used to create response object associated with InputStream
106 * @throws IOException
108 public static void respondOkWithStream(DMaaPContext ctx, String mediaType, final InputStream is)
111 * creates response object associated with streamwriter
113 respondOkWithStream(ctx, mediaType, new StreamWriter() {
115 public void write(OutputStream os) throws IOException {
127 * @throws IOException
129 public static void respondOkWithStream(DMaaPContext ctx, String mediaType, StreamWriter writer) throws IOException {
131 ctx.getResponse().setStatus(200);
132 OutputStream os = getStreamForBinaryResponse(ctx, mediaType);
138 * static method to create error objects
144 public static void respondWithError(DMaaPContext ctx, int errCode, String msg) {
146 ctx.getResponse().sendError(errCode, msg);
147 } catch (IOException excp) {
148 log.error(excp.getMessage(), excp);
153 * method to create error objects
159 public static void respondWithError(DMaaPContext ctx, int errCode, JSONObject body) {
161 sendErrorAndBody(ctx, errCode, body.toString(4), "application/json");
162 } catch (Exception excp) {
163 log.error(excp.getMessage(), excp);
168 * static method creates error object in JSON
174 public static void respondWithErrorInJson(DMaaPContext ctx, int errCode, String msg) {
176 JSONObject o = new JSONObject();
177 o.put("status", errCode);
178 o.put("message", msg);
179 respondWithError(ctx, errCode, o);
181 } catch (Exception excp) {
182 log.error(excp.getMessage(), excp);
187 * static method used to copy the stream with the help of another method
192 * @throws IOException
194 public static void copyStream(InputStream in, OutputStream out) throws IOException {
195 copyStream(in, out, 4096);
199 * static method to copy the streams
204 * @throws IOException
206 public static void copyStream(InputStream in, OutputStream out, int bufferSize) throws IOException {
207 byte[] buffer = new byte[bufferSize];
209 while ((len = in.read(buffer)) != -1) {
210 out.write(buffer, 0, len);
216 * interface used to define write method for outputStream
218 public static abstract interface StreamWriter {
220 * abstract method used to write the response
222 * @param paramOutputStream
223 * @throws IOException
225 public abstract void write(OutputStream paramOutputStream) throws IOException;
229 * static method returns stream for binary response
233 * @throws IOException
235 public static OutputStream getStreamForBinaryResponse(DMaaPContext ctx) throws IOException {
236 return getStreamForBinaryResponse(ctx, "application/octet-stream");
240 * static method returns stream for binaryResponses
245 * @throws IOException
247 public static OutputStream getStreamForBinaryResponse(DMaaPContext ctx, String contentType) throws IOException {
248 ctx.getResponse().setContentType(contentType);
250 boolean fResponseEntityAllowed = (!(ctx.getRequest().getMethod().equalsIgnoreCase("HEAD")));
252 OutputStream os = null;
253 if (fResponseEntityAllowed) {
254 os = ctx.getResponse().getOutputStream();
256 os = new NullStream();
266 private static class NullStream extends OutputStream {
271 public void write(int b) {
275 private static class NullWriter extends Writer {
282 public void write(char[] cbuf, int off, int len) {
288 public void flush() {
294 public void close() {
299 * sttaic method fetch stream for text
306 public static void sendErrorAndBody(DMaaPContext ctx, int err, String content, String mimeType) {
309 getStreamForTextResponse(ctx, mimeType).println(content);
310 } catch (IOException e) {
311 log.error(new StringBuilder().append("Error sending error response: ").append(e.getMessage()).toString(),
317 * method to set the code
322 public static void setStatus(DMaaPContext ctx, int code) {
323 ctx.getResponse().setStatus(code);
327 * static method returns stream for text response
331 * @throws IOException
333 public static PrintWriter getStreamForTextResponse(DMaaPContext ctx) throws IOException {
334 return getStreamForTextResponse(ctx, "text/html");
338 * static method returns stream for text response
343 * @throws IOException
345 public static PrintWriter getStreamForTextResponse(DMaaPContext ctx, String contentType) throws IOException {
346 ctx.getResponse().setContentType(contentType);
348 PrintWriter pw = null;
349 boolean fResponseEntityAllowed = (!(ctx.getRequest().getMethod().equalsIgnoreCase("HEAD")));
351 if (fResponseEntityAllowed) {
352 pw = ctx.getResponse().getWriter();
354 pw = new PrintWriter(new NullWriter());