01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17:
18: package org.apache.cocoon;
19:
20: import org.apache.avalon.framework.logger.AbstractLogEnabled;
21: import org.apache.cocoon.environment.Environment;
22: import org.apache.cocoon.environment.ObjectModelHelper;
23: import org.apache.cocoon.environment.Request;
24:
25: /**
26: * This is called from with {@link Cocoon#process(Environment)}, before any
27: * requests are passed onto the {@link Processor} and after the request has been
28: * processed. It allows all requests to be logged or monitored.
29: * NB Cocoon does not require an instance of this Component to function, but if
30: * there is one it will be used.
31: */
32: public class SampleRequestListener extends AbstractLogEnabled implements
33: RequestListener {
34:
35: /**
36: * <p>In this method you can call, for example:
37: * <code>Request req=ObjectModelHelper.getRequest(env.getObjectModel());</code>
38: * And then, you could use the following:
39: * <ul>
40: * <li>req.getRequestURI()</li>
41: * <li>req.getQueryString()</li>
42: * <li>req.getSession().getId()</li>
43: * <li>req.getLocale().getLanguage().toString()</li>
44: * </ul>
45: * <p>
46: * @param environment as supplied to {@link Processor#process(Environment)}
47: * from within {@link Cocoon#process(Environment)}.
48: */
49: public void onRequestStart(Environment environment) {
50: Request req = ObjectModelHelper.getRequest(environment
51: .getObjectModel());
52: getLogger().info(req.getRequestURI() + " started");
53: }
54:
55: /**
56: * <p>This method is called when a request has completed. This method is called before the
57: * response is committed.
58: * @param environment as supplied to {@link Processor#process(Environment)}
59: * from within {@link Cocoon#process(Environment)}.
60: */
61: public void onRequestEnd(Environment environment) {
62: Request req = ObjectModelHelper.getRequest(environment
63: .getObjectModel());
64: getLogger().info(req.getRequestURI() + " ended");
65: }
66:
67: /**
68: * <p>This method is called when an exception has occurred processing the request.
69: * @param environment as supplied to {@link Processor#process(Environment)}
70: * from within {@link Cocoon#process(Environment)}.
71: * @param throwable the error that occurred processing the request.
72: */
73: public void onRequestException(Environment environment,
74: Throwable throwable) {
75: Request req = ObjectModelHelper.getRequest(environment
76: .getObjectModel());
77: getLogger().info(
78: req.getRequestURI() + " failed with "
79: + throwable.getMessage());
80: }
81: }
|