/**
 * Copyright (C) 2014-2015 LinkedIn Corp. (pinot-core@linkedin.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.linkedin.pinot.broker.servlet;

import java.io.BufferedReader;
import java.io.IOException;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.linkedin.pinot.common.metrics.BrokerMeter;
import com.linkedin.pinot.common.metrics.BrokerMetrics;
import com.linkedin.pinot.common.response.BrokerResponse;
import com.linkedin.pinot.requestHandler.BrokerRequestHandler;


public class PinotClientRequestServlet extends HttpServlet {

  private static final long serialVersionUID = -3516093545255816357L;
  private static final Logger LOGGER = LoggerFactory.getLogger(PinotClientRequestServlet.class);

  private BrokerRequestHandler broker;
  private BrokerMetrics brokerMetrics;

  @Override
  public void init(ServletConfig config) throws ServletException {
    broker = (BrokerRequestHandler) config.getServletContext().getAttribute(BrokerRequestHandler.class.toString());
    brokerMetrics = (BrokerMetrics) config.getServletContext().getAttribute(BrokerMetrics.class.toString());
  }

  @Override
  protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    try {
      resp.setCharacterEncoding("UTF-8");
      BrokerResponse brokerResponse = broker.handleRequest(new JSONObject(req.getParameter("bql")));
      String jsonString = brokerResponse.toJsonString();
      resp.getOutputStream().print(jsonString);
      resp.getOutputStream().flush();
      resp.getOutputStream().close();
    } catch (final Exception e) {
      resp.getOutputStream().print(e.getMessage());
      resp.getOutputStream().flush();
      resp.getOutputStream().close();
      LOGGER.error("Caught exception while processing GET request", e);
      brokerMetrics.addMeteredGlobalValue(BrokerMeter.UNCAUGHT_GET_EXCEPTIONS, 1);
    }
  }

  @Override
  protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    try {
      resp.setCharacterEncoding("UTF-8");
      BrokerResponse brokerResponse = broker.handleRequest(extractJSON(req));
      String jsonString = brokerResponse.toJsonString();
      resp.getOutputStream().print(jsonString);
      resp.getOutputStream().flush();
      resp.getOutputStream().close();
    } catch (final Exception e) {
      resp.getOutputStream().print(e.getMessage());
      resp.getOutputStream().flush();
      resp.getOutputStream().close();
      LOGGER.error("Caught exception while processing POST request", e);
      brokerMetrics.addMeteredGlobalValue(BrokerMeter.UNCAUGHT_POST_EXCEPTIONS, 1);
    }
  }

  private JSONObject extractJSON(HttpServletRequest req) throws IOException, JSONException {
    final StringBuilder requestStr = new StringBuilder();
    String line;
    final BufferedReader reader = req.getReader();
    while ((line = reader.readLine()) != null) {
      requestStr.append(line);
    }
    return new JSONObject(requestStr.toString());
  }
}

@@ -735,16 +735,44 @@
 ption;%0A%0A
+import java.io.PrintWriter;%0A
 import j
@@ -2059,36 +2059,24 @@
 Exception %7B%0A
-    try %7B%0A  
     resp.set
@@ -2095,32 +2095,85 @@
 oding(%22UTF-8%22);%0A
+    PrintWriter writer = resp.getWriter();%0A    try %7B%0A
       BrokerResp
@@ -2320,38 +2320,22 @@
 ;%0A      
-resp.getOutputStream()
+writer
 .print(j
@@ -2356,68 +2356,36 @@
     
-resp.getOutputStream().flush();%0A      resp.getOutputStream()
+writer.flush();%0A      writer
 .clo
@@ -2430,38 +2430,22 @@
 %7B%0A      
-resp.getOutputStream()
+writer
 .print(e
@@ -2470,68 +2470,36 @@
     
-resp.getOutputStream().flush();%0A      resp.getOutputStream()
+writer.flush();%0A      writer
 .clo
@@ -2795,28 +2795,16 @@
 ption %7B%0A
-    try %7B%0A  
     resp
@@ -2831,24 +2831,77 @@
 g(%22UTF-8%22);%0A
+    PrintWriter writer = resp.getWriter();%0A    try %7B%0A
       Broker
@@ -3029,38 +3029,22 @@
 ;%0A      
-resp.getOutputStream()
+writer
 .print(j
@@ -3065,68 +3065,36 @@
     
-resp.getOutputStream().flush();%0A      resp.getOutputStream()
+writer.flush();%0A      writer
 .clo
@@ -3139,38 +3139,22 @@
 %7B%0A      
-resp.getOutputStream()
+writer
 .print(e
@@ -3179,68 +3179,36 @@
     
-resp.getOutputStream().flush();%0A      resp.getOutputStream()
+writer.flush();%0A      writer
 .clo

Fix PinotClientRequestServlet to handle special characters.