software.amazon.awssdk.services.kendra.model.UserContext Maven / Gradle / Ivy
Show all versions of kendra Show documentation
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with
* the License. A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file 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 software.amazon.awssdk.services.kendra.model;
import java.io.Serializable;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import software.amazon.awssdk.annotations.Generated;
import software.amazon.awssdk.core.SdkField;
import software.amazon.awssdk.core.SdkPojo;
import software.amazon.awssdk.core.protocol.MarshallLocation;
import software.amazon.awssdk.core.protocol.MarshallingType;
import software.amazon.awssdk.core.traits.ListTrait;
import software.amazon.awssdk.core.traits.LocationTrait;
import software.amazon.awssdk.core.util.DefaultSdkAutoConstructList;
import software.amazon.awssdk.core.util.SdkAutoConstructList;
import software.amazon.awssdk.utils.ToString;
import software.amazon.awssdk.utils.builder.CopyableBuilder;
import software.amazon.awssdk.utils.builder.ToCopyableBuilder;
/**
*
* Provides information about the user context for an Amazon Kendra index.
*
*
* User context filtering is a kind of personalized search with the benefit of controlling access to documents. For
* example, not all teams that search the company portal for information should access top-secret company documents, nor
* are these documents relevant to all users. Only specific users or groups of teams given access to top-secret
* documents should see these documents in their search results.
*
*
* You provide one of the following:
*
*
* -
*
* User token
*
*
* -
*
* User ID, the groups the user belongs to, and any data sources the groups can access.
*
*
*
*
* If you provide both, an exception is thrown.
*
*/
@Generated("software.amazon.awssdk:codegen")
public final class UserContext implements SdkPojo, Serializable, ToCopyableBuilder {
private static final SdkField TOKEN_FIELD = SdkField. builder(MarshallingType.STRING).memberName("Token")
.getter(getter(UserContext::token)).setter(setter(Builder::token))
.traits(LocationTrait.builder().location(MarshallLocation.PAYLOAD).locationName("Token").build()).build();
private static final SdkField USER_ID_FIELD = SdkField. builder(MarshallingType.STRING).memberName("UserId")
.getter(getter(UserContext::userId)).setter(setter(Builder::userId))
.traits(LocationTrait.builder().location(MarshallLocation.PAYLOAD).locationName("UserId").build()).build();
private static final SdkField> GROUPS_FIELD = SdkField
.> builder(MarshallingType.LIST)
.memberName("Groups")
.getter(getter(UserContext::groups))
.setter(setter(Builder::groups))
.traits(LocationTrait.builder().location(MarshallLocation.PAYLOAD).locationName("Groups").build(),
ListTrait
.builder()
.memberLocationName(null)
.memberFieldInfo(
SdkField. builder(MarshallingType.STRING)
.traits(LocationTrait.builder().location(MarshallLocation.PAYLOAD)
.locationName("member").build()).build()).build()).build();
private static final SdkField> DATA_SOURCE_GROUPS_FIELD = SdkField
.> builder(MarshallingType.LIST)
.memberName("DataSourceGroups")
.getter(getter(UserContext::dataSourceGroups))
.setter(setter(Builder::dataSourceGroups))
.traits(LocationTrait.builder().location(MarshallLocation.PAYLOAD).locationName("DataSourceGroups").build(),
ListTrait
.builder()
.memberLocationName(null)
.memberFieldInfo(
SdkField. builder(MarshallingType.SDK_POJO)
.constructor(DataSourceGroup::builder)
.traits(LocationTrait.builder().location(MarshallLocation.PAYLOAD)
.locationName("member").build()).build()).build()).build();
private static final List> SDK_FIELDS = Collections.unmodifiableList(Arrays.asList(TOKEN_FIELD, USER_ID_FIELD,
GROUPS_FIELD, DATA_SOURCE_GROUPS_FIELD));
private static final long serialVersionUID = 1L;
private final String token;
private final String userId;
private final List groups;
private final List dataSourceGroups;
private UserContext(BuilderImpl builder) {
this.token = builder.token;
this.userId = builder.userId;
this.groups = builder.groups;
this.dataSourceGroups = builder.dataSourceGroups;
}
/**
*
* The user context token for filtering search results for a user. It must be a JWT or a JSON token.
*
*
* @return The user context token for filtering search results for a user. It must be a JWT or a JSON token.
*/
public final String token() {
return token;
}
/**
*
* The identifier of the user you want to filter search results based on their access to documents.
*
*
* @return The identifier of the user you want to filter search results based on their access to documents.
*/
public final String userId() {
return userId;
}
/**
* For responses, this returns true if the service returned a value for the Groups property. This DOES NOT check
* that the value is non-empty (for which, you should check the {@code isEmpty()} method on the property). This is
* useful because the SDK will never return a null collection or map, but you may need to differentiate between the
* service returning nothing (or null) and the service returning an empty collection or map. For requests, this
* returns true if a value for the property was specified in the request builder, and false if a value was not
* specified.
*/
public final boolean hasGroups() {
return groups != null && !(groups instanceof SdkAutoConstructList);
}
/**
*
* The list of groups you want to filter search results based on the groups' access to documents.
*
*
* Attempts to modify the collection returned by this method will result in an UnsupportedOperationException.
*
*
* This method will never return null. If you would like to know whether the service returned this field (so that
* you can differentiate between null and empty), you can use the {@link #hasGroups} method.
*
*
* @return The list of groups you want to filter search results based on the groups' access to documents.
*/
public final List groups() {
return groups;
}
/**
* For responses, this returns true if the service returned a value for the DataSourceGroups property. This DOES NOT
* check that the value is non-empty (for which, you should check the {@code isEmpty()} method on the property).
* This is useful because the SDK will never return a null collection or map, but you may need to differentiate
* between the service returning nothing (or null) and the service returning an empty collection or map. For
* requests, this returns true if a value for the property was specified in the request builder, and false if a
* value was not specified.
*/
public final boolean hasDataSourceGroups() {
return dataSourceGroups != null && !(dataSourceGroups instanceof SdkAutoConstructList);
}
/**
*
* The list of data source groups you want to filter search results based on groups' access to documents in that
* data source.
*
*
* Attempts to modify the collection returned by this method will result in an UnsupportedOperationException.
*
*
* This method will never return null. If you would like to know whether the service returned this field (so that
* you can differentiate between null and empty), you can use the {@link #hasDataSourceGroups} method.
*
*
* @return The list of data source groups you want to filter search results based on groups' access to documents in
* that data source.
*/
public final List dataSourceGroups() {
return dataSourceGroups;
}
@Override
public Builder toBuilder() {
return new BuilderImpl(this);
}
public static Builder builder() {
return new BuilderImpl();
}
public static Class extends Builder> serializableBuilderClass() {
return BuilderImpl.class;
}
@Override
public final int hashCode() {
int hashCode = 1;
hashCode = 31 * hashCode + Objects.hashCode(token());
hashCode = 31 * hashCode + Objects.hashCode(userId());
hashCode = 31 * hashCode + Objects.hashCode(hasGroups() ? groups() : null);
hashCode = 31 * hashCode + Objects.hashCode(hasDataSourceGroups() ? dataSourceGroups() : null);
return hashCode;
}
@Override
public final boolean equals(Object obj) {
return equalsBySdkFields(obj);
}
@Override
public final boolean equalsBySdkFields(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (!(obj instanceof UserContext)) {
return false;
}
UserContext other = (UserContext) obj;
return Objects.equals(token(), other.token()) && Objects.equals(userId(), other.userId())
&& hasGroups() == other.hasGroups() && Objects.equals(groups(), other.groups())
&& hasDataSourceGroups() == other.hasDataSourceGroups()
&& Objects.equals(dataSourceGroups(), other.dataSourceGroups());
}
/**
* Returns a string representation of this object. This is useful for testing and debugging. Sensitive data will be
* redacted from this string using a placeholder value.
*/
@Override
public final String toString() {
return ToString.builder("UserContext").add("Token", token()).add("UserId", userId())
.add("Groups", hasGroups() ? groups() : null)
.add("DataSourceGroups", hasDataSourceGroups() ? dataSourceGroups() : null).build();
}
public final Optional getValueForField(String fieldName, Class clazz) {
switch (fieldName) {
case "Token":
return Optional.ofNullable(clazz.cast(token()));
case "UserId":
return Optional.ofNullable(clazz.cast(userId()));
case "Groups":
return Optional.ofNullable(clazz.cast(groups()));
case "DataSourceGroups":
return Optional.ofNullable(clazz.cast(dataSourceGroups()));
default:
return Optional.empty();
}
}
@Override
public final List> sdkFields() {
return SDK_FIELDS;
}
private static Function