Skip to content
This repository has been archived by the owner on Oct 9, 2020. It is now read-only.

Commit

Permalink
Failed to pass host this way
Browse files Browse the repository at this point in the history
  • Loading branch information
solsson committed May 26, 2019
1 parent 17c2ac3 commit 98ea58a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,10 @@ void updateDispatchersFromConfig() {
logger.info("The list of {} update targets is ready", dispatchers);
}

private List<String> getTargetsConfig() {
return java.util.Arrays.asList(targetsConfig.orElse("").split(TARGETS_CONFIG_SEPARATOR_REGEX));
List<String> getTargetsConfig() {
String conf = targetsConfig.orElse(null);
if (conf == null) return Collections.emptyList();
return java.util.Arrays.asList(conf.split(TARGETS_CONFIG_SEPARATOR_REGEX));
}

void stopDispatcher(UpdatesDispatcher dispatcher) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.apache.http.HttpHost;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
Expand All @@ -25,24 +26,28 @@ public class UpdatesDispatcherHttp implements UpdatesDispatcher {
ResponseHandlerAck responseHandler = new ResponseHandlerAck();
UpdateTarget target;
CloseableHttpClient client;
HttpClientContext context;

public UpdatesDispatcherHttp(String configuredTarget) {
target = new UpdateTarget(configuredTarget);
HttpHost host = target.getHttpclientContextHost(); // If we want to manage contexts
logger.info("Creating http client for host {} target {}", host, target);

context = HttpClientContext.create();
context.setTargetHost(host);

BasicHttpClientConnectionManager connectionManager = new BasicHttpClientConnectionManager();
client = HttpClients.createMinimal(connectionManager);
}

@Override
public void dispatch(String topicName, UpdatesBodyPerTopic body) throws TargetAckFailedException {
HttpPost post = new HttpPost("http://localhost/");
HttpPost post = new HttpPost(target.getHttpUriFromHost(topicName));
post.addHeader(UpdatesBodyPerTopic.HEADER_TOPIC, topicName); // TODO body should declare all headers instead
post.setEntity(getEntity(body));
ResponseResult result;
try {
result = client.execute(post, responseHandler);
result = client.execute(post, responseHandler, context);
} catch (ClientProtocolException e) {
throw new TargetAckFailedException(e);
} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,23 @@

import static org.junit.jupiter.api.Assertions.*;

import java.util.Optional;

import org.junit.jupiter.api.Test;

class OnUpdateForwarderTest {

@Test
void testGetDispatchers() {
void testGetTargetsConfig() {
OnUpdateForwarder forwarder = new OnUpdateForwarder();

forwarder.targetsConfig = Optional.empty();
assertNotNull(forwarder.getTargetsConfig());
assertEquals(0, forwarder.getTargetsConfig().size());

forwarder.targetsConfig = Optional.of("http://example.net/");
assertEquals(1, forwarder.getTargetsConfig().size());
assertEquals("http://example.net/", forwarder.getTargetsConfig().get(0));
}

}

0 comments on commit 98ea58a

Please sign in to comment.