1 22 23 package org.meshcms.extra; 24 25 import java.io.*; 26 import java.net.*; 27 import javax.servlet.http.*; 28 import org.meshcms.core.*; 29 import org.meshcms.util.*; 30 31 35 public class StaticExporter extends DirectoryParser { 36 public static final String REQUEST_ATTRIBUTE_CHECK = "meshcms-is-exporting"; 37 public static final String USER_AGENT_HEADER = "User-Agent"; 38 39 public static final String USER_AGENT = WebSite.APP_NAME + ' ' + 40 WebSite.VERSION_ID + " (" + 41 System.getProperty("java.version") + ' ' + 42 System.getProperty("java.vendor") + ", " + 43 System.getProperty("os.name") + ' ' + 44 System.getProperty("os.version") + ' ' + 45 System.getProperty("os.arch") + ')'; 46 47 File staticDir; 48 URL contextURL; 49 boolean checkDates = true; 50 Writer writer; 51 WebSite webSite; 52 53 58 public StaticExporter(WebSite webSite, URL contextURL, File staticDir) { 59 super(); 60 setProcessStartDir(true); 61 setRecursive(true); 62 setInitialDir(webSite.getRootFile()); 63 setStaticDir(staticDir); 64 this.webSite = webSite; 65 this.contextURL = contextURL; 66 } 67 68 71 public void setStaticDir(File staticDir) { 72 this.staticDir = staticDir; 73 } 74 75 78 public File getStaticDir() { 79 return staticDir; 80 } 81 82 85 public void setContextURL(URL contextURL) { 86 this.contextURL = contextURL; 87 } 88 89 92 public URL getContextURL() { 93 return contextURL; 94 } 95 96 101 public void setCheckDates(boolean checkDates) { 102 this.checkDates = checkDates; 103 } 104 105 108 public boolean getCheckDates() { 109 return checkDates; 110 } 111 112 115 public void setWriter(Writer writer) { 116 this.writer = writer; 117 } 118 119 122 public Writer getWriter() { 123 return writer; 124 } 125 126 protected void postProcess() { 127 MainWebSite mainWebSite = null; 128 129 if (webSite instanceof VirtualWebSite) { 130 mainWebSite = ((VirtualWebSite) webSite).getMainWebSite(); 131 } 132 133 write(""); 134 StaticExportCopier copier = new StaticExportCopier(staticDir); 135 copier.setInitialDir(initialDir); 136 copier.setWriter(writer); 137 copier.setCheckDates(checkDates); 138 copier.process(); 139 140 write(""); 141 StaticExportCleaner cleaner = new StaticExportCleaner(initialDir); 142 cleaner.setInitialDir(staticDir); 143 cleaner.setWriter(writer); 144 145 if (mainWebSite != null) { 146 cleaner.setProtectedPath(webSite.getAdminPath()); 147 } 148 149 cleaner.process(); 150 151 if (mainWebSite != null) { 152 File adminStaticDir = webSite.getAdminPath().getFile(staticDir); 153 File adminDir = mainWebSite.getFile(mainWebSite.getAdminPath()); 154 155 write(""); 156 StaticExportCopier adminCopier = new StaticExportCopier(adminStaticDir); 157 adminCopier.setMkDirs(true); 158 adminCopier.setInitialDir(adminDir); 159 adminCopier.setWriter(writer); 160 adminCopier.setCheckDates(checkDates); 161 adminCopier.process(); 162 163 write(""); 164 StaticExportCleaner adminCleaner = new StaticExportCleaner(adminDir); 165 adminCleaner.setInitialDir(adminStaticDir); 166 adminCleaner.setWriter(writer); 167 adminCleaner.process(); 168 } 169 } 170 171 protected boolean preProcessDirectory(File file, Path path) { 172 File dir = path.getFile(staticDir); 173 174 if (isExportable(path)) { 175 dir.mkdirs(); 176 } else { 177 new DirectoryRemover(dir).process(); 178 } 179 180 return dir.isDirectory(); 181 } 182 183 protected void processFile(File file, Path path) { 184 File staticFile = path.getFile(staticDir); 185 String fileName = file.getName(); 186 187 try { 188 if (FileTypes.isPage(fileName)) { 189 long time = System.currentTimeMillis(); 190 URL url = new URL(contextURL, path.toString()); 191 HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 192 connection.setRequestProperty(USER_AGENT_HEADER, USER_AGENT); 193 InputStream in = connection.getInputStream(); 194 OutputStream out = new FileOutputStream(staticFile); 195 Utils.copyStream(in, out, true); 196 connection.disconnect(); 197 write(path + " page generated in " + (System.currentTimeMillis() - time) + " ms"); 198 } 199 } catch (IOException ex) { 200 ex.printStackTrace(); 201 } 202 } 203 204 void write(String message) { 205 if (writer != null) { 206 try { 207 writer.write(message); 208 writer.write('\n'); 209 writer.flush(); 210 } catch (IOException ex) {} 211 } 212 } 213 214 public boolean isExportable(Path path) { 215 if (path == null || path.isRelative()) { 216 return false; 217 } 218 219 if (path.isRoot()) { 220 return true; 221 } 222 223 if (webSite.getAdminPath() == null || 224 !(path.equals(webSite.getPrivatePath()) || 225 path.equals(webSite.getVirtualSitesPath()))) { 226 String level1 = path.getElementAt(0).toLowerCase(); 227 return !(level1.equals("web-inf") || level1.equals("meta-inf")); 228 } 229 230 return false; 231 } 232 233 public void process() { 234 Configuration conf = webSite.getConfiguration(); 236 int cacheType = conf.getCacheType(); 237 conf.setCacheType(Configuration.NO_CACHE); 238 239 write("base url: " + contextURL); 240 241 long time = System.currentTimeMillis(); 243 webSite.updateSiteMap(true); 244 write("site map updated in " + (System.currentTimeMillis() - time) + " ms"); 245 246 write(""); 247 super.process(); 248 249 conf.setCacheType(cacheType); 251 } 252 253 public static boolean isExportRequest(HttpServletRequest request) { 254 Boolean bo = (Boolean ) request.getAttribute(REQUEST_ATTRIBUTE_CHECK); 256 257 if (bo == null) { 258 String ua = Utils.noNull(request.getHeader(USER_AGENT_HEADER)); 259 bo = Boolean.valueOf(ua.startsWith(WebSite.APP_NAME)); 260 request.setAttribute(REQUEST_ATTRIBUTE_CHECK, bo); 261 } 262 263 return bo.booleanValue(); 264 } 265 } 266 | Popular Tags |