View Javadoc

1   /*
2    * Copyright (c) 2006-2007 Creative Sphere Limited.
3    * All rights reserved. This program and the accompanying materials
4    * are made available under the terms of the Eclipse Public License v1.0
5    * which accompanies this distribution, and is available at
6    * http://www.eclipse.org/legal/epl-v10.html
7    *
8    * Contributors:
9    *
10   *   Creative Sphere - initial API and implementation
11   *
12   */
13  package org.abstracthorizon.danube.http.logging;
14  
15  import org.abstracthorizon.danube.connection.Connection;
16  import org.abstracthorizon.danube.http.HTTPConnection;
17  import org.abstracthorizon.danube.support.logging.patternsupport.PatternProcessor;
18  import org.abstracthorizon.danube.support.logging.util.StringUtil;
19  
20  import java.util.ArrayList;
21  
22  /**
23   * This processor adds following pattern codes:
24   *
25   * <ul>
26   * <li><code>%b</code> - bytes sent excluding headers or &quot;-&quot; if nothing</li>
27   * <li><code>%B</code> - bytes sent excluding headers or 0 if nothing</li>
28   * <li><code>%H</code> - request protocol</li>
29   * <li><code>%l</code> - always &quot;-&quot;</li>
30   * <li><code>%m</code> - request method</li>
31   * <li><code>%q</code> - query string</li>
32   * <li><code>%r</code> - first line of the request</li>
33   * <li><code>%s</code> - status code of the response</li>
34   * <li><code>%>s</code> - status code of the response (same as %s)</li>
35   * <li><code>%S</code> - user session ID (not implemented yet)</li>
36   * <li><code>%u</code> - remote authenticated user (not implemented yet)</li>
37   * <li><code>%U</code> - requested url path</li>
38   * <li><code>%v</code> - server local name (not implemented yet)</li>
39   * </ul>
40   *
41   * @author Daniel Sendula
42   */
43  public class HTTPPatternProcessor implements PatternProcessor {
44  
45      /** Cached index of bytes sent */
46      protected int bytesSentIndex = -1;
47  
48      /** Cached index of bytes sent with zero */
49      protected int bytesSent0Index = -1;
50  
51  
52      protected int requestProtocolIndex = -1;
53  
54  
55      protected int lIndex = -1;
56  
57      protected int requestMethodIndex = -1;
58  
59      protected int queryStringiIndex = -1;
60  
61      protected int firstLineOfRequestIndex = -1;
62  
63      protected int responseStatusIndex = -1;
64  
65      protected int requestedURLIndex = -1;
66  
67      protected int remoteUserIndex = -1;
68  
69      protected Pair[] requestHeaders;
70      protected Pair[] cookies;
71  
72      /**
73       * Constructor
74       */
75      public HTTPPatternProcessor() {
76      }
77  
78      /**
79       * Checks if parameters are present and if so replaces it and caches their indexes
80       * @param index next index to be used
81       * @param message message to be altered
82       */
83      public int init(int index, StringBuffer message) {
84          bytesSentIndex = -1;
85          bytesSent0Index = -1;
86          requestProtocolIndex = -1;
87          lIndex = -1;
88          requestMethodIndex = -1;
89          queryStringiIndex = -1;
90          firstLineOfRequestIndex = -1;
91          responseStatusIndex = -1;
92          requestedURLIndex = -1;
93          remoteUserIndex = -1;
94          requestHeaders = null;
95          cookies = null;
96  
97          if (message.indexOf("%b") >= 0) {
98              StringUtil.replaceAll(message, "%b", "{" + index + "}");
99              bytesSentIndex = index;
100             index = index + 1;
101         }
102 
103         if (message.indexOf("%B") >= 0) {
104             StringUtil.replaceAll(message, "%B", "{" + index + "}");
105             bytesSent0Index = index;
106             index = index + 1;
107         }
108 
109         if (message.indexOf("%H") >= 0) {
110             StringUtil.replaceAll(message, "%H", "{" + index + "}");
111             requestProtocolIndex = index;
112             index = index + 1;
113         }
114 
115         if (message.indexOf("%l") >= 0) {
116             StringUtil.replaceAll(message, "%l", "{" + index + "}");
117             lIndex = index;
118             index = index + 1;
119         }
120 
121         if (message.indexOf("%m") >= 0) {
122             StringUtil.replaceAll(message, "%m", "{" + index + "}");
123             requestMethodIndex = index;
124             index = index + 1;
125         }
126 
127         if (message.indexOf("%q") >= 0) {
128             StringUtil.replaceAll(message, "%q", "{" + index + "}");
129             queryStringiIndex = index;
130             index = index + 1;
131         }
132 
133         if (message.indexOf("%r") >= 0) {
134             StringUtil.replaceAll(message, "%r", "{" + index + "}");
135             firstLineOfRequestIndex = index;
136             index = index + 1;
137         }
138 
139         if ((message.indexOf("%s") >= 0) || (message.indexOf("%>s") >= 0)) {
140             StringUtil.replaceAll(message, "%s", "{" + index + "}");
141             StringUtil.replaceAll(message, "%>s", "{" + index + "}");
142             responseStatusIndex = index;
143             index = index + 1;
144         }
145 
146         if (message.indexOf("%U") >= 0) {
147             StringUtil.replaceAll(message, "%U", "{" + index + "}");
148             requestedURLIndex = index;
149             index = index + 1;
150         }
151 
152         if (message.indexOf("%u") >= 0) {
153             StringUtil.replaceAll(message, "%u", "{" + index + "}");
154             remoteUserIndex = index;
155             index = index + 1;
156         }
157 
158         int i = message.indexOf("%{");
159         if (i >= 0) {
160             ArrayList<Pair> requestHeaders = new ArrayList<Pair>();
161             ArrayList<Pair> cookies = new ArrayList<Pair>();
162             while (i >= 0) {
163                 int j = message.indexOf("}", i);
164                 if ((j > i) && (message.length() > j + 1)) {
165                     char c = message.charAt(j + 1);
166                     String name = message.substring(i + 2, j);
167                     Pair pair = new Pair();
168                     pair.name = name;
169                     pair.fieldIndex = index;
170                     boolean ok = true;
171                     if (c == 'i') {
172                         requestHeaders.add(pair);
173                     } else if (c == 'c') {
174                         cookies.add(pair);
175                     } else {
176                         ok = false;
177                     }
178                     if (ok) {
179                         message.replace(i, j+2, "{" + index + "}");
180                         index = index + 1;
181                     }
182                     i = message.indexOf("%{", i + 1);
183                 } else {
184                     i = -1;
185                 }
186             }
187             if (requestHeaders.size() > 0) {
188                 this.requestHeaders = new Pair[requestHeaders.size()];
189                 this.requestHeaders = requestHeaders.toArray(this.requestHeaders);
190             }
191             if (cookies.size() > 0) {
192                 this.cookies = new Pair[cookies.size()];
193                 this.cookies = cookies.toArray(this.cookies);
194             }
195         }
196 
197         return index;
198     }
199 
200     /**
201      * Adds parameter values to cached index positions
202      * @param connection connection
203      * @param array array
204      */
205     public void process(Connection connection, Object[] array) {
206         HTTPConnection httpConnection = (HTTPConnection)connection.adapt(HTTPConnection.class);
207 
208         if (bytesSentIndex >= 0) {
209             array[bytesSentIndex] = "-";
210         }
211         if (bytesSent0Index >= 0) {
212             array[bytesSent0Index] = 0;
213         }
214         if (requestProtocolIndex >= 0) {
215             array[requestProtocolIndex] = httpConnection.getRequestProtocol();
216         }
217         if (lIndex >= 0) {
218             array[lIndex] = "-";
219         }
220         if (requestMethodIndex >= 0) {
221             array[requestMethodIndex] = httpConnection.getRequestMethod();
222         }
223         if (queryStringiIndex >= 0) {
224             String q = httpConnection.getRequestURI();
225             int i = q.indexOf('?');
226             if (i >= 0) {
227                 q = q.substring(i);
228             } else {
229                 q = "-";
230             }
231             array[queryStringiIndex] = q;
232         }
233         if (firstLineOfRequestIndex >= 0) {
234             array[firstLineOfRequestIndex] = httpConnection.getRequestMethod() + " " + httpConnection.getRequestURI() + " " + httpConnection.getRequestProtocol();
235         }
236         if (responseStatusIndex >= 0) {
237             array[responseStatusIndex] = httpConnection.getResponseStatus().getCode();
238         }
239         if (requestedURLIndex >= 0) {
240             String q = httpConnection.getRequestURI();
241             int i = q.indexOf('?');
242             if (i >= 0) {
243                 q = q.substring(0, i);
244             }
245             array[requestedURLIndex] = q;
246         }
247         if (remoteUserIndex >= 0) {
248             array[remoteUserIndex] = "-";
249         }
250         if (requestHeaders.length > 0) {
251             for (Pair pair : requestHeaders) {
252                 String value = httpConnection.getRequestHeaders().getFirst(pair.name);
253                 if ((value == null) || (value.length() == 0)) {
254                     value = "";
255                 }
256                 array[pair.fieldIndex] = value;
257             }
258         }
259     }
260 
261     protected static class Pair {
262         public String name;
263         public int fieldIndex;
264     }
265 
266 }