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 com.att.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;
37 import com.att.nsa.cambria.beans.DMaaPContext;
40 * class is used to create response object which is given to user
46 public class DMaaPResponseBuilder {
48 //private static Logger log = Logger.getLogger(DMaaPResponseBuilder.class);
49 private static final EELFLogger log = EELFManager.getInstance().getLogger(DMaaPResponseBuilder.class);
50 protected static final int kBufferLength = 4096;
52 public static void setNoCacheHeadings(DMaaPContext ctx) {
53 HttpServletResponse response = ctx.getResponse();
54 response.addHeader("Cache-Control", "no-store, no-cache, must-revalidate");
55 response.addHeader("Pragma", "no-cache");
56 response.addHeader("Expires", "0");
60 * static method is used to create response object associated with
65 * @throws JSONException
68 public static void respondOk(DMaaPContext ctx, JSONObject result) throws JSONException, IOException {
70 respondOkWithStream(ctx, "application/json", new ByteArrayInputStream(result.toString(4).getBytes()));
75 * method used to set staus to 204
79 public static void respondOkNoContent(DMaaPContext ctx) {
81 ctx.getResponse().setStatus(204);
82 } catch (Exception excp) {
83 log.error(excp.getMessage(), excp);
88 * static method is used to create response object associated with html
93 public static void respondOkWithHtml(DMaaPContext ctx, String html) {
95 respondOkWithStream(ctx, "text/html", new ByteArrayInputStream(html.toString().getBytes()));
96 } catch (Exception excp) {
97 log.error(excp.getMessage(), excp);
102 * method used to create response object associated with InputStream
107 * @throws IOException
109 public static void respondOkWithStream(DMaaPContext ctx, String mediaType, final InputStream is)
112 * creates response object associated with streamwriter
114 respondOkWithStream(ctx, mediaType, new StreamWriter() {
116 public void write(OutputStream os) throws IOException {
128 * @throws IOException
130 public static void respondOkWithStream(DMaaPContext ctx, String mediaType, StreamWriter writer) throws IOException {
132 ctx.getResponse().setStatus(200);
133 OutputStream os = getStreamForBinaryResponse(ctx, mediaType);
139 * static method to create error objects
145 public static void respondWithError(DMaaPContext ctx, int errCode, String msg) {
147 ctx.getResponse().sendError(errCode, msg);
148 } catch (IOException excp) {
149 log.error(excp.getMessage(), excp);
154 * method to create error objects
160 public static void respondWithError(DMaaPContext ctx, int errCode, JSONObject body) {
162 sendErrorAndBody(ctx, errCode, body.toString(4), "application/json");
163 } catch (Exception excp) {
164 log.error(excp.getMessage(), excp);
169 * static method creates error object in JSON
175 public static void respondWithErrorInJson(DMaaPContext ctx, int errCode, String msg) {
177 JSONObject o = new JSONObject();
178 o.put("status", errCode);
179 o.put("message", msg);
180 respondWithError(ctx, errCode, o);
182 } catch (Exception excp) {
183 log.error(excp.getMessage(), excp);
188 * static method used to copy the stream with the help of another method
193 * @throws IOException
195 public static void copyStream(InputStream in, OutputStream out) throws IOException {
196 copyStream(in, out, 4096);
200 * static method to copy the streams
205 * @throws IOException
207 public static void copyStream(InputStream in, OutputStream out, int bufferSize) throws IOException {
208 byte[] buffer = new byte[bufferSize];
210 while ((len = in.read(buffer)) != -1) {
211 out.write(buffer, 0, len);
217 * interface used to define write method for outputStream
219 public static abstract interface StreamWriter {
221 * abstract method used to write the response
223 * @param paramOutputStream
224 * @throws IOException
226 public abstract void write(OutputStream paramOutputStream) throws IOException;
230 * static method returns stream for binary response
234 * @throws IOException
236 public static OutputStream getStreamForBinaryResponse(DMaaPContext ctx) throws IOException {
237 return getStreamForBinaryResponse(ctx, "application/octet-stream");
241 * static method returns stream for binaryResponses
246 * @throws IOException
248 public static OutputStream getStreamForBinaryResponse(DMaaPContext ctx, String contentType) throws IOException {
249 ctx.getResponse().setContentType(contentType);
251 boolean fResponseEntityAllowed = (!(ctx.getRequest().getMethod().equalsIgnoreCase("HEAD")));
253 OutputStream os = null;
254 if (fResponseEntityAllowed) {
255 os = ctx.getResponse().getOutputStream();
257 os = new NullStream();
267 private static class NullStream extends OutputStream {
272 public void write(int b) {
276 private static class NullWriter extends Writer {
283 public void write(char[] cbuf, int off, int len) {
289 public void flush() {
295 public void close() {
300 * sttaic method fetch stream for text
307 public static void sendErrorAndBody(DMaaPContext ctx, int err, String content, String mimeType) {
310 getStreamForTextResponse(ctx, mimeType).println(content);
311 } catch (IOException e) {
312 log.error(new StringBuilder().append("Error sending error response: ").append(e.getMessage()).toString(),
318 * method to set the code
323 public static void setStatus(DMaaPContext ctx, int code) {
324 ctx.getResponse().setStatus(code);
328 * static method returns stream for text response
332 * @throws IOException
334 public static PrintWriter getStreamForTextResponse(DMaaPContext ctx) throws IOException {
335 return getStreamForTextResponse(ctx, "text/html");
339 * static method returns stream for text response
344 * @throws IOException
346 public static PrintWriter getStreamForTextResponse(DMaaPContext ctx, String contentType) throws IOException {
347 ctx.getResponse().setContentType(contentType);
349 PrintWriter pw = null;
350 boolean fResponseEntityAllowed = (!(ctx.getRequest().getMethod().equalsIgnoreCase("HEAD")));
352 if (fResponseEntityAllowed) {
353 pw = ctx.getResponse().getWriter();
355 pw = new PrintWriter(new NullWriter());