Ejemplo n.º 1
0
        public override void ExecuteResult(ControllerContext context)
        {
            var request = context.HttpContext.Request;

            if (GenerateServiceOverview &&
                request.HttpMethod.Equals(
                    HttpVerbs.Get.ToString(),
                    StringComparison.OrdinalIgnoreCase))
            {
                new XmlRpcOverviewResult(GenerateServiceOverview, _services)
                .ExecuteResult(context);

                return;
            }

            if (!request.HttpMethod.Equals(
                    HttpVerbs.Post.ToString(),
                    StringComparison.OrdinalIgnoreCase))
            {
                throw new InvalidOperationException();
            }

            var requestInfo =
                XmlRpcRequestParser.GetRequestInformation(
                    request.InputStream);

            if (string.IsNullOrWhiteSpace(requestInfo.MethodName))
            {
                throw new InvalidOperationException(
                          "XmlRpc call does not contain a method.");
            }

            var methodInfo =
                XmlRpcRequestParser.GetRequestedMethod(requestInfo, _services);

            if (methodInfo == null)
            {
                throw new InvalidOperationException(
                          string.Concat(
                              "There was no implementation of IXmlRpcService ",
                              "found, that containins a method decorated with ",
                              " the XmlRpcMethodAttribute value'",
                              requestInfo.MethodName,
                              "'."));
            }

            var result =
                XmlRpcRequestParser.ExecuteRequestedMethod(
                    requestInfo, methodInfo, context.Controller);

            var response = context.RequestContext.HttpContext.Response;

            response.ContentType = "text/xml";

            var settings =
                new XmlWriterSettings
            {
                OmitXmlDeclaration = false,
                Encoding           = new UTF8Encoding(false), // Get rid of BOM
                Indent             = true,
            };

            using (var writer =
                       XmlWriter.Create(response.OutputStream, settings))
            {
                if (methodInfo.ResponseType == XmlRpcResponseType.Wrapped)
                {
                    WriteWrappedResponse(writer, result);
                    return;
                }
                WriteRawResponse(writer, result);
            }
        }
Ejemplo n.º 2
0
        public override void ExecuteResult(ControllerContext context)
        {
            if (!_generateOverview)
            {
                new HttpNotFoundResult().ExecuteResult(context);
                return;
            }

            var title = context.Controller.ValueProvider.GetValue("action").AttemptedValue;

            var methods = XmlRpcRequestParser.GetMethods(_services);

            using (var stringWriter = new StringWriter())
                using (var htmlWriter = new HtmlTextWriter(stringWriter))
                {
                    htmlWriter.RenderBeginTag(HtmlTextWriterTag.Html);
                    {
                        htmlWriter.RenderBeginTag(HtmlTextWriterTag.Head);
                        {
                            // Version Info
                            htmlWriter.Write("<!--");
                            htmlWriter.Write("XmlRpcMvc {0}", Assembly.GetExecutingAssembly().GetName().Version);
                            htmlWriter.Write("-->");

                            htmlWriter.RenderBeginTag(HtmlTextWriterTag.Title);
                            {
                                htmlWriter.Write(title);
                            }
                            htmlWriter.RenderEndTag();

                            // <meta name="robots" content="noindex" />
                            htmlWriter.AddAttribute(HtmlTextWriterAttribute.Name, "robots");
                            htmlWriter.AddAttribute(HtmlTextWriterAttribute.Content, "noindex");
                            htmlWriter.RenderBeginTag(HtmlTextWriterTag.Meta);
                            htmlWriter.RenderEndTag();

                            htmlWriter.AddAttribute(HtmlTextWriterAttribute.Type, "text/css");
                            htmlWriter.RenderBeginTag(HtmlTextWriterTag.Style);
                            {
                                htmlWriter.Write(@"
body {
    font-family: Segoe UI Light, Segoe WP Light, Segoe UI, Helvetica, sans-serif;
    padding: 0;
    margin: 0;
}

body > div {
    padding: 0 20px;
}

body > div > div {
    margin-bottom: 50px;
    border-top: 1px solid #CCCCCC;
    width: 90%;
}

h1 {
    background-color: #1BA1E2;
    color: white;
    padding: 5px 20px;
}

h2 {
    color: #1BA1E2;
}

ul {
    margin-bottom: 30px;
}

li {
    margin-bottom: 10px;
}

li > a {
    color: #000000;
}

table {
    width: 100%;
}

tr:nth-child(even) {
    background: #CCCCCC
}

tr:nth-child(odd) {
    background: #FFFFFF
}

td {
    height: 40px;
    vertical-align: middle;
    padding: 0 10px;
}
");
                            }
                            htmlWriter.RenderEndTag();
                        }
                        htmlWriter.RenderEndTag();

                        htmlWriter.RenderBeginTag(HtmlTextWriterTag.Body);
                        {
                            htmlWriter.RenderBeginTag(HtmlTextWriterTag.H1);
                            {
                                htmlWriter.Write(title);
                            }
                            htmlWriter.RenderEndTag();

                            htmlWriter.RenderBeginTag(HtmlTextWriterTag.Div);
                            {
                                htmlWriter.RenderBeginTag(HtmlTextWriterTag.P);
                                {
                                    htmlWriter.Write("The following methods are supported:");
                                }
                                htmlWriter.RenderEndTag();

                                // Method Names
                                htmlWriter.RenderBeginTag(HtmlTextWriterTag.Ul);
                                {
                                    foreach (var method in methods)
                                    {
                                        // Method Name
                                        htmlWriter.RenderBeginTag(HtmlTextWriterTag.Li);
                                        {
                                            htmlWriter.AddAttribute(HtmlTextWriterAttribute.Href, string.Concat("#", method.Value.Name));
                                            htmlWriter.RenderBeginTag(HtmlTextWriterTag.A);
                                            {
                                                htmlWriter.Write(method.Value.Name);
                                            }
                                            htmlWriter.RenderEndTag();
                                        }
                                        htmlWriter.RenderEndTag();
                                    }
                                }
                                htmlWriter.RenderEndTag();

                                foreach (var method in methods)
                                {
                                    var mi = method.Value.MethodInfo;

                                    htmlWriter.RenderBeginTag(HtmlTextWriterTag.Div);
                                    {
                                        // Method name
                                        htmlWriter.RenderBeginTag(HtmlTextWriterTag.H2);
                                        {
                                            htmlWriter.AddAttribute(HtmlTextWriterAttribute.Name, method.Value.Name);
                                            htmlWriter.RenderBeginTag(HtmlTextWriterTag.A);
                                            {
                                                htmlWriter.Write(method.Value.Name);
                                            }
                                            htmlWriter.RenderEndTag();
                                        }
                                        htmlWriter.RenderEndTag();

                                        // "Parameters" headline
                                        htmlWriter.RenderBeginTag(HtmlTextWriterTag.H3);
                                        {
                                            htmlWriter.Write("Parameters");
                                        }
                                        htmlWriter.RenderEndTag();

                                        // "Parameters" table
                                        htmlWriter.RenderBeginTag(HtmlTextWriterTag.Table);
                                        {
                                            var parameters = mi.GetParameters();

                                            foreach (var parameter in parameters)
                                            {
                                                htmlWriter.RenderBeginTag(HtmlTextWriterTag.Tr);
                                                {
                                                    htmlWriter.AddStyleAttribute(HtmlTextWriterStyle.Width, "30%");
                                                    htmlWriter.RenderBeginTag(HtmlTextWriterTag.Td);
                                                    {
                                                        htmlWriter.Write(parameter.ParameterType.Name);
                                                    }
                                                    htmlWriter.RenderEndTag();

                                                    htmlWriter.RenderBeginTag(HtmlTextWriterTag.Td);
                                                    {
                                                        htmlWriter.Write(parameter.Name);
                                                    }
                                                    htmlWriter.RenderEndTag();
                                                }
                                                htmlWriter.RenderEndTag();
                                            }
                                        }
                                        htmlWriter.RenderEndTag();

                                        // "Return Value" headline
                                        htmlWriter.RenderBeginTag(HtmlTextWriterTag.H3);
                                        {
                                            htmlWriter.Write("Return Value");
                                        }
                                        htmlWriter.RenderEndTag();

                                        // "Return Value" table
                                        htmlWriter.RenderBeginTag(HtmlTextWriterTag.Table);
                                        {
                                            htmlWriter.RenderBeginTag(HtmlTextWriterTag.Tr);
                                            {
                                                htmlWriter.AddStyleAttribute(HtmlTextWriterStyle.Width, "30%");
                                                htmlWriter.RenderBeginTag(HtmlTextWriterTag.Td);
                                                {
                                                    htmlWriter.Write(mi.ReturnType.Name);
                                                }
                                                htmlWriter.RenderEndTag();

                                                htmlWriter.RenderBeginTag(HtmlTextWriterTag.Td);
                                                {
                                                    htmlWriter.Write(
                                                        !string.IsNullOrEmpty(method.Value.Description)
                                                        ? method.Value.Description
                                                        : "-");
                                                }
                                                htmlWriter.RenderEndTag();
                                            }
                                            htmlWriter.RenderEndTag();
                                        }
                                        htmlWriter.RenderEndTag();
                                    }
                                    htmlWriter.RenderEndTag();
                                }
                            }
                            htmlWriter.RenderEndTag();
                        }
                        htmlWriter.RenderEndTag();
                    }
                    htmlWriter.RenderEndTag();

                    Content = stringWriter.ToString();
                    base.ExecuteResult(context);
                }
        }