use of com.canoo.platform.core.http.HttpURLConnectionFactory in project dolphin-platform by canoo.
the class HttpClientProvider method createService.
@Override
protected HttpClient createService(ClientConfiguration configuration) {
final HttpURLConnectionFactory connectionFactory = configuration.getHttpURLConnectionFactory();
final HttpClientImpl client = new HttpClientImpl(PlatformClient.getService(Gson.class), connectionFactory, configuration);
final ServiceLoader<RequestHandlerProvider> requestLoader = ServiceLoader.load(RequestHandlerProvider.class);
final Iterator<RequestHandlerProvider> requestIterator = requestLoader.iterator();
while (requestIterator.hasNext()) {
client.addRequestHandler(requestIterator.next().getHandler(configuration));
}
final ServiceLoader<ResponseHandlerProvider> responseLoader = ServiceLoader.load(ResponseHandlerProvider.class);
final Iterator<ResponseHandlerProvider> responseIterator = responseLoader.iterator();
while (responseIterator.hasNext()) {
client.addResponseHandler(responseIterator.next().getHandler(configuration));
}
return client;
}
use of com.canoo.platform.core.http.HttpURLConnectionFactory in project dolphin-platform by canoo.
the class TestDolphinPlatformHttpClientConnector method testBadResponse.
@Test(expectedExceptions = DolphinRemotingException.class)
public void testBadResponse() throws DolphinRemotingException, URISyntaxException {
PlatformClient.init(new HeadlessToolkit());
PlatformClient.getClientConfiguration().setHttpURLConnectionFactory(new HttpURLConnectionFactory() {
@Override
public HttpURLConnection create(URI url) throws IOException {
return new HttpURLConnection(url.toURL()) {
@Override
public void disconnect() {
}
@Override
public boolean usingProxy() {
return false;
}
@Override
public void connect() throws IOException {
}
@Override
public OutputStream getOutputStream() throws IOException {
return new ByteArrayOutputStream();
}
};
}
});
final ClientModelStore clientModelStore = new ClientModelStore(new DefaultModelSynchronizer(() -> null));
final DolphinPlatformHttpClientConnector connector = new DolphinPlatformHttpClientConnector(getDummyURL(), PlatformClient.getClientConfiguration(), clientModelStore, new JsonCodec(), new SimpleExceptionHandler(), new HttpClientImpl(new Gson(), PlatformClient.getClientConfiguration()));
final List<Command> commands = new ArrayList<>();
commands.add(new CreateContextCommand());
connector.transmit(commands);
}
use of com.canoo.platform.core.http.HttpURLConnectionFactory in project dolphin-platform by canoo.
the class TestDolphinPlatformHttpClientConnector method testSimpleCall.
@Test
public void testSimpleCall() throws DolphinRemotingException, URISyntaxException {
PlatformClient.init(new HeadlessToolkit());
PlatformClient.getClientConfiguration().setHttpURLConnectionFactory(new HttpURLConnectionFactory() {
@Override
public HttpURLConnection create(URI url) throws IOException {
return new HttpURLConnection(url.toURL()) {
@Override
public void disconnect() {
}
@Override
public boolean usingProxy() {
return false;
}
@Override
public void connect() throws IOException {
}
@Override
public int getResponseCode() throws IOException {
return HttpStatus.HTTP_OK;
}
@Override
public OutputStream getOutputStream() throws IOException {
return new ByteArrayOutputStream();
}
@Override
public InputStream getInputStream() throws IOException {
String response = "[{\"pmId\":\"p1\",\"clientSideOnly\":false,\"id\":\"CreatePresentationModel\",\"attributes\":[],\"pmType\":null,\"className\":\"com.canoo.dp.impl.remoting.legacy.communication.CreatePresentationModelCommand\"}]";
return new ByteArrayInputStream(response.getBytes("UTF-8"));
}
@Override
public String getHeaderField(String name) {
if (PlatformConstants.CLIENT_ID_HTTP_HEADER_NAME.equals(name)) {
return "TEST-ID";
}
return super.getHeaderField(name);
}
};
}
});
final ClientModelStore clientModelStore = new ClientModelStore(new DefaultModelSynchronizer(() -> null));
final DolphinPlatformHttpClientConnector connector = new DolphinPlatformHttpClientConnector(getDummyURL(), PlatformClient.getClientConfiguration(), clientModelStore, new JsonCodec(), new SimpleExceptionHandler(), PlatformClient.getService(HttpClient.class));
final CreatePresentationModelCommand command = new CreatePresentationModelCommand();
command.setPmId("p1");
Command rawCommand = command;
final List<Command> result = connector.transmit(Collections.singletonList(rawCommand));
Assert.assertEquals(result.size(), 1);
Assert.assertTrue(result.get(0) instanceof CreatePresentationModelCommand);
Assert.assertEquals(((CreatePresentationModelCommand) result.get(0)).getPmId(), "p1");
}
Aggregations