View Javadoc

1   /**
2    * Logback: the generic, reliable, fast and flexible logging framework.
3    * 
4    * Copyright (C) 2000-2008, QOS.ch
5    * 
6    * This library is free software, you can redistribute it and/or modify it under
7    * the terms of the GNU Lesser General Public License as published by the Free
8    * Software Foundation.
9    */
10  package ch.qos.logback.access.sift;
11  
12  import javax.servlet.http.HttpServletRequest;
13  import javax.servlet.http.HttpSession;
14  
15  import ch.qos.logback.access.spi.AccessEvent;
16  import ch.qos.logback.core.sift.Discriminator;
17  import ch.qos.logback.core.spi.ContextAwareBase;
18  
19  /**
20   * 
21   * AccessEventDiscriminator's job is to return the value of a designated field
22   * in an {@link AccessEvent} instance.
23   * 
24   * <p>The field is specified via the {@link FieldName} property.
25   * 
26   * @author Ceki G&uuml;lc&uuml;
27   * 
28   */
29  public class AccessEventDiscriminator extends ContextAwareBase implements
30      Discriminator<AccessEvent> {
31  
32    boolean started = false;
33  
34    /**
35     * At present time the followed fields can be designated: COOKIE,
36     * REQUEST_ATTRIBUTE, SESSION_ATTRIBUTE, REMOTE_ADDRESS,
37     * LOCAL_PORT,REQUEST_URI
38     * 
39     * <p> The first three fields require an additional key. For the
40     * SESSION_ATTRIBUTE field, the additional key named "id" has special meaning
41     * as it is mapped to the session id of the current http request.
42     */
43    public enum FieldName {
44      COOKIE, REQUEST_ATTRIBUTE, SESSION_ATTRIBUTE, REMOTE_ADDRESS, LOCAL_PORT, REQUEST_URI
45    }
46  
47    String defaultValue;
48    String key;
49    FieldName fieldName;
50    String additionalKey;
51  
52    public String getDiscriminatingValue(AccessEvent acccessEvent) {
53      String rawValue = getRawDiscriminatingValue(acccessEvent);
54      if (rawValue == null || rawValue.length() == 0) {
55        return defaultValue;
56      } else {
57        return rawValue;
58      }
59    }
60  
61    public String getRawDiscriminatingValue(AccessEvent acccessEvent) {
62      switch (fieldName) {
63      case COOKIE:
64        // tested
65        return acccessEvent.getCookie(additionalKey);
66      case LOCAL_PORT:
67        return String.valueOf(acccessEvent.getLocalPort());
68      case REQUEST_ATTRIBUTE:
69        // tested
70        return getRequestAttribute(acccessEvent);
71      case SESSION_ATTRIBUTE:
72        return getSessionAttribute(acccessEvent);
73      case REMOTE_ADDRESS:
74        return acccessEvent.getRemoteAddr();
75      case REQUEST_URI:
76        // tested
77        return getRequestURI(acccessEvent);
78      default:
79        return null;
80      }
81    }
82  
83    private String getRequestAttribute(AccessEvent acccessEvent) {
84      String attr = acccessEvent.getAttribute(additionalKey);
85      if (AccessEvent.NA.equals(attr)) {
86        return null;
87      } else {
88        return attr;
89      }
90    }
91  
92    private String getRequestURI(AccessEvent acccessEvent) {
93      String uri = acccessEvent.getRequestURI();
94      if (uri != null && uri.length() >= 1 && uri.charAt(0) == '/') {
95        return uri.substring(1);
96      } else {
97        return uri;
98      }
99    }
100 
101   private String getSessionAttribute(AccessEvent acccessEvent) {
102     HttpServletRequest req = acccessEvent.getRequest();
103     if (req != null) {
104       HttpSession session = req.getSession(false);
105       if (session != null) {
106         if ("id".equalsIgnoreCase(additionalKey)) {
107           return session.getId();
108         } else {
109           Object v = session.getAttribute(additionalKey);
110           if (v != null) {
111             return v.toString();
112           }
113         }
114       }
115     }
116     return null;
117   }
118 
119   public boolean isStarted() {
120     return started;
121   }
122 
123   public void start() {
124 
125     int errorCount = 0;
126 
127     if (defaultValue == null) {
128       addError("\"DefaultValue\" property must be set.");
129     }
130     if (fieldName == null) {
131       addError("\"FieldName\" property must be set.");
132       errorCount++;
133     }
134 
135     switch (fieldName) {
136     case SESSION_ATTRIBUTE:
137     case REQUEST_ATTRIBUTE:
138     case COOKIE:
139       if (additionalKey == null) {
140         addError("\"OptionalKey\" property is mandatory for field name "
141             + fieldName.toString());
142         errorCount++;
143       }
144     }
145 
146     if (errorCount == 0) {
147       started = true;
148     }
149   }
150 
151   public void stop() {
152     started = false;
153   }
154 
155   public void setFieldName(FieldName fieldName) {
156     this.fieldName = fieldName;
157   }
158 
159   public FieldName getFieldName() {
160     return fieldName;
161   }
162 
163   public String getAdditionalKey() {
164     return additionalKey;
165   }
166 
167   public void setAdditionalKey(String additionalKey) {
168     this.additionalKey = additionalKey;
169   }
170 
171   /**
172    * @see #setDefaultValue(String)
173    * @return
174    */
175   public String getDefaultValue() {
176     return defaultValue;
177   }
178 
179   /**
180    * The default value returned by this discriminator in case it cannot compute
181    * the discriminating value from the access event.
182    * 
183    * @param defaultValue
184    */
185   public void setDefaultValue(String defaultValue) {
186     this.defaultValue = defaultValue;
187   }
188 
189   public String getKey() {
190     return key;
191   }
192 
193   public void setKey(String key) {
194     this.key = key;
195   }
196 
197 }