KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jmeter > protocol > http > modifier > URLRewritingModifier


1 // $Header: /home/cvs/jakarta-jmeter/src/protocol/http/org/apache/jmeter/protocol/http/modifier/URLRewritingModifier.java,v 1.29.2.2 2004/10/07 17:52:35 sebb Exp $
2
/*
3  * Copyright 2003-2004 The Apache Software Foundation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17 */

18
19 package org.apache.jmeter.protocol.http.modifier;
20 import java.io.Serializable JavaDoc;
21
22 import junit.framework.TestCase;
23
24 import org.apache.jmeter.config.Argument;
25 import org.apache.jmeter.config.Arguments;
26 import org.apache.jmeter.processor.PreProcessor;
27 import org.apache.jmeter.protocol.http.sampler.HTTPSampler;
28 import org.apache.jmeter.protocol.http.sampler.HTTPSamplerBase;
29 import org.apache.jmeter.protocol.http.util.HTTPArgument;
30 import org.apache.jmeter.samplers.SampleResult;
31 import org.apache.jmeter.samplers.Sampler;
32 import org.apache.jmeter.testelement.AbstractTestElement;
33 import org.apache.jmeter.testelement.property.BooleanProperty;
34 import org.apache.jmeter.threads.JMeterContext;
35 import org.apache.jmeter.threads.JMeterContextService;
36 import org.apache.jmeter.util.JMeterUtils;
37 import org.apache.oro.text.regex.MatchResult;
38 import org.apache.oro.text.regex.Pattern;
39 import org.apache.oro.text.regex.Perl5Compiler;
40 import org.apache.oro.text.regex.Perl5Matcher;
41
42 /**
43  * @author mstover
44  * @author <a HREF="mailto:jsalvata@apache.org">Jordi Salvat i Alabart</a>
45  * @version $Revision: 1.29.2.2 $ updated on $Date: 2004/10/07 17:52:35 $
46  */

47 public class URLRewritingModifier
48     extends AbstractTestElement
49     implements Serializable JavaDoc, PreProcessor
50 {
51
52     private Pattern pathExtensionEqualsQuestionmarkRegexp;
53     private Pattern pathExtensionEqualsNoQuestionmarkRegexp;
54     private Pattern parameterRegexp;
55     private Pattern pathExtensionNoEqualsQuestionmarkRegexp;
56     private Pattern pathExtensionNoEqualsNoQuestionmarkRegexp;
57     //transient Perl5Compiler compiler = new Perl5Compiler();
58
private final static String JavaDoc ARGUMENT_NAME = "argument_name";
59     private final static String JavaDoc PATH_EXTENSION = "path_extension";
60     private final static String JavaDoc PATH_EXTENSION_NO_EQUALS =
61         "path_extension_no_equals";
62     private final static String JavaDoc PATH_EXTENSION_NO_QUESTIONMARK =
63         "path_extension_no_questionmark";
64
65     public void process()
66     {
67         JMeterContext ctx = getThreadContext();
68         Sampler sampler = ctx.getCurrentSampler();
69         SampleResult responseText = ctx.getPreviousResult();
70         if(responseText == null)
71         {
72             return;
73         }
74         initRegex(getArgumentName());
75         String JavaDoc text = new String JavaDoc(responseText.getResponseData());
76         Perl5Matcher matcher = JMeterUtils.getMatcher();
77         String JavaDoc value = "";
78         if (isPathExtension() && isPathExtensionNoEquals() && isPathExtensionNoQuestionmark())
79         {
80             if (matcher.contains(text, pathExtensionNoEqualsNoQuestionmarkRegexp))
81             {
82                 MatchResult result = matcher.getMatch();
83                 value = result.group(1);
84             }
85         }
86         else if (isPathExtension() && isPathExtensionNoEquals()) // && ! isPathExtensionNoQuestionmark
87
{
88             if (matcher.contains(text, pathExtensionNoEqualsQuestionmarkRegexp))
89             {
90                 MatchResult result = matcher.getMatch();
91                 value = result.group(1);
92             }
93         }
94         else if (isPathExtension() && isPathExtensionNoQuestionmark()) // && ! isPathExtensionNoEquals
95
{
96             if (matcher.contains(text, pathExtensionEqualsNoQuestionmarkRegexp))
97             {
98                 MatchResult result = matcher.getMatch();
99                 value = result.group(1);
100             }
101         }
102         else if (isPathExtension()) // && ! isPathExtensionNoEquals && ! isPathExtensionNoQuestionmark
103
{
104             if (matcher.contains(text, pathExtensionEqualsQuestionmarkRegexp))
105             {
106                 MatchResult result = matcher.getMatch();
107                 value = result.group(1);
108             }
109         }
110         else // if ! isPathExtension()
111
{
112             if (matcher.contains(text, parameterRegexp))
113             {
114                 MatchResult result = matcher.getMatch();
115                 for (int i=1; i<result.groups(); i++)
116                 {
117                     value = result.group(i);
118                     if (value != null) break;
119                 }
120             }
121         }
122
123         modify((HTTPSamplerBase) sampler, value);
124     }
125     private void modify(HTTPSamplerBase sampler, String JavaDoc value)
126     {
127         if (isPathExtension())
128         {
129             if (isPathExtensionNoEquals())
130             {
131                 sampler.setPath(
132                     sampler.getPath() + ";" + getArgumentName() + value);
133             }
134             else
135             {
136                 sampler.setPath(
137                     sampler.getPath() + ";" + getArgumentName() + "=" + value);
138             }
139         }
140         else
141         {
142             sampler.getArguments().removeArgument(getArgumentName());
143             sampler.getArguments().addArgument(
144                 new HTTPArgument(getArgumentName(), value, true));
145         }
146     }
147     public void setArgumentName(String JavaDoc argName)
148     {
149         setProperty(ARGUMENT_NAME, argName);
150     }
151     private void initRegex(String JavaDoc argName)
152     {
153         pathExtensionEqualsQuestionmarkRegexp =
154             JMeterUtils.getPatternCache().getPattern(
155                 ";"+argName + "=([^\"'>&\\s;]*)[&\\s\"'>;]?$?",
156                 Perl5Compiler.MULTILINE_MASK | Perl5Compiler.READ_ONLY_MASK);
157
158         pathExtensionEqualsNoQuestionmarkRegexp =
159             JMeterUtils.getPatternCache().getPattern(
160                 ";"+argName + "=([^\"'>&\\s;?]*)[&\\s\"'>;?]?$?",
161                 Perl5Compiler.MULTILINE_MASK | Perl5Compiler.READ_ONLY_MASK);
162
163         pathExtensionNoEqualsQuestionmarkRegexp =
164             JMeterUtils.getPatternCache().getPattern(
165                 ";"+argName + "([^\"'>&\\s;]*)[&\\s\"'>;]?$?",
166                 Perl5Compiler.MULTILINE_MASK | Perl5Compiler.READ_ONLY_MASK);
167
168         pathExtensionNoEqualsNoQuestionmarkRegexp =
169             JMeterUtils.getPatternCache().getPattern(
170                 ";"+argName + "([^\"'>&\\s;?]*)[&\\s\"'>;?]?$?",
171                 Perl5Compiler.MULTILINE_MASK | Perl5Compiler.READ_ONLY_MASK);
172
173         parameterRegexp =
174             JMeterUtils.getPatternCache().getPattern(
175                 "[;\\?&]"+argName + "=([^\"'>&\\s;]*)[&\\s\"'>;]?$?"
176                 + "|\\s[Nn][Aa][Mm][Ee]\\s*=\\s*[\"']"
177                     + argName
178                     + "[\"']"
179                     + "[^>]*"
180                     + "\\s[vV][Aa][Ll][Uu][Ee]\\s*=\\s*[\"']"
181                     + "([^\"']*)"
182                     + "[\"']"
183                 + "|\\s[vV][Aa][Ll][Uu][Ee]\\s*=\\s*[\"']"
184                     + "([^\"']*)"
185                     + "[\"']"
186                     + "[^>]*"
187                     + "\\s[Nn][Aa][Mm][Ee]\\s*=\\s*[\"']"
188                     + argName
189                     + "[\"']",
190                 Perl5Compiler.MULTILINE_MASK | Perl5Compiler.READ_ONLY_MASK);
191             // NOTE: the handling of simple- vs. double-quotes could be formally
192
// more accurate, but I can't imagine a session id containing
193
// either, so we should be OK. The whole set of expressions is a
194
// quick hack anyway, so who cares.
195
}
196     public String JavaDoc getArgumentName()
197     {
198         return getPropertyAsString(ARGUMENT_NAME);
199     }
200     public void setPathExtension(boolean pathExt)
201     {
202         setProperty(new BooleanProperty(PATH_EXTENSION, pathExt));
203     }
204     public void setPathExtensionNoEquals(boolean pathExtNoEquals)
205     {
206         setProperty(
207             new BooleanProperty(PATH_EXTENSION_NO_EQUALS, pathExtNoEquals));
208     }
209     public void setPathExtensionNoQuestionmark(boolean pathExtNoQuestionmark)
210     {
211         setProperty(
212             new BooleanProperty(PATH_EXTENSION_NO_QUESTIONMARK, pathExtNoQuestionmark));
213     }
214     public boolean isPathExtension()
215     {
216         return getPropertyAsBoolean(PATH_EXTENSION);
217     }
218     public boolean isPathExtensionNoEquals()
219     {
220         return getPropertyAsBoolean(PATH_EXTENSION_NO_EQUALS);
221     }
222     public boolean isPathExtensionNoQuestionmark()
223     {
224         return getPropertyAsBoolean(PATH_EXTENSION_NO_QUESTIONMARK);
225     }
226     
227     // TODO: add test cases for new jakarta commons http client
228
public static class Test extends TestCase
229     {
230         private SampleResult response = null;
231         
232         private JMeterContext context = null;
233         private URLRewritingModifier mod = null;
234
235         public Test(String JavaDoc name)
236         {
237             super(name);
238         }
239         public void setUp()
240         {
241             context = JMeterContextService.getContext();
242             mod = new URLRewritingModifier();
243             mod.setThreadContext(context);
244         }
245         public void testGrabSessionId() throws Exception JavaDoc
246         {
247             String JavaDoc html =
248                 "location: http://server.com/index.html"
249                     + "?session_id=jfdkjdkf%20jddkfdfjkdjfdf%22;";
250             response = new SampleResult();
251             response.setResponseData(html.getBytes());
252             mod.setArgumentName("session_id");
253             HTTPSampler sampler = createSampler();
254             sampler.addArgument("session_id", "adfasdfdsafasdfasd");
255             context.setCurrentSampler(sampler);
256             context.setPreviousResult(response);
257             mod.process();
258             Arguments args = sampler.getArguments();
259             assertEquals(
260                 "jfdkjdkf jddkfdfjkdjfdf\"",
261                 ((Argument) args.getArguments().get(0).getObjectValue())
262                     .getValue());
263             assertEquals(
264                 "http://server.com/index.html?"
265                     + "session_id=jfdkjdkf+jddkfdfjkdjfdf%22",
266                 sampler.toString());
267         }
268         public void testGrabSessionId2() throws Exception JavaDoc
269         {
270             String JavaDoc html =
271                 "<a HREF=\"http://server.com/index.html?"
272                     + "session_id=jfdkjdkfjddkfdfjkdjfdf\">";
273             response = new SampleResult();
274             response.setResponseData(html.getBytes());
275             mod.setArgumentName("session_id");
276             HTTPSampler sampler = createSampler();
277             context.setCurrentSampler(sampler);
278             context.setPreviousResult(response);
279             mod.process();
280             Arguments args = sampler.getArguments();
281             assertEquals(
282                 "jfdkjdkfjddkfdfjkdjfdf",
283                 ((Argument) args.getArguments().get(0).getObjectValue())
284                     .getValue());
285         }
286         private HTTPSampler createSampler()
287         {
288             HTTPSampler sampler = new HTTPSampler();
289             sampler.setDomain("server.com");
290             sampler.setPath("index.html");
291             sampler.setMethod(HTTPSampler.GET);
292             sampler.setProtocol("http");
293             return sampler;
294         }
295
296         public void testGrabSessionId3() throws Exception JavaDoc
297         {
298             String JavaDoc html = "href='index.html?session_id=jfdkjdkfjddkfdfjkdjfdf'";
299             response = new SampleResult();
300             response.setResponseData(html.getBytes());
301             mod.setArgumentName("session_id");
302             HTTPSampler sampler = createSampler();
303             context.setCurrentSampler(sampler);
304             context.setPreviousResult(response);
305             mod.process();
306             Arguments args = sampler.getArguments();
307             assertEquals(
308                 "jfdkjdkfjddkfdfjkdjfdf",
309                 ((Argument) args.getArguments().get(0).getObjectValue())
310                     .getValue());
311         }
312
313         public void testGrabSessionIdEndedInTab() throws Exception JavaDoc
314         {
315             String JavaDoc html = "href='index.html?session_id=jfdkjdkfjddkfdfjkdjfdf\t";
316             response = new SampleResult();
317             response.setResponseData(html.getBytes());
318             mod.setArgumentName("session_id");
319             HTTPSampler sampler = createSampler();
320             context.setCurrentSampler(sampler);
321             context.setPreviousResult(response);
322             mod.process();
323             Arguments args = sampler.getArguments();
324             assertEquals(
325                 "jfdkjdkfjddkfdfjkdjfdf",
326                 ((Argument) args.getArguments().get(0).getObjectValue())
327                     .getValue());
328         }
329         
330         public void testGrabSessionId4() throws Exception JavaDoc
331         {
332             String JavaDoc html =
333                 "href='index.html;%24sid%24KQNq3AAADQZoEQAxlkX8uQV5bjqVBPbT'";
334             response = new SampleResult();
335             response.setResponseData(html.getBytes());
336             mod.setArgumentName("%24sid%24");
337             mod.setPathExtension(true);
338             mod.setPathExtensionNoEquals(true);
339             HTTPSampler sampler = createSampler();
340             context.setCurrentSampler(sampler);
341             context.setPreviousResult(response);
342             mod.process();
343             //Arguments args = sampler.getArguments();
344
assertEquals(
345                 "index.html;%24sid%24KQNq3AAADQZoEQAxlkX8uQV5bjqVBPbT",
346                 sampler.getPath());
347         }
348
349         public void testGrabSessionIdFromForm() throws Exception JavaDoc
350         {
351             String JavaDoc[] html = new String JavaDoc[] {
352                 "<input name=\"sid\" value=\"myId\">",
353                 "<input name='sid' value='myId'>",
354                 "<input value=\"myId\" NAME='sid'>",
355                 "<input VALUE='myId' name=\"sid\">",
356                 "<input blah blah value=\"myId\" yoda yoda NAME='sid'>",
357             };
358             for (int i=0; i<html.length; i++)
359             {
360                 response = new SampleResult();
361                 response.setResponseData(html[i].getBytes());
362                 URLRewritingModifier mod = new URLRewritingModifier();
363                 mod.setThreadContext(context);
364                 mod.setArgumentName("sid");
365                 mod.setPathExtension(false);
366                 HTTPSampler sampler = createSampler();
367                 context.setCurrentSampler(sampler);
368                 context.setPreviousResult(response);
369                 mod.process();
370                 Arguments args = sampler.getArguments();
371                 assertEquals(
372                     "For case i="+i,
373                     "myId",
374                     ((Argument) args.getArguments().get(0).getObjectValue())
375                         .getValue());
376             }
377         }
378     }
379 }
380
Popular Tags