Skip to content

Commit

Permalink
Add proxy support to liveobjects connector
Browse files Browse the repository at this point in the history
  • Loading branch information
maciej-cz committed May 24, 2024
1 parent 673ca8c commit cfe90d2
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
class ConnectorWizardStep1 implements LNSConnectorWizardStep {

private final List<PropertyDescription> propertyDescriptions = List.of(
PropertyDescription.text("apikey", "API Key", true).withEncrypted(true));
PropertyDescription.text("apikey", "API Key", true).withEncrypted(true),
PropertyDescription.text("proxy-host", "Proxy Host", false),
PropertyDescription.number("proxy-port", "Proxy Port", false));

public String getName() {
return "step1";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
package lora.ns.liveobjects;

import static org.apache.commons.lang3.StringUtils.isNotBlank;
import static org.apache.commons.lang3.StringUtils.isNumeric;

import feign.Client.Proxied;
import feign.Feign.Builder;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.Proxy.Type;
import java.util.Base64;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Properties;

import org.apache.commons.lang3.NotImplementedException;
Expand Down Expand Up @@ -68,10 +77,13 @@ protected void init() {
final ch.qos.logback.classic.Logger serviceLogger = (ch.qos.logback.classic.Logger) LoggerFactory
.getLogger("lora.ns.liveobjects");
serviceLogger.setLevel(ch.qos.logback.classic.Level.DEBUG);
var feignBuilder = Feign.builder().decoder(new JacksonDecoder(objectMapper))
.encoder(new JacksonEncoder(objectMapper)).logger(new Slf4jLogger("lora.ns.liveobjects"))
var feignBuilder = Feign.builder()
.decoder(new JacksonDecoder(objectMapper))
.encoder(new JacksonEncoder(objectMapper))
.logger(new Slf4jLogger("lora.ns.liveobjects"))
.logLevel(Level.FULL)
.requestInterceptor(template -> template.header("X-API-KEY", properties.getProperty("apikey")));
addProxyConfigIfPresent(feignBuilder);
service = feignBuilder.target(LiveObjectsService.class, "https://liveobjects.orange-business.com/");
}

Expand Down Expand Up @@ -185,4 +197,24 @@ public List<String> getProfiles() {
public List<Group> getGroups() {
return service.getGroups();
}

private void addProxyConfigIfPresent(Builder feignBuilder) {
Optional<String> proxyHost = getProxyHost();
Optional<Integer> proxyPost = getProxyPost();
proxyHost.map(host -> new InetSocketAddress(host, proxyPost.orElse(80)))
.map(address -> new Proxy(Type.HTTP, address))
.ifPresent(proxy -> feignBuilder.client(new Proxied(null, null, proxy)));
}

private Optional<String> getProxyHost() {
return isNotBlank(properties.getProperty("proxy-host")) ?
Optional.of(properties.getProperty("proxy-host")) :
Optional.empty();
}

private Optional<Integer> getProxyPost() {
return isNumeric(properties.getProperty("proxy-port")) ?
Optional.of(Integer.parseInt(properties.getProperty("proxy-port"))) :
Optional.empty();
}
}

0 comments on commit cfe90d2

Please sign in to comment.