Example #1
0
  /**
   * Routes an HTTP request to the appropriate handler.
   *
   * @param sk
   * @param request
   * @return For synchronous handlers a method response is returned. For asynchronous handlers
   *     <code>null</code> is returned.
   * @throws Exception
   */
  RpcResponse routeRequest(Socket socket, HttpRequestBuffer request) throws Exception {
    String reqMountPoint = Utils.normalizeURIPath(request.getURI());
    ServerUnmarshallerAid aid = new ServerUnmarshallerAid(reqMountPoint);

    HttpRequestUnmarshaller unmarshaller = null;
    synchronized (this.reqUnmarshallers) {
      unmarshaller = (HttpRequestUnmarshaller) this.reqUnmarshallers.get(request.getMethod());

      if (unmarshaller == null) {
        // No registered handler for this method.
        // TODO: Add testcase
        Iterator iter = this.reqUnmarshallers.keySet().iterator();
        String allowed = Utils.join(", ", iter);
        throw new MethodNotAllowedResponseException(
            "(no handler for " + request.getMethod() + ")", allowed);
      }
    }

    RpcCall call;
    try {
      call = unmarshaller.unmarshal(request, aid);
    } catch (MethodNotAllowedException e) {
      // TODO: Add testcase
      String allowed = Utils.join(", ", e.getAllowed());
      throw new MethodNotAllowedResponseException(
          "(misconfigured handler for " + e.getMethod() + "?)", allowed);
    }

    RequestHandler handler = aid.getRequestHandler();
    if (handler == null) {
      // No match with a specific URI, try with a wildcard URI
      handler = this.lookupHandler(null, call.getName());
      if (handler == null) {
        throw new HttpResponseException(
            HttpConstants.StatusCodes._404_NOT_FOUND,
            "Not Found (handler for " + call.getName() + ")",
            request);
      }
    }

    try {
      return dispatchRpcCall(socket, request, call, handler);
    } catch (RpcFaultException e) {
      return e.toFault();
    }
  }
Example #2
0
  // TODO: Document
  protected Encoding selectResponseEncoding(HttpRequestBuffer request) {
    Map accepted = request.getAcceptedEncodings();
    if (accepted == null) {
      return null;
    }

    Iterator encodings = accepted.keySet().iterator();
    while (encodings.hasNext()) {
      String encodingName = (String) encodings.next();
      Encoding encoding = this.contentEncodingMap.getEncoding(encodingName);
      if (encoding != null) {
        return encoding;
      }
    }

    return null;
  }