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

io.prestosql.SessionRepresentation Maven / Gradle / Ivy

There is a newer version: 350
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.prestosql;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.collect.ImmutableMap;
import io.prestosql.connector.CatalogName;
import io.prestosql.metadata.SessionPropertyManager;
import io.prestosql.spi.QueryId;
import io.prestosql.spi.security.BasicPrincipal;
import io.prestosql.spi.security.Identity;
import io.prestosql.spi.security.SelectedRole;
import io.prestosql.spi.session.ResourceEstimates;
import io.prestosql.spi.type.TimeZoneKey;
import io.prestosql.sql.SqlPath;
import io.prestosql.transaction.TransactionId;

import java.time.Instant;
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.Set;

import static java.util.Collections.emptyMap;
import static java.util.Objects.requireNonNull;

public final class SessionRepresentation
{
    private final String queryId;
    private final Optional transactionId;
    private final boolean clientTransactionSupport;
    private final String user;
    private final Set groups;
    private final Optional principal;
    private final Optional source;
    private final Optional catalog;
    private final Optional schema;
    private final SqlPath path;
    private final Optional traceToken;
    private final TimeZoneKey timeZoneKey;
    private final Locale locale;
    private final Optional remoteUserAddress;
    private final Optional userAgent;
    private final Optional clientInfo;
    private final Set clientTags;
    private final Set clientCapabilities;
    private final Instant start;
    private final ResourceEstimates resourceEstimates;
    private final Map systemProperties;
    private final Map> catalogProperties;
    private final Map> unprocessedCatalogProperties;
    private final Map roles;
    private final Map preparedStatements;

    @JsonCreator
    public SessionRepresentation(
            @JsonProperty("queryId") String queryId,
            @JsonProperty("transactionId") Optional transactionId,
            @JsonProperty("clientTransactionSupport") boolean clientTransactionSupport,
            @JsonProperty("user") String user,
            @JsonProperty("groups") Set groups,
            @JsonProperty("principal") Optional principal,
            @JsonProperty("source") Optional source,
            @JsonProperty("catalog") Optional catalog,
            @JsonProperty("schema") Optional schema,
            @JsonProperty("path") SqlPath path,
            @JsonProperty("traceToken") Optional traceToken,
            @JsonProperty("timeZoneKey") TimeZoneKey timeZoneKey,
            @JsonProperty("locale") Locale locale,
            @JsonProperty("remoteUserAddress") Optional remoteUserAddress,
            @JsonProperty("userAgent") Optional userAgent,
            @JsonProperty("clientInfo") Optional clientInfo,
            @JsonProperty("clientTags") Set clientTags,
            @JsonProperty("clientCapabilities") Set clientCapabilities,
            @JsonProperty("resourceEstimates") ResourceEstimates resourceEstimates,
            @JsonProperty("start") Instant start,
            @JsonProperty("systemProperties") Map systemProperties,
            @JsonProperty("catalogProperties") Map> catalogProperties,
            @JsonProperty("unprocessedCatalogProperties") Map> unprocessedCatalogProperties,
            @JsonProperty("roles") Map roles,
            @JsonProperty("preparedStatements") Map preparedStatements)
    {
        this.queryId = requireNonNull(queryId, "queryId is null");
        this.transactionId = requireNonNull(transactionId, "transactionId is null");
        this.clientTransactionSupport = clientTransactionSupport;
        this.user = requireNonNull(user, "user is null");
        this.groups = requireNonNull(groups, "groups is null");
        this.principal = requireNonNull(principal, "principal is null");
        this.source = requireNonNull(source, "source is null");
        this.catalog = requireNonNull(catalog, "catalog is null");
        this.schema = requireNonNull(schema, "schema is null");
        this.path = requireNonNull(path, "path is null");
        this.traceToken = requireNonNull(traceToken, "traceToken is null");
        this.timeZoneKey = requireNonNull(timeZoneKey, "timeZoneKey is null");
        this.locale = requireNonNull(locale, "locale is null");
        this.remoteUserAddress = requireNonNull(remoteUserAddress, "remoteUserAddress is null");
        this.userAgent = requireNonNull(userAgent, "userAgent is null");
        this.clientInfo = requireNonNull(clientInfo, "clientInfo is null");
        this.clientTags = requireNonNull(clientTags, "clientTags is null");
        this.clientCapabilities = requireNonNull(clientCapabilities, "clientCapabilities is null");
        this.resourceEstimates = requireNonNull(resourceEstimates, "resourceEstimates is null");
        this.start = start;
        this.systemProperties = ImmutableMap.copyOf(systemProperties);
        this.roles = ImmutableMap.copyOf(roles);
        this.preparedStatements = ImmutableMap.copyOf(preparedStatements);

        ImmutableMap.Builder> catalogPropertiesBuilder = ImmutableMap.builder();
        for (Entry> entry : catalogProperties.entrySet()) {
            catalogPropertiesBuilder.put(entry.getKey(), ImmutableMap.copyOf(entry.getValue()));
        }
        this.catalogProperties = catalogPropertiesBuilder.build();

        ImmutableMap.Builder> unprocessedCatalogPropertiesBuilder = ImmutableMap.builder();
        for (Entry> entry : unprocessedCatalogProperties.entrySet()) {
            unprocessedCatalogPropertiesBuilder.put(entry.getKey(), ImmutableMap.copyOf(entry.getValue()));
        }
        this.unprocessedCatalogProperties = unprocessedCatalogPropertiesBuilder.build();
    }

    @JsonProperty
    public String getQueryId()
    {
        return queryId;
    }

    @JsonProperty
    public Optional getTransactionId()
    {
        return transactionId;
    }

    @JsonProperty
    public boolean isClientTransactionSupport()
    {
        return clientTransactionSupport;
    }

    @JsonProperty
    public String getUser()
    {
        return user;
    }

    @JsonProperty
    public Set getGroups()
    {
        return groups;
    }

    @JsonProperty
    public Optional getPrincipal()
    {
        return principal;
    }

    @JsonProperty
    public Optional getSource()
    {
        return source;
    }

    @JsonProperty
    public Optional getTraceToken()
    {
        return traceToken;
    }

    @JsonProperty
    public Optional getCatalog()
    {
        return catalog;
    }

    @JsonProperty
    public Optional getSchema()
    {
        return schema;
    }

    @JsonProperty
    public SqlPath getPath()
    {
        return path;
    }

    @JsonProperty
    public TimeZoneKey getTimeZoneKey()
    {
        return timeZoneKey;
    }

    @JsonProperty
    public Locale getLocale()
    {
        return locale;
    }

    @JsonProperty
    public Optional getRemoteUserAddress()
    {
        return remoteUserAddress;
    }

    @JsonProperty
    public Optional getUserAgent()
    {
        return userAgent;
    }

    @JsonProperty
    public Optional getClientInfo()
    {
        return clientInfo;
    }

    @JsonProperty
    public Set getClientTags()
    {
        return clientTags;
    }

    @JsonProperty
    public Set getClientCapabilities()
    {
        return clientCapabilities;
    }

    @JsonProperty
    public Instant getStart()
    {
        return start;
    }

    @JsonProperty
    public ResourceEstimates getResourceEstimates()
    {
        return resourceEstimates;
    }

    @JsonProperty
    public Map getSystemProperties()
    {
        return systemProperties;
    }

    @JsonProperty
    public Map> getCatalogProperties()
    {
        return catalogProperties;
    }

    @JsonProperty
    public Map> getUnprocessedCatalogProperties()
    {
        return unprocessedCatalogProperties;
    }

    @JsonProperty
    public Map getRoles()
    {
        return roles;
    }

    @JsonProperty
    public Map getPreparedStatements()
    {
        return preparedStatements;
    }

    public Session toSession(SessionPropertyManager sessionPropertyManager)
    {
        return toSession(sessionPropertyManager, emptyMap());
    }

    public Session toSession(SessionPropertyManager sessionPropertyManager, Map extraCredentials)
    {
        return new Session(
                new QueryId(queryId),
                transactionId,
                clientTransactionSupport,
                Identity.forUser(user)
                        .withGroups(groups)
                        .withPrincipal(principal.map(BasicPrincipal::new))
                        .withRoles(roles)
                        .withExtraCredentials(extraCredentials)
                        .build(),
                source,
                catalog,
                schema,
                path,
                traceToken,
                timeZoneKey,
                locale,
                remoteUserAddress,
                userAgent,
                clientInfo,
                clientTags,
                clientCapabilities,
                resourceEstimates,
                start,
                systemProperties,
                catalogProperties,
                unprocessedCatalogProperties,
                sessionPropertyManager,
                preparedStatements);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy