Skip to content

Commit 771ead1

Browse files
committed
Updated to Rewrite 1.0.0.Alpha3/SNAPSHOT
2 parents b43f614 + 833c3e0 commit 771ead1

18 files changed

Lines changed: 432 additions & 245 deletions

File tree

‎.gitignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*.DS_Store
55
target/
66
bin/
7+
generated-sources/
78
*.iml
89
*.ipr
910
test-output

‎CHANGELOG.txt‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
------------------------------------
2+
VERSION 4.0.0
3+
------------------------------------
4+
Regression Impact:
5+
* ViewID must now be strictly defined using the "value" attribute. E.g.: <view-id value="/view.id" />
6+
-- This can be done using the following regular expression replacement:
7+
Search: <view-id>\s*([^<]+?)\s*</view-id>
8+
Replace: <view-id value="$1" />
9+
10+
111
------------------------------------
212
VERSION 3.3.1
313
------------------------------------
@@ -8,7 +18,7 @@ Features & Enhancements:
818
Bugfixes:
919
* Implemented equals()/hashcode() for LazyExpression (#115)
1020
* Performance: URLPatternParser got created on every call to UrlMapping.getPatternParser()
11-
21+
* Fixed incorrect handling of Spring AOP proxies during bean name lookup - thanks to mbenson for the patch #(119)
1222

1323
------------------------------------
1424
VERSION 3.3.0

‎core/pom.xml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
</execution>
5252
</executions>
5353
<configuration>
54+
<generateDirectory>src/main/generated-sources</generateDirectory>
5455
<strict>true</strict>
5556
<verbose>false</verbose>
5657
<bindingDirectory>src/main/jaxb</bindingDirectory>

‎core/src/main/java/com/ocpsoft/pretty/faces/el/resolver/SpringBeanNameResolver.java‎

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import java.lang.reflect.InvocationTargetException;
1919
import java.lang.reflect.Method;
20+
import java.util.ArrayList;
2021

2122
import javax.servlet.ServletContext;
2223

@@ -53,11 +54,26 @@ public class SpringBeanNameResolver implements ELBeanNameResolver
5354
*/
5455
private final static String GET_BEAN_NAMES_METHOD = "getBeanNamesForType";
5556

57+
/**
58+
* FQCN of the ScopedProxyUtils class
59+
*/
60+
private final static String SCOPED_PROXY_UTILS_CLASS = "org.springframework.aop.scope.ScopedProxyUtils";
61+
62+
/**
63+
* Name of the static ScopedProxyUtils.getTargetBeanName() method
64+
*/
65+
private final static String GET_TARGET_BEAN_NAME_METHOD = "getTargetBeanName";
66+
5667
/**
5768
* The getBeanNamesForType() method
5869
*/
5970
private Method getBeanNamesMethod;
6071

72+
/**
73+
* The static ScopedProxyUtils.getTargetBeanName() method
74+
*/
75+
private Method getTargetBeanNameMethod;
76+
6177
/**
6278
* The Spring WebApplicationContext
6379
*/
@@ -94,6 +110,11 @@ public boolean init(ServletContext servletContext, ClassLoader classLoader)
94110
{
95111
log.debug("Spring detected. Enabling Spring bean name resolving.");
96112
}
113+
114+
// this is optional as the method is part of the Spring AOP module
115+
getTargetBeanNameMethod = getProxyTargetBeanNameMethod(classLoader);
116+
117+
// success
97118
return true;
98119

99120
}
@@ -146,6 +167,9 @@ public String getBeanName(Class<?> clazz)
146167
return null;
147168
}
148169

170+
// filter out scoped proxies, which are really Spring-internal names for beans we want:
171+
names = filterProxyNames(names);
172+
149173
// more than one name? Warn the user..
150174
if (names.length > 1)
151175
{
@@ -181,4 +205,115 @@ public String getBeanName(Class<?> clazz)
181205
return null;
182206
}
183207

208+
/**
209+
* Removes Spring internal bean names from the supplied list of names.
210+
*
211+
* @param names
212+
* The original list of names
213+
* @return the filtered list of names
214+
*/
215+
private String[] filterProxyNames(String[] names)
216+
{
217+
218+
// Spring AOP hasn't been found!
219+
if (getTargetBeanNameMethod == null)
220+
{
221+
return names;
222+
}
223+
224+
ArrayList<String> result = new ArrayList<String>();
225+
for (int i = names.length - 1; i >= 0; i--)
226+
{
227+
String name = names[i];
228+
if (name == null)
229+
{
230+
continue;
231+
}
232+
boolean isTargetBeanName = false;
233+
for (int j = 0; !isTargetBeanName && j < names.length; j++)
234+
{
235+
236+
// don't compare with itself
237+
if (j == i)
238+
{
239+
continue;
240+
}
241+
242+
// we will catch all reflection exceptions
243+
try
244+
{
245+
246+
// get the name Spring would use for the proxied bean
247+
String targetBeanName = (String) getTargetBeanNameMethod.invoke(null, names[j]);
248+
249+
// the name is a Spring internal name for the proxied bean
250+
isTargetBeanName = name.equals(targetBeanName);
251+
252+
}
253+
catch (IllegalArgumentException e)
254+
{
255+
log.warn(String.format("Internal error invoking %s", getTargetBeanNameMethod.getName()), e);
256+
}
257+
catch (IllegalAccessException e)
258+
{
259+
log.warn(String.format("Error invoking %s due to security restrictions",
260+
getTargetBeanNameMethod.getName()));
261+
}
262+
catch (InvocationTargetException e)
263+
{
264+
log.warn(String.format("Method %s has thrown an exception:", getTargetBeanNameMethod.getName()),
265+
e.getTargetException());
266+
}
267+
}
268+
269+
// no internal name -> add to result
270+
if (!isTargetBeanName)
271+
{
272+
result.add(name);
273+
}
274+
275+
}
276+
277+
return result.toArray(new String[result.size()]);
278+
}
279+
280+
/**
281+
* Tries to obtain the method ScopedProxyUtils.getTargetBeanName(). This may
282+
* fail because the class is part of Spring's AOP module.
283+
*
284+
* @param classLoader
285+
* The classloader to use for lookups
286+
* @return the target method or <code>null</code> if Spring AOP could not be
287+
* found
288+
*/
289+
private static Method getProxyTargetBeanNameMethod(ClassLoader classLoader)
290+
{
291+
try
292+
{
293+
Class<?> scopedProxyUtilsClass = Class.forName(SCOPED_PROXY_UTILS_CLASS, true, classLoader);
294+
return scopedProxyUtilsClass.getMethod(GET_TARGET_BEAN_NAME_METHOD, String.class);
295+
}
296+
catch (ClassNotFoundException e)
297+
{
298+
// will happen when Spring AOP is not on the classpath
299+
if (log.isDebugEnabled())
300+
{
301+
log.debug(String.format("Could not find %s#%s method; filtering of proxy bean names has been disabled.",
302+
SCOPED_PROXY_UTILS_CLASS, GET_TARGET_BEAN_NAME_METHOD));
303+
}
304+
}
305+
catch (SecurityException e)
306+
{
307+
// security issue
308+
log.warn(String.format("Unable to find method %s on class %s due to security restrictions.",
309+
GET_TARGET_BEAN_NAME_METHOD, SCOPED_PROXY_UTILS_CLASS));
310+
}
311+
catch (NoSuchMethodException e)
312+
{
313+
// Spring is expected to offer this method
314+
log.warn(String.format("Cannot find method %s on class %s.", GET_TARGET_BEAN_NAME_METHOD,
315+
SCOPED_PROXY_UTILS_CLASS));
316+
}
317+
return null;
318+
}
184319
}

‎core/src/main/java/com/ocpsoft/rewrite/prettyfaces/InboundRewriteRuleAdaptor.java‎

Lines changed: 64 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,12 @@
2525
import com.ocpsoft.pretty.faces.config.rewrite.RewriteRule;
2626
import com.ocpsoft.pretty.faces.rewrite.RewriteEngine;
2727
import com.ocpsoft.pretty.faces.url.QueryString;
28-
import com.ocpsoft.pretty.faces.url.URL;
2928
import com.ocpsoft.pretty.faces.util.StringUtils;
30-
import com.ocpsoft.rewrite.EvaluationContext;
31-
import com.ocpsoft.rewrite.config.Condition;
32-
import com.ocpsoft.rewrite.config.Operation;
3329
import com.ocpsoft.rewrite.config.Rule;
34-
import com.ocpsoft.rewrite.event.InboundRewrite;
35-
import com.ocpsoft.rewrite.servlet.config.HttpCondition;
36-
import com.ocpsoft.rewrite.servlet.config.HttpOperation;
30+
import com.ocpsoft.rewrite.event.Rewrite;
3731
import com.ocpsoft.rewrite.servlet.http.event.HttpInboundServletRewrite;
3832
import com.ocpsoft.rewrite.servlet.http.event.HttpServletRewrite;
33+
import com.ocpsoft.rewrite.servlet.util.URLBuilder;
3934

4035
/**
4136
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
@@ -50,84 +45,78 @@ public InboundRewriteRuleAdaptor(final RewriteRule rule)
5045
this.rule = rule;
5146
}
5247

53-
public Condition getCondition()
48+
@Override
49+
public String getId()
5450
{
55-
return new HttpCondition() {
56-
57-
@Override
58-
public boolean evaluateHttp(final HttpServletRewrite event, final EvaluationContext context)
59-
{
60-
if ((event instanceof InboundRewrite)
61-
&& rule.isInbound()
62-
&& rule.matches(new URL(event.getRequestURL()).decode().toURL()
63-
+ QueryString.build(event.getRequestQueryStringSeparator()
64-
+ event.getRequestQueryString()).toQueryString()))
65-
{
66-
return true;
67-
}
68-
return false;
69-
}
70-
};
51+
return toString();
7152
}
7253

73-
public Operation getOperation()
54+
@Override
55+
public boolean evaluate(final Rewrite event, final com.ocpsoft.rewrite.context.EvaluationContext context)
7456
{
75-
return new HttpOperation() {
76-
77-
@Override
78-
public void performHttp(final HttpServletRewrite event, final EvaluationContext context)
79-
{
80-
RewriteEngine engine = new RewriteEngine();
81-
String originalUrl = new URL(event.getRequestURL()).decode().toURL()
82-
+ QueryString.build(event.getRequestQueryStringSeparator()
83-
+ event.getRequestQueryString()).toQueryString();
84-
String newUrl = engine.processInbound(event.getRequest(), event.getResponse(), rule, originalUrl);
57+
if ((event instanceof HttpInboundServletRewrite)
58+
&& rule.isInbound()
59+
&& rule.matches(URLBuilder.build(((HttpServletRewrite) event).getRequestPath()).decode().toURL()
60+
+ QueryString.build(((HttpServletRewrite) event).getRequestQueryStringSeparator()
61+
+ ((HttpServletRewrite) event).getRequestQueryString()).toQueryString()))
62+
{
63+
return true;
64+
}
65+
return false;
66+
}
8567

86-
if (!Redirect.CHAIN.equals(rule.getRedirect()))
87-
{
88-
/*
89-
* An HTTP redirect has been triggered; issue one if we have a URL or if the current URL has been
90-
* modified.
91-
*/
68+
@Override
69+
public void perform(final Rewrite event, final com.ocpsoft.rewrite.context.EvaluationContext context)
70+
{
71+
RewriteEngine engine = new RewriteEngine();
72+
String originalUrl = URLBuilder.build(((HttpServletRewrite) event).getRequestPath()).decode().toURL()
73+
+ QueryString.build(((HttpServletRewrite) event).getRequestQueryStringSeparator()
74+
+ ((HttpServletRewrite) event).getRequestQueryString()).toQueryString();
75+
String newUrl = engine.processInbound(((HttpServletRewrite) event).getRequest(),
76+
((HttpServletRewrite) event).getResponse(), rule, originalUrl);
9277

93-
String redirectURL = null;
78+
if (!Redirect.CHAIN.equals(rule.getRedirect()))
79+
{
80+
/*
81+
* An HTTP redirect has been triggered; issue one if we have a URL or if the current URL has been
82+
* modified.
83+
*/
9484

95-
/*
96-
* The rewrite changed the URL and no 'url' attribute has been set for the rule.
97-
*/
98-
if (StringUtils.isBlank(rule.getUrl()) && !originalUrl.equals(newUrl))
99-
{
85+
String redirectURL = null;
10086

101-
/*
102-
* Add context path and encode request using encodeRedirectURL().
103-
*/
104-
redirectURL = event.getContextPath() + newUrl;
105-
}
106-
else if (StringUtils.isNotBlank(rule.getUrl()))
107-
{
108-
/*
109-
* This is a custom location - don't call encodeRedirectURL() and don't add context path, just
110-
* redirect to the encoded URL
111-
*/
112-
redirectURL = newUrl.trim();
113-
}
87+
/*
88+
* The rewrite changed the URL and no 'url' attribute has been set for the rule.
89+
*/
90+
if (StringUtils.isBlank(rule.getUrl()) && !originalUrl.equals(newUrl))
91+
{
11492

115-
if (redirectURL != null)
116-
{
117-
if (Redirect.PERMANENT.equals(rule.getRedirect()))
118-
((HttpInboundServletRewrite) event).redirectPermanent(redirectURL);
119-
if (Redirect.TEMPORARY.equals(rule.getRedirect()))
120-
((HttpInboundServletRewrite) event).redirectTemporary(redirectURL);
121-
}
122-
}
123-
else {
124-
if (!originalUrl.equals(newUrl))
125-
{
126-
((HttpInboundServletRewrite) event).forward(newUrl);
127-
}
128-
}
93+
/*
94+
* Add context path and encode request using encodeRedirectURL().
95+
*/
96+
redirectURL = ((HttpServletRewrite) event).getContextPath() + newUrl;
97+
}
98+
else if (StringUtils.isNotBlank(rule.getUrl()))
99+
{
100+
/*
101+
* This is a custom location - don't call encodeRedirectURL() and don't add context path, just
102+
* redirect to the encoded URL
103+
*/
104+
redirectURL = newUrl.trim();
129105
}
130106

131-
};
107+
if (redirectURL != null)
108+
{
109+
if (Redirect.PERMANENT.equals(rule.getRedirect()))
110+
((HttpInboundServletRewrite) event).redirectPermanent(redirectURL);
111+
if (Redirect.TEMPORARY.equals(rule.getRedirect()))
112+
((HttpInboundServletRewrite) event).redirectTemporary(redirectURL);
113+
}
114+
}
115+
else {
116+
if (!originalUrl.equals(newUrl))
117+
{
118+
((HttpInboundServletRewrite) event).forward(newUrl);
119+
}
120+
}
132121
}
133122
}

0 commit comments

Comments
 (0)