private bool rewrite(GadgetSpec spec, Uri source, MutableContent mc, String mimeType, java.io.Writer writer)
        {
            // Dont rewrite content if the spec is unavailable
            if (spec == null)
            {
                return(false);
            }

            // Store the feature in the spec so we dont keep parsing it
            ContentRewriterFeature rewriterFeature = new ContentRewriterFeature(spec, includeUrls, excludeUrls, expires, includeTags);

            if (!rewriterFeature.isRewriteEnabled())
            {
                return(false);
            }
            if (isHTML(mimeType))
            {
                Dictionary <String, IHtmlTagTransformer> transformerMap = new Dictionary <string, IHtmlTagTransformer>();

                if (ProxyUrl != null)
                {
                    ILinkRewriter      linkRewriter = CreateLinkRewriter(spec, rewriterFeature);
                    LinkingTagRewriter rewriter     = new LinkingTagRewriter(linkRewriter, source);
                    HashSet <String>   toProcess    = new HashSet <string>();
                    foreach (var item in rewriter.getSupportedTags())
                    {
                        toProcess.Add(item);
                    }

                    toProcess.IntersectWith(rewriterFeature.getIncludedTags());

                    foreach (string tag in toProcess)
                    {
                        transformerMap[tag] = rewriter;
                    }
                    if (rewriterFeature.getIncludedTags().Contains("style"))
                    {
                        transformerMap["style"] = new StyleTagRewriter(source, linkRewriter);
                    }
                }
                if (ConcatUrl != null &&
                    rewriterFeature.getIncludedTags().Contains("script"))
                {
                    transformerMap["script"] = new JavascriptTagMerger(spec, rewriterFeature, ConcatUrl, source);
                }
                HtmlRewriter.rewrite(new StringReader(mc.getContent()), source, transformerMap, writer);
                return(true);
            }
            if (isCSS(mimeType))
            {
                if (ProxyUrl != null)
                {
                    CssRewriter.rewrite(new StringReader(mc.getContent()), source, CreateLinkRewriter(spec, rewriterFeature), writer, false);
                    return(true);
                }
                return(false);
            }
            return(false);
        }
Example #2
0
        /**
         * @param concatBase Base url of the Concat servlet. Expected to be of the
         *                   form www.host.com/concat?
         * @param relativeUrlBase to resolve relative urls
         */
        public JavascriptTagMerger(GadgetSpec spec, ContentRewriterFeature rewriterFeature,
                                   String concatBase, Uri relativeUrlBase)
        {
            // Force the mime-type to mimic browser expectation so rewriters
            // can function properly
            this.concatBase = concatBase
                              + ProxyBase.REWRITE_MIME_TYPE_PARAM
                              + "=text/javascript&"
                              + "gadget="
                              + HttpUtility.UrlEncode(spec.getUrl().ToString())
                              + "&fp="
                              + rewriterFeature.getFingerprint()
                              + '&';

            this.relativeUrlBase = relativeUrlBase;
        }
        /**
         * @param concatBase Base url of the Concat servlet. Expected to be of the
         *                   form www.host.com/concat?
         * @param relativeUrlBase to resolve relative urls
         */
        public JavascriptTagMerger(GadgetSpec spec, ContentRewriterFeature rewriterFeature,
                                   String concatBase, Uri relativeUrlBase)
        {
            // Force the mime-type to mimic browser expectation so rewriters
            // can function properly
            this.concatBase = concatBase
                              + ProxyBase.REWRITE_MIME_TYPE_PARAM
                              + "=text/javascript&"
                              + "gadget="
                              + HttpUtility.UrlEncode(spec.getUrl().ToString())
                              + "&fp="
                              + rewriterFeature.getFingerprint()
                              + '&';

            this.relativeUrlBase = relativeUrlBase;
        }
Example #4
0
 protected internal ILinkRewriter CreateLinkRewriter(GadgetSpec spec, ContentRewriterFeature rewriterFeature)
 {
     return new ProxyingLinkRewriter(spec.getUrl(), rewriterFeature, ProxyUrl);
 }
Example #5
0
        private bool rewrite(GadgetSpec spec, Uri source, MutableContent mc, String mimeType, java.io.Writer writer)
        {
            // Dont rewrite content if the spec is unavailable
            if (spec == null)
            {
                return false;
            }

            // Store the feature in the spec so we dont keep parsing it
            ContentRewriterFeature rewriterFeature = new ContentRewriterFeature(spec, includeUrls, excludeUrls, expires, includeTags);

            if (!rewriterFeature.isRewriteEnabled())
            {
                return false;
            }
            if (isHTML(mimeType))
            {
                Dictionary<String, IHtmlTagTransformer> transformerMap = new Dictionary<string, IHtmlTagTransformer>();

                if (ProxyUrl != null)
                {
                    ILinkRewriter linkRewriter = CreateLinkRewriter(spec, rewriterFeature);
                    LinkingTagRewriter rewriter = new LinkingTagRewriter(linkRewriter, source);
                    HashSet<String> toProcess = new HashSet<string>();
                    foreach (var item in rewriter.getSupportedTags())
                    {
                        toProcess.Add(item);
                    }

                    toProcess.IntersectWith(rewriterFeature.getIncludedTags());

                    foreach (string tag in toProcess)
                    {
                        transformerMap[tag] = rewriter;
                    }
                    if (rewriterFeature.getIncludedTags().Contains("style"))
                    {
                        transformerMap["style"] = new StyleTagRewriter(source, linkRewriter);
                    }
                }
                if (ConcatUrl != null &&
                    rewriterFeature.getIncludedTags().Contains("script"))
                {
                    transformerMap["script"] = new JavascriptTagMerger(spec, rewriterFeature, ConcatUrl, source);

                }
                HtmlRewriter.rewrite(new StringReader(mc.getContent()), source, transformerMap, writer);
                return true;
            }
            if (isCSS(mimeType))
            {
                if (ProxyUrl != null)
                {
                    CssRewriter.rewrite(new StringReader(mc.getContent()), source, CreateLinkRewriter(spec, rewriterFeature), writer, false);
                    return true;
                }
                return false;
            }
            return false;
        }
 protected internal ILinkRewriter CreateLinkRewriter(GadgetSpec spec, ContentRewriterFeature rewriterFeature)
 {
     return(new ProxyingLinkRewriter(spec.getUrl(), rewriterFeature, ProxyUrl));
 }