001/** 002 * Copyright 2014 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 066 * @return 201 with the transaction id and expiration date 067 * @throws URISyntaxException 068 */ 069 @POST 070 public Response createTransaction(@PathParam("path") final String externalPath, 071 @Context final HttpServletRequest req) throws URISyntaxException { 072 073 if (session instanceof TxSession) { 074 final Transaction t = txService.getTransaction(session); 075 LOGGER.debug("renewing transaction {}", t.getId()); 076 t.updateExpiryDate(); 077 return noContent().expires(t.getExpires()).build(); 078 } 079 080 if (externalPath != null && !externalPath.isEmpty()) { 081 return status(BAD_REQUEST).build(); 082 } 083 084 final Principal userPrincipal = req.getUserPrincipal(); 085 String userName = null; 086 if (userPrincipal != null) { 087 userName = userPrincipal.getName(); 088 } 089 090 final Transaction t = txService.beginTransaction(session, userName); 091 LOGGER.info("Created transaction '{}'", t.getId()); 092 093 return created(new URI(translator().toDomain("/tx:" + t.getId()).toString())).expires( 094 t.getExpires()).build(); 095 } 096 097 /** 098 * Commit a transaction resource 099 * 100 * @param externalPath 101 * @return 204 102 */ 103 @POST 104 @Path("fcr:commit") 105 public Response commit(@PathParam("path") final String externalPath) { 106 LOGGER.info("Commit transaction '{}'", externalPath); 107 return finalizeTransaction(externalPath, true); 108 109 } 110 111 /** 112 * Rollback a transaction 113 * @return 204 114 */ 115 @POST 116 @Path("fcr:rollback") 117 public Response rollback(@PathParam("path") final String externalPath) { 118 LOGGER.info("Rollback transaction '{}'", externalPath); 119 return finalizeTransaction(externalPath, false); 120 } 121 122 private Response finalizeTransaction(@PathParam("path") 123 final String externalPath, final boolean commit) { 124 125 final String path = toPath(translator(), externalPath); 126 if (!path.equals("/")) { 127 return status(BAD_REQUEST).build(); 128 } 129 130 final String txId; 131 if (session instanceof TxSession) { 132 txId = ((TxSession) session).getTxId(); 133 } else { 134 txId = ""; 135 } 136 137 if (txId.isEmpty()) { 138 LOGGER.debug("cannot finalize an empty tx id {} at path {}", 139 txId, path); 140 return status(BAD_REQUEST).build(); 141 } 142 143 if (commit) { 144 LOGGER.debug("commiting transaction {} at path {}", txId, path); 145 txService.commit(txId); 146 147 } else { 148 LOGGER.debug("rolling back transaction {} at path {}", txId, 149 path); 150 txService.rollback(txId); 151 } 152 return noContent().build(); 153 } 154 155 @Override 156 protected Session session() { 157 return session; 158 } 159}