Search in sources :

Example 1 with BadRequestResponse

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."));
        }
    }
}
Also used : AlreadySyncingResponse(org.syncany.operations.daemon.messages.AlreadySyncingResponse) BadRequestResponse(org.syncany.operations.daemon.messages.BadRequestResponse) Response(org.syncany.operations.daemon.messages.api.Response) FolderRequestHandler(org.syncany.operations.daemon.messages.api.FolderRequestHandler) BadRequestResponse(org.syncany.operations.daemon.messages.BadRequestResponse) AlreadySyncingResponse(org.syncany.operations.daemon.messages.AlreadySyncingResponse) File(java.io.File) ConfigException(org.syncany.config.ConfigException) Subscribe(com.google.common.eventbus.Subscribe)

Example 2 with BadRequestResponse

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."));
    }
}
Also used : BadRequestResponse(org.syncany.operations.daemon.messages.BadRequestResponse) Request(org.syncany.operations.daemon.messages.api.Request) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Example 3 with BadRequestResponse

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;
}
Also used : AlreadySyncingResponse(org.syncany.operations.daemon.messages.AlreadySyncingResponse) FolderResponse(org.syncany.operations.daemon.messages.api.FolderResponse) BadRequestResponse(org.syncany.operations.daemon.messages.BadRequestResponse) HttpResponse(org.apache.http.HttpResponse) Response(org.syncany.operations.daemon.messages.api.Response) BadRequestResponse(org.syncany.operations.daemon.messages.BadRequestResponse) AlreadySyncingResponse(org.syncany.operations.daemon.messages.AlreadySyncingResponse) FolderResponse(org.syncany.operations.daemon.messages.api.FolderResponse)

Example 4 with BadRequestResponse

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."));
    }
}
Also used : AddWatchManagementResponse(org.syncany.operations.daemon.messages.AddWatchManagementResponse) RemoveWatchManagementResponse(org.syncany.operations.daemon.messages.RemoveWatchManagementResponse) ListWatchesManagementResponse(org.syncany.operations.daemon.messages.ListWatchesManagementResponse) BadRequestResponse(org.syncany.operations.daemon.messages.BadRequestResponse) Response(org.syncany.operations.daemon.messages.api.Response) ManagementRequestHandler(org.syncany.operations.daemon.messages.api.ManagementRequestHandler) BadRequestResponse(org.syncany.operations.daemon.messages.BadRequestResponse) ConfigException(org.syncany.config.ConfigException) Subscribe(com.google.common.eventbus.Subscribe)

Example 5 with BadRequestResponse

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."));
    }
}
Also used : BadRequestResponse(org.syncany.operations.daemon.messages.BadRequestResponse) File(java.io.File) FileInputStream(java.io.FileInputStream)

Aggregations

BadRequestResponse (org.syncany.operations.daemon.messages.BadRequestResponse)5 Response (org.syncany.operations.daemon.messages.api.Response)3 Subscribe (com.google.common.eventbus.Subscribe)2 File (java.io.File)2 ConfigException (org.syncany.config.ConfigException)2 AlreadySyncingResponse (org.syncany.operations.daemon.messages.AlreadySyncingResponse)2 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 HttpResponse (org.apache.http.HttpResponse)1 AddWatchManagementResponse (org.syncany.operations.daemon.messages.AddWatchManagementResponse)1 ListWatchesManagementResponse (org.syncany.operations.daemon.messages.ListWatchesManagementResponse)1 RemoveWatchManagementResponse (org.syncany.operations.daemon.messages.RemoveWatchManagementResponse)1 FolderRequestHandler (org.syncany.operations.daemon.messages.api.FolderRequestHandler)1 FolderResponse (org.syncany.operations.daemon.messages.api.FolderResponse)1 ManagementRequestHandler (org.syncany.operations.daemon.messages.api.ManagementRequestHandler)1 Request (org.syncany.operations.daemon.messages.api.Request)1