Search in sources :

Example 16 with CommandSpecException

use of org.locationtech.geogig.web.api.CommandSpecException in project GeoGig by boundlessgeo.

the class UpdateRefWeb method run.

/**
     * Runs the command and builds the appropriate response.
     * 
     * @param context - the context to use for this command
     * 
     * @throws CommandSpecException
     */
@Override
public void run(CommandContext context) {
    if (name == null) {
        throw new CommandSpecException("No name was given.");
    } else if (!(delete) && newValue == null) {
        throw new CommandSpecException("Nothing specified to update with, must specify either deletion or new value to update to.");
    }
    final Context geogig = this.getCommandLocator(context);
    Optional<Ref> ref;
    try {
        ref = geogig.command(RefParse.class).setName(name).call();
        if (!ref.isPresent()) {
            throw new CommandSpecException("Invalid name: " + name);
        }
        if (ref.get() instanceof SymRef) {
            Optional<Ref> target = geogig.command(RefParse.class).setName(newValue).call();
            if (target.isPresent() && !(target.get() instanceof SymRef)) {
                ref = geogig.command(UpdateSymRef.class).setDelete(delete).setName(name).setNewValue(target.get().getName()).call();
            } else {
                throw new CommandSpecException("Invalid new target: " + newValue);
            }
        } else {
            Optional<ObjectId> target = geogig.command(RevParse.class).setRefSpec(newValue).call();
            if (target.isPresent()) {
                ref = geogig.command(UpdateRef.class).setDelete(delete).setName(ref.get().getName()).setNewValue(target.get()).call();
            } else {
                throw new CommandSpecException("Invalid new value: " + newValue);
            }
        }
    } catch (Exception e) {
        context.setResponseContent(CommandResponse.error("Aborting UpdateRef: " + e.getMessage()));
        return;
    }
    if (ref.isPresent()) {
        final Ref newRef = ref.get();
        context.setResponseContent(new CommandResponse() {

            @Override
            public void write(ResponseWriter out) throws Exception {
                out.start();
                out.writeUpdateRefResponse(newRef);
                out.finish();
            }
        });
    }
}
Also used : Context(org.locationtech.geogig.api.Context) CommandContext(org.locationtech.geogig.web.api.CommandContext) ObjectId(org.locationtech.geogig.api.ObjectId) UpdateRef(org.locationtech.geogig.api.plumbing.UpdateRef) CommandResponse(org.locationtech.geogig.web.api.CommandResponse) CommandSpecException(org.locationtech.geogig.web.api.CommandSpecException) UpdateSymRef(org.locationtech.geogig.api.plumbing.UpdateSymRef) Ref(org.locationtech.geogig.api.Ref) UpdateRef(org.locationtech.geogig.api.plumbing.UpdateRef) SymRef(org.locationtech.geogig.api.SymRef) UpdateSymRef(org.locationtech.geogig.api.plumbing.UpdateSymRef) SymRef(org.locationtech.geogig.api.SymRef) UpdateSymRef(org.locationtech.geogig.api.plumbing.UpdateSymRef) ResponseWriter(org.locationtech.geogig.web.api.ResponseWriter) RefParse(org.locationtech.geogig.api.plumbing.RefParse) CommandSpecException(org.locationtech.geogig.web.api.CommandSpecException)

Example 17 with CommandSpecException

use of org.locationtech.geogig.web.api.CommandSpecException in project GeoGig by boundlessgeo.

the class BeginTransaction method run.

/**
     * Runs the command and builds the appropriate response.
     * 
     * @param context - the context to use for this command
     * 
     * @throws CommandSpecException
     */
@Override
public void run(CommandContext context) {
    if (this.getTransactionId() != null) {
        throw new CommandSpecException("Tried to start a transaction within a transaction.");
    }
    final GeoGIG geogig = context.getGeoGIG();
    final GeogigTransaction transaction = geogig.command(TransactionBegin.class).call();
    context.setResponseContent(new CommandResponse() {

        @Override
        public void write(ResponseWriter out) throws Exception {
            out.start();
            out.writeTransactionId(transaction.getTransactionId());
            out.finish();
        }
    });
}
Also used : GeogigTransaction(org.locationtech.geogig.api.GeogigTransaction) ResponseWriter(org.locationtech.geogig.web.api.ResponseWriter) TransactionBegin(org.locationtech.geogig.api.plumbing.TransactionBegin) CommandSpecException(org.locationtech.geogig.web.api.CommandSpecException) CommandResponse(org.locationtech.geogig.web.api.CommandResponse) GeoGIG(org.locationtech.geogig.api.GeoGIG) CommandSpecException(org.locationtech.geogig.web.api.CommandSpecException)

Example 18 with CommandSpecException

use of org.locationtech.geogig.web.api.CommandSpecException in project GeoGig by boundlessgeo.

the class BlameWebOp method run.

/**
     * Runs the command and builds the appropriate response.
     * 
     * @param context - the context to use for this command
     */
@Override
public void run(CommandContext context) {
    final Context geogig = this.getCommandLocator(context);
    Optional<ObjectId> commit = Optional.absent();
    if (branchOrCommit != null) {
        commit = geogig.command(RevParse.class).setRefSpec(branchOrCommit).call();
        if (!commit.isPresent()) {
            throw new CommandSpecException("Could not resolve branch or commit");
        }
    }
    try {
        final BlameReport report = geogig.command(BlameOp.class).setPath(path).setCommit(commit.orNull()).call();
        context.setResponseContent(new CommandResponse() {

            @Override
            public void write(ResponseWriter out) throws Exception {
                out.start();
                try {
                    out.writeBlameReport(report);
                } catch (XMLStreamException e) {
                    throw new CommandSpecException("Error writing stream.");
                }
                out.finish();
            }
        });
    } catch (BlameException e) {
        switch(e.statusCode) {
            case PATH_NOT_FEATURE:
                throw new CommandSpecException("The supplied path does not resolve to a feature");
            case FEATURE_NOT_FOUND:
                throw new CommandSpecException("The supplied path does not exist");
        }
    }
}
Also used : Context(org.locationtech.geogig.api.Context) CommandContext(org.locationtech.geogig.web.api.CommandContext) BlameOp(org.locationtech.geogig.api.porcelain.BlameOp) BlameException(org.locationtech.geogig.api.porcelain.BlameException) ObjectId(org.locationtech.geogig.api.ObjectId) BlameReport(org.locationtech.geogig.api.porcelain.BlameReport) CommandResponse(org.locationtech.geogig.web.api.CommandResponse) BlameException(org.locationtech.geogig.api.porcelain.BlameException) XMLStreamException(javax.xml.stream.XMLStreamException) CommandSpecException(org.locationtech.geogig.web.api.CommandSpecException) XMLStreamException(javax.xml.stream.XMLStreamException) ResponseWriter(org.locationtech.geogig.web.api.ResponseWriter) RevParse(org.locationtech.geogig.api.plumbing.RevParse) CommandSpecException(org.locationtech.geogig.web.api.CommandSpecException)

Example 19 with CommandSpecException

use of org.locationtech.geogig.web.api.CommandSpecException in project GeoGig by boundlessgeo.

the class CheckoutWebOp method run.

/**
     * Runs the command and builds the appropriate response
     * 
     * @throws CommandSpecException
     */
@Override
public void run(CommandContext context) {
    if (this.getTransactionId() == null) {
        throw new CommandSpecException("No transaction was specified, checkout requires a transaction to preserve the stability of the repository.");
    }
    final Context geogig = this.getCommandLocator(context);
    CheckoutOp command = geogig.command(CheckoutOp.class);
    if (branchOrCommit != null) {
        Optional<Ref> head = geogig.command(RefParse.class).setName(Ref.HEAD).call();
        if (!head.isPresent()) {
            throw new CommandSpecException("Repository has no HEAD, can't merge.");
        }
        final String target = ((SymRef) head.get()).getTarget();
        command.setSource(branchOrCommit).call();
        context.setResponseContent(new CommandResponse() {

            @Override
            public void write(ResponseWriter out) throws Exception {
                out.start();
                out.writeElement("OldTarget", target);
                out.writeElement("NewTarget", branchOrCommit);
                out.finish();
            }
        });
    } else if (path != null) {
        command.addPath(path);
        if (ours && !theirs) {
            command.setOurs(ours);
        } else if (theirs && !ours) {
            command.setTheirs(theirs);
        } else {
            throw new CommandSpecException("Please specify either ours or theirs to update the feature path specified.");
        }
        command.call();
        context.setResponseContent(new CommandResponse() {

            @Override
            public void write(ResponseWriter out) throws Exception {
                out.start();
                out.writeElement("Path", path);
                out.writeElement("Strategy", ours ? "ours" : "theirs");
                out.finish();
            }
        });
    } else {
        throw new CommandSpecException("No branch or commit specified for checkout.");
    }
}
Also used : Context(org.locationtech.geogig.api.Context) CommandContext(org.locationtech.geogig.web.api.CommandContext) Ref(org.locationtech.geogig.api.Ref) SymRef(org.locationtech.geogig.api.SymRef) SymRef(org.locationtech.geogig.api.SymRef) ResponseWriter(org.locationtech.geogig.web.api.ResponseWriter) CheckoutOp(org.locationtech.geogig.api.porcelain.CheckoutOp) CommandSpecException(org.locationtech.geogig.web.api.CommandSpecException) CommandResponse(org.locationtech.geogig.web.api.CommandResponse) CommandSpecException(org.locationtech.geogig.web.api.CommandSpecException)

Example 20 with CommandSpecException

use of org.locationtech.geogig.web.api.CommandSpecException in project GeoGig by boundlessgeo.

the class Commit method run.

/**
     * Runs the command and builds the appropriate response
     * 
     * @param context - the context to use for this command
     * @throws CommandSpecException
     */
@Override
public void run(CommandContext context) {
    if (this.getTransactionId() == null) {
        throw new CommandSpecException("No transaction was specified, commit requires a transaction to preserve the stability of the repository.");
    }
    final Context geogig = this.getCommandLocator(context);
    RevCommit commit;
    try {
        commit = geogig.command(CommitOp.class).setAuthor(authorName.orNull(), authorEmail.orNull()).setMessage(message).setAllowEmpty(true).setAll(all).call();
        assert commit != null;
    } catch (NothingToCommitException noChanges) {
        context.setResponseContent(CommandResponse.warning("Nothing to commit"));
        commit = null;
    } catch (IllegalStateException e) {
        context.setResponseContent(CommandResponse.warning(e.getMessage()));
        commit = null;
    }
    if (commit != null) {
        final RevCommit commitToWrite = commit;
        final ObjectId parentId = commit.parentN(0).or(ObjectId.NULL);
        final Iterator<DiffEntry> diff = geogig.command(DiffOp.class).setOldVersion(parentId).setNewVersion(commit.getId()).call();
        context.setResponseContent(new CommandResponse() {

            @Override
            public void write(ResponseWriter out) throws Exception {
                out.start();
                out.writeCommitResponse(commitToWrite, diff);
                out.finish();
            }
        });
    }
}
Also used : Context(org.locationtech.geogig.api.Context) CommandContext(org.locationtech.geogig.web.api.CommandContext) ObjectId(org.locationtech.geogig.api.ObjectId) NothingToCommitException(org.locationtech.geogig.api.porcelain.NothingToCommitException) DiffOp(org.locationtech.geogig.api.porcelain.DiffOp) CommandResponse(org.locationtech.geogig.web.api.CommandResponse) NothingToCommitException(org.locationtech.geogig.api.porcelain.NothingToCommitException) CommandSpecException(org.locationtech.geogig.web.api.CommandSpecException) ResponseWriter(org.locationtech.geogig.web.api.ResponseWriter) CommandSpecException(org.locationtech.geogig.web.api.CommandSpecException) RevCommit(org.locationtech.geogig.api.RevCommit) DiffEntry(org.locationtech.geogig.api.plumbing.diff.DiffEntry)

Aggregations

CommandSpecException (org.locationtech.geogig.web.api.CommandSpecException)25 CommandResponse (org.locationtech.geogig.web.api.CommandResponse)21 ResponseWriter (org.locationtech.geogig.web.api.ResponseWriter)21 Context (org.locationtech.geogig.api.Context)16 CommandContext (org.locationtech.geogig.web.api.CommandContext)15 ObjectId (org.locationtech.geogig.api.ObjectId)9 RevCommit (org.locationtech.geogig.api.RevCommit)9 Optional (com.google.common.base.Optional)6 NodeRef (org.locationtech.geogig.api.NodeRef)5 Ref (org.locationtech.geogig.api.Ref)5 RevObjectParse (org.locationtech.geogig.api.plumbing.RevObjectParse)5 IOException (java.io.IOException)4 RevFeature (org.locationtech.geogig.api.RevFeature)4 RevFeatureType (org.locationtech.geogig.api.RevFeatureType)4 RevObject (org.locationtech.geogig.api.RevObject)4 RevTree (org.locationtech.geogig.api.RevTree)4 GeoGIG (org.locationtech.geogig.api.GeoGIG)3 GeogigTransaction (org.locationtech.geogig.api.GeogigTransaction)3 Remote (org.locationtech.geogig.api.Remote)3 FindTreeChild (org.locationtech.geogig.api.plumbing.FindTreeChild)3