Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Performance improvements #9

Closed
wants to merge 1 commit into
from
Jump to file or symbol
Failed to load files and symbols.
+62 −33
Split
@@ -77,7 +77,7 @@ protected PrettyContext(final HttpServletRequest request)
contextPath = request.getContextPath();
String requestUrl = stripContextPath(request.getRequestURI());
- if (requestUrl.matches(JSESSIONID_REGEX))
+ if (request.isRequestedSessionIdFromURL())
{
requestUrl = requestUrl.replaceFirst(JSESSIONID_REGEX, JSESSIONID_REPLACEMENT);
}
@@ -257,6 +257,11 @@ public UrlMapping getCurrentMapping()
return currentMapping;
}
+ void setCurrentMapping(UrlMapping mapping)
+ {
+ currentMapping = mapping;
+ }
+
/**
* Return the current viewId to which the current request will be forwarded
* to JSF.
@@ -30,6 +30,7 @@
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
+import com.ocpsoft.pretty.faces.config.mapping.UrlMapping;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@@ -88,8 +89,10 @@ public void doFilter(final ServletRequest req, final ServletResponse resp, final
else
{
URL url = context.getRequestURL();
- if (getConfig().isURLMapped(url))
+ UrlMapping mapping = getConfig().getMappingForUrl(url);
+ if (mapping != null)
{
+ context.setCurrentMapping(mapping);
PrettyContext.setCurrentContext(request, context); // set
String viewId = context.getCurrentViewId();
@@ -56,7 +56,7 @@ public URL buildURL(final UrlMapping mapping)
{
FacesContext context = FacesContext.getCurrentInstance();
- URLPatternParser parser = new URLPatternParser(mapping.getPattern());
+ URLPatternParser parser = mapping.getPatternParser();
List<PathParameter> parameters = parser.getPathParameters();
List<String> parameterValues = new ArrayList<String>();
for (PathParameter injection : parameters)
@@ -47,7 +47,7 @@ public void injectParameters(final FacesContext context)
log.trace("Injecting parameters");
PrettyContext prettyContext = PrettyContext.getCurrentInstance(context);
URL url = prettyContext.getRequestURL();
- UrlMapping mapping = prettyContext.getConfig().getMappingForUrl(url);
+ UrlMapping mapping = prettyContext.getCurrentMapping();
if (mapping != null)
{
@@ -53,7 +53,7 @@ public void validateParameters(final FacesContext context)
log.trace("Validating parameters.");
PrettyContext prettyContext = PrettyContext.getCurrentInstance(context);
URL url = prettyContext.getRequestURL();
- UrlMapping mapping = prettyContext.getConfig().getMappingForUrl(url);
+ UrlMapping mapping = prettyContext.getCurrentMapping();
if (mapping != null)
{
@@ -19,8 +19,10 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
+import java.util.HashSet;
import java.util.List;
import java.util.Map;
+import java.util.Set;
import com.ocpsoft.pretty.PrettyContext;
import com.ocpsoft.pretty.PrettyFilter;
@@ -35,6 +37,7 @@
private List<RewriteRule> globalRewriteRules = new ArrayList<RewriteRule>();
private String dynaviewId = "";
private Map<String, UrlMapping> cachedMappings = new HashMap<String, UrlMapping>();
+ private Set<String> cachedNonMappedUrls = new HashSet<String>();
/**
* Set the current DynaView ID. This is used when calculating dynamic viewIds specified in pretty-config.xml (Do not
@@ -76,7 +79,7 @@ public void setGlobalRewriteRules(final List<RewriteRule> rules)
*/
public List<UrlMapping> getMappings()
{
- return Collections.unmodifiableList(mappings);
+ return mappings;
}
/**
@@ -98,6 +101,10 @@ public UrlMapping getMappingForUrl(final URL url)
if (cachedMappings.containsKey(mappingKey)) {
return cachedMappings.get(mappingKey);
}
+ if(cachedNonMappedUrls.contains(mappingKey)) {
+ // TODO should we make this configurable?
+ return null;
+ }
for (UrlMapping mapping : getMappings())
{
if (mapping.matches(url))
@@ -108,6 +115,7 @@ public UrlMapping getMappingForUrl(final URL url)
return mapping;
}
}
+ cachedNonMappedUrls.add(mappingKey);
return null;
}
@@ -119,7 +127,7 @@ public UrlMapping getMappingForUrl(final URL url)
public boolean isMappingId(final String id)
{
UrlMapping mapping = getMappingById(id);
- return mapping instanceof UrlMapping;
+ return mapping != null;
}
/**
@@ -38,26 +38,29 @@ public Metadata copy()
public String buildURLFromSegments(final List<String> segments)
{
+ if(segments.isEmpty())
+ {
+ return "/";
+ }
StringBuilder result = new StringBuilder();
if (hasLeadingSlash())
{
- result.append("/");
+ result.append('/');
}
- for (Iterator<String> iter = segments.iterator(); iter.hasNext();)
+ for (String segment : segments)
{
- String segment = iter.next();
- result.append(segment);
- if (iter.hasNext())
- {
- result.append("/");
- }
+ result.append(segment).append('/');
}
- if (hasTrailingSlash() && !result.toString().endsWith("/"))
+ if (!hasTrailingSlash())
+ {
+ result.deleteCharAt(result.length() - 1);
+ }
+ if(result.length() == 2 && result.indexOf("//") > -1)
{
- result.append("/");
+ return "/";
}
return result.toString();
}
@@ -27,6 +27,7 @@
{
private static final String SUFFIX = "#-p#";
private static final String PREFIX = "#p-#";
+ private static final Pattern TEMPLATE_PATTERN = Pattern.compile(PREFIX + "(\\d+)" + SUFFIX);
private String template;
private String regex;
@@ -47,7 +48,7 @@ static public String parameterize(final int paramIndex)
*/
public static Matcher getTemplateMatcher(final String target)
{
- return Pattern.compile(PREFIX + "(\\d+)" + SUFFIX).matcher(target);
+ return TEMPLATE_PATTERN.matcher(target);
}
/**
@@ -15,21 +15,28 @@
*/
package com.ocpsoft.pretty.faces.url;
+import com.ocpsoft.pretty.faces.util.StringUtils;
+
+import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
+import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
+import java.util.regex.Pattern;
public class URL
{
private Metadata metadata = new Metadata();
private String originalURL = "";
private List<String> segments;
+ private static final Pattern SLASH_PATTERN = Pattern.compile("/");
+
private final Map<String, List<String>> decodedSegments = new HashMap<String, List<String>>();
/**
@@ -52,7 +59,7 @@ public URL(final String url)
}
String trimmedUrl = trimSurroundingSlashes(url);
- String[] segments = trimmedUrl.split("/");
+ String[] segments = SLASH_PATTERN.split(trimmedUrl);
this.segments = Arrays.asList(segments);
}
@@ -143,12 +150,10 @@ private static String decodeSegment(final String segment)
{
try
{
- final URI uri = new URI(("http://localhost/" + segment).replace(" ", "%20").replace("\"", "%22"));
- return uri.getPath().substring(1);
- }
- catch (URISyntaxException e)
- {
- throw new IllegalArgumentException(e);
+ return URLDecoder.decode(segment, "UTF-8");
+ } catch (UnsupportedEncodingException e) {
+ // can never happen, because UTF-8 must be supported by all JVMs
+ throw new IllegalStateException(e);
}
}
@@ -260,19 +265,23 @@ public void setMetadata(final Metadata metadata)
*/
private String trimSurroundingSlashes(final String url)
{
- String result = null;
if (url != null)
{
- result = url.trim();
- if (result.startsWith("/"))
+ if(StringUtils.isBlank(url)) {
+ return url;
+ }
+ StringBuilder result = new StringBuilder(url.trim());
+ if (result.charAt(0) == '/')
{
- result = result.substring(1);
+ result.deleteCharAt(0);
}
- if (result.endsWith("/"))
+ int last = result.length() - 1;
+ if (last != -1 && result.charAt(last) == '/')
{
- result = result.substring(0, result.length() - 1);
+ result.deleteCharAt(last);
}
+ return result.toString();
}
- return result;
+ return null;
}
}
@@ -185,7 +185,7 @@ public String build(final UrlMapping urlMapping, final boolean encodeUrl, final
String result = "";
if (urlMapping != null)
{
- URLPatternParser parser = new URLPatternParser(urlMapping.getPattern());
+ URLPatternParser parser = urlMapping.getPatternParser();
List<String> pathParams = new ArrayList<String>();
List<QueryParameter> queryParams = new ArrayList<QueryParameter>();