Search in sources :

Example 1 with UsernamePassword

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;
}
Also used : java.util(java.util) DEFAULT_CONTENT_TYPE(org.openremote.agent.protocol.http.HTTPProtocol.DEFAULT_CONTENT_TYPE) ConnectionStatus(org.openremote.model.asset.agent.ConnectionStatus) URISyntaxException(java.net.URISyntaxException) AttributeRef(org.openremote.model.attribute.AttributeRef) ValueUtil(org.openremote.model.util.ValueUtil) ResteasyWebTarget(org.jboss.resteasy.client.jaxrs.ResteasyWebTarget) Supplier(java.util.function.Supplier) WebTargetBuilder(org.openremote.container.web.WebTargetBuilder) Attribute(org.openremote.model.attribute.Attribute) AbstractNettyIOClientProtocol(org.openremote.agent.protocol.io.AbstractNettyIOClientProtocol) AttributeEvent(org.openremote.model.attribute.AttributeEvent) SyslogCategory(org.openremote.model.syslog.SyslogCategory) TextUtil(org.openremote.model.util.TextUtil) URI(java.net.URI) HttpHeaders(org.apache.http.HttpHeaders) OAuthGrant(org.openremote.model.auth.OAuthGrant) ValueType(org.openremote.model.value.ValueType) DEFAULT_HTTP_METHOD(org.openremote.agent.protocol.http.HTTPProtocol.DEFAULT_HTTP_METHOD) Pair(org.openremote.model.util.Pair) Invocation(javax.ws.rs.client.Invocation) Logger(java.util.logging.Logger) Entity(javax.ws.rs.client.Entity) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) Container(org.openremote.model.Container) PROTOCOL(org.openremote.model.syslog.SyslogCategory.PROTOCOL) BasicAuthHelper(org.jboss.resteasy.util.BasicAuthHelper) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) Response(javax.ws.rs.core.Response) ResteasyClient(org.jboss.resteasy.client.jaxrs.ResteasyClient) ChannelHandler(io.netty.channel.ChannelHandler) WebTargetBuilder.createClient(org.openremote.container.web.WebTargetBuilder.createClient) UsernamePassword(org.openremote.model.auth.UsernamePassword) ProtocolUtil(org.openremote.model.protocol.ProtocolUtil) AttributeExecuteStatus(org.openremote.model.attribute.AttributeExecuteStatus) ValueType(org.openremote.model.value.ValueType) URI(java.net.URI) UsernamePassword(org.openremote.model.auth.UsernamePassword) OAuthGrant(org.openremote.model.auth.OAuthGrant)

Example 2 with UsernamePassword

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);
}
Also used : URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) URIBuilder(org.apache.http.client.utils.URIBuilder) UsernamePassword(org.openremote.model.auth.UsernamePassword) WebTargetBuilder(org.openremote.container.web.WebTargetBuilder) OAuthGrant(org.openremote.model.auth.OAuthGrant)

Aggregations

URI (java.net.URI)2 URISyntaxException (java.net.URISyntaxException)2 WebTargetBuilder (org.openremote.container.web.WebTargetBuilder)2 OAuthGrant (org.openremote.model.auth.OAuthGrant)2 UsernamePassword (org.openremote.model.auth.UsernamePassword)2 ChannelHandler (io.netty.channel.ChannelHandler)1 java.util (java.util)1 TimeUnit (java.util.concurrent.TimeUnit)1 Consumer (java.util.function.Consumer)1 Supplier (java.util.function.Supplier)1 Logger (java.util.logging.Logger)1 Entity (javax.ws.rs.client.Entity)1 Invocation (javax.ws.rs.client.Invocation)1 MultivaluedHashMap (javax.ws.rs.core.MultivaluedHashMap)1 Response (javax.ws.rs.core.Response)1 HttpHeaders (org.apache.http.HttpHeaders)1 URIBuilder (org.apache.http.client.utils.URIBuilder)1 ResteasyClient (org.jboss.resteasy.client.jaxrs.ResteasyClient)1 ResteasyWebTarget (org.jboss.resteasy.client.jaxrs.ResteasyWebTarget)1 BasicAuthHelper (org.jboss.resteasy.util.BasicAuthHelper)1