Skip to content

Commit fd42833

Browse files
committed
RuleBuilder defaults to always perform
1 parent f68841b commit fd42833

7 files changed

Lines changed: 163 additions & 77 deletions

File tree

‎api/src/main/java/com/ocpsoft/rewrite/config/RuleBuilder.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
public class RuleBuilder implements Rule
2727
{
2828
private String id = "";
29-
private Condition condition;
29+
private Condition condition = new True();
3030
private Operation operation;
3131

3232
/**
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* JBoss, Home of Professional Open Source
3+
* Copyright 2011, Red Hat, Inc., and individual contributors
4+
* by the @authors tag. See the copyright.txt in the distribution for a
5+
* full listing of individual contributors.
6+
*
7+
* This is free software; you can redistribute it and/or modify it
8+
* under the terms of the GNU Lesser General Public License as
9+
* published by the Free Software Foundation; either version 2.1 of
10+
* the License, or (at your option) any later version.
11+
*
12+
* This software is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15+
* Lesser General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU Lesser General Public
18+
* License along with this software; if not, write to the Free
19+
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20+
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21+
*/
22+
package com.ocpsoft.rewrite.showcase.composite;
23+
24+
/**
25+
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
26+
*/
27+
public class Base64EncodingStrategy implements EncodingStrategy
28+
{
29+
@Override
30+
public String encode(final String value)
31+
{
32+
return Base64.encodeBytes(value.getBytes());
33+
}
34+
35+
@Override
36+
public String decode(final String value)
37+
{
38+
return new String(Base64.decode(value));
39+
}
40+
41+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* JBoss, Home of Professional Open Source
3+
* Copyright 2011, Red Hat, Inc., and individual contributors
4+
* by the @authors tag. See the copyright.txt in the distribution for a
5+
* full listing of individual contributors.
6+
*
7+
* This is free software; you can redistribute it and/or modify it
8+
* under the terms of the GNU Lesser General Public License as
9+
* published by the Free Software Foundation; either version 2.1 of
10+
* the License, or (at your option) any later version.
11+
*
12+
* This software is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15+
* Lesser General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU Lesser General Public
18+
* License along with this software; if not, write to the Free
19+
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20+
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21+
*/
22+
package com.ocpsoft.rewrite.showcase.composite;
23+
24+
/**
25+
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
26+
*
27+
*/
28+
public interface ChecksumStrategy
29+
{
30+
31+
}

‎showcase/composite-query/src/main/java/com/ocpsoft/rewrite/showcase/composite/CompositeConfigProvider.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public Configuration getConfiguration(final ServletContext context)
1414
{
1515
return ConfigurationBuilder.begin()
1616

17-
.addRule(EncodeQuery.params().to("c"))
17+
.defineRule().perform(EncodeQuery.params().to("c"))
1818

1919
.addRule(Join.path("/").to("/index.xhtml"))
2020

‎showcase/composite-query/src/main/java/com/ocpsoft/rewrite/showcase/composite/EncodeQuery.java‎

Lines changed: 23 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,13 @@
2121
*/
2222
package com.ocpsoft.rewrite.showcase.composite;
2323

24-
import java.io.IOException;
25-
import java.io.InputStream;
26-
import java.io.InputStreamReader;
27-
import java.io.Reader;
28-
import java.io.UnsupportedEncodingException;
24+
import java.util.ArrayList;
25+
import java.util.Arrays;
26+
import java.util.List;
2927

3028
import com.ocpsoft.rewrite.config.Condition;
31-
import com.ocpsoft.rewrite.config.Rule;
29+
import com.ocpsoft.rewrite.config.Operation;
30+
import com.ocpsoft.rewrite.config.True;
3231
import com.ocpsoft.rewrite.context.EvaluationContext;
3332
import com.ocpsoft.rewrite.event.Rewrite;
3433
import com.ocpsoft.rewrite.servlet.config.DispatchType;
@@ -41,20 +40,23 @@
4140
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
4241
*
4342
*/
44-
public class EncodeQuery implements Rule
43+
public class EncodeQuery implements Operation
4544
{
4645
private static final String STATE = EncodeQuery.class.getName();
4746
private String name;
47+
private ChecksumStrategy checksumStrategy = new HashCodeChecksumStrategy();
48+
private EncodingStrategy encodingStrategy = new Base64EncodingStrategy();
49+
private Condition condition = new True();
50+
private final List<String> params = new ArrayList<String>();
51+
private final List<String> excludedParams = new ArrayList<String>();
4852

4953
public EncodeQuery()
5054
{}
5155

5256
public EncodeQuery(final String[] params)
53-
{}
54-
55-
public static EncodeQuery all()
5657
{
57-
return new EncodeQuery();
58+
if ((params != null) && (params.length > 0))
59+
this.params.addAll(Arrays.asList(params));
5860
}
5961

6062
public static EncodeQuery params(final String... params)
@@ -64,31 +66,26 @@ public static EncodeQuery params(final String... params)
6466

6567
public EncodeQuery excluding(final String... params)
6668
{
67-
// TODO implement
69+
if ((params != null) && (params.length > 0))
70+
this.excludedParams.addAll(Arrays.asList(params));
6871
return this;
6972
}
7073

7174
public EncodeQuery when(final Condition condition)
7275
{
73-
// TODO implement
76+
this.condition = condition;
7477
return this;
7578
}
7679

77-
public EncodeQuery usingEncoder()
80+
public EncodeQuery withEncodingStrategy(final EncodingStrategy strategy)
7881
{
79-
// TODO implement
82+
this.encodingStrategy = strategy;
8083
return this;
8184
}
8285

83-
public EncodeQuery usingDecoder()
86+
public EncodeQuery withChecksumStrategy(final ChecksumStrategy strategy)
8487
{
85-
// TODO implement
86-
return this;
87-
}
88-
89-
public EncodeQuery usingChecksumStrategy()
90-
{
91-
// TODO implement
88+
this.checksumStrategy = strategy;
9289
return this;
9390
}
9491

@@ -98,18 +95,6 @@ public EncodeQuery to(final String param)
9895
return this;
9996
}
10097

101-
@Override
102-
public String getId()
103-
{
104-
return EncodeQuery.class.getName() + this.hashCode();
105-
}
106-
107-
@Override
108-
public boolean evaluate(final Rewrite event, final EvaluationContext context)
109-
{
110-
return true;
111-
}
112-
11398
@Override
11499
public void perform(final Rewrite event, final EvaluationContext context)
115100
{
@@ -123,7 +108,7 @@ public void perform(final Rewrite event, final EvaluationContext context)
123108
String value = query.decode().getParameter(name);
124109
if (value != null)
125110
{
126-
String decoded = decode(value);
111+
String decoded = encodingStrategy.decode(value);
127112
String newUrl = in.getRequestPath() + "?" + decoded;
128113
setProcessed(event);
129114
in.forward(newUrl);
@@ -132,7 +117,7 @@ public void perform(final Rewrite event, final EvaluationContext context)
132117
// TODO enable inbound correction
133118
else if (!query.isEmpty() && DispatchType.isRequest().evaluate(event, context))
134119
{
135-
String encoded = encode(in.getRequestQueryString());
120+
String encoded = encodingStrategy.encode(in.getRequestQueryString());
136121
setProcessed(event);
137122
in.redirectTemporary(in.getContextPath() + in.getRequestPath()
138123
+ "?" + name + "=" + encoded);
@@ -150,7 +135,7 @@ else if (event instanceof HttpOutboundServletRewrite)
150135

151136
if (!query.isEmpty())
152137
{
153-
String encoded = encode(out.getRequestQueryString());
138+
String encoded = encodingStrategy.encode(out.getRequestQueryString());
154139
setProcessed(event);
155140

156141
String outboundURL = out.getOutboundURL();
@@ -177,41 +162,4 @@ public void setProcessed(final Rewrite event)
177162
((HttpServletRewrite) event).getRequest().setAttribute(STATE, EncodeQuery.class.getName());
178163
}
179164

180-
private String encode(final String query)
181-
{
182-
return Base64.encodeBytes(query.getBytes());
183-
}
184-
185-
private String decode(final String value)
186-
{
187-
return new String(Base64.decode(value));
188-
}
189-
190-
/**
191-
* Return a {@link String} containing the contents of the given {@link InputStream}
192-
*/
193-
public static String streamToString(final InputStream stream)
194-
{
195-
StringBuilder out = new StringBuilder();
196-
try {
197-
final char[] buffer = new char[0x10000];
198-
Reader in = new InputStreamReader(stream, "UTF-8");
199-
int read;
200-
do {
201-
read = in.read(buffer, 0, buffer.length);
202-
if (read > 0) {
203-
out.append(buffer, 0, read);
204-
}
205-
}
206-
while (read >= 0);
207-
}
208-
catch (UnsupportedEncodingException e) {
209-
throw new RuntimeException(e);
210-
}
211-
catch (IOException e) {
212-
throw new RuntimeException(e);
213-
}
214-
return out.toString();
215-
}
216-
217165
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* JBoss, Home of Professional Open Source
3+
* Copyright 2011, Red Hat, Inc., and individual contributors
4+
* by the @authors tag. See the copyright.txt in the distribution for a
5+
* full listing of individual contributors.
6+
*
7+
* This is free software; you can redistribute it and/or modify it
8+
* under the terms of the GNU Lesser General Public License as
9+
* published by the Free Software Foundation; either version 2.1 of
10+
* the License, or (at your option) any later version.
11+
*
12+
* This software is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15+
* Lesser General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU Lesser General Public
18+
* License along with this software; if not, write to the Free
19+
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20+
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21+
*/
22+
package com.ocpsoft.rewrite.showcase.composite;
23+
24+
/**
25+
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
26+
*
27+
*/
28+
public interface EncodingStrategy
29+
{
30+
31+
String encode(String value);
32+
33+
String decode(String value);
34+
35+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* JBoss, Home of Professional Open Source
3+
* Copyright 2011, Red Hat, Inc., and individual contributors
4+
* by the @authors tag. See the copyright.txt in the distribution for a
5+
* full listing of individual contributors.
6+
*
7+
* This is free software; you can redistribute it and/or modify it
8+
* under the terms of the GNU Lesser General Public License as
9+
* published by the Free Software Foundation; either version 2.1 of
10+
* the License, or (at your option) any later version.
11+
*
12+
* This software is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15+
* Lesser General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU Lesser General Public
18+
* License along with this software; if not, write to the Free
19+
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20+
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21+
*/
22+
package com.ocpsoft.rewrite.showcase.composite;
23+
24+
/**
25+
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
26+
*
27+
*/
28+
public class HashCodeChecksumStrategy implements ChecksumStrategy
29+
{
30+
31+
}

0 commit comments

Comments
 (0)