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);
        }
        public RewriterResults rewrite(Gadget gadget, MutableContent content)
        {
            if (gadget.getSpec().getModulePrefs().getFeatures().ContainsKey("caja") ||
                "1".Equals(gadget.getContext().getParameter("caja")))
            {
                URI          retrievedUri = gadget.getContext().getUrl();
                UriCallback2 cb           = new UriCallback2(retrievedUri);

                MessageQueue          mq    = new SimpleMessageQueue();
                DefaultGadgetRewriter rw    = new DefaultGadgetRewriter(mq);
                CharProducer          input = CharProducer.Factory.create(
                    new java.io.StringReader(content.getContent()),
                    FilePosition.instance(new InputSource(new java.net.URI(retrievedUri.ToString())), 2, 1, 1));
                java.lang.StringBuilder output = new java.lang.StringBuilder();

                try
                {
                    rw.rewriteContent(new java.net.URI(retrievedUri.ToString()), input, cb, output);
                }
                catch (GadgetRewriteException e)
                {
                    throwCajolingException(e, mq);
                    return(RewriterResults.notCacheable());
                }
                catch (IOException e)
                {
                    throwCajolingException(e, mq);
                    return(RewriterResults.notCacheable());
                }
                content.setContent(tameCajaClientApi() + output.ToString());
            }
            return(null);
        }
        public virtual RewriterResults rewrite(sRequest request, sResponse original, MutableContent content)
        {
            ByteArrayOutputStream baos = new ByteArrayOutputStream((content.getContent().Length * 110) / 100);
            OutputStreamWriter output = new OutputStreamWriter(baos);
            String mimeType = original.getHeader("Content-Type");
            if (request.RewriteMimeType != null)
            {
                mimeType = request.RewriteMimeType;
            }
            GadgetSpec spec = null;
            if (request.Gadget != null)
            {
                spec = _specFactory.getGadgetSpec(request.Gadget.toJavaUri(), false);
            }
            if (rewrite(spec, request.getUri(),
                        content,
                        mimeType,
                        output))
            {
                content.setContent(Encoding.Default.GetString(baos.toByteArray()));
                return RewriterResults.cacheableIndefinitely();

            }

            return null;
        }
        public virtual RewriterResults rewrite(sRequest request, sResponse original, MutableContent content)
        {
            ByteArrayOutputStream baos   = new ByteArrayOutputStream((content.getContent().Length * 110) / 100);
            OutputStreamWriter    output = new OutputStreamWriter(baos);
            String mimeType = original.getHeader("Content-Type");

            if (request.RewriteMimeType != null)
            {
                mimeType = request.RewriteMimeType;
            }
            GadgetSpec spec = null;

            if (request.Gadget != null)
            {
                spec = _specFactory.getGadgetSpec(request.Gadget.toJavaUri(), false);
            }
            if (rewrite(spec, request.getUri(),
                        content,
                        mimeType,
                        output))
            {
                content.setContent(Encoding.Default.GetString(baos.toByteArray()));
                return(RewriterResults.cacheableIndefinitely());
            }

            return(null);
        }
        public String rewriteGadget(Gadget gadget, View currentView)
        {
            if (currentView == null)
            {
                // Nothing to rewrite.
                return(null);
            }
            MutableContent mc = GetMutableContent(gadget.getSpec(), currentView);

            foreach (IContentRewriter rewriter in rewriters)
            {
                rewriter.rewrite(gadget, mc);
            }
            return(mc.getContent());
        }
        public String rewriteGadget(Gadget gadget, String content)
        {
            if (content == null)
            {
                // Nothing to rewrite.
                return(null);
            }

            MutableContent mc = GetMutableContent(content);

            foreach (IContentRewriter rewriter in rewriters)
            {
                rewriter.rewrite(gadget, mc);
            }

            return(mc.getContent());
        }
        public sResponse rewriteHttpResponse(sRequest req, sResponse resp)
        {
            String         originalContent = resp.responseString;
            MutableContent mc = GetMutableContent(originalContent);

            foreach (IContentRewriter rewriter in rewriters)
            {
                rewriter.rewrite(req, resp, mc);
            }

            String rewrittenContent = mc.getContent();

            if (rewrittenContent.Equals(originalContent))
            {
                return(resp);
            }

            return(new HttpResponseBuilder(resp).setResponseString(rewrittenContent).create());
        }
Exemple #8
0
        public RewriterResults rewrite(Gadget gadget, MutableContent content)
        {
            if (gadget.getSpec().getModulePrefs().getFeatures().ContainsKey("caja") ||
                "1".Equals(gadget.getContext().getParameter("caja"))) 
            {
                URI retrievedUri = gadget.getContext().getUrl();
                UriCallback2 cb = new UriCallback2(retrievedUri);

                MessageQueue mq = new SimpleMessageQueue();
                DefaultGadgetRewriter rw = new DefaultGadgetRewriter(mq);
                CharProducer input = CharProducer.Factory.create(
                    new java.io.StringReader(content.getContent()),
                    FilePosition.instance(new InputSource(new java.net.URI(retrievedUri.ToString())), 2, 1, 1));
                java.lang.StringBuilder output = new java.lang.StringBuilder();

                try 
                {
                    rw.rewriteContent(new java.net.URI(retrievedUri.ToString()), input, cb, output);
                }
                catch (GadgetRewriteException e)
                {
                    throwCajolingException(e, mq);
                    return RewriterResults.notCacheable();
                }
                catch (IOException e) 
                {
                    throwCajolingException(e, mq);
                    return RewriterResults.notCacheable();
                }
                content.setContent(tameCajaClientApi() + output.ToString());

            }
            return null;
        }
        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;
        }