Search in sources :

Example 1 with Request

use of org.syncany.operations.daemon.messages.api.Request in project syncany by syncany.

the class XmlMessageFactoryTest method testXmlToRequestSuccess.

@Test
public void testXmlToRequestSuccess() throws Exception {
    Request request = XmlMessageFactory.toRequest("<getFileFolderRequest><id>1234</id><root>/some/path</root><fileHistoryId>beefbeefbeef</fileHistoryId><version>1337</version></getFileFolderRequest>");
    assertEquals(GetFileFolderRequest.class, request.getClass());
    assertEquals(1234, ((GetFileFolderRequest) request).getId());
    assertEquals("/some/path", ((GetFileFolderRequest) request).getRoot());
    assertEquals("beefbeefbeef", ((GetFileFolderRequest) request).getFileHistoryId());
    assertEquals(1337, ((GetFileFolderRequest) request).getVersion());
}
Also used : ListWatchesManagementRequest(org.syncany.operations.daemon.messages.ListWatchesManagementRequest) GetFileFolderRequest(org.syncany.operations.daemon.messages.GetFileFolderRequest) Request(org.syncany.operations.daemon.messages.api.Request) Test(org.junit.Test)

Example 2 with Request

use of org.syncany.operations.daemon.messages.api.Request in project syncany by syncany.

the class JsonMessageFactoryTest method testJsonToRequestSuccess.

@Test
public void testJsonToRequestSuccess() throws Exception {
    Request request = JsonMessageFactory.toRequest("{\"GetFileFolderRequest\":{\"id\":\"1234\",\"root\":\"/some/path\",\"fileHistoryId\":\"beefbeefbeef\",\"version\":\"1337\"}}");
    assertEquals(GetFileFolderRequest.class, request.getClass());
    assertEquals(1234, ((GetFileFolderRequest) request).getId());
    assertEquals("/some/path", ((GetFileFolderRequest) request).getRoot());
    assertEquals("beefbeefbeef", ((GetFileFolderRequest) request).getFileHistoryId());
    assertEquals(1337, ((GetFileFolderRequest) request).getVersion());
}
Also used : ListWatchesManagementRequest(org.syncany.operations.daemon.messages.ListWatchesManagementRequest) GetFileFolderRequest(org.syncany.operations.daemon.messages.GetFileFolderRequest) Request(org.syncany.operations.daemon.messages.api.Request) Test(org.junit.Test)

Example 3 with Request

use of org.syncany.operations.daemon.messages.api.Request 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 4 with Request

use of org.syncany.operations.daemon.messages.api.Request in project syncany by syncany.

the class JsonMessageFactoryTest method testJsonToRequestSuccess.

@Test
public void testJsonToRequestSuccess() throws Exception {
    Request request = JsonMessageFactory.toRequest("{\"GetFileFolderRequest\":{\"id\":\"1234\",\"root\":\"/some/path\",\"fileHistoryId\":\"beefbeefbeef\",\"version\":\"1337\"}}");
    assertEquals(GetFileFolderRequest.class, request.getClass());
    assertEquals(1234, ((GetFileFolderRequest) request).getId());
    assertEquals("/some/path", ((GetFileFolderRequest) request).getRoot());
    assertEquals("beefbeefbeef", ((GetFileFolderRequest) request).getFileHistoryId());
    assertEquals(1337, ((GetFileFolderRequest) request).getVersion());
}
Also used : ListWatchesManagementRequest(org.syncany.operations.daemon.messages.ListWatchesManagementRequest) GetFileFolderRequest(org.syncany.operations.daemon.messages.GetFileFolderRequest) Request(org.syncany.operations.daemon.messages.api.Request) Test(org.junit.Test)

Example 5 with Request

use of org.syncany.operations.daemon.messages.api.Request in project syncany by syncany.

the class CommandLineClient method sendToRest.

private int sendToRest(Command command, String commandName, String[] commandArgs, File portFile) {
    try {
        // Read port config (for daemon) from port file
        PortTO portConfig = readPortConfig(portFile);
        // Create authentication details
        CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        credentialsProvider.setCredentials(new AuthScope(SERVER_HOSTNAME, portConfig.getPort()), new UsernamePasswordCredentials(portConfig.getUser().getUsername(), portConfig.getUser().getPassword()));
        // Allow all hostnames in CN; this is okay as long as hostname is localhost/127.0.0.1!
        // See: https://github.com/syncany/syncany/pull/196#issuecomment-52197017
        X509HostnameVerifier hostnameVerifier = new AllowAllHostnameVerifier();
        // Fetch the SSL context (using the user key/trust store)
        SSLContext sslContext = UserConfig.createUserSSLContext();
        // Create client with authentication details
        CloseableHttpClient client = HttpClients.custom().setSslcontext(sslContext).setHostnameVerifier(hostnameVerifier).setDefaultCredentialsProvider(credentialsProvider).build();
        // Build and send request, print response
        Request request = buildFolderRequestFromCommand(command, commandName, commandArgs, config.getLocalDir().getAbsolutePath());
        String serverUri = SERVER_SCHEMA + SERVER_HOSTNAME + ":" + portConfig.getPort() + SERVER_REST_API;
        String xmlMessageString = XmlMessageFactory.toXml(request);
        StringEntity xmlMessageEntity = new StringEntity(xmlMessageString);
        HttpPost httpPost = new HttpPost(serverUri);
        httpPost.setEntity(xmlMessageEntity);
        logger.log(Level.INFO, "Sending HTTP Request to: " + serverUri);
        logger.log(Level.FINE, httpPost.toString());
        logger.log(Level.FINE, xmlMessageString);
        HttpResponse httpResponse = client.execute(httpPost);
        int exitCode = handleRestResponse(command, httpResponse);
        return exitCode;
    } catch (Exception e) {
        logger.log(Level.SEVERE, "Command " + command.toString() + " FAILED. ", e);
        return showErrorAndExit(e.getMessage());
    }
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpPost(org.apache.http.client.methods.HttpPost) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) AllowAllHostnameVerifier(org.apache.http.conn.ssl.AllowAllHostnameVerifier) Request(org.syncany.operations.daemon.messages.api.Request) FolderRequest(org.syncany.operations.daemon.messages.api.FolderRequest) PortTO(org.syncany.config.to.PortTO) HttpResponse(org.apache.http.HttpResponse) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider) SSLContext(javax.net.ssl.SSLContext) ConfigException(org.syncany.config.ConfigException) IOException(java.io.IOException) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) StringEntity(org.apache.http.entity.StringEntity) X509HostnameVerifier(org.apache.http.conn.ssl.X509HostnameVerifier) AuthScope(org.apache.http.auth.AuthScope)

Aggregations

Request (org.syncany.operations.daemon.messages.api.Request)6 Test (org.junit.Test)4 GetFileFolderRequest (org.syncany.operations.daemon.messages.GetFileFolderRequest)4 ListWatchesManagementRequest (org.syncany.operations.daemon.messages.ListWatchesManagementRequest)4 IOException (java.io.IOException)2 FileNotFoundException (java.io.FileNotFoundException)1 SSLContext (javax.net.ssl.SSLContext)1 HttpResponse (org.apache.http.HttpResponse)1 AuthScope (org.apache.http.auth.AuthScope)1 UsernamePasswordCredentials (org.apache.http.auth.UsernamePasswordCredentials)1 CredentialsProvider (org.apache.http.client.CredentialsProvider)1 HttpPost (org.apache.http.client.methods.HttpPost)1 AllowAllHostnameVerifier (org.apache.http.conn.ssl.AllowAllHostnameVerifier)1 X509HostnameVerifier (org.apache.http.conn.ssl.X509HostnameVerifier)1 StringEntity (org.apache.http.entity.StringEntity)1 BasicCredentialsProvider (org.apache.http.impl.client.BasicCredentialsProvider)1 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)1 ConfigException (org.syncany.config.ConfigException)1 PortTO (org.syncany.config.to.PortTO)1 BadRequestResponse (org.syncany.operations.daemon.messages.BadRequestResponse)1