Changeset 7832 for mauRepo/NewmoonJSP
- Timestamp:
- 24/01/11 17:15:15 (10 years ago)
- Location:
- mauRepo/NewmoonJSP/src/main/java/ndg/newmoon/web
- Files:
-
- 1 added
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
mauRepo/NewmoonJSP/src/main/java/ndg/newmoon/web/NewmoonContextListener.java
r7826 r7832 12 12 public class NewmoonContextListener implements ServletContextListener { 13 13 14 public static final String EXIST_HOME = "exist.home";14 //public static final String EXIST_HOME = "exist.home"; 15 15 public static final String TMP_DIR = "tmp.dir"; 16 public static final String NM_RULES_CONFORMANCE = "nm.rules.conformance"; 17 public static final String NM_RULES_SCHEMA_ENCODING = "nm.rules.schema.encoding"; 18 public static final String NM_REPORT_STYLESHEET = "nm.report.schema"; 16 19 17 20 /** … … 21 24 22 25 public void contextDestroyed(ServletContextEvent event) { 23 System.clearProperty(EXIST_HOME);26 //System.clearProperty(EXIST_HOME); 24 27 try { 25 28 DBManager.getInstance().stopExist(); … … 30 33 31 34 public void contextInitialized(ServletContextEvent event) { 35 /* 32 36 System.setProperty(EXIST_HOME, event.getServletContext().getRealPath("/") + "WEB-INF/existData"); 33 37 if (LOG.isInfoEnabled()) { 34 38 LOG.info("System property exist.home set to " + System.getProperty(EXIST_HOME)); 35 39 } 36 event.getServletContext().setAttribute(TMP_DIR, event.getServletContext().getRealPath("/") + "WEB-INF/tmpDir"); 40 */ 41 42 String webInfPath = event.getServletContext().getRealPath("/WEB-INF"); 43 event.getServletContext().setAttribute(TMP_DIR, webInfPath + "/tmpDir"); 37 44 if (LOG.isInfoEnabled()) { 38 45 LOG.info("ServletContext attribute tmp.dir set to " + event.getServletContext().getAttribute(TMP_DIR)); 39 46 } 47 event.getServletContext().setAttribute(NM_RULES_CONFORMANCE, webInfPath + "/rules/conformance-test"); 48 event.getServletContext().setAttribute(NM_RULES_SCHEMA_ENCODING, webInfPath + "/rules/schema-encoding"); 49 event.getServletContext().setAttribute(NM_REPORT_STYLESHEET, webInfPath + "/resources/conf-test/results-stylesheet.xsl"); 40 50 } 41 51 -
mauRepo/NewmoonJSP/src/main/java/ndg/newmoon/web/XMIEncoderBean.java
r7826 r7832 2 2 3 3 import java.io.File; 4 import java.io.File Filter;5 import java.io.File OutputStream;4 import java.io.FileInputStream; 5 import java.io.FileNotFoundException; 6 6 import java.io.IOException; 7 import java.util.ArrayList; 8 import java.util.Enumeration; 9 import java.util.List; 10 import java.util.zip.ZipEntry; 11 import java.util.zip.ZipException; 12 import java.util.zip.ZipFile; 7 import java.io.InputStream; 8 import java.io.OutputStream; 13 9 10 import javax.faces.context.ExternalContext; 14 11 import javax.faces.context.FacesContext; 15 12 import javax.servlet.ServletContext; 13 import javax.servlet.http.HttpServletResponse; 16 14 17 15 import ndg.services.newmoon.NewmoonException; 18 import ndg.services.newmoon.XMIEncoder;19 import ndg.services.newmoon.NewmoonManager.GML_VERSION;20 16 21 17 import org.apache.commons.io.FileUtils; 22 18 import org.apache.commons.io.FilenameUtils; 23 19 import org.apache.commons.io.IOUtils; 24 import org.apache.commons.io.filefilter.AndFileFilter;25 import org.apache.commons.io.filefilter.DirectoryFileFilter;26 import org.apache.commons.io.filefilter.IOFileFilter;27 import org.apache.commons.io.filefilter.NotFileFilter;28 import org.apache.commons.io.filefilter.RegexFileFilter;29 20 import org.richfaces.event.FileUploadEvent; 30 21 import org.richfaces.model.UploadedFile; 22 import org.slf4j.Logger; 23 import org.slf4j.LoggerFactory; 31 24 32 25 public class XMIEncoderBean { 33 26 34 private static GML_VERSION gmlVersion = GML_VERSION.v3_2; 35 36 private String getTempDir() { 37 ServletContext context = (ServletContext)FacesContext.getCurrentInstance().getExternalContext().getContext(); 38 return (String)context.getAttribute(NewmoonContextListener.TMP_DIR); 27 private XMIEncoderHelper helper = null; 28 private boolean encodeReady = false; 29 30 private static final ServletContext sc = (ServletContext) FacesContext.getCurrentInstance().getExternalContext() 31 .getContext(); 32 33 /** 34 * Logger for this class. 35 */ 36 private static final Logger LOG = LoggerFactory.getLogger(XMIEncoderBean.class); 37 38 public void xmiListener(FileUploadEvent event) throws NewmoonException { 39 UploadedFile requestItem = event.getUploadedFile(); 40 File requestTmpDir = new File((String) sc.getAttribute(NewmoonContextListener.TMP_DIR), 41 FilenameUtils.getBaseName(requestItem.getName())); 42 43 if (requestTmpDir.exists()) { 44 try { 45 FileUtils.deleteDirectory(requestTmpDir); 46 } catch (IOException e) { 47 LOG.warn("Problems to cleanup the tempDir", e); 48 } 49 } 50 51 requestTmpDir.mkdir(); 52 File zipRequest = new File(requestTmpDir, requestItem.getName()); 53 try { 54 FileUtils.writeByteArrayToFile(zipRequest, requestItem.getData()); 55 } catch (IOException e) { 56 throw new NewmoonException("Upload error", e); 57 } 58 helper = new XMIEncoderHelper(zipRequest); 59 new Thread(helper).start(); 39 60 } 40 61 41 public void xmiListener(FileUploadEvent event) throws NewmoonException { 42 UploadedFile item = event.getUploadedFile(); 43 File tmpDir = new File(getTempDir(), FilenameUtils.getBaseName(item.getName())); 44 tmpDir.mkdir(); 45 File zip = new File(tmpDir, item.getName()); 62 public boolean isEncodeReady() { 63 return helper != null ? helper.isEncodeReady() : false; 64 } 65 66 public void downloadZip(OutputStream out, Object data) throws IOException { 67 //data is the item passed throught the VALUE attribute in the a4j:mediaOutput 68 //actually is not used 69 boolean delete = false; 70 if (helper == null) 71 return; 46 72 try { 47 FileUtils.writeByteArrayToFile(zip, item.getData()); 48 unzipPackage(zip, tmpDir); 49 encodePackage(new File(tmpDir, FilenameUtils.getBaseName(item.getName()))); 50 FileUtils.deleteDirectory(tmpDir); 51 } catch (IOException e) { 52 throw new NewmoonException("Encoding error", e); 73 finalizeResponse(helper.getZipResponse(), out); 74 delete = true; 75 } finally { 76 if (delete) { 77 FileUtils.deleteDirectory(helper.getRequestTmpDir()); 78 helper = null; 79 encodeReady = false; 80 } 53 81 } 54 82 } 55 83 56 private void encodePackage(File tmpDir) throws IOException, NewmoonException { 57 File register = null; 58 File xmi = null; 84 private void finalizeResponse(File zipFile, OutputStream output) throws IOException { 85 // Prepare. 86 byte[] zip; 87 zip = IOUtils.toByteArray(new FileInputStream(zipFile)); 59 88 60 FileFilter ff = new RegexFileFilter("\\A.+-register\\..+\\Z"); 61 File[] regs = tmpDir.listFiles(ff); 62 if (regs == null || regs.length != 1) { 63 throw new IOException("none or more than one register"); 64 } 65 register = regs[0]; 89 FacesContext facesContext = FacesContext.getCurrentInstance(); 90 ExternalContext externalContext = facesContext.getExternalContext(); 91 HttpServletResponse response = (HttpServletResponse) externalContext.getResponse(); 66 92 67 List<IOFileFilter> ffs = new ArrayList<IOFileFilter>();68 ffs.add(new NotFileFilter((IOFileFilter) ff));69 ffs.add(new NotFileFilter(DirectoryFileFilter.DIRECTORY));93 response.reset(); 94 response.setContentType("application/zip"); 95 response.setHeader("Content-disposition", "attachment; filename=\"" + zipFile.getName() + "\""); 70 96 71 File[] xmis = tmpDir.listFiles((FileFilter)new AndFileFilter(ffs)); 72 if (xmis == null || xmis.length != 1) { 73 throw new IOException("none or more than one xmi"); 74 } 75 xmi = xmis[0]; 76 77 XMIEncoder encoder = new XMIEncoder(); 78 String workingPath = FilenameUtils.getBaseName(FilenameUtils.getFullPathNoEndSeparator(xmi.getPath())); 79 80 List<String> encFilePaths = encoder.doEncode(xmi, FilenameUtils.getName(register.getPath()), 81 workingPath, gmlVersion, FilenameUtils.getBaseName(xmi.getPath())); 82 for (String filePath : encFilePaths) { 83 System.out.println(filePath); 84 } 85 } 86 87 private void unzipPackage(File is, File tmpDir) throws ZipException, IOException { 88 ZipFile zipFile = new ZipFile(is); 89 90 if (!tmpDir.isDirectory()) { 91 throw new IOException("tmpDir is not a Directory"); 92 } 93 94 Enumeration<? extends ZipEntry> zipen = zipFile.entries(); 95 ZipEntry item = null; 96 String base = tmpDir.getPath() + File.separatorChar; 97 while (zipen.hasMoreElements()) { 98 item = zipen.nextElement(); 99 100 if (item.isDirectory()) { 101 (new File(base + item.getName())).mkdir(); 102 continue; 103 } 104 105 // System.err.println("Extracting file: " + item.getName()); 106 IOUtils.copy(zipFile.getInputStream(item), new FileOutputStream(base + item.getName())); 107 } 97 // Write file to response. 98 //OutputStream output; 99 //output = response.getOutputStream(); 100 output.write(zip); 101 output.flush(); 102 output.close(); 103 // facesContext.responseComplete(); 108 104 } 109 105 }
Note: See TracChangeset
for help on using the changeset viewer.