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.taglib.util;
016    
017    import com.liferay.portal.kernel.util.StringPool;
018    import com.liferay.portal.kernel.util.Time;
019    import com.liferay.portal.kernel.util.Validator;
020    import com.liferay.portal.kernel.webcache.WebCacheItem;
021    import com.liferay.portal.kernel.webcache.WebCachePoolUtil;
022    
023    import javax.servlet.jsp.JspException;
024    import javax.servlet.jsp.JspWriter;
025    import javax.servlet.jsp.tagext.TagSupport;
026    
027    /**
028     * @author Brian Wing Shun Chan
029     */
030    public class GetUrlTag extends TagSupport {
031    
032            @Override
033            public int doEndTag() throws JspException {
034                    try {
035                            WebCacheItem wci = new GetUrlWebCacheItem(_url, _expires);
036    
037                            String content = (String)WebCachePoolUtil.get(
038                                    GetUrlTag.class.getName() + StringPool.PERIOD + _url, wci);
039    
040                            if (Validator.isNotNull(_var)) {
041                                    pageContext.setAttribute(_var, content);
042                            }
043                            else {
044                                    JspWriter jspWriter = pageContext.getOut();
045    
046                                    jspWriter.print(content);
047                            }
048    
049                            return EVAL_PAGE;
050                    }
051                    catch (Exception e) {
052                            throw new JspException(e);
053                    }
054            }
055    
056            public void setExpires(long expires) {
057                    _expires = expires;
058            }
059    
060            public void setUrl(String url) {
061                    _url = url;
062            }
063    
064            public void setVar(String var) {
065                    _var = var;
066            }
067    
068            private long _expires = Time.WEEK;
069            private String _url;
070            private String _var;
071    
072    }