Index: lams_learning/src/java/org/lamsfoundation/lams/learning/export/web/action/CSSBundler.java =================================================================== diff -u --- lams_learning/src/java/org/lamsfoundation/lams/learning/export/web/action/CSSBundler.java (revision 0) +++ lams_learning/src/java/org/lamsfoundation/lams/learning/export/web/action/CSSBundler.java (revision eb7240215d5100a3624b61903fac9af0d8a26b5e) @@ -0,0 +1,257 @@ +/**************************************************************** + * Copyright (C) 2005 LAMS Foundation (http://lamsfoundation.org) + * ============================================================= + * License Information: http://lamsfoundation.org/licensing/lams/2.0/ + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + * USA + * + * http://www.gnu.org/licenses/gpl.txt + * **************************************************************** + */ +/* $Id$ */ + +package org.lamsfoundation.lams.learning.export.web.action; + +import java.io.BufferedInputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStream; +import java.net.MalformedURLException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; + +import javax.servlet.http.Cookie; +import javax.servlet.http.HttpServletRequest; + +import org.apache.log4j.Logger; +import org.lamsfoundation.lams.util.CSSThemeUtil; +import org.lamsfoundation.lams.util.Configuration; +import org.lamsfoundation.lams.util.ConfigurationKeys; +import org.lamsfoundation.lams.util.HttpUrlConnectionUtil; + +public class CSSBundler { + + private static Logger log = Logger.getLogger(CSSBundler.class); + + Map filesToCopy = null; + List directoriesRequired = null; + HttpServletRequest request = null; + Cookie[] cookies = null; + String outputDirectory = null; + String centralPath = null; + + /** + * @param centralPath the directory path to the lams-central.war. Assumes that it is an expanded war. + */ + public CSSBundler(HttpServletRequest request, Cookie[] cookies, String outputDirectory) { + filesToCopy = new HashMap(); + directoriesRequired = new ArrayList(); + this.request = request; + this.cookies = cookies; + this.outputDirectory = outputDirectory; + + this.centralPath = Configuration.get(ConfigurationKeys.LAMS_EAR_DIR); + if ( centralPath == null ) { + log.error("Unable to get path to the LAMS ear from the configuration file - the exported portfolios will be missing parts of their css (style and images)."); + } else { + centralPath = centralPath + File.separator + "lams-central.war"; + } + } + + /** Bundle the stylesheets. + * + * @param outputDirectory directory for the export + * @param request + * @param cookies + * @throws IOException + */ + public void bundleStylesheet( ) throws IOException + { + File central = new File(centralPath); + if ( centralPath == null || ! central.canRead() || ! central.isDirectory() ) { + log.error("Unable to get the path for lams-central or unable to read it as a directory. Bundling stylesheets via http but not including images"); + bundleViaHTTP(request, cookies); + } else { + log.debug("Copying stylesheets and images from path "+centralPath); + bundleViaCopy(); + } + + } + + /** Fallback code if it can't do normal file copies (because it doesn't know the path to lams-central. + * Only does the stylesheets, not the images + * + * @param outputDirectory + * @param request + * @param cookies + * @throws IOException + * @throws FileNotFoundException + * @throws MalformedURLException + */ + private void bundleViaHTTP(HttpServletRequest request, Cookie[] cookies) throws MalformedURLException, FileNotFoundException, IOException { + + String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort(); + + List themeList = CSSThemeUtil.getAllUserThemes(); + + Iterator i = themeList.iterator(); + + while (i.hasNext()) + { + String theme = (String)i.next(); + + String url = basePath + "/lams/css/" + theme + ".css"; + HttpUrlConnectionUtil.writeResponseToFile(url, outputDirectory, theme + ".css", cookies); //cookies aren't really needed here. + } + + // include the special IE stylesheet + String url = basePath + "/lams/css/ie-styles.css"; + HttpUrlConnectionUtil.writeResponseToFile(url, outputDirectory, "ie-styles.css", cookies); //cookies aren't really needed here. + + } + + /** Preferred method - copies the files from lams-central.war + * + * @param outputDirectory + * @throws IOException + */ + private void bundleViaCopy() throws IOException { + + // build up a list of themes to copy + setupThemeList(); + + // build up a list of images to copy + setupImageList(); + + // now copy all those files + createDirectories(); + for ( Map.Entry fileEntry : filesToCopy.entrySet() ) { + copyFile((String)fileEntry.getKey(), (File)fileEntry.getValue()); + } + } + + /** + * + */ + private void setupThemeList() { + // build up a list of themes to copy + String cssDirectory = outputDirectory+File.separatorChar+"css"; + directoriesRequired.add(cssDirectory); + + List themeList = CSSThemeUtil.getAllUserThemes(); + Iterator i = themeList.iterator(); + while (i.hasNext()) + { + String theme = (String)i.next(); + addThemeFile(cssDirectory, theme); + } + + // include the special IE stylesheet + addThemeFile(cssDirectory, "ie-styles"); + } + + private void addThemeFile(String cssDirectory, String themeName) { + String theme = themeName + ".css"; + File themeFile = new File(centralPath + "/css/" + theme); + if ( ! themeFile.canRead() ) { + log.error("Unable to read theme file "+themeFile.getAbsolutePath()); + } else { + filesToCopy.put(cssDirectory+File.separatorChar+theme,themeFile); + } + } + + private void setupImageList() { + + // TODO add an entry in the theme definition that defines where to find all the stylesheet images. + String imageDirectory = centralPath+File.separatorChar+"images"+File.separatorChar+"css"; + String outputImageDirectory = outputDirectory+File.separatorChar+"images"+File.separatorChar+"css"; + directoriesRequired.add(outputImageDirectory); + + File dir = new File(imageDirectory); + if ( ! dir.canRead() || ! dir.isDirectory() ) { + log.error("Unable to read css image directory "+dir.getAbsolutePath()); + + } else { + + File[] files = dir.listFiles(); + for ( File imageFile: files ) { + filesToCopy.put(outputImageDirectory+File.separatorChar+imageFile.getName(),imageFile); + } + } + } + + private void createDirectories() { + + for ( String directoryPath: directoriesRequired) { + File dir = new File(directoryPath); + if ( ! dir.mkdirs() ) { + log.error("Unable to create directory for export portfolio: "+directoryPath); + } + } + } + + private void copyFile(String filePath, File file) throws IOException { + + FileInputStream is = new FileInputStream(file); + OutputStream os = null; + try { + + if ( log.isDebugEnabled() ) { + log.debug("Writing out file to "+filePath); + } + + int bufLen = 1024; // 1 Kbyte + byte[] buf = new byte[1024]; // output buffer + os = new FileOutputStream(filePath); + + BufferedInputStream in = new BufferedInputStream(is); + int len = 0; + while((len = in.read(buf,0,bufLen)) != -1){ + os.write(buf,0,len); + } + + } catch ( IOException e ) { + String message = "Unable to write out file needed for export portfolio. File was "+filePath; + log.error(message,e); + throw e; + + } finally { + + try { + if ( is != null ) + is.close(); + } catch (IOException e1) { + String message = "Unable to close input export portfolio file due to IOException"; + log.warn(message,e1); + } + + try { + if ( os != null ) + os.close(); + } catch (IOException e2) { + String message = "Unable to close output export portfolio file due to IOException"; + log.warn(message,e2); + } + } + + + } +} Index: lams_learning/src/java/org/lamsfoundation/lams/learning/export/web/action/MainExportServlet.java =================================================================== diff -u -r810a3898f726d0eae005128cde246c8e0d3c0ed3 -reb7240215d5100a3624b61903fac9af0d8a26b5e --- lams_learning/src/java/org/lamsfoundation/lams/learning/export/web/action/MainExportServlet.java (.../MainExportServlet.java) (revision 810a3898f726d0eae005128cde246c8e0d3c0ed3) +++ lams_learning/src/java/org/lamsfoundation/lams/learning/export/web/action/MainExportServlet.java (.../MainExportServlet.java) (revision eb7240215d5100a3624b61903fac9af0d8a26b5e) @@ -26,8 +26,6 @@ import java.io.IOException; import java.io.PrintWriter; -import java.util.Iterator; -import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.Cookie; @@ -43,9 +41,7 @@ import org.lamsfoundation.lams.learning.export.service.IExportPortfolioService; import org.lamsfoundation.lams.tool.ToolAccessMode; import org.lamsfoundation.lams.usermanagement.dto.UserDTO; -import org.lamsfoundation.lams.util.CSSThemeUtil; import org.lamsfoundation.lams.util.FileUtil; -import org.lamsfoundation.lams.util.HttpUrlConnectionUtil; import org.lamsfoundation.lams.util.WebUtil; import org.lamsfoundation.lams.web.session.SessionManager; import org.lamsfoundation.lams.web.util.AttributeNames; @@ -68,6 +64,7 @@ */ public class MainExportServlet extends HttpServlet { + private static final long serialVersionUID = 7788509831929373666L; private String exportTmpDir; @@ -100,7 +97,7 @@ /** Get the cookies that were sent along with this request, then pass it onto export service */ Cookie[] cookies = request.getCookies(); - + IExportPortfolioService exportService = ExportPortfolioServiceProxy.getExportPortfolioService(this.getServletContext()); // ILearnerService learnerService = LearnerServiceProxy.getLearnerService(this.getServletContext()); @@ -128,7 +125,8 @@ exportService.generateMainPage(request, portfolios, cookies); //bundle the stylesheet with the package - bundleStylesheetWithExportPackage(exportTmpDir, request, cookies); + CSSBundler bundler = new CSSBundler(request, cookies, exportTmpDir); + bundler.bundleStylesheet(); //zip up the contents of the temp export folder String zipFilename = exportService.zipPortfolio(ExportPortfolioConstants.ZIP_FILENAME, exportTmpDir); @@ -151,26 +149,5 @@ return absolutePath.substring(tempSysDirName.length()+1, absolutePath.length()); } - private void bundleStylesheetWithExportPackage(String directory, HttpServletRequest request, Cookie[] cookies) throws IOException - { - String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort(); - List themeList = CSSThemeUtil.getAllUserThemes(); - - Iterator i = themeList.iterator(); - - while (i.hasNext()) - { - String theme = (String)i.next(); - - String url = basePath + "/lams/css/" + theme + ".css"; - HttpUrlConnectionUtil.writeResponseToFile(url, directory, theme + ".css", cookies); //cookies aren't really needed here. - } - - // include the special IE stylesheet - String url = basePath + "/lams/css/ie-styles.css"; - HttpUrlConnectionUtil.writeResponseToFile(url, directory, "ie-styles.css", cookies); //cookies aren't really needed here. - - } - } Index: lams_learning/web/exportPortfolio/main.jsp =================================================================== diff -u -r810a3898f726d0eae005128cde246c8e0d3c0ed3 -reb7240215d5100a3624b61903fac9af0d8a26b5e --- lams_learning/web/exportPortfolio/main.jsp (.../main.jsp) (revision 810a3898f726d0eae005128cde246c8e0d3c0ed3) +++ lams_learning/web/exportPortfolio/main.jsp (.../main.jsp) (revision eb7240215d5100a3624b61903fac9af0d8a26b5e) @@ -36,24 +36,28 @@ <%-- can't use the normal "lams:css localLink=true" as that points to ../default.css --%> - - + + +

-
+
+
+

 

+