Пример #1
0
  private void streamResponse(Response resp, ResponseParams rp) throws IOException {
    OutputStream body = null;
    try {
      if (rp.staticFile) {
        String fileName = rp.message;

        body = resp.getOutputStream();

        byte[] buffer = new byte[32 * 1024];
        InputStream input = configReader.getStaticFile(fileName);
        int bytesRead;
        while ((bytesRead = input.read(buffer, 0, buffer.length)) > 0) {
          body.write(buffer, 0, bytesRead);
        }

      } else {
        body = resp.getPrintStream();
        if (body != null) {
          ((PrintStream) body).print(rp.message);
        }
      }
    } finally {
      if (body != null) {
        try {
          body.close();
        } catch (IOException e) {
          LOGGER.log(Level.SEVERE, "Exception on MockServer close", e);
        }
      }
    }
  }
  @Override
  public void handle(Request request, Response response, SubscribeRequest message)
      throws Exception {
    String key = message.getKey();
    StatusResponse status = new StatusResponse(key, true);
    manager.subscribe(message);
    String content = gson.toJson(status);
    OutputStream output = response.getOutputStream();
    byte[] data = content.getBytes("UTF-8");

    response.setContentType("text/json");
    response.setContentLength(data.length);
    output.write(data);
    output.flush();
    response.close();
  }