001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.bean;
016    
017    import com.liferay.portal.kernel.util.HtmlUtil;
018    
019    import java.lang.reflect.InvocationHandler;
020    import java.lang.reflect.InvocationTargetException;
021    import java.lang.reflect.Method;
022    
023    /**
024     * Wraps a bean so that all strings returned from <code>@AutoEscape</code>
025     * annotated methods are automatically HTML escaped.
026     *
027     * <p>
028     * For a usage example see {@link
029     * com.liferay.portlet.shopping.util.ShoppingUtil#getBreadcrumbs(
030     * com.liferay.portlet.shopping.model.ShoppingCategory,
031     * javax.servlet.jsp.PageContext, javax.portlet.RenderRequest,
032     * javax.portlet.RenderResponse) ShoppingUtil.getBreadcrumbs} .
033     * </p>
034     *
035     * @author Shuyang Zhou
036     * @see    AutoEscape
037     */
038    public class AutoEscapeBeanHandler implements InvocationHandler {
039    
040            public AutoEscapeBeanHandler(Object bean) {
041                    _bean = bean;
042            }
043    
044            public Object getBean() {
045                    return _bean;
046            }
047    
048            public Object invoke(Object proxy, Method method, Object[] args)
049                    throws Throwable {
050    
051                    String methodName = method.getName();
052    
053                    if (methodName.startsWith("set")) {
054                            throw new IllegalAccessException(
055                                    "Setter methods cannot be called on an escaped bean");
056                    }
057    
058                    if (methodName.endsWith("isEscapedModel")) {
059                            return true;
060                    }
061                    else if (methodName.endsWith("toEscapedModel")) {
062                            return proxy;
063                    }
064    
065                    Object result = null;
066    
067                    try {
068                            result = method.invoke(_bean, args);
069                    }
070                    catch (InvocationTargetException ite) {
071                            throw ite.getTargetException();
072                    }
073    
074                    if (method.getAnnotation(AutoEscape.class) != null) {
075                            result = HtmlUtil.escape((String)result);
076                    }
077    
078                    return result;
079            }
080    
081            private Object _bean;
082    
083    }