use of org.openremote.model.auth.UsernamePassword in project openremote by openremote.
the class WebsocketAgentProtocol method doCreateIoClient.
@Override
protected WebsocketIOClient<String> doCreateIoClient() throws Exception {
String uriStr = agent.getConnectUri().orElseThrow(() -> new IllegalArgumentException("Missing or invalid connectUri: " + agent));
URI uri = new URI(uriStr);
/* We're going to fail hard and fast if optional meta items are incorrectly configured */
Optional<OAuthGrant> oAuthGrant = agent.getOAuthGrant();
Optional<UsernamePassword> usernameAndPassword = agent.getUsernamePassword();
Optional<ValueType.MultivaluedStringMap> headers = agent.getConnectHeaders();
Optional<WebsocketSubscription[]> subscriptions = agent.getConnectSubscriptions();
if (!oAuthGrant.isPresent() && usernameAndPassword.isPresent()) {
String authValue = BasicAuthHelper.createHeader(usernameAndPassword.get().getUsername(), usernameAndPassword.get().getPassword());
headers = Optional.of(headers.map(h -> {
h.remove(HttpHeaders.AUTHORIZATION);
h.replace(HttpHeaders.AUTHORIZATION, Collections.singletonList(authValue));
return h;
}).orElseGet(() -> {
ValueType.MultivaluedStringMap h = new ValueType.MultivaluedStringMap();
h.put(HttpHeaders.AUTHORIZATION, Collections.singletonList(authValue));
return h;
}));
}
clientHeaders = headers.orElse(null);
WebsocketIOClient<String> websocketClient = new WebsocketIOClient<>(uri, headers.orElse(null), oAuthGrant.orElse(null));
Map<String, List<String>> finalHeaders = headers.orElse(null);
subscriptions.ifPresent(websocketSubscriptions -> addProtocolConnectedTask(() -> doSubscriptions(finalHeaders, websocketSubscriptions)));
return websocketClient;
}
use of org.openremote.model.auth.UsernamePassword in project openremote by openremote.
the class HTTPProtocol method doStart.
@Override
protected void doStart(Container container) throws Exception {
String baseUri = agent.getBaseURI().orElseThrow(() -> new IllegalArgumentException("Missing or invalid base URI attribute: " + this));
if (baseUri.endsWith("/")) {
baseUri = baseUri.substring(0, baseUri.length() - 1);
}
URI uri;
try {
uri = new URIBuilder(baseUri).build();
} catch (URISyntaxException e) {
LOG.log(Level.SEVERE, "Invalid URI", e);
throw e;
}
/* We're going to fail hard and fast if optional meta items are incorrectly configured */
Optional<OAuthGrant> oAuthGrant = agent.getOAuthGrant();
Optional<UsernamePassword> usernameAndPassword = agent.getUsernamePassword();
boolean followRedirects = agent.getFollowRedirects().orElse(false);
Optional<ValueType.MultivaluedStringMap> headers = agent.getRequestHeaders();
Optional<ValueType.MultivaluedStringMap> queryParams = agent.getRequestQueryParameters();
Integer readTimeout = agent.getRequestTimeoutMillis().orElse(null);
WebTargetBuilder webTargetBuilder;
if (readTimeout != null) {
webTargetBuilder = new WebTargetBuilder(WebTargetBuilder.createClient(executorService, WebTargetBuilder.CONNECTION_POOL_SIZE, readTimeout.longValue(), null), uri);
} else {
webTargetBuilder = new WebTargetBuilder(client, uri);
}
if (oAuthGrant.isPresent()) {
LOG.info("Adding OAuth");
webTargetBuilder.setOAuthAuthentication(oAuthGrant.get());
} else {
usernameAndPassword.ifPresent(userPass -> {
LOG.info("Adding Basic Authentication");
webTargetBuilder.setBasicAuthentication(userPass.getUsername(), userPass.getPassword());
});
}
headers.ifPresent(webTargetBuilder::setInjectHeaders);
queryParams.ifPresent(webTargetBuilder::setInjectQueryParameters);
webTargetBuilder.followRedirects(followRedirects);
LOG.fine("Creating web target client '" + baseUri + "'");
webTarget = webTargetBuilder.build();
setConnectionStatus(ConnectionStatus.CONNECTED);
}
Aggregations