예제 #1
0
        private Object getErrorJson(ResponseItem responseItem)
        {
            Dictionary <String, Object> error = new Dictionary <String, Object>();

            error.Add("code", responseItem.getErrorCode());

            String message = errorTitles[responseItem.getErrorCode()];

            if (message == null)
            {
                message = responseItem.getErrorMessage();
            }
            else
            {
                if (!string.IsNullOrEmpty(responseItem.getErrorMessage()))
                {
                    message += ": " + responseItem.getErrorMessage();
                }
            }

            if (!string.IsNullOrEmpty(message))
            {
                error.Add("message", message);
            }

            if (responseItem.getResponse() != null)
            {
                error.Add("data", responseItem.getResponse());
            }

            return(error);
        }
예제 #2
0
        protected override void sendError(HttpResponse servletResponse, ResponseItem responseItem)
        {
            int errorCode = responseItem.getErrorCode();

            if (errorCode < 0)
            {
                // Map JSON-RPC error codes into HTTP error codes as best we can
                // TODO: Augment the error message (if missing) with a default
                switch (errorCode)
                {
                case -32700:
                case -32602:
                case -32600:
                    // Parse error, invalid params, and invalid request
                    errorCode = (int)HttpStatusCode.BadRequest;
                    break;

                case -32601:
                    // Procedure doesn't exist
                    errorCode = (int)HttpStatusCode.NotImplemented;
                    break;

                case -32603:
                default:
                    // Internal server error, or any application-defined error
                    errorCode = (int)HttpStatusCode.InternalServerError;
                    break;
                }
            }
            servletResponse.ContentType       = ContentTypes.OUTPUT_TEXT_CONTENT_TYPE;
            servletResponse.StatusCode        = errorCode;
            servletResponse.StatusDescription = responseItem.getErrorMessage();
            servletResponse.Output.Write(responseItem.getErrorMessage());
        }
예제 #3
0
        Object getJsonResponse(String key, ResponseItem responseItem, RpcRequestItem requestItem)
        {
            var result = new Dictionary <string, object>();

            if (key != null)
            {
                result.Add("id", key);
            }
            if (responseItem.getErrorCode() < 200 ||
                responseItem.getErrorCode() >= 400)
            {
                result.Add("error", getErrorJson(responseItem));
            }
            else
            {
                Object response = responseItem.getResponse();
                if (response != null &&
                    response is DataCollection)
                {
                    result.Add("data", ((DataCollection)response).entry);
                }
                else if (response != null && (response is IRestfulCollection))
                {
                    var map        = new Dictionary <string, object>();
                    var collection = (IRestfulCollection)response;
                    map.Add("startIndex", collection.startIndex);
                    map.Add("totalResults", collection.totalResults);
                    if (requestItem != null && requestItem.getCount() != null)
                    {
                        map.Add("itemsPerPage", requestItem.getCount());
                    }
                    map.Add("list", collection.getEntry());
                    result.Add("data", map);
                }
                else
                {
                    result.Add("data", response);
                }
            }
            return(result);
        }
예제 #4
0
        private void handleSingleRequest(HttpRequest servletRequest,
                                         HttpResponse servletResponse, ISecurityToken token, BeanConverter converter)
        {
            RestfulRequestItem requestItem = new RestfulRequestItem(servletRequest, token, converter);
            var asyncResult = HandleRequestItem(requestItem);

            // handle just the one case where we can't find a handler
            if (asyncResult == null)
            {
                sendError(servletResponse, new ResponseItem((int)HttpStatusCode.InternalServerError,
                                                            "The service " + requestItem.getService() + " is not implemented"));
                return;
            }
            ResponseItem responseItem = getResponseItem(asyncResult);

            servletResponse.ContentType = converter.GetContentType();
            if (responseItem.getErrorCode() >= 200 && responseItem.getErrorCode() < 400)
            {
                Object resp = responseItem.getResponse();
                // put single object responses into restfulcollection
                if (!(resp is DataCollection) && !(resp is IRestfulCollection))
                {
                    switch (requestItem.getService())
                    {
                    case IHandlerDispatcher.ACTIVITY_ROUTE:
                        if (resp is Activity)
                        {
                            resp = new RestfulCollection <Activity>((Activity)resp);
                        }

                        break;

                    case IHandlerDispatcher.PEOPLE_ROUTE:
                        if (resp is Person)
                        {
                            resp = new RestfulCollection <Person>((Person)resp);
                        }
                        break;

                    case IHandlerDispatcher.APPDATA_ROUTE:
                        if (requestItem.getOperation() != "POST")
                        {
                            resp = new DataCollection(new Dictionary <string, Dictionary <string, string> > {
                                { "entry", (Dictionary <string, string>)resp }
                            });
                        }
                        break;

                    default:
                        resp = new Dictionary <string, object> {
                            { "entry", resp }
                        };
                        break;
                    }
                }

                servletResponse.Output.Write(converter.ConvertToString(resp, requestItem));
            }
            else
            {
                sendError(servletResponse, responseItem);
            }
        }