2121 */
2222package 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
3028import 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 ;
3231import com .ocpsoft .rewrite .context .EvaluationContext ;
3332import com .ocpsoft .rewrite .event .Rewrite ;
3433import com .ocpsoft .rewrite .servlet .config .DispatchType ;
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}
0 commit comments