Skip to content

Commit

Permalink
(#19) Improve page rendering performance
Browse files Browse the repository at this point in the history
  • Loading branch information
maiconandsilva committed Mar 20, 2023
1 parent 84d5eb3 commit e794035
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 81 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.zkoss.web.util.resource;

import grails.config.Config;
import grails.core.GrailsApplication;
import grails.util.Holders;
import groovy.lang.Writable;
import groovy.text.Template;
import org.apache.commons.logging.Log;
Expand All @@ -20,6 +22,7 @@

import java.io.*;
import java.net.URL;
import java.nio.file.Files;
import java.util.Map;

@SuppressWarnings("deprecation")
Expand Down Expand Up @@ -112,8 +115,8 @@ private StringReader preprocessGSP(final Map<?, ?> config, final long length, fi

private PageDefinition parse(final String path, final org.springframework.core.io.Resource resource,
final Object extra) throws Throwable {
final Map<?, ?> config = grailsApplication.getConfig().flatten();
final Boolean disable = (Boolean)config.get(CONFIG_ZKGRAILS_TAGLIB_DISABLE);
final Config config = grailsApplication.getConfig();
final Boolean disable = config.getProperty(CONFIG_ZKGRAILS_TAGLIB_DISABLE, Boolean.class);
final Locator locator = (Locator)((extra != null) ? extra : PageDefinitions.getLocator(webApp, path));
if (disable != null && disable) {
return new Parser(webApp, locator).parse(new InputStreamReader(resource.getInputStream()), path);
Expand All @@ -133,13 +136,14 @@ protected PageDefinition parse(final String path, final URL url, final Object ex
@Override
protected PageDefinition parse(final String path, final File file, final Object extra) throws Exception {
final GrailsApplication grailsApplication = (GrailsApplication) appCtx.getBean("grailsApplication");
final Map<?, ?> config = grailsApplication.getConfig().flatten();
final Boolean disable = (Boolean) config.get(CONFIG_ZKGRAILS_TAGLIB_DISABLE);
final Config config = grailsApplication.getConfig();
final Boolean disable = config.getProperty(CONFIG_ZKGRAILS_TAGLIB_DISABLE, Boolean.class);

final Locator locator = (Locator)((extra != null) ? extra : PageDefinitions.getLocator(webApp, path));
if (disable != null && disable) {
return new Parser(webApp, locator).parse(file, path);
}
final StringReader reader = preprocessGSP(config, file.length(), new FileInputStream(file));
final StringReader reader = preprocessGSP(config, file.length(), Files.newInputStream(file.toPath()));
final PageDefinition pgdef = new Parser(webApp, locator).parse(reader, Servlets.getExtension(path));
pgdef.setRequestPath(path);
return pgdef;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,7 @@ public void init(FilterConfig fc) {
layoutViewResolver = WebUtils.lookupViewResolver(applicationContext);

final GrailsApplication grailsApplication = GrailsWebUtil.lookupApplication(fc.getServletContext());

// TODO: Verify the line below. In war files it returns NavigableMapConfig
Object encoding = Holders.getFlatConfig().get(CONFIG_OPTION_GSP_ENCODING);
if (encoding instanceof String) {
defaultEncoding = (String) encoding;
}
defaultEncoding = grailsApplication.getConfig().getProperty(CONFIG_OPTION_GSP_ENCODING, defaultEncoding);

Map<String, PersistenceContextInterceptor> interceptors = applicationContext.getBeansOfType(PersistenceContextInterceptor.class);
if (!interceptors.isEmpty()) {
Expand Down Expand Up @@ -190,8 +185,7 @@ public void doFilter(ServletRequest rq, ServletResponse rs, FilterChain chain)
if (content == null || response.isCommitted()) {
return;
}
Content content2 = applyLive(request, content);
new GrailsNoDecorator().render(content2, webAppContext);
new GrailsNoDecorator().render(content, webAppContext);
return;
}

Expand Down Expand Up @@ -220,7 +214,6 @@ public void doFilter(ServletRequest rq, ServletResponse rs, FilterChain chain)
if (content == null || response.isCommitted()) {
return;
}
// applyLive(request, content);
detectContentTypeFromPage(content, response);
new GrailsNoDecorator().render(content, webAppContext);
dispatched = true;
Expand All @@ -244,69 +237,6 @@ public void doFilter(ServletRequest rq, ServletResponse rs, FilterChain chain)
}
}

private Content applyLive(HttpServletRequest request, Content content) throws IOException {
if(Environment.getCurrent() == Environment.DEVELOPMENT) {
// if ZK Grails in the Dev mode
// insert z-it-live.js
if(content instanceof GSPSitemeshPage) {
GSPSitemeshPage page = (GSPSitemeshPage)content;
String pageContent = page.getPage();
if(pageContent == null) {
return content;
}
String contextPath = request.getContextPath();
//
// src="/zello/zkau/
if(pageContent.indexOf("src=\""+ contextPath + "/zkau/") > 0) {
StreamCharBuffer buffer = new StreamCharBuffer();
LinkGenerator grailsLinkGenerator = (LinkGenerator) applicationContext.getBean("grailsLinkGenerator");
String link = grailsLinkGenerator.resource(new HashMap<String, String>(){{
put("dir","ext/js");
put("file","z-it-live.js");
}});
buffer.getWriter().write(
pageContent.replace("</head>",
"<script type=\"text/javascript\" src=\"" + link + "\" charset=\"UTF-8\"></script>\n</head>")
);
page.setPageBuffer(buffer);
}
return content;
} else if(content instanceof HTMLPage2Content) {
HTMLPage2Content page2Content = (HTMLPage2Content)content;
try {
Field fPage = HTMLPage2Content.class.getDeclaredField("page");
fPage.setAccessible(true);
GrailsTokenizedHTMLPage htmlPage = (GrailsTokenizedHTMLPage)fPage.get(page2Content);
String pageContent = htmlPage.getPage();
String head = htmlPage.getHead();
String body = htmlPage.getBody();

String contextPath = request.getContextPath();
//
// src="/zello/zkau/
if(pageContent.indexOf("src=\""+ contextPath + "/zkau/") > 0) {
LinkGenerator grailsLinkGenerator = (LinkGenerator) applicationContext.getBean("grailsLinkGenerator");
String link = grailsLinkGenerator.resource(new HashMap<String, String>(){{
put("dir","ext/js");
put("file","z-it-live.js");
}});
pageContent = pageContent.replace("</head>", "<script type=\"text/javascript\" src=\"" + link + "\" charset=\"UTF-8\"></script>\n</head>");
head = head + "\n<script type=\"text/javascript\" src=\"" + link + "\" charset=\"UTF-8\"></script>\n";
CharArray newBody = new CharArray(body.length());
newBody.append(body);
CharArray newHead = new CharArray(head.length());
newHead.append(head);
GrailsTokenizedHTMLPage newHtmlPage = new GrailsTokenizedHTMLPage(pageContent.toCharArray(), newBody, newHead);
return new HTMLPage2Content(newHtmlPage);
}
} catch (NoSuchFieldException | IllegalAccessException e) {
e.printStackTrace();
}
}
}
return content;
}

private String detectZULFile(String pageContent, String contextPath) {
// detect uu:'/zello/zkau'
int i = pageContent.indexOf("uu:'"+ contextPath + "/zkau'");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import grails.web.mapping.UrlMappingsHolder;
import grails.web.mapping.exceptions.UrlMappingException;
import grails.web.mime.MimeType;
import groovy.util.ConfigObject;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.codehaus.groovy.control.MultipleCompilationErrorsException;
Expand All @@ -43,6 +44,7 @@
import org.grails.web.util.GrailsApplicationAttributes;
import org.grails.web.util.WebUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.util.ClassUtils;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.context.support.WebApplicationContextUtils;
Expand Down Expand Up @@ -423,11 +425,12 @@ private void processFilterChain(HttpServletRequest request, HttpServletResponse
}
}

protected void createStackTraceFilterer() {
protected void createStackTraceFilterer() throws LinkageError {
try {
filterer = (StackTraceFilterer) GrailsClassUtils.instantiateFromFlatConfig(
grailsConfig.flatten(), "grails.logging.stackTraceFiltererClass", DefaultStackTraceFilterer.class.getName());
// TODO: verify
String className = application.getConfig().getProperty(
"grails.logging.stackTraceFiltererClass", DefaultStackTraceFilterer.class.getName());
filterer = (StackTraceFilterer) ClassUtils.forName(className, ClassUtils.getDefaultClassLoader()).newInstance();

}
catch (Throwable t) {
logger.error("Problem instantiating StackTracePrinter class, using default: " + t.getMessage());
Expand Down

0 comments on commit e794035

Please sign in to comment.