Exemple #1
0
        public Task Handle(HttpContext context, int prefixLength)
        {
            var path = context.Request.Path.Value;

            if (path.Length == prefixLength)
            {
                return(context.Response.WriteError("Command not specified", HttpStatusCode.BadRequest));
            }
            var command = path.Substring(prefixLength + 1);

            if (command.Length == 0)
            {
                return(context.Response.WriteError("Command not specified", HttpStatusCode.BadRequest));
            }
            var commandType = CommandsRepository.Find(command);

            if (commandType == null)
            {
                return(context.Response.WriteError($"Unknown command: {command}", HttpStatusCode.NotFound));
            }
            switch (context.Request.Method)
            {
            case "POST":
                return(Application.Execute(commandType, context.Request.Body, context));

            case "GET":
                return(Application.Execute(commandType, null, context));

            default:
                return(Utility.WriteError(context.Response, "Unsuported method type", HttpStatusCode.MethodNotAllowed));
            }
        }
Exemple #2
0
        public SoapResultDescription Execute(SoapCommandDescription[] soapCommands)
        {
            IServerCommandDescription <XElement>[] processingCommands;
            if (soapCommands == null || soapCommands.Length == 0)
            {
                throw new FaultException("SOAP commands missing.");
            }

            if (soapCommands.Where(sc => sc.CommandName == null ||
                                   ServerCommands.Find(sc.CommandName) == null).Any())
            {
                var unknownCommand =
                    (from sc in soapCommands
                     where sc.CommandName == null ||
                     ServerCommands.Find(sc.CommandName) == null
                     select sc.CommandName)
                    .First();

                throw new FaultException("Unknown command: " + unknownCommand);
            }

            processingCommands = PrepareCommands(soapCommands).ToArray();

            var result = ProcessingEngine.Execute <XElement, XElement>(processingCommands, Thread.CurrentPrincipal);

            if (result.Status == HttpStatusCode.ServiceUnavailable)
            {
                HttpRuntime.UnloadAppDomain();
            }
            if (result.Status == HttpStatusCode.InternalServerError || result.ExecutedCommandResults == null)
            {
                throw new FrameworkException(result.Message);
            }
            if ((int)result.Status >= 300)
            {
                throw new FaultException(result.Message);
            }

            return(ConvertToSoapResult(result));
        }
Exemple #3
0
        public Stream Post(Stream message)
        {
            var template = ThreadContext.Request.UriTemplateMatch;
            var command  = template.RelativePathSegments.FirstOrDefault();
            var args     = Uri.UnescapeDataString(template.QueryParameters.ToString()).Split('&');

            if (command == null)
            {
                // RelavitvePathSegments doesn't work with wilcard templates in Mono
                if (template.WildcardPathSegments.Count > 0)
                {
                    command = template.WildcardPathSegments[0];
                }

                if (command == null)
                {
                    return(Utility.ReturnError("Command not specified", HttpStatusCode.BadRequest));
                }
            }

            var commandType = CommandsRepository.Find(command);

            if (commandType == null)
            {
                return(Utility.ReturnError("Unknown command " + command, HttpStatusCode.NotFound));
            }

            var accept = (ThreadContext.Request.Accept ?? "application/xml").ToLowerInvariant();

            var sessionID = ThreadContext.Request.GetHeader("X-NGS-Session-ID") ?? string.Empty;
            var scope     = ObjectFactory.FindScope(sessionID);

            if (!string.IsNullOrEmpty(sessionID) && scope == null)
            {
                return(Utility.ReturnError("Unknown session: " + sessionID, HttpStatusCode.BadRequest));
            }
            var engine = scope != null?scope.Resolve <IProcessingEngine>() : ProcessingEngine;

            switch (ThreadContext.Request.ContentType)
            {
            case "application/json":
                return(ExecuteCommand(engine, Serialization, new JsonCommandDescription(args, message, commandType), accept));

            case "application/x-protobuf":
                return(ExecuteCommand(engine, Serialization, new ProtobufCommandDescription(args, message, commandType), accept));

            default:
                return(ExecuteCommand(engine, Serialization, new XmlCommandDescription(args, message, commandType), accept));
            }
        }
Exemple #4
0
        public void Execute(string command, Stream message, HttpContext context)
        {
            if (command == null)
            {
                context.Response.WriteError("Command not specified", HttpStatusCode.BadRequest);
                return;
            }
            var commandType = CommandsRepository.Find(command);

            if (commandType == null)
            {
                context.Response.WriteError("Unknown command: " + command, HttpStatusCode.NotFound);
                return;
            }
            Execute(commandType, message, context);
        }
        public Stream Post(Stream message)
        {
            var request  = ThreadContext.Request;
            var response = ThreadContext.Response;
            var template = request.UriTemplateMatch;
            var command  = template.RelativePathSegments.Count > 0 ? template.RelativePathSegments[0] : null;

            if (command == null)
            {
                return(response.ReturnError("Command not specified", HttpStatusCode.BadRequest));
            }

            var commandType = CommandsRepository.Find(command);

            if (commandType == null)
            {
                return(response.ReturnError("Unknown command: " + command, HttpStatusCode.NotFound));
            }

            var start = Stopwatch.GetTimestamp();

            var accept = (request.Accept ?? "application/json").ToLowerInvariant();

            var engine    = ProcessingEngine;
            var sessionID = request.GetHeaderLowercase("x-revenj-session-id");

            if (sessionID != null)
            {
                var scope = ObjectFactory.FindScope(sessionID);
                if (scope == null)
                {
                    return(response.ReturnError("Unknown session: " + sessionID, HttpStatusCode.BadRequest));
                }
                engine = scope.Resolve <IProcessingEngine>();
            }

            Stream stream;

            switch (request.ContentType)
            {
            case "application/json":
                stream =
                    ExecuteCommands(
                        engine,
                        Serialization,
                        new[] { new JsonCommandDescription(template.QueryParameters, message, commandType) },
                        request,
                        response,
                        accept);
                break;

            case "application/x-protobuf":
                stream =
                    ExecuteCommands(
                        engine,
                        Serialization,
                        new[] { new ProtobufCommandDescription(template.QueryParameters, message, commandType) },
                        request,
                        response,
                        accept);
                break;

            default:
                if (message != null)
                {
                    XElement el;
                    try { el = XElement.Load(message); }
                    catch (Exception ex)
                    {
                        return
                            (response.ReturnError(
                                 "Error parsing request body as XML. " + ex.Message,
                                 request.ContentType == null ? HttpStatusCode.UnsupportedMediaType : HttpStatusCode.BadRequest));
                    }
                    stream =
                        ExecuteCommands(
                            engine,
                            Serialization,
                            new[] { new XmlCommandDescription(el, commandType) },
                            request,
                            response,
                            accept);
                }
                else
                {
                    stream =
                        ExecuteCommands(
                            engine,
                            Serialization,
                            new[] { new XmlCommandDescription(template.QueryParameters, commandType) },
                            request,
                            response,
                            accept);
                }
                break;
            }
            var elapsed = (decimal)(Stopwatch.GetTimestamp() - start) / TimeSpan.TicksPerMillisecond;

            response.AddHeader("X-Duration", elapsed.ToString(CultureInfo.InvariantCulture));
            return(stream);
        }