KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > server > webapp > RewriteInvocation


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.server.webapp;
31
32 import com.caucho.config.*;
33 import com.caucho.config.types.InitProgram;
34 import com.caucho.server.dispatch.ErrorFilterChain;
35 import com.caucho.server.dispatch.ForwardFilterChain;
36 import com.caucho.server.dispatch.Invocation;
37 import com.caucho.server.dispatch.MovedFilterChain;
38 import com.caucho.server.dispatch.RedirectFilterChain;
39 import com.caucho.server.dispatch.ServletConfigImpl;
40 import com.caucho.util.L10N;
41
42 import javax.annotation.PostConstruct;
43 import javax.servlet.FilterChain JavaDoc;
44 import javax.servlet.ServletException JavaDoc;
45 import javax.servlet.ServletRequest JavaDoc;
46 import javax.servlet.ServletResponse JavaDoc;
47 import javax.servlet.http.HttpServletResponse JavaDoc;
48 import java.util.ArrayList JavaDoc;
49 import java.util.logging.Level JavaDoc;
50 import java.util.logging.Logger JavaDoc;
51 import java.util.regex.Matcher JavaDoc;
52 import java.util.regex.Pattern JavaDoc;
53
54 /**
55  * Configuration for a rewrite-url
56  */

57 public class RewriteInvocation {
58   private static final L10N L = new L10N(RewriteInvocation.class);
59   private static final Logger JavaDoc log
60     = Logger.getLogger(RewriteInvocation.class.getName());
61
62   private final static FilterChain ACCEPT_CHAIN;
63
64   private final WebApp _webApp;
65
66   private final ArrayList JavaDoc<Program> _programList = new ArrayList JavaDoc<Program>();
67
68   public RewriteInvocation()
69   {
70     _webApp = null;
71   }
72
73   public RewriteInvocation(WebApp webApp)
74   {
75     _webApp = webApp;
76   }
77   
78   /**
79    * Adds an accept
80    */

81   public void addDispatch(Accept accept)
82   {
83     _programList.add(accept);
84   }
85
86   /**
87    * Adds a load-balance
88    */

89   public LoadBalance createLoadBalance()
90   {
91     if (_webApp == null)
92       throw new ConfigException(L.l("<load-balance> requires a web-app. Host-based <rewrite-dispatch> can not use <load-balance>."));
93
94     LoadBalance loadBalance = new LoadBalance(_webApp);
95     
96     _programList.add(loadBalance);
97
98     return loadBalance;
99   }
100
101   /**
102    * Adds a moved-permanently
103    */

104   public void addMovedPermanently(Moved moved)
105   {
106     moved.setStatusCode(301);
107     
108     _programList.add(moved);
109   }
110
111   /**
112    * Adds a forward.
113    */

114   public Forward createForward()
115   {
116     return new Forward();
117   }
118
119   /**
120    * Adds a forward.
121    */

122   public void addForward(Forward forward)
123   {
124     _programList.add(forward);
125   }
126
127   /**
128    * Adds a forbidden.
129    */

130   public Error JavaDoc createForbidden()
131   {
132     Error JavaDoc error = new Error JavaDoc(HttpServletResponse.SC_FORBIDDEN);
133     
134     _programList.add(error);
135
136     return error;
137   }
138
139   /**
140    * Adds a gone.
141    */

142   public Error JavaDoc createGone()
143   {
144     Error JavaDoc error = new Error JavaDoc(HttpServletResponse.SC_GONE);
145     
146     _programList.add(error);
147
148     return error;
149   }
150
151   /**
152    * Adds a not-found.
153    */

154   public Error JavaDoc createNotFound()
155   {
156     Error JavaDoc error = new Error JavaDoc(HttpServletResponse.SC_NOT_FOUND);
157     
158     _programList.add(error);
159
160     return error;
161   }
162
163   /**
164    * Adds a rewrite
165    */

166   public void addRewrite(Rewrite rewrite)
167   {
168     _programList.add(rewrite);
169   }
170
171   /**
172    * Adds a redirect.
173    */

174   public void addRedirect(Redirect redirect)
175   {
176     _programList.add(redirect);
177   }
178
179   public FilterChain map(String JavaDoc uri, Invocation invocation)
180     throws ServletException JavaDoc
181   {
182     for (int i = 0; i < _programList.size(); i++) {
183       Program program = _programList.get(i);
184       
185       uri = program.rewrite(uri);
186
187       FilterChain chain = program.dispatch(uri);
188
189       if (chain == ACCEPT_CHAIN)
190     return null;
191       else if (chain != null)
192     return chain;
193     }
194     
195     return null;
196   }
197
198   static class Program {
199     public String JavaDoc rewrite(String JavaDoc uri)
200     {
201       return uri;
202     }
203     
204     public FilterChain dispatch(String JavaDoc uri)
205       throws ServletException JavaDoc
206     {
207       return null;
208     }
209   }
210
211   public static class Rewrite extends Program {
212     private Pattern JavaDoc _regexp;
213     private String JavaDoc _replacement;
214
215     public String JavaDoc getTagName()
216     {
217       return "rewrite";
218     }
219
220     /**
221      * Sets the regular expression.
222      */

223     public void setRegexp(String JavaDoc regexp)
224     {
225       _regexp = Pattern.compile(regexp);
226     }
227
228     /**
229      * Sets the target.
230      */

231     public void setReplacement(String JavaDoc replacement)
232     {
233       _replacement = replacement;
234     }
235
236     /**
237      * Init
238      */

239     @PostConstruct
240     public void init()
241       throws ConfigException
242     {
243       if (_regexp == null)
244     throw new ConfigException(L.l("{0} needs 'regexp' attribute.",
245                       getTagName()));
246       if (_replacement == null)
247     throw new ConfigException(L.l("{0} needs 'replacement' attribute.",
248                       getTagName()));
249     }
250     
251     public String JavaDoc rewrite(String JavaDoc uri)
252     {
253       Matcher JavaDoc matcher = _regexp.matcher(uri);
254
255       if (matcher.find()) {
256     matcher.reset();
257     return matcher.replaceAll(_replacement);
258       }
259       else
260     return uri;
261     }
262   }
263
264   public static class Accept extends Program {
265     private Pattern JavaDoc _regexp;
266
267     public String JavaDoc getTagName()
268     {
269       return "accept";
270     }
271
272     /**
273      * Sets the regular expression.
274      */

275     public void setRegexp(String JavaDoc regexp)
276     {
277       _regexp = Pattern.compile(regexp);
278     }
279
280     /**
281      * Init
282      */

283     @PostConstruct
284     public void init()
285       throws ConfigException
286     {
287       if (_regexp == null)
288     throw new ConfigException(L.l("{0} needs 'regexp' attribute.",
289                       getTagName()));
290     }
291
292     public FilterChain dispatch(String JavaDoc uri)
293     {
294       Matcher JavaDoc matcher = _regexp.matcher(uri);
295
296       if (matcher.find())
297     return ACCEPT_CHAIN;
298       else
299     return null;
300     }
301   }
302
303   public static class Redirect extends Program {
304     private String JavaDoc _target;
305     private Pattern JavaDoc _regexp;
306
307     /**
308      * Sets the regular expression.
309      */

310     public void setRegexp(String JavaDoc regexp)
311     {
312       _regexp = Pattern.compile(regexp);
313     }
314
315     public void setTarget(String JavaDoc target)
316     {
317       _target = target;
318     }
319
320     public FilterChain dispatch(String JavaDoc uri)
321     {
322       Matcher JavaDoc matcher = _regexp.matcher(uri);
323
324       if (matcher.find()) {
325     matcher.reset();
326     uri = matcher.replaceAll(_target);
327
328     return new RedirectFilterChain(uri);
329       }
330       else
331     return null;
332     }
333
334     @PostConstruct
335     public void init()
336       throws ConfigException
337     {
338       if (_regexp == null)
339     throw new ConfigException(L.l("redirect needs 'regexp' attribute."));
340       if (_target == null)
341     throw new ConfigException(L.l("redirect needs 'target' attribute."));
342     }
343   }
344
345   public static class Moved extends Program {
346     private int _code = 302;
347     
348     private String JavaDoc _target;
349     private Pattern JavaDoc _regexp;
350
351     /**
352      * Sets the regular expression.
353      */

354     public void setRegexp(String JavaDoc regexp)
355     {
356       _regexp = Pattern.compile(regexp);
357     }
358
359     public void setTarget(String JavaDoc target)
360     {
361       _target = target;
362     }
363
364     /**
365      * Sets the redirect code.
366      */

367     public void setStatusCode(int code)
368     {
369       _code = code;
370     }
371
372     public FilterChain dispatch(String JavaDoc uri)
373     {
374       Matcher JavaDoc matcher = _regexp.matcher(uri);
375
376       if (matcher.find()) {
377     matcher.reset();
378     uri = matcher.replaceAll(_target);
379
380     return new MovedFilterChain(_code, uri);
381       }
382       else
383     return null;
384     }
385
386     @PostConstruct
387     public void init()
388       throws ConfigException
389     {
390       if (_regexp == null)
391     throw new ConfigException(L.l("moved needs 'regexp' attribute."));
392       if (_target == null)
393     throw new ConfigException(L.l("moved needs 'target' attribute."));
394     }
395   }
396
397   public static class LoadBalance extends Program {
398     private final WebApp _webApp;
399     
400     private Pattern JavaDoc _regexp;
401     private ServletConfigImpl _servlet;
402
403     private BuilderProgramContainer _program = new BuilderProgramContainer();
404
405     LoadBalance(WebApp webApp)
406     {
407       _webApp = webApp;
408     }
409
410     /**
411      * Sets the regular expression.
412      */

413     public void setRegexp(String JavaDoc regexp)
414     {
415       _regexp = Pattern.compile(regexp);
416     }
417
418     public void addBuilderProgram(BuilderProgram program)
419     {
420       _program.addProgram(program);
421     }
422
423     public FilterChain dispatch(String JavaDoc uri)
424       throws ServletException JavaDoc
425     {
426       Matcher JavaDoc matcher = _regexp.matcher(uri);
427
428       if (matcher.find()) {
429     return _servlet.createServletChain();
430       }
431       else
432     return null;
433     }
434
435     @PostConstruct
436     public void init()
437       throws ConfigException, ServletException JavaDoc
438     {
439       if (_regexp == null)
440     throw new ConfigException(L.l("load-balance needs 'regexp' attribute."));
441
442       try {
443     _servlet = new ServletConfigImpl();
444
445     _servlet.setServletName("resin-dispatch-lb");
446     Class JavaDoc cl = Class.forName("com.caucho.servlets.LoadBalanceServlet");
447     _servlet.setServletClass("com.caucho.servlets.LoadBalanceServlet");
448
449     _servlet.setInit(new InitProgram(_program));
450
451     _webApp.addServlet(_servlet);
452       } catch (ClassNotFoundException JavaDoc e) {
453     log.log(Level.FINER, e.toString(), e);
454     
455     throw new ConfigException(L.l("load-balance requires Resin Professional"));
456       }
457     }
458   }
459
460   public class Forward extends Program {
461     private String JavaDoc _target;
462     private Pattern JavaDoc _regexp;
463
464     /**
465      * Sets the regular expression.
466      */

467     public void setRegexp(String JavaDoc regexp)
468     {
469       _regexp = Pattern.compile(regexp);
470     }
471
472     public void setTarget(String JavaDoc target)
473     {
474       _target = target;
475     }
476
477     public FilterChain dispatch(String JavaDoc uri)
478     {
479       Matcher JavaDoc matcher = _regexp.matcher(uri);
480
481       if (matcher.find()) {
482     matcher.reset();
483     
484     String JavaDoc targetUri = matcher.replaceAll(_target);
485
486     FilterChain chain = new ForwardFilterChain(targetUri);
487
488     return chain;
489       }
490       else
491     return null;
492     }
493
494     @PostConstruct
495     public void init()
496       throws ConfigException
497     {
498       if (_regexp == null)
499     throw new ConfigException(L.l("redirect needs 'regexp' attribute."));
500       if (_target == null)
501     throw new ConfigException(L.l("redirect needs 'target' attribute."));
502     }
503   }
504
505   public static class Error extends Program {
506     private int _code;
507     private Pattern JavaDoc _regexp;
508
509     Error(int code)
510     {
511       _code = code;
512     }
513
514     /**
515      * Sets the regular expression.
516      */

517     public void setRegexp(String JavaDoc regexp)
518     {
519       _regexp = Pattern.compile(regexp);
520     }
521
522     public FilterChain dispatch(String JavaDoc uri)
523     {
524       Matcher JavaDoc matcher = _regexp.matcher(uri);
525
526       if (matcher.find()) {
527     matcher.reset();
528
529     return new ErrorFilterChain(_code);
530       }
531       else
532     return null;
533     }
534
535     @PostConstruct
536     public void init()
537       throws ConfigException
538     {
539       if (_regexp == null)
540     throw new ConfigException(L.l("error needs 'regexp' attribute."));
541     }
542   }
543
544   static {
545     ACCEPT_CHAIN = new FilterChain() {
546     public void doFilter(ServletRequest JavaDoc req, ServletResponse JavaDoc res) {}
547       };
548   }
549 }
550   
551
Popular Tags