use of org.syncany.operations.daemon.messages.BadRequestResponse in project syncany by syncany.
the class WatchRunner method onRequestReceived.
@Subscribe
public void onRequestReceived(FolderRequest folderRequest) {
File requestRootFolder = new File(folderRequest.getRoot());
boolean localDirMatches = requestRootFolder.equals(config.getLocalDir());
if (localDirMatches) {
logger.log(Level.INFO, "Received " + folderRequest);
try {
if (!watchOperation.isSyncRunning() && !watchOperation.isSyncRequested()) {
watchOperation.pause();
FolderRequestHandler handler = FolderRequestHandler.createFolderRequestHandler(folderRequest, config);
Response response = handler.handleRequest(folderRequest);
if (response != null) {
eventBus.post(response);
}
watchOperation.resume();
} else {
logger.log(Level.WARNING, "FolderRequest discarded : ", folderRequest);
eventBus.post(new AlreadySyncingResponse(folderRequest.getId(), "FolderRequest discarded."));
}
} catch (Exception e) {
logger.log(Level.FINE, "Failed to process request", e);
eventBus.post(new BadRequestResponse(folderRequest.getId(), "Invalid request."));
}
}
}
use of org.syncany.operations.daemon.messages.BadRequestResponse in project syncany by syncany.
the class InternalRestHandler method handleNormalRequest.
private void handleNormalRequest(HttpServerExchange exchange) throws IOException {
// TODO [high] Read entire file to memory. Dangerous!
String message = IOUtils.toString(exchange.getInputStream());
logger.log(Level.INFO, "REST message received: " + message);
try {
Request request;
switch(requestFormatType) {
case JSON:
request = JsonMessageFactory.toRequest(message);
break;
case XML:
request = XmlMessageFactory.toRequest(message);
break;
default:
throw new Exception("Unknown request format. Valid formats are " + Joiner.on(", ").join(RequestFormatType.values()));
}
daemonWebServer.putRequestFormatType(request.getId(), requestFormatType);
daemonWebServer.putCacheRestRequest(request.getId(), exchange);
eventBus.post(request);
} catch (Exception e) {
logger.log(Level.WARNING, "Invalid request received; cannot serialize to Request.", e);
eventBus.post(new BadRequestResponse(-1, "Invalid request."));
}
}
use of org.syncany.operations.daemon.messages.BadRequestResponse in project syncany by syncany.
the class CommandLineClient method handleRestResponse.
private int handleRestResponse(Command command, HttpResponse httpResponse) throws Exception {
logger.log(Level.FINE, "Received HttpResponse: " + httpResponse);
String responseStr = IOUtils.toString(httpResponse.getEntity().getContent());
logger.log(Level.FINE, "Responding to message with responseString: " + responseStr);
Response response = XmlMessageFactory.toResponse(responseStr);
if (response instanceof FolderResponse) {
FolderResponse folderResponse = (FolderResponse) response;
command.printResults(folderResponse.getResult());
return 0;
} else if (response instanceof AlreadySyncingResponse) {
out.println("Daemon is already syncing, please retry later.");
return 1;
} else if (response instanceof BadRequestResponse) {
out.println(response.getMessage());
return 1;
}
return 1;
}
use of org.syncany.operations.daemon.messages.BadRequestResponse in project syncany by syncany.
the class WatchServer method onManagementRequestReceived.
@Subscribe
public void onManagementRequestReceived(ManagementRequest managementRequest) {
logger.log(Level.INFO, "Received " + managementRequest);
try {
ManagementRequestHandler handler = ManagementRequestHandler.createManagementRequestHandler(managementRequest);
Response response = handler.handleRequest(managementRequest);
if (response != null) {
eventBus.post(response);
}
} catch (ClassNotFoundException e) {
// Not logging 'e'!
logger.log(Level.FINE, "No handler found for management request class " + managementRequest.getClass() + ". Ignoring.");
} catch (Exception e) {
logger.log(Level.FINE, "Failed to process request", e);
eventBus.post(new BadRequestResponse(managementRequest.getId(), "Invalid request."));
}
}
use of org.syncany.operations.daemon.messages.BadRequestResponse in project syncany by syncany.
the class InternalRestHandler method handleFileRequest.
private void handleFileRequest(HttpServerExchange exchange) throws FileNotFoundException, IOException {
String tempFileToken = exchange.getRelativePath().substring("/file/".length());
File tempFile = daemonWebServer.getFileTokenTempFileFromCache(tempFileToken);
if (tempFile != null) {
logger.log(Level.INFO, "- Temp file: " + tempFileToken);
IOUtils.copy(new FileInputStream(tempFile), exchange.getOutputStream());
exchange.endExchange();
} else {
logger.log(Level.WARNING, "Invalid request received; Cannot find file token " + tempFileToken);
eventBus.post(new BadRequestResponse(-1, "Invalid request."));
}
}
Aggregations