package SwordMod; import org.crosswire.sword.web.PackageMod; import org.crosswire.sword.mgr.SWMgr; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import java.io.File; import java.io.FileOutputStream; import java.util.Date; import com.objectspace.jgl.HashMap; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; public class Verify extends HttpServlet { static Object packageBuildSemephore = new Object(); //Initialize global variables public void init(ServletConfig config) throws ServletException { super.init(config); } public void service(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, java.io.IOException { String sections[] = new String[] { "attic", "avattic", "av", "beta", "experimental", "exp", "wycliffe" }; String spsection = ""; for (String s : sections) { String sparam = request.getParameter(s); if (sparam != null && !sparam.equals("")) { spsection = s; break; } } if ("exp".equals(spsection)) spsection = "experimental"; // handle old "exp" param for backward compat String modName = null; String pkgType = null; String mgrDir = "/home/ftp/pub/sword/"+spsection+"raw/"; response.setContentType("text/html"); try { boolean rebuild = true; modName = request.getParameterValues("modName")[0]; pkgType = request.getParameterValues("pkgType")[0]; // assert we have a valid module name request if (SWMgr.getInstance(mgrDir).config.sections.get(modName) == null) { return; } // assert we have a valid type String [] validTypes = new String [] { "raw", "rawzip", "win", "mac" }; java.util.Arrays.sort(validTypes); if (java.util.Arrays.binarySearch(validTypes, pkgType) < 0) { return; } // must be for some backward compat, or old links from external sites. Remove sometime in the future. if ("raw".equals(pkgType)) { pkgType = "rawzip"; } // ----- File dest = new File("/home/ftp/pub/sword/" + spsection + "packages/" + pkgType + "/" + modName + ".zip"); if (dest.canRead()) { long timeMade = dest.lastModified(); if (PackageMod.getLastModFile(SWMgr.getInstance(mgrDir), mgrDir + "/", modName) < timeMade) { rebuild = false; } } if (rebuild) { dest.delete(); FileOutputStream outFile = new FileOutputStream(dest); if (pkgType.equals("win")) { ZipOutputStream zstream = new ZipOutputStream(outFile); File dataDir = new File("/home/sword/winmodinst/"); String names[] = dataDir.list(); for (int i = 0; i < names.length; i++) { if ( (!names[i].equals(".")) && (!names[i].equals(".."))) { PackageMod.copyFileToZip(zstream, "/home/sword/winmodinst/", names[i], ""); } } zstream.putNextEntry(new ZipEntry("data.zip")); PackageMod.sendToZipStream(zstream, SWMgr.getInstance(mgrDir), mgrDir + "/", modName, true, ""); zstream.close(); } else if ("mac".equals(pkgType)) { PackageMod.sendToZipStream(outFile, SWMgr.getInstance(mgrDir), mgrDir + "/", modName, false, modName+".swd"); } else { // raw PackageMod.sendToZipStream(outFile, SWMgr.getInstance(mgrDir), mgrDir + "/", modName, false, ""); } outFile.flush(); outFile.close(); } } catch (Exception e) { e.printStackTrace(); } finally { try { String url = "/ftpmirror/pub/sword/" + spsection + "packages/" + pkgType + "/" + modName + ".zip"; synchronized (packageBuildSemephore) { FileOutputStream log = new FileOutputStream( "/var/log/httpd/pkgDownloads", true); log.write(new String(pkgType + "|" + modName + "|" + new Date() + "\n").getBytes()); log.close(); } response.getOutputStream().println( "Click here to download if it doesn't start automatically"); HashMap section = (HashMap) SWMgr.getInstance(mgrDir).config.sections. get(modName); String about = (String) section.get("About"); String desc = (String) section.get("Description"); if (about != null) { response.getOutputStream().println("

About: " + desc + "


"); about = about.replaceAll("\\\\pard", ""); about = about.replaceAll("\\\\par", "
"); about = about.replaceAll("\\\\\\w+", ""); response.getOutputStream().println(about); } response.getOutputStream().println(""); response.getOutputStream().close(); } catch (Exception e1) { e1.printStackTrace(); } } } }