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());
}
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());
}
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."));
}
}
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());
}
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());
}
}
Aggregations