use of com.cinchapi.concourse.thrift.TransactionToken in project concourse by cinchapi.
the class ConcourseServer method stage.
@Override
@TranslateClientExceptions
@PluginRestricted
@VerifyAccessToken
public TransactionToken stage(AccessToken creds, String env) throws TException {
TransactionToken token = new TransactionToken(creds, Time.now());
Transaction transaction = getEngine(env).startTransaction();
transactions.put(token, transaction);
Logger.info("Started Transaction {}", transaction);
return token;
}
use of com.cinchapi.concourse.thrift.TransactionToken in project concourse by cinchapi.
the class PluginSerializerTest method randomRemoteMessage.
/**
* Generate a random {@link RemoteMessage} for testing.
*
* @return a RemoteMessage
*/
private RemoteMessage randomRemoteMessage() {
int seed = Random.getInt();
RemoteMessage message;
if (seed % 3 == 0) {
message = Reflection.newInstance("com.cinchapi.concourse.server.plugin.RemoteAttributeExchange", Random.getString(), Random.getString());
} else if (seed % 2 == 0) {
AccessToken creds = new AccessToken(ByteBuffer.wrap(Random.getString().getBytes(StandardCharsets.UTF_8)));
TransactionToken transaction = new TransactionToken(creds, Time.now());
String method = Random.getSimpleString();
String environment = Random.getSimpleString();
int argsCount = Math.abs(Random.getInt()) % 8;
ComplexTObject[] args = new ComplexTObject[argsCount];
for (int i = 0; i < argsCount; ++i) {
args[i] = ComplexTObject.fromJavaObject(Random.getObject());
}
message = Reflection.newInstance("com.cinchapi.concourse.server.plugin.RemoteMethodRequest", method, creds, transaction, environment, args);
} else {
AccessToken creds = new AccessToken(ByteBuffer.wrap(Random.getString().getBytes(StandardCharsets.UTF_8)));
ComplexTObject response = ComplexTObject.fromJavaObject(Random.getObject());
message = Reflection.newInstance("com.cinchapi.concourse.server.plugin.RemoteMethodResponse", creds, response);
}
return message;
}
use of com.cinchapi.concourse.thrift.TransactionToken in project concourse by cinchapi.
the class AccessTokenVerificationAdvice method invoke.
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
AccessToken token = null;
TransactionToken transaction = null;
Object[] args = invocation.getArguments();
int index = 0;
while (token == null && (transaction == null || index < args.length)) {
Object arg = args[index];
if (arg instanceof AccessToken) {
token = (AccessToken) arg;
} else if (arg instanceof TransactionToken) {
transaction = (TransactionToken) arg;
}
++index;
}
if (token != null) {
ConcourseServer concourse = (ConcourseServer) invocation.getThis();
if (concourse.inspector().isValidToken(token)) {
if (transaction == null || (transaction != null && transaction.getAccessToken().equals(token) && concourse.inspector().isValidTransaction(transaction))) {
return invocation.proceed();
} else {
throw new IllegalArgumentException("Invalid transaction");
}
} else {
throw new SecurityException("Invalid access token");
}
} else {
throw new SecurityException("Unauthorized");
}
}
use of com.cinchapi.concourse.thrift.TransactionToken in project concourse by cinchapi.
the class HttpServer method initialize.
/**
* Initialize a {@link EndpointContainer container} by registering all of
* its
* endpoints.
*
* @param container the {@link EndpointContainer} to initialize
*/
private static void initialize(EndpointContainer container) {
for (final Endpoint endpoint : container.endpoints()) {
String action = endpoint.getAction();
Route route = new Route(endpoint.getPath()) {
@Override
public Object handle(Request request, Response response) {
response.type(endpoint.getContentType().toString());
// The HttpRequests preprocessor assigns attributes to the
// request in order for the Endpoint to make calls into
// ConcourseServer.
AccessToken creds = (AccessToken) request.attribute(GlobalState.HTTP_ACCESS_TOKEN_ATTRIBUTE);
String environment = MoreObjects.firstNonNull((String) request.attribute(GlobalState.HTTP_ENVIRONMENT_ATTRIBUTE), GlobalState.DEFAULT_ENVIRONMENT);
String fingerprint = (String) request.attribute(GlobalState.HTTP_FINGERPRINT_ATTRIBUTE);
// does the fingerprint match?
if ((boolean) request.attribute(GlobalState.HTTP_REQUIRE_AUTH_ATTRIBUTE) && creds == null) {
halt(401);
}
if (!Strings.isNullOrEmpty(fingerprint) && !fingerprint.equals(HttpRequests.getFingerprint(request))) {
Logger.warn("Request made with mismatching fingerprint. Expecting {} but got {}", HttpRequests.getFingerprint(request), fingerprint);
halt(401);
}
TransactionToken transaction = null;
try {
Long timestamp = Longs.tryParse((String) request.attribute(GlobalState.HTTP_TRANSACTION_TOKEN_ATTRIBUTE));
transaction = creds != null && timestamp != null ? new TransactionToken(creds, timestamp) : transaction;
} catch (NullPointerException e) {
}
try {
return endpoint.serve(request, response, creds, transaction, environment);
} catch (Exception e) {
if (e instanceof HttpError) {
response.status(((HttpError) e).getCode());
} else if (e instanceof SecurityException || e instanceof java.lang.SecurityException) {
response.removeCookie(GlobalState.HTTP_AUTH_TOKEN_COOKIE);
response.status(401);
} else if (e instanceof IllegalArgumentException) {
response.status(400);
} else {
response.status(500);
Logger.error("", e);
}
JsonObject json = new JsonObject();
json.addProperty("error", e.getMessage());
return json.toString();
}
}
};
if (action.equals("get")) {
Spark.get(route);
} else if (action.equals("post")) {
Spark.post(route);
} else if (action.equals("put")) {
Spark.put(route);
} else if (action.equals("delete")) {
Spark.delete(route);
} else if (action.equals("upsert")) {
Spark.post(route);
Spark.put(route);
} else if (action.equals("options")) {
Spark.options(route);
}
}
}
use of com.cinchapi.concourse.thrift.TransactionToken in project concourse by cinchapi.
the class ConcourseThriftDriver method abort.
@Override
public void abort() {
execute(() -> {
if (transaction != null) {
final TransactionToken token = transaction;
transaction = null;
core.abort(creds, token, environment);
}
return null;
});
}
Aggregations