public Response makeDirectoryOrUpdateFile()

in taverna-server-webapp/src/main/java/org/apache/taverna/server/master/DirectoryREST.java [255:296]


	public Response makeDirectoryOrUpdateFile(List<PathSegment> parent,
			MakeOrUpdateDirEntry op, UriInfo ui) throws NoUpdateException,
			FilesystemAccessException, NoDirectoryEntryException {
		support.permitUpdate(run);
		DirectoryEntry container = fileUtils.getDirEntry(run, parent);
		if (!(container instanceof Directory))
			throw new FilesystemAccessException("You may not "
					+ ((op instanceof MakeDirectory) ? "make a subdirectory of"
							: "place a file in") + " a file.");
		if (op.name == null || op.name.length() == 0)
			throw new FilesystemAccessException("missing name attribute");
		Directory d = (Directory) container;
		UriBuilder ub = secure(ui).path("{name}");

		// Make a directory in the context directory

		if (op instanceof MakeDirectory) {
			Directory target = d.makeSubdirectory(support.getPrincipal(),
					op.name);
			return created(ub.build(target.getName())).build();
		}

		// Make or set the contents of a file

		File f = null;
		for (DirectoryEntry e : d.getContents()) {
			if (e.getName().equals(op.name)) {
				if (e instanceof Directory)
					throw new FilesystemAccessException(
							"You may not overwrite a directory with a file.");
				f = (File) e;
				break;
			}
		}
		if (f == null) {
			f = d.makeEmptyFile(support.getPrincipal(), op.name);
			f.setContents(op.contents);
			return created(ub.build(f.getName())).build();
		}
		f.setContents(op.contents);
		return seeOther(ub.build(f.getName())).build();
	}