001/**
002 * Copyright 2015 DuraSpace, Inc.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.fcrepo.http.api;
017
018import static javax.ws.rs.core.Response.created;
019import static javax.ws.rs.core.Response.noContent;
020import static javax.ws.rs.core.Response.status;
021import static javax.ws.rs.core.Response.Status.BAD_REQUEST;
022import static org.slf4j.LoggerFactory.getLogger;
023
024import java.net.URI;
025import java.net.URISyntaxException;
026import java.security.Principal;
027
028import javax.inject.Inject;
029import javax.jcr.Session;
030import javax.servlet.http.HttpServletRequest;
031import javax.ws.rs.POST;
032import javax.ws.rs.Path;
033import javax.ws.rs.PathParam;
034import javax.ws.rs.core.Context;
035import javax.ws.rs.core.Response;
036
037import org.fcrepo.kernel.Transaction;
038import org.fcrepo.kernel.TxSession;
039import org.fcrepo.kernel.services.TransactionService;
040import org.slf4j.Logger;
041import org.springframework.beans.factory.annotation.Autowired;
042import org.springframework.context.annotation.Scope;
043
044/**
045 * Transactions over REST
046 *
047 * @author awoods
048 * @author gregjan
049 */
050@Scope("prototype")
051@Path("/{path: .*}/fcr:tx")
052public class FedoraTransactions extends FedoraBaseResource {
053
054    private static final Logger LOGGER = getLogger(FedoraTransactions.class);
055
056    @Autowired
057    private TransactionService txService;
058
059    @Inject
060    protected Session session;
061
062    /**
063     * Create a new transaction resource and add it to the registry
064     *
065     * @param externalPath the external path
066     * @param req the http servlet request
067     * @return 201 with the transaction id and expiration date
068     * @throws URISyntaxException if URI syntax exception occurred
069     */
070    @POST
071    public Response createTransaction(@PathParam("path") final String externalPath,
072                                      @Context final HttpServletRequest req) throws URISyntaxException {
073
074        if (session instanceof TxSession) {
075            final Transaction t = txService.getTransaction(session);
076            LOGGER.debug("renewing transaction {}", t.getId());
077            t.updateExpiryDate();
078            return noContent().expires(t.getExpires()).build();
079        }
080
081        if (externalPath != null && !externalPath.isEmpty()) {
082            return status(BAD_REQUEST).build();
083        }
084
085        final Principal userPrincipal = req.getUserPrincipal();
086        String userName = null;
087        if (userPrincipal != null) {
088            userName = userPrincipal.getName();
089        }
090
091        final Transaction t = txService.beginTransaction(session, userName);
092        LOGGER.info("Created transaction '{}'", t.getId());
093
094        return created(new URI(translator().toDomain("/tx:" + t.getId()).toString())).expires(
095                t.getExpires()).build();
096    }
097
098    /**
099     * Commit a transaction resource
100     *
101     * @param externalPath the external path
102     * @return 204
103     */
104    @POST
105    @Path("fcr:commit")
106    public Response commit(@PathParam("path") final String externalPath) {
107        LOGGER.info("Commit transaction '{}'", externalPath);
108        return finalizeTransaction(externalPath, true);
109
110    }
111
112    /**
113     * Rollback a transaction
114     *
115     * @param externalPath the external path
116     * @return 204
117     */
118    @POST
119    @Path("fcr:rollback")
120    public Response rollback(@PathParam("path") final String externalPath) {
121        LOGGER.info("Rollback transaction '{}'", externalPath);
122        return finalizeTransaction(externalPath, false);
123    }
124
125    private Response finalizeTransaction(@PathParam("path")
126        final String externalPath, final boolean commit) {
127
128        final String path = toPath(translator(), externalPath);
129        if (!path.equals("/")) {
130            return status(BAD_REQUEST).build();
131        }
132
133        final String txId;
134        if (session instanceof TxSession) {
135            txId = ((TxSession) session).getTxId();
136        } else {
137            txId = "";
138        }
139
140        if (txId.isEmpty()) {
141            LOGGER.debug("cannot finalize an empty tx id {} at path {}",
142                    txId, path);
143            return status(BAD_REQUEST).build();
144        }
145
146        if (commit) {
147            LOGGER.debug("commiting transaction {} at path {}", txId, path);
148            txService.commit(txId);
149
150        } else {
151            LOGGER.debug("rolling back transaction {} at path {}", txId,
152                    path);
153            txService.rollback(txId);
154        }
155        return noContent().build();
156    }
157
158    @Override
159    protected Session session() {
160        return session;
161    }
162}