/** * 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(); } }
private RpcResponse dispatchRpcCall( Socket socket, HttpRequestBuffer request, RpcCall call, RequestHandler handler) throws Exception, HttpResponseException { RpcCallContext context = this.newRpcCallContext(socket, request); if (handler instanceof SynchronousRequestHandler) { RpcResponse rsp = ((SynchronousRequestHandler) handler).handleRequest(call, context); if (rsp == null) { throw new HttpResponseException( HttpConstants.StatusCodes._500_INTERNAL_SERVER_ERROR, "A synchronous handler (for " + call.getName() + ") returned a null value"); } return rsp; } else { // TODO: _TEST_ Select the content based on Accept-Encoding // Encoding rspEncoding = this.selectResponseEncoding(request); // SocketResponseChannel rspChannel = new SocketResponseChannel(this, request, socket, // rspEncoding); SocketResponseChannel rspChannel = this.newSocketResponseChannel(socket, request); ((AsynchronousRequestHandler) handler).handleRequest(call, context, rspChannel); return null; } }