[prettyfaces] r476 committed - Fixed PrettyFacesWrappedResponse to work outside of the JSF lifecycle ...

5 views
Skip to first unread message

prett...@googlecode.com

unread,
Dec 11, 2010, 1:52:36 PM12/11/10
to prettyfac...@googlegroups.com
Revision: 476
Author: chkalt
Date: Sat Dec 11 10:51:22 2010
Log: Fixed PrettyFacesWrappedResponse to work outside of the JSF lifecycle
(#78)
http://code.google.com/p/prettyfaces/source/detail?r=476

Modified:
/prettyfaces/trunk/core/src/main/java/com/ocpsoft/pretty/PrettyFilter.java

/prettyfaces/trunk/core/src/main/java/com/ocpsoft/pretty/faces/servlet/PrettyFacesWrappedResponse.java

=======================================
---
/prettyfaces/trunk/core/src/main/java/com/ocpsoft/pretty/PrettyFilter.java
Tue Oct 19 09:11:21 2010
+++
/prettyfaces/trunk/core/src/main/java/com/ocpsoft/pretty/PrettyFilter.java
Sat Dec 11 10:51:22 2010
@@ -59,10 +59,11 @@
public void doFilter(final ServletRequest req, final ServletResponse
resp, final FilterChain chain)
throws IOException, ServletException
{
- HttpServletResponse response = new
PrettyFacesWrappedResponse((HttpServletResponse) resp, getConfig());
+ HttpServletRequest request = (HttpServletRequest) req;
+
+ HttpServletResponse response = new
PrettyFacesWrappedResponse(request.getContextPath(), (HttpServletResponse)
resp, getConfig());
req.setAttribute(PrettyContext.CONFIG_KEY, getConfig());

- HttpServletRequest request = (HttpServletRequest) req;
PrettyContext context = PrettyContext.newDetachedInstance(request);

rewrite(request, response);
=======================================
---
/prettyfaces/trunk/core/src/main/java/com/ocpsoft/pretty/faces/servlet/PrettyFacesWrappedResponse.java
Wed Dec 8 23:29:52 2010
+++
/prettyfaces/trunk/core/src/main/java/com/ocpsoft/pretty/faces/servlet/PrettyFacesWrappedResponse.java
Sat Dec 11 10:51:22 2010
@@ -22,11 +22,9 @@
import java.util.Map.Entry;

import javax.faces.component.UIParameter;
-import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;

-import com.ocpsoft.pretty.PrettyContext;
import com.ocpsoft.pretty.PrettyException;
import com.ocpsoft.pretty.faces.config.PrettyConfig;
import com.ocpsoft.pretty.faces.config.mapping.PathParameter;
@@ -45,9 +43,12 @@

private final PrettyConfig prettyConfig;

- public PrettyFacesWrappedResponse(final HttpServletResponse response,
final PrettyConfig config)
+ private final String contextPath;
+
+ public PrettyFacesWrappedResponse(final String contextPath, final
HttpServletResponse response, final PrettyConfig config)
{
super(response);
+ this.contextPath = contextPath;
this.prettyConfig = config;
}

@@ -74,28 +75,20 @@
@Override
public String encodeURL(final String url)
{
- FacesContext context = FacesContext.getCurrentInstance();
- PrettyContext prettyContext = PrettyContext.getCurrentInstance();
-
- String result = rewritePrettyMappings(url, context, prettyContext);
-
- result = rewrite(result, prettyContext);
+ String result = rewritePrettyMappings(url);
+
+ result = rewrite(result);

return super.encodeURL(result);
}

- private String rewritePrettyMappings(final String url, final
FacesContext context, final PrettyContext prettyContext)
- {
- String contextPath = "";
+ private String rewritePrettyMappings(final String url)
+ {
String result = url;

- if ((url != null) && (context != null)
&& !context.getResponseComplete())
- {
- String strippedUrl = prettyContext.stripContextPath(url);
- if (!url.equals(strippedUrl))
- {
- contextPath = prettyContext.getContextPath();
- }
+ if (url != null)
+ {
+ String strippedUrl = stripContextPath(url);

List<UrlMapping> matches = new ArrayList<UrlMapping>();
for (UrlMapping m : prettyConfig.getMappings())
@@ -164,16 +157,16 @@
return result;
}

- private String rewrite(final String url, final PrettyContext context)
+ private String rewrite(final String url)
{
String result = "";
if (url != null)
{
- String strippedUrl = context.stripContextPath(url);
+ String strippedUrl = stripContextPath(url);

if (!strippedUrl.equals(url))
{
- result = PrettyContext.getCurrentInstance().getContextPath();
+ result = contextPath;
}

try
@@ -191,4 +184,17 @@
}
return result;
}
-}
+
+ /**
+ * If the given URL is prefixed with this request's context-path,
return the
+ * URI without the context path. Otherwise return the URI unchanged.
+ */
+ private String stripContextPath(String uri)
+ {
+ if (uri.startsWith(contextPath))
+ {
+ uri = uri.substring(contextPath.length());
+ }
+ return uri;
+ }
+}

Reply all
Reply to author
Forward
0 new messages