11package org .ocpsoft .rewrite .showcase .access ;
22
3- import javax .inject .Inject ;
43import javax .servlet .ServletContext ;
54
65import org .joda .time .DateTime ;
76import org .ocpsoft .rewrite .config .Configuration ;
87import org .ocpsoft .rewrite .config .ConfigurationBuilder ;
8+ import org .ocpsoft .rewrite .config .Direction ;
99import org .ocpsoft .rewrite .config .jodatime .JodaTime ;
1010import org .ocpsoft .rewrite .config .jodatime .TimeCondition ;
11+ import org .ocpsoft .rewrite .context .EvaluationContext ;
1112import org .ocpsoft .rewrite .servlet .config .DispatchType ;
1213import org .ocpsoft .rewrite .servlet .config .Domain ;
1314import org .ocpsoft .rewrite .servlet .config .Forward ;
15+ import org .ocpsoft .rewrite .servlet .config .HttpCondition ;
1416import org .ocpsoft .rewrite .servlet .config .HttpConfigurationProvider ;
17+ import org .ocpsoft .rewrite .servlet .config .Lifecycle ;
1518import org .ocpsoft .rewrite .servlet .config .Path ;
1619import org .ocpsoft .rewrite .servlet .config .rule .Join ;
20+ import org .ocpsoft .rewrite .servlet .http .event .HttpServletRewrite ;
1721
1822/**
1923 * @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
2024 *
2125 */
2226public class AccessRewriteConfiguration extends HttpConfigurationProvider
2327{
24- @ Inject
25- private TimerBean timerBean ;
26-
2728 @ Override
2829 public Configuration getConfiguration (final ServletContext context )
2930 {
@@ -35,28 +36,54 @@ public Configuration getConfiguration(final ServletContext context)
3536 */
3637 .addRule (Join .path ("/" ).to ("/index.xhtml" ).withInboundCorrection ())
3738
38- /*
39- * Time based access control (only grants access during the first half of each minute)
40- */
41- .addRule (Join .path ("/{global}" ).to ("/{global}.xhtml" )
42- .when (JodaTime .matches (timeGranted ).and (DispatchType .isRequest ()))
43- .withInboundCorrection ())
44-
4539 /*
4640 * Domain based access control (only grants access to specific domains)
4741 */
48- .addRule (Join .path ("/domain" ).to ("/domain.xhtml" )
49- .when (Domain .matches (".*rhcloud.*" ).or (Domain .matches ("localhost" ))))
42+ .addRule (Join .path ("/domain" ).to ("/domain.xhtml" ).withInboundCorrection ().when (new HttpCondition () {
43+ @ Override
44+ public boolean evaluateHttp (HttpServletRewrite event , EvaluationContext context )
45+ {
46+ if (Direction
47+ .isInbound ()
48+ .andNot (Domain .matches ("localhost" ).or (
49+ Domain .matches ("{1}rhcloud{2}" ).where ("1" ).matches (".*" ).where ("2" )
50+ .matches (".*" )))
51+ .evaluate (event , context ))
52+ return false ;
53+ return true ;
54+ }
55+ }))
5056
5157 /*
52- * Deny access to anything except the index unless we matched one of the rules above.
58+ * Time based access control (only grants access during the first half of each minute)
5359 */
60+ .addRule (Join .path ("/timer" ).to ("/timer.xhtml" )
61+ .withInboundCorrection ().when (new HttpCondition () {
62+ @ Override
63+ public boolean evaluateHttp (HttpServletRewrite event , EvaluationContext context )
64+ {
65+ if (Direction .isInbound ().andNot (JodaTime .matches (timeGranted )).evaluate (event , context ))
66+ return false ;
67+ return true ;
68+ }
69+ }))
70+
5471 .defineRule ()
55- .when (Path .matches (".*" ).and (DispatchType .isRequest ())
56- .andNot (Path .matches (".*javax.faces.resource.*" ))
57- .andNot (Path .matches ("/" )))
72+ .when (Direction .isInbound ()
73+ .and (DispatchType .isForward ())
74+ .and (Path .matches ("/timer.xhtml" ))
75+ .andNot (JodaTime .matches (timeGranted ))
76+ .and (DispatchType .isRequest ()))
77+ .perform (Lifecycle .handled ())
5878
79+ .defineRule ()
80+ .when (Direction
81+ .isInbound ()
82+ .and (DispatchType .isRequest ())
83+ .andNot (Path .matches ("{1}javax.faces.resource{2}" ).where ("1" ).matches (".*" ).where ("2" )
84+ .matches (".*" )))
5985 .perform (Forward .to ("/accessDenied.xhtml" ));
86+
6087 }
6188
6289 private final TimeCondition timeGranted = new TimeCondition () {
0 commit comments