thanks maurice!

johan do you want to merge this into the one in the core you wrote?

-igor


On 6/23/06, Maurice Marrink < [EMAIL PROTECTED]> wrote:
Hi,

I recently needed the ServerAndClientTimeFilter to also show the
server time of ajax calls so i made the following modifications. Note
that i took the source of 1.2 rc3 for this. Also note that this code
is likely not going to work with dojo requests but it should not break
them. Anyway here is the code, use it as you like.

Maurice

package wicket.markup.html;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory ;

import wicket.Application;
import wicket.IResponseFilter;
import wicket.RequestCycle;
import wicket.Session;
import wicket.model.Model;
import wicket.util.string.AppendingStringBuffer;
import wicket.util.string._javascript_Utils ;

/**
* This is a filter that injects _javascript_ code to the top head portion and
* after the body so that the time can me measured what the client parse time
* was for this page. It also reports the total server parse/response time in
* the client and logs the server response time and response size it took for a
* specific response in the server log.
*
* You can specify what the status text should be like this:
* ServerAndClientTimeFilter.statustext=My Application, Server parsetime:
* ${servertime}, Client parsetime: ${clienttime}
* likewise for ajax request use ajax.ServerAndClientTimeFilter.statustext
*
* @author jcompagner
*/
public class ServerAndClientTimeFilter implements IResponseFilter
{
        private static Log log = LogFactory.getLog(ServerAndClientTimeFilter.class);

        /**
         * @see wicket.IResponseFilter#filter(java.lang.StringBuffer)
         */
        public AppendingStringBuffer filter(AppendingStringBuffer responseBuffer)
        {
                int headIndex = responseBuffer.indexOf("<head>");
                int bodyIndex = responseBuffer.indexOf("</body>");
                int ajaxStart=responseBuffer.indexOf ("<ajax-response>");
                int ajaxEnd=responseBuffer.indexOf("</ajax-response>");
                long timeTaken = System.currentTimeMillis() -
RequestCycle.get().getStartTime();
                if(headIndex != -1 && bodyIndex != -1)
                {
                        AppendingStringBuffer endScript = new AppendingStringBuffer(150);
                        endScript.append ("\n").append(_javascript_Utils.SCRIPT_OPEN_TAG);
                        endScript.append("\nwindow.defaultStatus='");
                        endScript.append(getStatusString(timeTaken,
" ServerAndClientTimeFilter.statustext"));
                        endScript.append("';\n").append(_javascript_Utils.SCRIPT_CLOSE_TAG).append("\n");
                        responseBuffer.insert (bodyIndex - 1, endScript);
                        responseBuffer.insert(headIndex + 6, "\n" + _javascript_Utils.SCRIPT_OPEN_TAG
                                        + "\nvar clientTimeVariable = new Date().getTime();\n"
                                        + _javascript_Utils.SCRIPT_CLOSE_TAG + "\n");
                }
                else if(ajaxStart !=-1 && ajaxEnd!=-1)
                {
                        AppendingStringBuffer startScript = new AppendingStringBuffer(250);
                        startScript.append("<evaluate><![CDATA[window.defaultStatus='");
                        startScript.append(getStatusString(timeTaken,
"ajax.ServerAndClientTimeFilter.statustext "));
                        startScript.append("';]]></evaluate>");
                        responseBuffer.insert(ajaxEnd,startScript.toString());
                        responseBuffer.insert (ajaxStart+15,
"<evaluate><![CDATA[clientTimeVariable = new
Date().getTime();]]></evaluate>");
                }
                log.info( timeTaken + "ms server time taken for request " +
RequestCycle.get().getRequest().getURL() + " response size: " +
responseBuffer.length());
                return responseBuffer;
        }

        /**
         * Returns a locale specific status message about the server and client time.
         * @param timeTaken the server time it took
         * @param resourceKey The key for the locale specific string lookup
         * @return String with the status message
         */
        private String getStatusString(long timeTaken, String resourceKey)
        {
                Map<String,String> map = new HashMap<String,String>(4);
                map.put("clienttime", "' + (new Date().getTime() -
clientTimeVariable)/1000 +  's");
                map.put("servertime", ((double)timeTaken) / 1000 + "s");
                AppendingStringBuffer defaultValue = new AppendingStringBuffer(128);
                defaultValue.append ("Server parsetime: ");
                defaultValue.append(((double)timeTaken) / 1000);
                defaultValue
                                .append("s, Client parsetime: ' + (new Date().getTime() -
clientTimeVariable)/1000 +  's");
                String txt = Application.get().getResourceSettings().getLocalizer().getString(
                                resourceKey, null, Model.valueOf(map),
                                 Session.get().getLocale(), Session.get().getStyle(),
defaultValue.toString());
                return txt;
        }

}

Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
Wicket-develop mailing list
Wicket-develop@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/wicket-develop

Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
Wicket-develop mailing list
Wicket-develop@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/wicket-develop

Reply via email to