private List<String> toDomainList(Mappings mappings) { return mappings .select(Mapping.Type.Domain) .asStream() .map(Mapping::asString) .map(Mapping.Type.Domain::withoutPrefix) .collect(Guavate.toImmutableList()); } }
@VisibleForTesting List<MailAddress> handleMappings(Mappings mappings, Mail mail, MailAddress recipient) { boolean isLocal = true; Map<Boolean, List<MailAddress>> mailAddressSplit = mappings.asStream() .map(mapping -> mapping.appendDomainIfNone(defaultDomainSupplier)) .map(Mapping::asMailAddress) .flatMap(OptionalUtils::toStream) .collect(Collectors.partitioningBy(mailAddress -> mailetContext.isLocalServer(mailAddress.getDomain()))); forwardToRemoteAddress(mail, recipient, mailAddressSplit.get(!isLocal)); return mailAddressSplit.get(isLocal); }
private Stream<Mapping> recurseMapping(Stream<Mapping> nonRecursiveResult, User targetUser, int remainingLoops) throws ErrorMappingException, RecipientRewriteTableException { Mappings childMappings = getMappings(targetUser, remainingLoops - 1); if (childMappings.isEmpty()) { return nonRecursiveResult; } else { return childMappings.asStream(); } }
@GET @Path(ROOT_PATH + "/{" + GROUP_ADDRESS + "}") @ApiOperation(value = "listing group members") @ApiImplicitParams({ @ApiImplicitParam(required = true, dataType = "string", name = GROUP_ADDRESS, paramType = "path") }) @ApiResponses(value = { @ApiResponse(code = HttpStatus.OK_200, message = "OK", response = List.class), @ApiResponse(code = HttpStatus.BAD_REQUEST_400, message = "The group is not an address"), @ApiResponse(code = HttpStatus.NOT_FOUND_404, message = "The group does not exist"), @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500, message = "Internal server error - Something went bad on the server side.") }) public ImmutableSortedSet<String> listGroupMembers(Request request, Response response) throws RecipientRewriteTableException { MailAddress groupAddress = parseMailAddress(request.params(GROUP_ADDRESS)); Mappings mappings = Optional.ofNullable(recipientRewriteTable.getUserDomainMappings(MappingSource.fromMailAddress(groupAddress))) .orElse(MappingsImpl.empty()) .select(Mapping.Type.Group); ensureNonEmptyMappings(mappings); return mappings .asStream() .map(Mapping::asMailAddress) .flatMap(OptionalUtils::toStream) .map(MailAddress::asString) .collect(Guavate.toImmutableSortedSet()); }
@GET @Path(ROOT_PATH + "/{" + FORWARD_BASE_ADDRESS + "}") @ApiOperation(value = "listing forward destinations") @ApiImplicitParams({ @ApiImplicitParam(required = true, dataType = "string", name = FORWARD_BASE_ADDRESS, paramType = "path") }) @ApiResponses(value = { @ApiResponse(code = HttpStatus.OK_200, message = "OK", response = List.class), @ApiResponse(code = HttpStatus.BAD_REQUEST_400, message = "The forward is not an address"), @ApiResponse(code = HttpStatus.NOT_FOUND_404, message = "The forward does not exist"), @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500, message = "Internal server error - Something went bad on the server side.") }) public ImmutableSet<ForwardDestinationResponse> listForwardDestinations(Request request, Response response) throws RecipientRewriteTableException { MailAddress baseAddress = parseMailAddress(request.params(FORWARD_BASE_ADDRESS)); Mappings mappings = Optional.ofNullable(recipientRewriteTable.getUserDomainMappings(MappingSource.fromMailAddress(baseAddress))) .orElse(MappingsImpl.empty()) .select(Mapping.Type.Forward); ensureNonEmptyMappings(mappings); return mappings.asStream() .map(mapping -> mapping.asMailAddress() .orElseThrow(() -> new IllegalStateException(String.format("Can not compute address for mapping %s", mapping.asString())))) .map(MailAddress::asString) .sorted() .map(ForwardDestinationResponse::new) .collect(Guavate.toImmutableSet()); }
public void tearDown() throws Exception { Map<MappingSource, Mappings> mappings = virtualUserTable.getAllMappings(); if (mappings != null) { for (MappingSource key : virtualUserTable.getAllMappings().keySet()) { Mappings map = mappings.get(key); map.asStream() .forEach(Throwing.consumer(mapping -> virtualUserTable.removeMapping(key, mapping))); } } LifecycleUtil.dispose(virtualUserTable); }
private Mappings getMappings(User user, int mappingLimit) throws ErrorMappingException, RecipientRewriteTableException { // We have to much mappings throw ErrorMappingException to avoid // infinity loop if (mappingLimit == 0) { throw new TooManyMappingException("554 Too many mappings to process"); } Mappings targetMappings = mapAddress(user.getLocalPart(), user.getDomainPart().get()); try { return MappingsImpl.fromMappings( targetMappings.asStream() .flatMap(Throwing.function((Mapping target) -> convertAndRecurseMapping(user, target, mappingLimit)).sneakyThrow())); } catch (SkipMappingProcessingException e) { return MappingsImpl.empty(); } }