Skip to content

Commit

Permalink
Fix untested pull-request breaking all metadata pages
Browse files Browse the repository at this point in the history
  • Loading branch information
mythz committed Jul 22, 2013
1 parent 8c131b8 commit 48e3035
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 39 deletions.
Expand Up @@ -86,7 +86,7 @@ protected override void Render(HtmlTextWriter output)
debugOnlyInfo.AppendLine("</ul>");
}

var renderedTemplate = string.Format(
var renderedTemplate = HtmlTemplates.Format(
HtmlTemplates.IndexOperationsTemplate,
this.Title,
this.MetadataPageBodyHtml,
Expand Down
Expand Up @@ -44,7 +44,7 @@ public virtual string RequestUri

public void Render(HtmlTextWriter output)
{
var renderedTemplate = string.Format(HtmlTemplates.OperationControlTemplate,
var renderedTemplate = HtmlTemplates.Format(HtmlTemplates.OperationControlTemplate,
Title,
HttpRequest.GetParentAbsolutePath().ToParentPath() + MetadataConfig.DefaultMetadataUri,
ContentFormat.ToUpper(),
Expand Down
Expand Up @@ -17,7 +17,7 @@ protected override void Render(HtmlTextWriter output)
ListItems = this.OperationNames,
ListItemTemplate = @"<li><a href=""?op={0}"">{0}</a></li>"
}.ToString();
var renderedTemplate = string.Format(HtmlTemplates.OperationsControlTemplate,
var renderedTemplate = HtmlTemplates.Format(HtmlTemplates.OperationsControlTemplate,
this.Title, this.MetadataOperationPageBodyHtml, operationsPart);
output.Write(renderedTemplate);
}
Expand Down
@@ -1,4 +1,4 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>{0}</title>
Expand Down Expand Up @@ -125,11 +125,11 @@ <h2>{3}</h2>
{6}

<div class="example">
<!-- REST Examples -->
<!-- REST Examples -->

<h3>HTTP + {2}</h3>
<p> The following are sample HTTP requests and responses.
The placeholders shown need to be replaced with actual values.</p>
<h3>HTTP + {2}</h3>
<p> The following are sample HTTP requests and responses.
The placeholders shown need to be replaced with actual values.</p>

<div class="request">
<pre>
Expand Down
@@ -1,4 +1,4 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
Expand Down Expand Up @@ -55,7 +55,7 @@ <h1>{0}</h1>

<form>
<p> The following operations are supported.
For a more information please view the <a href="../../Metadata">Service Documentation</a>.
For a more information please view the <a href="../../metadata">Service Documentation</a>.
</p>

{2}
Expand Down
@@ -1,57 +1,77 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.IO;
using ServiceStack.Common.Utils;

namespace ServiceStack.WebHost.Endpoints.Support.Templates
{
public static class HtmlTemplates
{
public static string IndexOperationsTemplate;
public static string OperationControlTemplate;
public static string OperationsControlTemplate;

private const string IndexOperationsFileName = "IndexOperations.html";
private const string OperationControlFileName = "OperationControl.html";
private const string OperationsControlFileName = "OperationsControl.html";

private static string indexOperationsTemplate;
public static string IndexOperationsTemplate
{
get
{
return indexOperationsTemplate ??
(indexOperationsTemplate = LoadEmbeddedHtmlTemplate("IndexOperations.html"));
}
set { indexOperationsTemplate = value; }
}

static HtmlTemplates()
private static string operationControlTemplate;
public static string OperationControlTemplate
{
var UseCustomPath = EndpointHost.Config.UseCustomMetadataTemplates;
get
{
return operationControlTemplate ??
(operationControlTemplate = LoadEmbeddedHtmlTemplate("OperationControl.html"));
}
set { operationControlTemplate = value; }
}

if (UseCustomPath)
private static string operationsControlTemplate;
public static string OperationsControlTemplate
{
get
{
IndexOperationsTemplate = LoadExternal(IndexOperationsFileName);
OperationControlTemplate = LoadExternal(OperationControlFileName);
OperationsControlTemplate = LoadExternal(OperationsControlFileName);
return operationsControlTemplate ??
(operationsControlTemplate = LoadEmbeddedHtmlTemplate("OperationsControl.html"));
}
else
set { operationsControlTemplate = value; }
}


static HtmlTemplates()
{
if (EndpointHost.Config.UseCustomMetadataTemplates)
{
IndexOperationsTemplate = LoadEmbeddedHtmlTemplate(IndexOperationsFileName);
OperationControlTemplate = LoadEmbeddedHtmlTemplate(OperationControlFileName);
OperationsControlTemplate = LoadEmbeddedHtmlTemplate(OperationsControlFileName);
TryLoadExternal("IndexOperations.html", ref indexOperationsTemplate);
TryLoadExternal("OperationControl.html", ref operationControlTemplate);
TryLoadExternal("OperationsControl.html", ref operationsControlTemplate);
}
}

private static string LoadExternal(string templateName)
private static bool TryLoadExternal(string templateName, ref string template)
{
try
{
return File.ReadAllText(Path.Combine(EndpointHost.AppHost.VirtualPathProvider.RootDirectory.RealPath + "/" + EndpointHost.Config.MetadataCustomPath, templateName));
var staticFilePath = PathUtils.CombinePaths(
EndpointHost.AppHost.VirtualPathProvider.RootDirectory.RealPath,
EndpointHost.Config.MetadataCustomPath,
templateName);

template = File.ReadAllText(staticFilePath);
return true;
}
catch (Exception ex)
{
return LoadEmbeddedHtmlTemplate(templateName);
return false;
}
}

private static string LoadEmbeddedHtmlTemplate(string templateName)
{
string _resourceNamespace = typeof(HtmlTemplates).Namespace + ".Html.";
var stream = typeof(HtmlTemplates).Assembly.GetManifestResourceStream(_resourceNamespace + templateName);
var resourceNamespace = typeof(HtmlTemplates).Namespace + ".Html.";
var stream = typeof(HtmlTemplates).Assembly.GetManifestResourceStream(resourceNamespace + templateName);
if (stream == null)
{
throw new FileNotFoundException(
Expand All @@ -64,5 +84,14 @@ private static string LoadEmbeddedHtmlTemplate(string templateName)
}
}

public static string Format(string template, params object[] args)
{
for (int i = 0; i < args.Length; i++)
{
template = template.Replace(@"{" + i + "}", args[i].ToString());
}
return template;
}

}
}
Expand Up @@ -190,7 +190,7 @@
<WebProjectProperties>
<UseIIS>False</UseIIS>
<AutoAssignPort>True</AutoAssignPort>
<DevelopmentServerPort>23711</DevelopmentServerPort>
<DevelopmentServerPort>65318</DevelopmentServerPort>
<DevelopmentServerVPath>/</DevelopmentServerVPath>
<IISUrl>
</IISUrl>
Expand Down
Expand Up @@ -95,7 +95,7 @@
<WebProjectProperties>
<UseIIS>False</UseIIS>
<AutoAssignPort>True</AutoAssignPort>
<DevelopmentServerPort>21110</DevelopmentServerPort>
<DevelopmentServerPort>65323</DevelopmentServerPort>
<DevelopmentServerVPath>/</DevelopmentServerVPath>
<IISUrl>
</IISUrl>
Expand Down

0 comments on commit 48e3035

Please sign in to comment.