@Override
  public void storeRepresentation(final Representation entity) throws ResourceException {
    try {
      if (vfs.exists(uri)) writeFile(entity);
      else {
        final VFSPath parent = uri.getCollection();
        if (VFSPath.ROOT.equals(parent) || vfs.exists(parent)) writeFile(entity);
        else {
          try {
            vfs.makeCollections(parent);
          } catch (IOException e) {
            throw new ResourceException(
                Status.SERVER_ERROR_INTERNAL, "Could not create directories.");
          }
          writeFile(entity);
        }
      }

      /*//if (!vfs.isCollection(uri)) {
      	final OutputStream os = vfs.getOutputStream(uri);
      	try {
      		final InputStream is = entity.getStream();
      		WritableVFSResource.copyStream(is, os);
      		is.close();
      		os.close();
      	} catch (final IOException exception) {
      		throw new ResourceException(Status.SERVER_ERROR_INTERNAL, exception);
      	}
      //} else
      //	getResponse().setStatus(Status.SERVER_ERROR_NOT_IMPLEMENTED);*/
    } catch (final NotFoundException nf) {
      nf.printStackTrace();
      throw new ResourceException(Status.CLIENT_ERROR_NOT_FOUND, nf);
    } catch (final ConflictException conflict) {
      throw new ResourceException(Status.CLIENT_ERROR_CONFLICT, conflict);
    } catch (final Exception part) {
      throw new ResourceException(Status.SERVER_ERROR_INSUFFICIENT_STORAGE, part);
    }
  }
Esempio n. 2
0
  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>"));
  }