All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.googlecode.gwtrpcplus.server.internal.servlet.GwtRpcPlusBasicServlet Maven / Gradle / Ivy

The newest version!
package com.googlecode.gwtrpcplus.server.internal.servlet;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.ArrayList;

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

import com.google.gwt.user.server.rpc.RPCServletUtils;
import com.googlecode.gwtrpcplus.server.internal.RpcManagerServer;

/**
 * Bind this to /$modulename$/gwtrpcplus
 */
public class GwtRpcPlusBasicServlet extends HttpServlet {
  private static final long serialVersionUID = 1L;

  protected final transient RpcManagerServer manager;

  public GwtRpcPlusBasicServlet(RpcManagerServer manager) {
    this.manager = manager;
  }

  @Override
  protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    if ("true".equals(req.getHeader("longpush")))
      longpush(req, resp);
    else
      request(req, resp);
  }

  protected void longpush(HttpServletRequest request, HttpServletResponse resp) {
    String clientId = request.getHeader("clientId");
    if (clientId == null) {
      resp.setStatus(HttpServletResponse.SC_BAD_REQUEST);
      return;
    }

    // TODO Move to RpcManager
    ArrayList responses = new ArrayList();
    
    String r = manager.getResponseAndWait(clientId);
    if (r != null) {
      // We have a response in the queue, so answer it directly
      responses.add(r);
      // Add the other Responses queued
      while ((r = manager.getResponse(clientId)) != null)
        responses.add(r);
    }

    StringBuffer b = new StringBuffer();
    try {
      for (String re : responses) {
        b.append(re);
        b.append("\n");
      }
      String response = b.toString();
      boolean gzipEncode = RPCServletUtils.acceptsGzipEncoding(request)
          && RPCServletUtils.exceedsUncompressedContentLengthLimit(response);

      RPCServletUtils.writeResponse(getServletContext(), resp, response, gzipEncode);
    } catch (IOException e) {
      e.printStackTrace();
    }
  }

  protected void request(HttpServletRequest request, HttpServletResponse resp) throws IOException {
    StringBuilder data = new StringBuilder();
    {
      BufferedReader in = request.getReader();
      char[] tmp = new char[100];
      int len = 0;
      do {
        len = in.read(tmp, 0, tmp.length);
        if (len > 0)
          data.append(tmp, 0, len);
      } while (len >= 0);
    }

    String clientId = request.getHeader("clientId");
    if (clientId == null) {
      resp.setStatus(HttpServletResponse.SC_BAD_REQUEST);
      return;
    }

    manager.onCall(clientId, data.toString(), request);

    // TODO Make async
    String response = manager.getResponse(clientId);

    if (response != null) {
      boolean gzipEncode = RPCServletUtils.acceptsGzipEncoding(request)
          && RPCServletUtils.exceedsUncompressedContentLengthLimit(response);

      RPCServletUtils.writeResponse(getServletContext(), resp, response, gzipEncode);
    } else {
      resp.setStatus(HttpServletResponse.SC_OK);
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy