KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > webapp > balancer > rules > CharacterEncodingRule


1 package org.apache.webapp.balancer.rules;
2
3 import javax.servlet.http.HttpServletRequest JavaDoc;
4
5
6 /**
7  * This rule redirects requests if they
8  * are for a specific character encoding.
9  *
10  * @author Yoav Shapira
11  */

12 public class CharacterEncodingRule extends BaseRule {
13     /**
14      * The character encoding.
15      */

16     private String JavaDoc encoding;
17
18     /**
19      * Sets the character encoding.
20      *
21      * @param theEncoding The encoding value
22      */

23     public void setEncoding(String JavaDoc theEncoding) {
24         if (theEncoding == null) {
25             throw new IllegalArgumentException JavaDoc("The encoding cannot be null.");
26         } else {
27             encoding = theEncoding;
28         }
29     }
30
31     /**
32      * Returns the desired encoding.
33      *
34      * @return String
35      */

36     protected String JavaDoc getEncoding() {
37         return encoding;
38     }
39
40     /**
41      * @see org.apache.webapp.balancer.Rule#matches(HttpServletRequest request)
42      */

43     public boolean matches(HttpServletRequest JavaDoc request) {
44         String JavaDoc actualEncoding = request.getCharacterEncoding();
45
46         return (getEncoding().compareTo(actualEncoding) == 0);
47     }
48
49     /**
50      * Returns a String representation of this object.
51      *
52      * @return String
53      */

54     public String JavaDoc toString() {
55         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
56
57         buffer.append("[");
58         buffer.append(getClass().getName());
59         buffer.append(": ");
60
61         buffer.append("Target encoding: ");
62         buffer.append(getEncoding());
63         buffer.append(" / ");
64
65         buffer.append("Redirect URL: ");
66         buffer.append(getRedirectUrl());
67
68         buffer.append("]");
69
70         return buffer.toString();
71     }
72 }
73
74
75 // End of file: CharacterEncodingRule.java
76
Popular Tags