001    /**
002     * Copyright (c) 2000-present 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.portlet.documentlibrary.context.util;
016    
017    import java.io.IOException;
018    
019    import java.util.HashMap;
020    import java.util.Map;
021    
022    import javax.servlet.RequestDispatcher;
023    import javax.servlet.ServletException;
024    import javax.servlet.http.HttpServletRequest;
025    import javax.servlet.http.HttpServletResponse;
026    
027    /**
028     * @author Ivan Zaera
029     */
030    public class JSPRenderer {
031    
032            public JSPRenderer(String jspPath) {
033                    _jspPath = jspPath;
034            }
035    
036            public void render(HttpServletRequest request, HttpServletResponse response)
037                    throws IOException, ServletException {
038    
039                    Map<String, Object> savedAttributes = new HashMap<>();
040    
041                    for (String key : _attributes.keySet()) {
042                            savedAttributes.put(key, request.getAttribute(key));
043    
044                            request.setAttribute(key, _attributes.get(key));
045                    }
046    
047                    RequestDispatcher requestDispatcher = request.getRequestDispatcher(
048                            _jspPath);
049    
050                    requestDispatcher.include(request, response);
051    
052                    for (String key : savedAttributes.keySet()) {
053                            request.setAttribute(key, savedAttributes.get(key));
054                    }
055            }
056    
057            public void setAttribute(String key, Object value) {
058                    _attributes.put(key, value);
059            }
060    
061            private final Map<String, Object> _attributes = new HashMap<>();
062            private final String _jspPath;
063    
064    }