/
FedoraBaseResource.java
148 lines (124 loc) · 4.91 KB
/
FedoraBaseResource.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
* The contents of this file are subject to the license and copyright
* detailed in the LICENSE and NOTICE files at the root of the source
* tree.
*/
package org.fcrepo.http.api;
import org.fcrepo.common.db.DbTransactionExecutor;
import org.fcrepo.http.commons.AbstractResource;
import org.fcrepo.http.commons.api.rdf.HttpIdentifierConverter;
import org.fcrepo.http.commons.session.TransactionProvider;
import org.fcrepo.kernel.api.Transaction;
import org.fcrepo.kernel.api.TransactionManager;
import org.fcrepo.kernel.api.exception.PathNotFoundException;
import org.fcrepo.kernel.api.identifiers.FedoraId;
import org.fcrepo.kernel.api.models.FedoraResource;
import org.fcrepo.kernel.api.models.ResourceFactory;
import org.fcrepo.kernel.api.models.ResourceHelper;
import org.slf4j.Logger;
import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.SecurityContext;
import java.security.Principal;
import static org.slf4j.LoggerFactory.getLogger;
/**
* @author cabeer
* @since 10/5/14
*/
abstract public class FedoraBaseResource extends AbstractResource {
private static final Logger LOGGER = getLogger(FedoraBaseResource.class);
@Context
protected SecurityContext securityContext;
@Inject
protected ResourceFactory resourceFactory;
@Inject
protected ResourceHelper resourceHelper;
@Context protected HttpServletRequest servletRequest;
@Inject
protected TransactionManager txManager;
@Inject
protected DbTransactionExecutor dbTransactionExecutor;
private TransactionProvider txProvider;
private Transaction transaction;
protected HttpIdentifierConverter identifierConverter;
protected HttpIdentifierConverter identifierConverter() {
if (identifierConverter == null) {
identifierConverter = new HttpIdentifierConverter(
uriInfo.getBaseUriBuilder().clone().path(FedoraLdp.class));
}
return identifierConverter;
}
/**
* Gets a fedora resource by id. Uses the provided transaction if it is uncommitted,
* or uses a new transaction.
*
* @param transaction the fedora transaction
* @param fedoraId identifier of the resource
* @return the requested FedoraResource
*/
protected FedoraResource getFedoraResource(final Transaction transaction, final FedoraId fedoraId)
throws PathNotFoundException {
return resourceFactory.getResource(transaction, fedoraId);
}
/**
* @param transaction the transaction in which to check
* @param fedoraId identifier of the object to check
* @param includeDeleted Whether to check for deleted resources too.
* @return Returns true if an object with the provided id exists
*/
protected boolean doesResourceExist(final Transaction transaction, final FedoraId fedoraId,
final boolean includeDeleted) {
return resourceHelper.doesResourceExist(transaction, fedoraId, includeDeleted);
}
/**
*
* @param transaction the transaction in which to check
* @param fedoraId identifier of the object to check
* @return Returns true if object does not exist but whose ID starts other resources that do exist.
*/
protected boolean isGhostNode(final Transaction transaction, final FedoraId fedoraId) {
return resourceHelper.isGhostNode(transaction, fedoraId);
}
protected String getUserPrincipal() {
final Principal p = securityContext.getUserPrincipal();
return p == null ? null : p.getName();
}
protected Transaction transaction() {
if (transaction == null) {
txProvider = new TransactionProvider(txManager, servletRequest,
uriInfo.getBaseUri(), fedoraPropsConfig.getJmsBaseUrl());
transaction = txProvider.provide();
}
return transaction;
}
/**
* Executes the runnable within a DB transaction that will retry entire block on MySQL deadlock exceptions.
* If the runnable throws an exception, after completing any retires, then the Fedora transaction is marked
* as failed.
*
* @param action the code to execute
*/
protected void doInDbTxWithRetry(final Runnable action) {
try {
dbTransactionExecutor.doInTxWithRetry(action);
} catch (RuntimeException e) {
transaction().fail();
throw e;
}
}
/**
* Executes the runnable within a DB transaction. The block is NOT retried on MySQL deadlock exceptions.
* If the runnable throws an exception, then the Fedora transaction is marked as failed.
*
* @param action the code to execute
*/
protected void doInDbTx(final Runnable action) {
try {
dbTransactionExecutor.doInTx(action);
} catch (RuntimeException e) {
transaction().fail();
throw e;
}
}
}