All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.trino.server.SessionContext Maven / Gradle / Ivy

There is a newer version: 465
Show newest version
/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.trino.server;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import io.trino.Session;
import io.trino.client.ProtocolHeaders;
import io.trino.spi.security.Identity;
import io.trino.spi.security.SelectedRole;
import io.trino.spi.session.ResourceEstimates;
import io.trino.transaction.TransactionId;

import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.Set;

import static com.google.common.collect.ImmutableMap.toImmutableMap;
import static java.util.Objects.requireNonNull;

public class SessionContext
{
    private final ProtocolHeaders protocolHeaders;

    private final Optional catalog;
    private final Optional schema;
    private final Optional path;

    private final Optional authenticatedIdentity;
    private final Identity identity;
    private final Identity originalIdentity;
    private final SelectedRole selectedRole;

    private final Optional source;
    private final Optional traceToken;
    private final Optional userAgent;
    private final Optional remoteUserAddress;
    private final Optional timeZoneId;
    private final Optional language;
    private final Set clientTags;
    private final Set clientCapabilities;
    private final ResourceEstimates resourceEstimates;

    private final Map systemProperties;
    private final Map> catalogSessionProperties;

    private final Map preparedStatements;

    private final Optional transactionId;
    private final boolean clientTransactionSupport;
    private final Optional clientInfo;

    public SessionContext(
            ProtocolHeaders protocolHeaders,
            Optional catalog,
            Optional schema,
            Optional path,
            Optional authenticatedIdentity,
            Identity identity,
            Identity originalIdentity,
            SelectedRole selectedRole,
            Optional source,
            Optional traceToken,
            Optional userAgent,
            Optional remoteUserAddress,
            Optional timeZoneId,
            Optional language,
            Set clientTags,
            Set clientCapabilities,
            ResourceEstimates resourceEstimates,
            Map systemProperties,
            Map> catalogSessionProperties,
            Map preparedStatements,
            Optional transactionId,
            boolean clientTransactionSupport,
            Optional clientInfo)
    {
        this.protocolHeaders = requireNonNull(protocolHeaders, "protocolHeaders is null");
        this.catalog = requireNonNull(catalog, "catalog is null");
        this.schema = requireNonNull(schema, "schema is null");
        this.path = requireNonNull(path, "path is null");
        this.authenticatedIdentity = requireNonNull(authenticatedIdentity, "authenticatedIdentity is null");
        this.identity = requireNonNull(identity, "identity is null");
        this.originalIdentity = requireNonNull(originalIdentity, "originalIdentity is null");
        this.selectedRole = requireNonNull(selectedRole, "selectedRole is null");
        this.source = requireNonNull(source, "source is null");
        this.traceToken = requireNonNull(traceToken, "traceToken is null");
        this.userAgent = requireNonNull(userAgent, "userAgent is null");
        this.remoteUserAddress = requireNonNull(remoteUserAddress, "remoteUserAddress is null");
        this.timeZoneId = requireNonNull(timeZoneId, "timeZoneId is null");
        this.language = requireNonNull(language, "language is null");
        this.clientTags = ImmutableSet.copyOf(requireNonNull(clientTags, "clientTags is null"));
        this.clientCapabilities = ImmutableSet.copyOf(requireNonNull(clientCapabilities, "clientCapabilities is null"));
        this.resourceEstimates = requireNonNull(resourceEstimates, "resourceEstimates is null");
        this.systemProperties = ImmutableMap.copyOf(requireNonNull(systemProperties, "systemProperties is null"));
        requireNonNull(catalogSessionProperties, "catalogSessionProperties is null");
        this.catalogSessionProperties = catalogSessionProperties.entrySet().stream()
                .collect(toImmutableMap(Entry::getKey, entry -> ImmutableMap.copyOf(entry.getValue())));
        this.preparedStatements = ImmutableMap.copyOf(requireNonNull(preparedStatements, "preparedStatements is null"));
        this.transactionId = requireNonNull(transactionId, "transactionId is null");
        this.clientTransactionSupport = clientTransactionSupport;
        this.clientInfo = requireNonNull(clientInfo, "clientInfo is null");
    }

    public ProtocolHeaders getProtocolHeaders()
    {
        return protocolHeaders;
    }

    public Optional getAuthenticatedIdentity()
    {
        return authenticatedIdentity;
    }

    public Identity getIdentity()
    {
        return identity;
    }

    public Identity getOriginalIdentity()
    {
        return originalIdentity;
    }

    public SelectedRole getSelectedRole()
    {
        return selectedRole;
    }

    public Optional getCatalog()
    {
        return catalog;
    }

    public Optional getSchema()
    {
        return schema;
    }

    public Optional getPath()
    {
        return path;
    }

    public Optional getSource()
    {
        return source;
    }

    public Optional getRemoteUserAddress()
    {
        return remoteUserAddress;
    }

    public Optional getUserAgent()
    {
        return userAgent;
    }

    public Optional getClientInfo()
    {
        return clientInfo;
    }

    public Set getClientTags()
    {
        return clientTags;
    }

    public Set getClientCapabilities()
    {
        return clientCapabilities;
    }

    public ResourceEstimates getResourceEstimates()
    {
        return resourceEstimates;
    }

    public Optional getTimeZoneId()
    {
        return timeZoneId;
    }

    public Optional getLanguage()
    {
        return language;
    }

    public Map getSystemProperties()
    {
        return systemProperties;
    }

    public Map> getCatalogSessionProperties()
    {
        return catalogSessionProperties;
    }

    public Map getPreparedStatements()
    {
        return preparedStatements;
    }

    public Optional getTransactionId()
    {
        return transactionId;
    }

    public boolean supportClientTransaction()
    {
        return clientTransactionSupport;
    }

    public Optional getTraceToken()
    {
        return traceToken;
    }

    @VisibleForTesting
    public static SessionContext fromSession(Session session)
    {
        requireNonNull(session, "session is null");

        Set enabledRoles = session.getIdentity().getEnabledRoles();
        SelectedRole selectedRole;
        if (enabledRoles.isEmpty()) {
            selectedRole = new SelectedRole(SelectedRole.Type.NONE, Optional.empty());
        }
        else if (enabledRoles.size() == 1) {
            selectedRole = new SelectedRole(SelectedRole.Type.ROLE, Optional.of(enabledRoles.iterator().next()));
        }
        else {
            selectedRole = new SelectedRole(SelectedRole.Type.ALL, Optional.empty());
        }

        return new SessionContext(
                session.getProtocolHeaders(),
                session.getCatalog(),
                session.getSchema(),
                Optional.of(session.getPath().getRawPath()),
                Optional.empty(),
                session.getIdentity(),
                session.getOriginalIdentity(),
                selectedRole,
                session.getSource(),
                session.getTraceToken(),
                session.getUserAgent(),
                session.getRemoteUserAddress(),
                Optional.of(session.getTimeZoneKey().getId()),
                Optional.of(session.getLocale().getLanguage()),
                session.getClientTags(),
                session.getClientCapabilities(),
                session.getResourceEstimates(),
                session.getSystemProperties(),
                session.getCatalogProperties(),
                session.getPreparedStatements(),
                session.getTransactionId(),
                session.isClientTransactionSupport(),
                session.getClientInfo());
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy