Moving and Renaming resources

In Webdav moving and renaming files and folders are all done with the MOVE request, which is supported in Milton with the MoveableResource interface.

void moveTo(CollectionResource rDest, String name) throws ConflictException, NotAuthorizedException, BadRequestException;

The incoming MOVE request includes a URL which identifies the resource to move and a destination url to which it should be moved. Milton seperates the destination into the parent collection and the name, and provides that information seperately when it calls your move method

In our example, for simplicity, we will permit renames but we will not allow moves to other collections.

So modify the GalaxyResource class as follows:

public class GalaxyResource extends AbstractResource implements CollectionResource, MakeCollectionableResource, MoveableResource {

    private static org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger(GalaxyResource.class);
    private ArrayList<Resource> children;
    private final RootUniverseResource parent;
    private final Galaxy galaxy;

    public GalaxyResource(RootUniverseResource parent, Galaxy galaxy) {
        this.parent = parent;
        this.galaxy = galaxy;
    }

    @Override
    public void moveTo(CollectionResource rDest, String newName) throws ConflictException, NotAuthorizedException, BadRequestException {
        if( rDest != parent ) {
            throw new BadRequestException("Cant move galaxy to a different folder. Current parent=" +parent.getName() + " dest parent=" + rDest.getName());
        }
        newName = newName.replace(".properties", ""); // need to strip suffix we add in getName method
        galaxy.setName(newName);
    }

This will throw a bad request exception if the user tries to move to a different folder, but permits renames. Actually permitting folder moves isnt hard, just need to update the parent reference on the data object.