throw new NeedNewOAuthTokenException("POST with no OAuth credentials: " + userContext);
}
try {
xsrfHelper.verify(XSRF_ACTION, requireParameter(req, "token"));
} catch (XsrfTokenExpiredException e) {
throw new BadRequestException(e);
} catch (InvalidSecurityTokenException e) {
throw new BadRequestException(e);
}
String action = requireParameter(req, "action");
if ("findwaves".equals(action) || "findandimport".equals(action)) {
SourceInstance instance =
sourceInstanceFactory.parseUnchecked(requireParameter(req, "instance"));
// Rather than enqueueing just one interval 2008-01-01 to 2013-01-01, we
// split that interval into random parts. See the note on randomization
// in FindRemoteWavesProcessor.
log.info("Enqueueing find waves tasks");
@Nullable ImportSettings autoImportSettings;
if ("findwaves".equals(action)) {
autoImportSettings = null;
} else {
autoImportSettings = new ImportSettingsGsonImpl();
autoImportSettings.setSynthesizeHistory(!preserveHistory);
if ("private".equals(requireParameter(req, "sharingmode"))) {
autoImportSettings.setSharingMode(ImportSharingMode.PRIVATE);
} else if ("shared".equals(requireParameter(req, "sharingmode"))) {
autoImportSettings.setSharingMode(ImportSharingMode.SHARED);
} else if ("privateunlessparticipant".equals(requireParameter(req, "sharingmode"))) {
autoImportSettings.setSharingMode(ImportSharingMode.PRIVATE_UNLESS_PARTICIPANT);
} else {
throw new BadRequestException("Bad sharingmode");
}
}
enqueueTasks(findProcessor.get().makeRandomTasksForInterval(instance,
DaysSinceEpoch.fromYMD(2008, 1, 1),
DaysSinceEpoch.fromYMD(2013, 1, 1),
autoImportSettings));
} else if ("importwavelet".equals(action)) {
SourceInstance instance =
sourceInstanceFactory.parseUnchecked(requireParameter(req, "instance"));
WaveId waveId = WaveId.deserialise(requireParameter(req, "waveid"));
WaveletId waveletId = WaveletId.deserialise(requireParameter(req, "waveletid"));
ImportWaveletTask task = new ImportWaveletTaskGsonImpl();
task.setInstance(instance.serialize());
task.setWaveId(waveId.serialise());
task.setWaveletId(waveletId.serialise());
ImportSettings settings = new ImportSettingsGsonImpl();
if ("private".equals(requireParameter(req, "sharingmode"))) {
settings.setSharingMode(ImportSettings.ImportSharingMode.PRIVATE);
} else if ("shared".equals(requireParameter(req, "sharingmode"))) {
settings.setSharingMode(ImportSettings.ImportSharingMode.SHARED);
} else {
throw new BadRequestException("Unexpected import sharing mode");
}
settings.setSynthesizeHistory(!preserveHistory);
task.setSettings(settings);
@Nullable String existingSlobIdToIgnore = optionalParameter(req, "ignoreexisting", null);
if (existingSlobIdToIgnore != null) {
task.setExistingSlobIdToIgnore(existingSlobIdToIgnore);
}
final ImportTaskPayload payload = new ImportTaskPayloadGsonImpl();
payload.setImportWaveletTask(task);
log.info("Enqueueing import task for " + waveId
+ "; synthesizeHistory=" + task.getSettings().getSynthesizeHistory());
enqueueTasks(ImmutableList.of(payload));
} else if ("canceltasks".equals(action)) {
log.info("Cancelling all tasks for " + userId);
try {
new RetryHelper().run(new RetryHelper.VoidBody() {
@Override public void run() throws RetryableFailure, PermanentFailure {
CheckedTransaction tx = datastore.get().beginTransaction();
try {
if (perUserTable.get().deleteAllTasks(tx, userId)) {
tx.commit();
}
} finally {
tx.close();
}
}
});
} catch (PermanentFailure e) {
throw new IOException("Failed to delete tasks", e);
}
} else if ("forgetwaves".equals(action)) {
log.info("Forgetting all waves for " + userId);
try {
new RetryHelper().run(new RetryHelper.VoidBody() {
@Override public void run() throws RetryableFailure, PermanentFailure {
CheckedTransaction tx = datastore.get().beginTransaction();
try {
if (perUserTable.get().deleteAllWaves(tx, userId)) {
tx.commit();
}
} finally {
tx.close();
}
}
});
} catch (PermanentFailure e) {
throw new IOException("Failed to delete tasks", e);
}
} else {
throw new BadRequestException("Unknown action: " + action);
}
// TODO(ohler): Send 303, not 302. See
// http://en.wikipedia.org/wiki/Post/Redirect/Get .
resp.sendRedirect(req.getServletPath());
}