All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.ajax4jsf.framework.DebugOutputMaker Maven / Gradle / Ivy

Go to download

Ajax4jsf is an open source extension to the JavaServer Faces standard that adds AJAX capability to JSF applications without requiring the writing of any JavaScript.

The newest version!
/**
 * Licensed under the Common Development and Distribution License,
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.sun.com/cddl/
 *   
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 
 * implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */

package org.ajax4jsf.framework;

import java.beans.PropertyDescriptor;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;

import javax.faces.FacesException;
import javax.faces.component.UIComponent;
import javax.faces.component.UIViewRoot;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.faces.el.MethodBinding;
import javax.faces.el.ValueBinding;
import javax.faces.event.PhaseId;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.ajax4jsf.framework.resource.InternetResource;
import org.ajax4jsf.framework.resource.InternetResourceBuilder;
import org.ajax4jsf.framework.util.message.Messages;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.PropertyUtils;


/**
 * @author [email protected] (latest modification by $Author: alexsmirnov $)
 * @version $Revision: 1.6 $ $Date: 2006/07/27 13:59:38 $
 *
 */
public class DebugOutputMaker {
    private final static String LT = "<";
    private final static String GT = ">";
	// Attributes that should not be printed
	static public final HashSet IGNORE_ATTRIBUTES;

	static {
		IGNORE_ATTRIBUTES = new HashSet();
		IGNORE_ATTRIBUTES.add("attributes");
		IGNORE_ATTRIBUTES.add("children");
		IGNORE_ATTRIBUTES.add("childCount");
		IGNORE_ATTRIBUTES.add("class");
		IGNORE_ATTRIBUTES.add("facets");
		IGNORE_ATTRIBUTES.add("facetsAndChildren");
		IGNORE_ATTRIBUTES.add("parent");
		IGNORE_ATTRIBUTES.add("actionListeners");
		IGNORE_ATTRIBUTES.add("valueChangeListeners");
		IGNORE_ATTRIBUTES.add("validators");
		IGNORE_ATTRIBUTES.add("rowData");
		IGNORE_ATTRIBUTES.add("javax.faces.webapp.COMPONENT_IDS");
		IGNORE_ATTRIBUTES.add("javax.faces.webapp.FACET_NAMES");
		IGNORE_ATTRIBUTES.add("javax.faces.webapp.CURRENT_VIEW_ROOT");
	}
	
	private static final String LOGFILE_PARAM = "org.ajax4jsf.LOGFILE";

	/**
	 * Output error state for lifecycle.
	 * 
	 * @param context
	 * @param phase
	 *            TODO
	 * @param error
	 * @throws IOException
	 */
	public void writeErrorMessage(FacesContext context, Throwable e, String phase)
			throws FacesException {
		if (null == context) {
			throw new FacesException(Messages.getMessage(Messages.FACES_CONTEXT_NOT_CREATED), e);
		}
		ExternalContext externalContext = context
		.getExternalContext();
		if (null == externalContext) {
			throw new FacesException(Messages.getMessage(Messages.FACES_CONTEXT_HAS_NOT_EXTERNAL), e);
		}
		// debug for http servlet environment, portlets needs other debugger
		HttpServletResponse response;
		HttpServletRequest request;
		try {
			response = (HttpServletResponse) externalContext
					.getResponse();
			request = (HttpServletRequest) externalContext.getRequest();
			response.setContentType("text/html;charset=UTF-8");
			// set internal server error status
			response.setStatus(500);
		} catch (Exception er) {
			throw new FacesException(Messages.getMessage(Messages.FACES_CONTEXT_HAS_NOT_RESPONSE), e);
		}
		PrintWriter out;
		try {
			out = response.getWriter();
		} catch (IOException e1) {
			throw new FacesException(e1.getMessage(), e);
		}
		UIViewRoot viewRoot = context.getViewRoot();
		String viewId;
		if (null != viewRoot) {
			viewId = viewRoot.getViewId();
		} else {
			viewId = request.getRequestURL().toString();
		}
		// output html prolog
		out.println("" +
				Messages.getMessage(Messages.ERROR_ON_PAGE, viewId) +
				"");
		// write script
		writeScriptAndStyle(out);
		// Header
		PhaseId facesPhase = (PhaseId) context.getExternalContext().getRequestMap().get(DebugLifecycle.PHASE_ID_PARAM);
		
		String errorMessage = 
			(facesPhase == null) 
					? Messages.getMessage(Messages.LIFECYCLE_ERROR, viewId, phase)
					: Messages.getMessage(Messages.LIFECYCLE_ERROR_AT_PHASE, new Object[]{viewId, phase, facesPhase.toString()});
		out.println("

"); out.println(errorMessage); out.println("

"); response.setHeader("Ajax-Error-Message", errorMessage+",\n caused by "+e.getClass().getName()+", with message: "+e.getMessage()); // Output exceptions writeExceptionStack(e, out); // Output view tree : if (null != viewRoot) { writeComponentsTree(context, out); } else { out.print("

" + Messages.getMessage(Messages.COMPONENT_TREE_NOT_CREATED) + "

"); } // Out scope variables writeContextVariables(context, out); // Write log output iframe : writeLog(context, out); // out html tail out.println(""); out.flush(); out.close(); } /** * @param e * @param out */ public void writeExceptionStack(Throwable e, PrintWriter out) { out.println("

Exceptions:

"); Throwable error = e; int errorId = 0; String caused = "exception "; while (null != error) { out.print("

"); writeToggleMark(out,"exception" + errorId); out.print( caused + error.getClass().getName() + " : " + error.getMessage() + "

"); out.println(""); error = error.getCause(); caused = "caused by "; errorId++; } } /** * @param context * @param out */ public void writeContextVariables(FacesContext context, PrintWriter out) { out.print("

"); writeToggleMark(out,"variables"); out.println("Faces variables:

"); } /** * @param context * @param out */ public void writeComponentsTree(FacesContext context, PrintWriter out) { out .print("

"); writeToggleMark(out,"tree"); out.println("Component tree:

"); } public void writeScriptAndStyle(PrintWriter out) { writeScript(out); writeStyleSheet(out); } /** * @param context * @param out * @throws FacesException */ public void writeLog(FacesContext context, PrintWriter out) throws FacesException { String logname = context.getExternalContext().getInitParameter(LOGFILE_PARAM); if (null != logname) { InternetResource logResource = InternetResourceBuilder.getInstance().createResource(this,logname); out.print("

"); writeToggleMark(out, "log"); out .println("Faces log:

"); } } public void writeToggleMark(PrintWriter out,String id) { out.print(""); out.print("+ "); } /** * Out component with properties and it's facets and childrens * @param context TODO * @param out * @param viewRoot */ private void writeComponent(FacesContext context, PrintWriter out, UIComponent component, String facetName) { String clientId = "_tree:"+component.getClientId(context); out.println("
"); writeToggleMark(out,clientId); // out component name if (null != facetName) { out.print("Facet:'" + facetName + "' "); } out.println("" + component.getClass().getName() + " Id:["+component.getId()+"]"); out.println("
"); out.println(""); if (component.getFacetsAndChildren().hasNext()) { out.println("
"); // out childs of this component // facets for (Iterator facetEntry = component.getFacets().entrySet() .iterator(); facetEntry.hasNext();) { Map.Entry entry = (Map.Entry) facetEntry.next(); writeComponent(context, out, (UIComponent) entry.getValue(), (String) entry.getKey()); } // childs components for (Iterator childIter = component.getChildren().iterator(); childIter .hasNext();) { UIComponent child = (UIComponent) childIter.next(); writeComponent(context, out, child, null); } out.println("
"); } } private void writeAttribute(PrintWriter out, String name, Object value) { if (IGNORE_ATTRIBUTES.contains(name)) return; if (name.startsWith("javax.faces.webapp.UIComponentTag.")) { name = name .substring("javax.faces.webapp.UIComponentTag.".length()); } out.print("
  • "); out.print(name); out.print("=\""); if (value != null) { if (value instanceof UIComponent) { out.print("[id:"); out.print(((UIComponent) value).getId()); out.print(']'); } else if (value instanceof MethodBinding) { out.print(((MethodBinding) value).getExpressionString()); } else if (value instanceof ValueBinding) { out.print(((ValueBinding) value).getExpressionString()); } else { out.print(value.toString()); } } else { out.print("NULL"); } out.println("\"
  • "); } private void writeVariables(PrintWriter out, FacesContext faces) { ExternalContext ctx = faces.getExternalContext(); writeVariables(out, ctx.getRequestParameterMap(), "Request Parameters"); writeVariables(out, ctx.getRequestMap(), "Request Attributes"); if (ctx.getSession(false) != null) { writeVariables(out, ctx.getSessionMap(), "Session Attributes"); } writeVariables(out, ctx.getApplicationMap(), "Application Attributes"); } private void writeVariables(PrintWriter out, Map vars, String caption) { out.print(""); boolean written = false; if (!vars.isEmpty()) { SortedMap map = new TreeMap(vars); Map.Entry entry = null; String key = null; for (Iterator itr = map.entrySet().iterator(); itr.hasNext(); ) { entry = (Map.Entry) itr.next(); key = entry.getKey().toString(); if (key.indexOf('.') == -1) { out.println(""); written = true; } } } if (!written) { out.println(""); } out.println("
    "); out.print(caption); out.println("
    NameValue
    "); out.println(key.replaceAll("<", LT).replaceAll(">", GT)); out.println(""); Object value = entry.getValue(); out.println(value.toString().replaceAll("<", LT).replaceAll(">", GT)); out.println(""); try { PropertyDescriptor propertyDescriptors[] = PropertyUtils.getPropertyDescriptors(value); if (propertyDescriptors.length>0) { out.print("
      "); for (int i = 0; i < propertyDescriptors.length; i++) { String beanPropertyName = propertyDescriptors[i].getName(); if (PropertyUtils.isReadable(value,beanPropertyName )) { out.print("
    • "); out.print(beanPropertyName+" = "+BeanUtils.getProperty(value,beanPropertyName)); out.print("
    • "); } } out.print("
    "); } } catch (Exception e) { // TODO: log exception } out.println("
    None
    "); } /** * @param out */ private void writeScript(PrintWriter out) { out .println(""); } /** * @param out */ private void writeStyleSheet(PrintWriter out) { out .println(""); } }




    © 2015 - 2024 Weber Informatics LLC | Privacy Policy