Source Code

for file /ajaxcore/GetJavaScriptProxy.aspx

<%@ Page Language="C#" %>

<%@ OutputCache Duration="1" VaryByParam="service" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Net" %>
<%@ Import Namespace="System.Xml" %>
<%@ Import Namespace="System.Xml.Xsl" %>
<%@ Import Namespace="System.Web.Configuration" %>

<script runat="server">
  // GetJavaScriptProxy.aspx
  // Javascript proxy generator for SOAP based web services
  // Copyright by Matthias Hertel, http://www.mathertel.de
  // This work is licensed under a BSD style license. See http://www.mathertel.de/License.aspx
  // More information on: http://ajaxaspects.blogspot.com/ and http://ajaxaspekte.blogspot.com/
  // 19.07.2005 white space removed
  // 20.07.2005 more datatypes and XML Documents
  // 04.09.2005 XslCompiledTransform
  // 24.09.2006 direct use of wsdl files enabled
  // 14.07.2007 server side caching of the generated JavaScript proxy enabled
  //            by specifying the OutputCache option.
  //            (sample: Here set to 1, but it should be something like 3600 on stable production servers)
  // 03.07.2008 enable external schema: add TrustedXslt and the standard resolver

  private string GetConfig(string key)
  {
    //System.Configuration.ConfigurationSettings.AppSettings["")
    //System.Configuration.Configuration config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(null);
    //System.Configuration.KeyValueConfigurationElement entry = config.AppSettings.Settings[key];

    string v = WebConfigurationManager.AppSettings[key];

    return (v);
  }


  private string CreateClientProxies(string url)
  {
    string proxy = GetConfig("WebProxy");

    if ((url != null) && (url.StartsWith("~/")))
      url = Request.ApplicationPath + url.Substring(1);
    if (url.EndsWith(".asmx", StringComparison.InvariantCultureIgnoreCase))
      url = url + "?WSDL";
    if (url.EndsWith(".svc", StringComparison.InvariantCultureIgnoreCase))
      url = url + "?WSDL";
    Uri uri = new Uri(Request.Url, url);

    HttpWebRequest req = (HttpWebRequest)WebRequest.Create(uri);
    // req.Credentials = CredentialCache.DefaultCredentials;
    // req.Proxy = WebRequest.DefaultWebProxy; // running on the same server !
    if (proxy != null)
      req.Proxy = new WebProxy(proxy); //  "http://winproxy.server.lan:3128"

    req.Timeout = 6 * 1000; // 6 seconds

    WebResponse res = req.GetResponse();
#if DOTNET11
    XmlDocument data = new XmlDocument();
    data.Load(res.GetResponseStream());

    XslTransform xsl = new XslTransform();
    xsl.Load(Server.MapPath("~/ajaxcore/wsdl.xslt"));

    System.IO.StringWriter sOut = new System.IO.StringWriter();
    xsl.Transform(data, null, sOut, null);
#else
    XmlReader data = XmlReader.Create(res.GetResponseStream());

    // Create an XmlUrlResolver with default credentials.
    XmlUrlResolver resolver = new XmlUrlResolver();
    // resolver.Credentials = CredentialCache.DefaultCredentials;

    XslCompiledTransform xsl = new XslCompiledTransform();
    xsl.Load(Server.MapPath("~/ajaxcore/wsdl.xslt"), XsltSettings.TrustedXslt, resolver);

    System.IO.StringWriter sOut = new System.IO.StringWriter();
    xsl.Transform(data, null, sOut);
#endif
    return (sOut.ToString());
  } // CreateClientProxies
</script>
<%
  Response.Clear();

  string host = Request.Url.Host;
  string authority = Request.Url.GetLeftPart(UriPartial.Authority);

  //string proxy = GetConfig("WebProxy");
  //Response.Write("<pre>" + (proxy == null ? "ja" : "null") + "</pre>");

  //Response.Write("<pre>" + host + "</pre>");
  //Response.Write("<pre>" + authority + "</pre>");

  string asText = Request.QueryString["html"];

  if (asText != null)
  {
    Response.ContentType = "text/html";
    Response.Write("<pre>");
  }
  else
  {
    Response.ContentType = "application/x-javascript";
  } // if

  string fileName = Request.QueryString["service"];
  if (fileName == null)
    fileName = "/AJAXEngine/S02_AJAXCoreSamples/CalcService.asmx";

  if (host.ToLower().Equals("www.mathertel.de"))
  {
    fileName = authority + fileName;
  }

  //Response.Write("<pre>" + fileName + "</pre>");
  //Response.End();

  // get good filenames only (local folder)
  if ((fileName.IndexOf('$') >= 0) || (Regex.IsMatch(fileName, @"\b(COM\d|LPT\d|CON|PRN|AUX|NUL)\b", RegexOptions.IgnoreCase)))
    throw new ApplicationException("Error in filename.");

  //if (!Server.MapPath(fileName).StartsWith(Request.PhysicalApplicationPath, StringComparison.InvariantCultureIgnoreCase))
  //  throw new ApplicationException("Can show local files only.");

  string ret = CreateClientProxies(fileName);
  ret = Regex.Replace(ret, @"\n *", "\n");
  ret = Regex.Replace(ret, @"\r\n *""", "\"");
  ret = Regex.Replace(ret, @"\r\n, *""", ",\"");
  ret = Regex.Replace(ret, @"\r\n\]", "]");
  ret = Regex.Replace(ret, @"\r\n; *", ";");
  Response.Write(ret);
%>


This page is part of the http://www.mathertel.de/ web site.

For updates and discussions see http://ajaxaspects.blogspot.com/.