com.hubspot.imap.protocol.capabilities.Capabilities Maven / Gradle / Ivy
package com.hubspot.imap.protocol.capabilities;
import com.google.common.base.Splitter;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
public class Capabilities {
private static final Splitter SPACE_SPLITTER = Splitter
.on(" ")
.omitEmptyStrings()
.trimResults();
private final List capabilities;
private final List authMechanisms;
public Capabilities() {
this(Collections.emptyList());
}
public Capabilities(List capabilities) {
this.capabilities = capabilities;
this.authMechanisms =
capabilities
.stream()
.filter(capability -> capability instanceof AuthCapability)
.map(capability -> ((AuthCapability) capability))
.map(authCapability -> authCapability.getMechanism())
.collect(Collectors.toList());
}
public List getCapabilities() {
return capabilities;
}
public List getAuthMechanisms() {
return authMechanisms;
}
public static Capabilities parseFrom(String input) {
List capabilities = SPACE_SPLITTER
.splitToList(input)
.stream()
.map(Capabilities::parseSingle)
.collect(Collectors.toList());
return new Capabilities(capabilities);
}
private static Capability parseSingle(String capability) {
// We need to split the mechanism out for auth capabilities
if (capability.startsWith("AUTH")) {
String[] parts = capability.split("=");
if (parts.length < 2) {
return StandardCapabilities.AUTH;
}
return new AuthCapability(parts[1]);
}
return StandardCapabilities
.fromString(capability)
.orElse(new UnknownCapability(capability));
}
}