Skip to content

Commit

Permalink
SHINDIG-1935
Browse files Browse the repository at this point in the history
Delivered For Yun Zhi Lin
Fix sizeof warnings when parsing templates with large amounts of text

git-svn-id: https://svn.apache.org/repos/asf/shindig/trunk@1553211 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
ryanjbaxter committed Dec 23, 2013
1 parent efa2706 commit c7fde72
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -152,4 +152,13 @@ under the License.
-->
<sizeOfPolicy maxDepth="5000" maxDepthExceededBehavior="abort"/>
</cache>

<!-- Used to cache Gadget template library -->
<cache name="parsedXml">
<!--
The elements stored in this cache are complex and the default sizeOfPolicy
is insufficient.
-->
<sizeOfPolicy maxDepth="5000" maxDepthExceededBehavior="continue"/>
</cache>
</ehcache>
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
*/
package org.apache.shindig.gadgets.templates;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;

import org.apache.shindig.auth.AnonymousSecurityToken;
import org.apache.shindig.common.cache.Cache;
import org.apache.shindig.common.cache.CacheProvider;
Expand Down Expand Up @@ -82,6 +86,27 @@ public TemplateLibrary loadTemplateLibrary(GadgetContext context, Uri uri) throw
}

if (element == null) {
// JIRA 1935
// rewrite the template content to reduce the object number counted by ehcache
if (!context.getDebug()) {
BufferedReader reader = new BufferedReader(new StringReader(content));
StringBuilder sb = new StringBuilder();
String s;
try {
while ((s = reader.readLine()) != null) {
sb.append(s);
}
content = sb.toString();
} catch (IOException e) {
// not re-throw exception here
// If it fails to rewrite the string, just uses the original string for xml parsing
} finally {
try {
reader.close();
} catch (IOException e) {}
}
}

element = XmlUtil.parse(content);
if (key != null) {
parsedXmlCache.addElement(key, element);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,28 +24,37 @@

import org.apache.shindig.auth.SecurityToken;
import org.apache.shindig.common.uri.Uri;
import org.apache.shindig.common.xml.XmlException;
import org.apache.shindig.common.xml.XmlUtil;
import org.apache.shindig.expressions.Expressions;
import org.apache.shindig.gadgets.GadgetContext;
import org.apache.shindig.gadgets.GadgetException;
import org.apache.shindig.gadgets.http.HttpRequest;
import org.apache.shindig.gadgets.http.HttpResponse;
import org.apache.shindig.gadgets.http.HttpResponseBuilder;
import org.apache.shindig.gadgets.http.RequestPipeline;
import org.apache.shindig.gadgets.templates.tags.TagHandler;

import org.junit.Test;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

import com.google.common.collect.ImmutableMap;

public class TemplateLibraryFactoryTest {

public static final Uri SPEC_URL = Uri.parse("http://www.example.org/dir/g.xml");
public static final Uri TEMPLATE_URL = Uri.parse("http://www.example.org/dir/template.xml");
public static final String NAMESPACE_URI = "#my";
private static final String TEMPLATE_LIBRARY =
"<Templates xmlns:my='#my'>" +
" <Namespace prefix='my' url='#my'/>" +
" <JavaScript>script</JavaScript>" +
" <Style>style</Style>" +
" <Template tag='my:Tag1'>external1</Template>" +
" <Template tag='my:Tag2'>external2</Template>" +
" <Template tag='my:Tag3'>external3</Template>" +
" <Template tag='my:Tag4'>external4</Template>" +
"<Templates xmlns:my='#my'>" + '\n' +
" <Namespace prefix='my' url='#my'/>" + '\n' +
" <JavaScript>script;\nscript2</JavaScript>" + '\n' +
" <Style>style\nstyle2</Style>" + '\n' +
" <Template tag='my:Tag1'>external1</Template>" + '\n' +
" <Template tag='my:Tag2'>external2</Template>" + '\n' +
" <Template tag='my:Tag3'>external3</Template>" + '\n' +
" <Template tag='my:Tag4'>external4</Template>" + '\n' +
"</Templates>";

@Test
Expand Down Expand Up @@ -83,6 +92,66 @@ public boolean getIgnoreCache() {
assertTrue(pipeline.request.getIgnoreCache());
}

@Test
public void testTemplateLibraryRewrite() throws GadgetException, XmlException {
CapturingPipeline pipeline = new CapturingPipeline();
TemplateLibraryFactory factory = new TemplateLibraryFactory( pipeline, null );
GadgetContext context = new GadgetContext() {
@Override
public Uri getUrl() {
return SPEC_URL;
}

@Override
public String getContainer() {
return "default";
}

@Override
public boolean getDebug() {
return false;
}

@Override
public boolean getIgnoreCache() {
return true;
}
};

TemplateLibrary library = factory.loadTemplateLibrary(context, TEMPLATE_URL);
TagRegistry registry = library.getTagRegistry();

assertNotNull(registry.getHandlerFor(new TagRegistry.NSName(NAMESPACE_URI, "Tag1")));
assertNotNull(registry.getHandlerFor(new TagRegistry.NSName(NAMESPACE_URI, "Tag2")));
assertNotNull(registry.getHandlerFor(new TagRegistry.NSName(NAMESPACE_URI, "Tag3")));
assertNotNull(registry.getHandlerFor(new TagRegistry.NSName(NAMESPACE_URI, "Tag4")));

TagHandler handler = registry.getHandlerFor(new TagRegistry.NSName(NAMESPACE_URI, "Tag1"));
Element doc = XmlUtil.parse(TEMPLATE_LIBRARY);
Node result = doc.getOwnerDocument().createDocumentFragment();
Element tag = doc.getOwnerDocument().createElement("test");
final TemplateContext templateContext = new TemplateContext(null, ImmutableMap.<String, Object>of());
TemplateProcessor processor = new DefaultTemplateProcessor(Expressions.forTesting()) {
@Override
public TemplateContext getTemplateContext() {
return templateContext;
}
};
handler.process(result, tag, processor);

assertEquals("<STYLE>stylestyle2</STYLE>" +
"<JAVASCRIPT>script;script2</JAVASCRIPT>", serializeResources(templateContext));
}

private String serializeResources(TemplateContext context) {
StringBuilder builder = new StringBuilder();
for (TemplateResource resource : context.getResources()) {
builder.append(resource);
}

return builder.toString();
}

private static class CapturingPipeline implements RequestPipeline {
HttpRequest request;

Expand Down

0 comments on commit c7fde72

Please sign in to comment.