public static synchronized String getNextDraftID(VFS vfs, String speciesID) { int nextID = 0; VFSPath rootURI; try { rootURI = VFSUtils.parseVFSPath(ServerPaths.getDraftAssessmentRootURL(speciesID)); } catch (VFSPathParseException e) { e.printStackTrace(); return null; } if (vfs.exists(rootURI)) { // If there's already regionals for this guy, // get the next VFSPathToken[] tokens; try { tokens = vfs.list(rootURI); for (VFSPathToken curToken : tokens) { String filename = curToken.toString(); filename = filename.replaceAll(".xml", ""); if (!SISContainerApp.amIOnline()) filename = filename.replaceAll("offline", ""); System.out.println("Crawling file " + curToken + " for new regional ID."); try { int value = Integer.valueOf(filename.split("_")[1]); nextID = Math.max(value, nextID); } catch (NumberFormatException e) { SysDebugger.getNamedInstance(SISContainerApp.SEVERE_LOG) .println( "Annoying file in path " + curToken + " non-conformant " + "to standard region assessment file name pattern."); } } } catch (NotFoundException e) { SysDebugger.getNamedInstance(SISContainerApp.SEVERE_LOG) .println("Big WTF. " + "List failed on existing path " + rootURI.toString()); return null; } nextID++; // Increment it one past the highest found. } String assessmentID = speciesID + "_" + nextID; return assessmentID; }
private boolean buildSite() { String url = hostRoot + "/" + siteID + "/vfs"; final File spec = new File(url); boolean dirsMade; try { dirsMade = spec.mkdirs(); } catch (SecurityException e) { return false; } if (!dirsMade) // Either site exists or didnt make dirs return false; String[] reqDirs = new String[] {"HEAD", "META", "UNDO"}; for (int i = 0; i < reqDirs.length && dirsMade; i++) { try { dirsMade = new File(url + "/" + reqDirs[i]).mkdir(); } catch (SecurityException e) { return false; } } if (!dirsMade) return false; vfs = null; try { vfs = VFSFactory.getVersionedVFS(spec); } catch (final NotFoundException nf) { setErrorMessage("VFS " + spec.getPath() + " could not be opened."); } final VFSPathToken[] defaultDirectories = new VFSPathToken[] { new VFSPathToken("templates"), new VFSPathToken("includes"), new VFSPathToken("images"), new VFSPathToken("css") }; for (VFSPathToken directory : defaultDirectories) { try { vfs.makeCollections(VFSPath.ROOT.child(directory)); } catch (IOException e) { TrivialExceptionHandler.ignore(this, e); } } return DocumentUtils.writeVFSFile( "/index.html", vfs, DocumentUtils.impl.createDocumentFromString( "<html><head><title>" + siteID + " Index Page</title></head>" + "<body><h1>Hello, World!</h1>" + "<p>Welcome to your new GoGoEgo Site!</p>" + "<p><b>Site ID:</b> " + siteID + "</p>" + "<p><b>Regex Match:</b> " + matches + "</p>" + "<p><b>HTTPS Host:</b> " + (httpsHost == null ? "None" : httpsHost) + "</p>" + (repositoryURI == null ? "" : "<p><b>Repository URI:</b> " + repositoryURI + "</p>") + "<h2>Admin Tool</h2>" + "<p>To begin editing your site, please login to the " + "<a href=\"/admin/index.html\"> admin tool.</a></p>" + "</body></html>")); }
public Document doImport(final ZipInputStream zis, final ImportMode mode) throws ResourceException { final Document document = BaseDocumentUtils.impl.newDocument(); final Element root = document.createElement("results"); root.setAttribute("mode", mode.toString()); ZipEntry ze; try { ze = zis.getNextEntry(); } catch (IOException e) { throw new ResourceException(Status.SERVER_ERROR_INTERNAL, e); } byte[] buf = new byte[1024]; while (ze != null) { final VFSPath path; try { path = VFSUtils.parseVFSPath(ze.getName()); } catch (VFSUtils.VFSPathParseException e) { e.printStackTrace(); try { ze = zis.getNextEntry(); continue; } catch (IOException f) { throw new ResourceException(Status.SERVER_ERROR_INTERNAL, f); } } if (isRestricted(path)) { if (restrictionListener != null && !ze.isDirectory()) { try { final VFSPath restrictedUri = VFSUtils.parseVFSPath(ze.getName()); final ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] in_bytes = buf; int n; while ((n = zis.read(in_bytes, 0, 1024)) > -1) baos.write(in_bytes, 0, n); ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); restrictionListener.handle(restrictedUri, bais); } catch (Exception e) { e.printStackTrace(); TrivialExceptionHandler.ignore(this, e); } } try { ze = zis.getNextEntry(); continue; } catch (IOException f) { throw new ResourceException(Status.SERVER_ERROR_INTERNAL, f); } } final Element el; if (ze.isDirectory()) { el = document.createElement("folder"); el.setAttribute("path", path.toString()); if (!vfs.exists(path)) { try { vfs.makeCollections(path); } catch (IOException e) { // Let it fail below for proper iteration behavior TrivialExceptionHandler.ignore(this, e); } } else { try { el.setAttribute("status", "skipped"); root.appendChild(el); ze = zis.getNextEntry(); continue; } catch (IOException e) { throw new ResourceException(Status.SERVER_ERROR_INTERNAL, e); } } } else { el = document.createElement("file"); el.setAttribute("path", path.toString()); if (ImportMode.FRESHEN.equals(mode) && vfs.exists(path)) { try { long localTime = vfs.getLastModified(path); long remoteTime = ze.getTime(); if (localTime >= remoteTime) { el.setAttribute("status", "skipped"); root.appendChild(el); ze = zis.getNextEntry(); continue; } } catch (Exception e) { TrivialExceptionHandler.ignore(this, e); } } // Create parent dir if it does not already exist if (!vfs.exists(path.getCollection())) { try { vfs.makeCollections(path.getCollection()); } catch (IOException e) { // Let it fail below for proper iteration behavior TrivialExceptionHandler.ignore(this, e); } } final OutputStream os; try { os = vfs.getOutputStream(path); } catch (IOException e) { try { ze = zis.getNextEntry(); el.setAttribute("status", "error"); root.appendChild(el); continue; } catch (IOException f) { throw new ResourceException(Status.SERVER_ERROR_INTERNAL, f); } } int n; try { while ((n = zis.read(buf, 0, 1024)) > -1) os.write(buf, 0, n); } catch (IOException e) { el.setAttribute("status", "error"); root.appendChild(el); continue; } finally { try { os.close(); } catch (IOException e) { TrivialExceptionHandler.ignore(this, e); } } } el.setAttribute("status", "success"); root.appendChild(el); try { ze = zis.getNextEntry(); } catch (IOException e) { throw new ResourceException(Status.SERVER_ERROR_INTERNAL, e); } } try { zis.close(); } catch (IOException e) { TrivialExceptionHandler.ignore(this, e); } document.appendChild(root); return document; }