001    /**
002     * Copyright (c) 2000-2013 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            @Override
051            public Object invoke(Object proxy, Method method, Object[] arguments)
052                    throws Throwable {
053    
054                    String methodName = method.getName();
055    
056                    if (methodName.startsWith("set")) {
057                            throw new IllegalAccessException(
058                                    "Setter methods cannot be called on an escaped bean");
059                    }
060    
061                    if (methodName.endsWith("isEscapedModel")) {
062                            return true;
063                    }
064                    else if (methodName.endsWith("toEscapedModel")) {
065                            return proxy;
066                    }
067    
068                    Object result = null;
069    
070                    try {
071                            result = method.invoke(_bean, arguments);
072                    }
073                    catch (InvocationTargetException ite) {
074                            throw ite.getTargetException();
075                    }
076    
077                    if (method.getAnnotation(AutoEscape.class) != null) {
078                            result = HtmlUtil.escape((String)result);
079                    }
080    
081                    return result;
082            }
083    
084            private Serializable _bean;
085    
086    }