KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > portal > event > aspect > impl > ActionCounterEventAspect


1 /*
2  * Copyright 1999-2002,2004-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.portal.event.aspect.impl;
17
18 import org.apache.avalon.framework.logger.AbstractLogEnabled;
19 import org.apache.avalon.framework.parameters.ParameterException;
20 import org.apache.avalon.framework.parameters.Parameterizable;
21 import org.apache.avalon.framework.parameters.Parameters;
22 import org.apache.avalon.framework.thread.ThreadSafe;
23 import org.apache.cocoon.environment.ObjectModelHelper;
24 import org.apache.cocoon.environment.Request;
25 import org.apache.cocoon.environment.Response;
26 import org.apache.cocoon.portal.PortalService;
27 import org.apache.cocoon.portal.event.aspect.EventAspect;
28 import org.apache.cocoon.portal.event.aspect.EventAspectContext;
29
30 /**
31  * This aspect "disables" the back button of the browser and tries to avoid
32  * problems with the user browsing in multiple windows.
33  * This event attaches a unique number to each request. For each user only the
34  * current number is "active". Every request comming in containing an older
35  * number is disregarded and therefore ignored.
36  * WARNING: This aspect solves some problems while introducing new ones. Some
37  * features of the portal do NOT work when this aspect is used.
38  *
39  * @author <a HREF="mailto:cziegeler@s-und-n.de">Carsten Ziegeler</a>
40  * @author <a HREF="mailto:volker.schmitt@basf-it-services.com">Volker Schmitt</a>
41  *
42  * @version CVS $Id: ActionCounterEventAspect.java 232222 2005-08-12 07:28:58Z cziegeler $
43  */

44 public class ActionCounterEventAspect
45     extends AbstractLogEnabled
46     implements EventAspect,
47                ThreadSafe,
48                Parameterizable {
49
50     protected final static String JavaDoc ATTRIBUTE_NAME = ActionCounterEventAspect.class.getName();
51
52     /** The name of the parameter to check */
53     protected String JavaDoc parameterName;
54
55     /* (non-Javadoc)
56      * @see org.apache.cocoon.portal.event.aspect.EventAspect#process(org.apache.cocoon.portal.event.aspect.EventAspectContext, org.apache.cocoon.portal.PortalService)
57      */

58     public void process(EventAspectContext context, PortalService service) {
59         final String JavaDoc requestParameterName = context.getAspectParameters().getParameter("parameter-name", this.parameterName);
60
61         int actionCount;
62
63         Integer JavaDoc actionValue = (Integer JavaDoc) service.getAttribute(ATTRIBUTE_NAME);
64         if (null == actionValue) {
65             actionValue = new Integer JavaDoc(0);
66             service.setAttribute(ATTRIBUTE_NAME, actionValue);
67             actionCount = 0;
68         } else {
69             actionCount = actionValue.intValue() + 1;
70             service.setAttribute(ATTRIBUTE_NAME, new Integer JavaDoc(actionCount));
71         }
72
73         final Request request = ObjectModelHelper.getRequest( context.getObjectModel() );
74         String JavaDoc value = request.getParameter( requestParameterName );
75         if ( value != null && actionCount > 0) {
76             // get number
77
int number = 0;
78             try {
79                 number = Integer.parseInt( value );
80             } catch (Exception JavaDoc ignore) {
81                 number = -1;
82             }
83
84             if ( number == actionCount - 1) {
85                 // and invoke next one
86
context.invokeNext( service );
87             }
88         }
89         service.getComponentManager().getLinkService().addUniqueParameterToLink( requestParameterName, String.valueOf(actionCount));
90         
91         final Response response = ObjectModelHelper.getResponse( context.getObjectModel() );
92         response.setHeader("Cache-Control", "no-cache");
93         response.addHeader("Cache-Control", "no-store");
94         response.setHeader("Pragma", "no-cache");
95         response.setHeader("Expires", "Thu, 01 Jan 2000 00:00:00 GMT");
96     }
97
98     /* (non-Javadoc)
99      * @see org.apache.avalon.framework.parameters.Parameterizable#parameterize(org.apache.avalon.framework.parameters.Parameters)
100      */

101     public void parameterize(Parameters parameters)
102     throws ParameterException {
103         this.parameterName = parameters.getParameter("parameter-name", "cocoon-portal-action");
104     }
105 }
106
Popular Tags