View Javadoc

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