@Override public void addAddressMapping(String fromUser, String fromDomain, String toAddress) throws RecipientRewriteTableException { MappingSource source = MappingSource.fromUser(fromUser, fromDomain); rrt.addAddressMapping(source, toAddress); }
/** * Returns the real recipient given a virtual username and domain. * * @param user * the virtual user * @param domain * the virtual domain * @return the real recipient address, or <code>null</code> if no mapping * exists */ public static String getTargetString(String user, Domain domain, Map<MappingSource, String> mappings) { return OptionalUtils.or( Optional.ofNullable(mappings.get(MappingSource.fromUser(user, domain))), Optional.ofNullable(mappings.get(MappingSource.fromDomain(domain)))) .orElse(null); }
/** * Remove a mapping for the given user and domain * * @param user the user * @param domain the domain * @param mapping the mapping * @throws RecipientRewriteTableException */ private void doRemoveMapping(MappingSource source, String mapping) throws RecipientRewriteTableException { EntityManager entityManager = entityManagerFactory.createEntityManager(); final EntityTransaction transaction = entityManager.getTransaction(); try { transaction.begin(); entityManager.createNamedQuery("deleteMapping") .setParameter("user", source.getFixedUser()) .setParameter("domain", source.getFixedDomain()) .setParameter("targetAddress", mapping) .executeUpdate(); transaction.commit(); } catch (PersistenceException e) { LOGGER.debug("Failed to remove mapping", e); if (transaction.isActive()) { transaction.rollback(); } throw new RecipientRewriteTableException("Unable to remove mapping", e); } finally { entityManager.close(); } }
@Override public void addDomainMapping(String domain, String targetDomain) throws RecipientRewriteTableException { MappingSource source = MappingSource.fromDomain(Domain.of(domain)); rrt.addAliasDomainMapping(source, Domain.of(targetDomain)); }
@Test void getDomainMappingsEmptyMappingsAreFilteredOut() throws RecipientRewriteTableException { MappingSource nonEmptyMapping = MappingSource.fromDomain(Domain.of("abc.com")); MappingSource emptyMapping = MappingSource.fromDomain(Domain.of("def.com")); Map<MappingSource, Mappings> mappings = ImmutableMap.of( nonEmptyMapping, MappingsImpl.fromRawString("domain:a.com"), emptyMapping, MappingsImpl.empty() ); when(recipientRewriteTable.getAllMappings()).thenReturn(mappings); Map<String, List<String>> map = when() .get() .then() .contentType(ContentType.JSON) .statusCode(HttpStatus.OK_200) .extract() .body() .jsonPath() .getMap("."); assertThat(map) .containsKey(nonEmptyMapping.asString()) .doesNotContainKey(emptyMapping.asString()); }
@Override public Map<String, Mappings> getAllMappings() throws RecipientRewriteTableException { return rrt.getAllMappings() .entrySet() .stream() .collect( Guavate.toImmutableMap( entry -> entry.getKey().asString(), entry -> entry.getValue())); }
@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()) )); }
@Override public void removeDomainMapping(String domain, String targetDomain) throws RecipientRewriteTableException { MappingSource source = MappingSource.fromDomain(Domain.of(domain)); rrt.removeAliasDomainMapping(source, Domain.of(targetDomain)); }
@Override public Map<String, Mappings> listMappings() throws Exception { return recipientRewriteTable.getAllMappings() .entrySet() .stream() .collect( Guavate.toImmutableMap( entry -> entry.getKey().asString(), Map.Entry::getValue)); }
@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()); }
@Override public void removeErrorMapping(String user, String domain, String error) throws RecipientRewriteTableException { MappingSource source = MappingSource.fromUser(user, domain); rrt.removeErrorMapping(source, error); }
@Override protected Mappings mapAddress(String user, Domain domain) { return OptionalUtils.orSuppliers( () -> retrieveMappings(MappingSource.fromUser(user, domain)), () -> retrieveMappings(MappingSource.fromDomain(domain))) .orElse(MappingsImpl.empty()); }
@Override public void addDomainAliasMapping(String aliasDomain, String deliveryDomain) throws Exception { recipientRewriteTable.addAliasDomainMapping(MappingSource.fromDomain(Domain.of(aliasDomain)), Domain.of(deliveryDomain)); }
.createNamedQuery("updateMapping") .setParameter("targetAddress", mapping) .setParameter("user", source.getFixedUser()) .setParameter("domain", source.getFixedDomain()) .executeUpdate(); transaction.commit();
@Override public void removeErrorMapping(MappingSource source, String error) throws RecipientRewriteTableException { LOGGER.info("Remove error mapping => {} for source: {}", error, source.asString()); removeMapping(source, Mapping.error(error)); }
@Override public Mappings getUserDomainMappings(String user, String domain) throws RecipientRewriteTableException { MappingSource source = MappingSource.fromUser(user, domain); return rrt.getUserDomainMappings(source); }
@Override protected Mappings mapAddress(String user, Domain domain) { return OptionalUtils.orSuppliers( () -> retrieveMappings(MappingSource.fromUser(User.fromLocalPartWithDomain(user, domain))), () -> retrieveMappings(MappingSource.fromDomain(domain))) .orElse(MappingsImpl.empty()); }
@When("alias domain mapping \"([^\"]*)\" for \"([^\"]*)\" domain is removed") public void removeAliasDomainMappingForDomain(String aliasdomain, String domain) throws Throwable { rewriteTable.removeAliasDomainMapping(MappingSource.fromDomain(Domain.of(aliasdomain)), Domain.of(domain)); }
/** * Add mapping for given user and domain * * @param user the user * @param domain the domain * @param mapping the mapping * @throws RecipientRewriteTableException */ private void doAddMapping(MappingSource source, String mapping) throws RecipientRewriteTableException { EntityManager entityManager = entityManagerFactory.createEntityManager(); final EntityTransaction transaction = entityManager.getTransaction(); try { transaction.begin(); JPARecipientRewrite jpaRecipientRewrite = new JPARecipientRewrite(source.getFixedUser(), Domain.of(source.getFixedDomain()), mapping); entityManager.persist(jpaRecipientRewrite); transaction.commit(); } catch (PersistenceException e) { LOGGER.debug("Failed to save virtual user", e); if (transaction.isActive()) { transaction.rollback(); } throw new RecipientRewriteTableException("Unable to add mapping", e); } finally { entityManager.close(); } }
@Override public void removeRegexMapping(MappingSource source, String regex) throws RecipientRewriteTableException { LOGGER.info("Remove regex mapping => {} for source: {}", regex, source.asString()); removeMapping(source, Mapping.regex(regex)); }