package org.nextime.ion.export;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.Vector;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import javax.activation.FileTypeMap;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.nextime.ion.export.bean.ResourceXmlBean;
import org.nextime.ion.export.bean.Resources;
import org.nextime.ion.framework.business.Publication;
import org.nextime.ion.framework.business.Section;
import org.nextime.ion.framework.mapping.Mapping;
public class ExportServlet extends HttpServlet {
private String resourcesPath;
private String imagesPath;
private String frontUrl;
private byte[] buf = new byte[512];
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
response.setContentType("application/zip");
ZipOutputStream zip =
new ZipOutputStream(response.getOutputStream());
// add resources
Vector beans = Resources.getResourceXmlBeans(this);
for (int i = 0; i < beans.size(); i++) {
ResourceXmlBean bean = (ResourceXmlBean) beans.get(i);
File root =
new File(
getServletContext().getRealPath("/"),
resourcesPath);
File dir = new File(root, bean.getDirectory());
File[] files = dir.listFiles();
for (int k = 0; k < files.length; k++) {
File file = files[k];
if (file.isFile()) {
InputStream in = new FileInputStream(file);
ZipEntry ze =
new ZipEntry(
"resources/"
+ bean.getId()
+ "/"
+ file.getName());
zip.putNextEntry(ze);
// Transfer bytes from the file to the ZIP file
int len;
while ((len = in.read(buf)) > 0) {
zip.write(buf, 0, len);
}
zip.closeEntry();
in.close();
}
}
}
// add images
File root =
new File(getServletContext().getRealPath("/"), imagesPath);
File[] files = root.listFiles();
for (int k = 0; k < files.length; k++) {
File file = files[k];
if (file.isFile()) {
InputStream in = new FileInputStream(file);
ZipEntry ze = new ZipEntry("images/" + file.getName());
zip.putNextEntry(ze);
// Transfer bytes from the file to the ZIP file
int len;
while ((len = in.read(buf)) > 0) {
zip.write(buf, 0, len);
}
zip.closeEntry();
in.close();
}
}
// add sections
Mapping.begin();
Vector sections = Section.listAll();
Mapping.rollback();
for (int i = 0; i < sections.size(); i++) {
Section section = (Section) sections.get(i);
try {
URL url =
new URL(
frontUrl+"/section/"
+ section.getId()
+ ".html?static=true&nocache=true");
BufferedReader reader =
new BufferedReader(
new InputStreamReader(url.openStream()));
String content = "<!-- �ON static export -->";
String line = "";
while (line != null) {
line = reader.readLine();
if (line != null) {
content += line;
}
}
ZipEntry ze =
new ZipEntry("section/" + section.getId() + ".html");
zip.putNextEntry(ze);
zip.write(content.getBytes());
zip.closeEntry();
} catch (Exception e) {
System.out.println(
"Static export error, section " + section.getId());
}
}
// add publications
Mapping.begin();
Vector publications = Publication.listAll();
Mapping.rollback();
for (int i = 0; i < publications.size(); i++) {
Publication publication = (Publication) publications.get(i);
try {
URL url =
new URL(
frontUrl+"/publication/"
+ publication.getId()
+ ".html?static=true&nocache=true");
BufferedReader reader =
new BufferedReader(
new InputStreamReader(url.openStream()));
String content = "<!-- �ON static export -->";
String line = "";
while (line != null) {
line = reader.readLine();
if (line != null) {
content += line;
}
}
ZipEntry ze =
new ZipEntry(
"publication/" + publication.getId() + ".html");
zip.putNextEntry(ze);
zip.write(content.getBytes());
zip.closeEntry();
} catch (Exception e) {
System.out.println(
"Static export error, publication "
+ publication.getId());
}
}
// add index
URL url =
new URL(
frontUrl+"?static=true&nocache=true");
BufferedReader reader =
new BufferedReader(new InputStreamReader(url.openStream()));
String content = "<!-- �ON static export -->";
String line = "";
while (line != null) {
line = reader.readLine();
if (line != null) {
content += line;
}
}
ZipEntry ze = new ZipEntry("section/index.html");
zip.putNextEntry(ze);
zip.write(content.getBytes());
zip.closeEntry();
ze = new ZipEntry("index.html");
zip.putNextEntry(ze);
String redirect =
"<META HTTP-EQUIV=\"Refresh\" CONTENT=\"0;URL=section/index.html\">";
zip.write(redirect.getBytes());
zip.closeEntry();
zip.close();
} catch (Exception e) {
throw new ServletException(e);
}
response.getOutputStream().flush();
response.getOutputStream().close();
}
public void init() throws ServletException {
resourcesPath = getInitParameter("resourcesPath");
imagesPath = getInitParameter("imagesPath");
frontUrl = getInitParameter("frontUrl");
}
}