View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.myfaces.tobago.util;
21  
22  import org.slf4j.Logger;
23  import org.slf4j.LoggerFactory;
24  
25  import javax.faces.application.FacesMessage;
26  import javax.faces.application.ProjectStage;
27  import javax.faces.context.ExternalContext;
28  import javax.faces.context.FacesContext;
29  import javax.faces.event.PhaseEvent;
30  import javax.faces.event.PhaseId;
31  import javax.faces.event.PhaseListener;
32  import javax.servlet.http.HttpServletRequest;
33  import javax.servlet.http.HttpServletResponse;
34  import java.text.MessageFormat;
35  import java.util.Iterator;
36  import java.util.Map;
37  
38  public class DebugPhaseListener implements PhaseListener {
39  
40    private static final Logger LOG = LoggerFactory.getLogger(DebugPhaseListener.class);
41  
42    private static final String KEY = DebugPhaseListener.class.getName() + "_ID_";
43  
44    @Override
45    public void afterPhase(final PhaseEvent phaseEvent) {
46      final FacesContext facesContext = phaseEvent.getFacesContext();
47      final boolean productionMode = facesContext.isProjectStage(ProjectStage.Production);
48      if (facesContext.getResponseComplete() || productionMode) {
49        return;
50      }
51  
52      if (LOG.isInfoEnabled()) {
53        final Long end = System.currentTimeMillis();
54        final Map<String, Object> map = facesContext.getExternalContext().getRequestMap();
55        map.put(KEY + phaseEvent.getPhaseId().getOrdinal() + "E", end);
56  
57        if (LOG.isTraceEnabled()) {
58          LOG.trace("After Phase :" + phaseEvent.getPhaseId() + " Time=" + end);
59        }
60  
61        if (LOG.isDebugEnabled()) {
62          final Long start = (Long) map.get(KEY + phaseEvent.getPhaseId().getOrdinal() + "S");
63          LOG.debug("Phase " + phaseEvent.getPhaseId() + " needs " + (end - start + " milliseconds"));
64        }
65  
66        if (phaseEvent.getPhaseId().getOrdinal() == 6) {
67          if (LOG.isTraceEnabled()) {
68            final HttpServletResponse response = (HttpServletResponse) facesContext.getExternalContext().getResponse();
69            LOG.trace(" response Locale            = '" + response.getLocale() + "'");
70            LOG.trace(" response ContentType       = '" + response.getContentType() + "'");
71            LOG.trace(" response CharacterEncoding = '{" + response.getCharacterEncoding() + "}'");
72          }
73  
74          final Long start = (Long) map.get(KEY + "1S");
75          if (start != null) {
76            LOG.info("Total response time : " + (end - start + " milliseconds"));
77          }
78        }
79        for (final Iterator iterator = facesContext.getClientIdsWithMessages(); iterator.hasNext();) {
80          final String clientId = (String) iterator.next();
81  
82          for (final Iterator messageIterator = facesContext.getMessages(clientId); messageIterator.hasNext();) {
83            final FacesMessage msg = (FacesMessage) messageIterator.next();
84            LOG.info(MessageFormat.format("Faces message found."
85                + "\n  Component: {0} \n  Severity : {1}"
86                + "\n  Summary  : {2} \n  Detail   : {3}",
87                clientId, msg.getSeverity(), msg.getSummary(), msg.getDetail()));
88          }
89        }
90      }
91    }
92  
93    @Override
94    public void beforePhase(final PhaseEvent phaseEvent) {
95      final FacesContext facesContext = phaseEvent.getFacesContext();
96      final boolean productionMode = facesContext.isProjectStage(ProjectStage.Production);
97      if (facesContext.getResponseComplete()) {
98        LOG.info("Response is completed.");
99        return;
100     }
101     if (productionMode) {
102       LOG.warn("DebugPhaseListener disabled, because the project stage is 'production'.");
103       return;
104     }
105 
106     if (LOG.isInfoEnabled()) {
107       final PhaseId phaseId = phaseEvent.getPhaseId();
108       if (LOG.isDebugEnabled() || phaseId.getOrdinal() == 1) {
109 
110         final ExternalContext externalContext = facesContext.getExternalContext();
111 
112         if (LOG.isTraceEnabled() && PhaseId.RESTORE_VIEW == phaseId) {
113           // this is before restoreView
114 
115           final Object request = externalContext.getRequest();
116           if (request instanceof HttpServletRequest) {
117             final HttpServletRequest servletRequest = (HttpServletRequest) request;
118             LOG.trace("RequestURI = " + servletRequest.getRequestURI());
119           }
120           final Map headerMap = externalContext.getRequestHeaderMap();
121           for (final Object key : headerMap.keySet()) {
122             LOG.trace("Header : '" + key + "' = '" + headerMap.get(key) + "'");
123           }
124           final Map parameterMap = externalContext.getRequestParameterMap();
125           for (final Object key : parameterMap.keySet()) {
126             LOG.trace("Param  : '" + key + "' = '" + parameterMap.get(key) + "'");
127           }
128         }
129 
130         final Long start = System.currentTimeMillis();
131         final Map<String, Object> map = externalContext.getRequestMap();
132         map.put(KEY + phaseId.getOrdinal() + "S", start);
133 
134         if (LOG.isDebugEnabled()) {
135           Long end = null;
136           int ordinal = phaseId.getOrdinal();
137           while (end == null && ordinal > 0) {
138             end = (Long) map.get(KEY + --ordinal + "E");
139           }
140           if (end != null) {
141             LOG.debug("Time between phases " + ordinal + " and " + phaseId.getOrdinal() + ": "
142                 + (start - end) + " milliseconds");
143           }
144         }
145         if (LOG.isTraceEnabled()) {
146           LOG.trace("Before Phase :" + phaseId + " Time=" + start);
147         }
148       }
149     }
150   }
151 
152   @Override
153   public PhaseId getPhaseId() {
154     return PhaseId.ANY_PHASE;
155   }
156 }