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.io.Serializable;
020    
021    import java.lang.reflect.InvocationHandler;
022    import java.lang.reflect.InvocationTargetException;
023    import java.lang.reflect.Method;
024    
025    /**
026     * Wraps a bean so that all strings returned from <code>@AutoEscape</code>
027     * annotated methods are automatically HTML escaped.
028     *
029     * <p>
030     * For a usage example see {@link
031     * com.liferay.portlet.shopping.util.ShoppingUtil#getBreadcrumbs(
032     * com.liferay.portlet.shopping.model.ShoppingCategory,
033     * javax.servlet.jsp.PageContext, javax.portlet.RenderRequest,
034     * javax.portlet.RenderResponse) ShoppingUtil.getBreadcrumbs} .
035     * </p>
036     *
037     * @author Shuyang Zhou
038     * @see    AutoEscape
039     */
040    public class AutoEscapeBeanHandler implements InvocationHandler, Serializable {
041    
042            public AutoEscapeBeanHandler(Object bean) {
043                    _bean = (Serializable)bean;
044            }
045    
046            public Object getBean() {
047                    return _bean;
048            }
049    
050            public Object invoke(Object proxy, Method method, Object[] arguments)
051                    throws Throwable {
052    
053                    String methodName = method.getName();
054    
055                    if (methodName.startsWith("set")) {
056                            throw new IllegalAccessException(
057                                    "Setter methods cannot be called on an escaped bean");
058                    }
059    
060                    if (methodName.endsWith("isEscapedModel")) {
061                            return true;
062                    }
063                    else if (methodName.endsWith("toEscapedModel")) {
064                            return proxy;
065                    }
066    
067                    Object result = null;
068    
069                    try {
070                            result = method.invoke(_bean, arguments);
071                    }
072                    catch (InvocationTargetException ite) {
073                            throw ite.getTargetException();
074                    }
075    
076                    if (method.getAnnotation(AutoEscape.class) != null) {
077                            result = HtmlUtil.escape((String)result);
078                    }
079    
080                    return result;
081            }
082    
083            private Serializable _bean;
084    
085    }