@Override protected Mappings mapAddress(String user, Domain domain) throws RecipientRewriteTableException { Mappings mapping = getMapping(user, domain, "selectExactMappings"); if (!mapping.isEmpty()) { return mapping; } return getMapping(user, domain, "selectMappings"); }
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(DOMAIN_MAPPINGS) @ApiOperation(value = "Lists all domain mappings.") @ApiResponses(value = { @ApiResponse(code = HttpStatus.OK_200, message = "Domain mappings.", responseContainer = "Map"), @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500, message = "Internal server error - Something went bad on the server side.") }) public Map<String, List<String>> getAllMappings(Request request, Response response) throws RecipientRewriteTableException { return recipientRewriteTable.getAllMappings() .entrySet() .stream() .filter(mappingsEntry -> !mappingsEntry.getValue().isEmpty()) .filter(mappingsEntry -> mappingsEntry.getValue().contains(Mapping.Type.Domain)) .collect(Guavate.toImmutableMap( mappingsEntry -> mappingsEntry.getKey().getFixedDomain(), mappingsEntry -> toDomainList(mappingsEntry.getValue()) )); }
private boolean isRedirected(MailAddress recipient, String username) { LOGGER.debug("Unknown user {} check if it's an alias", username); try { Mappings targetString = recipientRewriteTable.getMappings(recipient.getLocalPart(), recipient.getDomain()); if (!targetString.isEmpty()) { return true; } } catch (ErrorMappingException e) { return true; } catch (RecipientRewriteTableException e) { LOGGER.info("Unable to access RecipientRewriteTable", e); return false; } return false; }
private RrtExecutionResult executeRrtForRecipient(Mail mail, MailAddress recipient) { try { Mappings mappings = virtualTableStore.getMappings(recipient.getLocalPart(), recipient.getDomain()); if (mappings != null && !mappings.isEmpty()) { List<MailAddress> newMailAddresses = handleMappings(mappings, mail, recipient); return RrtExecutionResult.success(newMailAddresses); } return RrtExecutionResult.success(recipient); } catch (ErrorMappingException | RecipientRewriteTableException e) { LOGGER.warn("Could not rewrite recipient {}", recipient, e); return RrtExecutionResult.error(recipient); } }
@GET @Path(SPECIFIC_MAPPING_PATH) @ApiOperation(value = "Lists mappings for specific domain.") @ApiImplicitParams({ @ApiImplicitParam(required = true, dataType = "string", name = FROM_DOMAIN, paramType = "path") }) @ApiResponses(value = { @ApiResponse(code = HttpStatus.OK_200, message = "Domain mappings.", responseContainer = "List"), @ApiResponse(code = HttpStatus.NOT_FOUND_404, message = "Not existing mappings."), @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500, message = "Internal server error - Something went bad on the server side.") }) public List<String> getMapping(Request request, Response response) throws RecipientRewriteTableException { MappingSource mappingSource = mappingSourceFrom(request); return Optional.ofNullable(recipientRewriteTable.getUserDomainMappings(mappingSource).select(Mapping.Type.Domain)) .filter(mappings -> !mappings.isEmpty()) .filter(mappings -> mappings.contains(Mapping.Type.Domain)) .map(this::toDomainList) .orElseThrow(() -> ErrorResponder.builder() .statusCode(HttpStatus.NOT_FOUND_404) .type(ErrorResponder.ErrorType.NOT_FOUND) .message(String.format("Cannot find mappings for %s", mappingSource.getFixedDomain())) .haltError()); }
private void ensureNonEmptyMappings(Mappings mappings) { if (mappings == null || mappings.isEmpty()) { throw ErrorResponder.builder() .statusCode(HttpStatus.NOT_FOUND_404) .type(ErrorType.INVALID_ARGUMENT) .message("The group does not exist") .haltError(); } } }
private void ensureNonEmptyMappings(Mappings mappings) { if (mappings == null || mappings.isEmpty()) { throw ErrorResponder.builder() .statusCode(HttpStatus.NOT_FOUND_404) .type(ErrorType.INVALID_ARGUMENT) .message("The forward does not exist") .haltError(); } } }
@Override public void addMapping(MappingSource source, Mapping mapping) throws RecipientRewriteTableException { Mappings map = getUserDomainMappings(source); if (!map.isEmpty()) { Mappings updatedMappings = MappingsImpl.from(map).add(mapping).build(); doUpdateMapping(source, updatedMappings.serialize()); } else { doAddMapping(source, mapping.asString()); } }
private void updateConfiguration(MappingSource source, Mappings oldMappings, Mappings updatedMappings) throws RecipientRewriteTableException { if (oldMappings != null) { removeMappingsFromConfig(source, oldMappings); } if (!updatedMappings.isEmpty()) { defaultConfiguration.addProperty("mapping", source.getFixedUser() + "@" + source.getFixedDomain() + "=" + updatedMappings.serialize()); } try { virtualUserTable.configure(defaultConfiguration); } catch (Exception e) { if (updatedMappings.size() > 0) { throw new RecipientRewriteTableException("Error update mapping", e); } } }