/** * 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(); } }
.createNamedQuery("updateMapping") .setParameter("targetAddress", mapping) .setParameter("user", source.getFixedUser()) .setParameter("domain", source.getFixedDomain()) .executeUpdate();
@Override public Mappings getUserDomainMappings(MappingSource source) throws RecipientRewriteTableException { EntityManager entityManager = entityManagerFactory.createEntityManager(); final EntityTransaction transaction = entityManager.getTransaction(); try { transaction.begin(); @SuppressWarnings("unchecked") List<JPARecipientRewrite> virtualUsers = entityManager.createNamedQuery("selectUserDomainMapping") .setParameter("user", source.getFixedUser()) .setParameter("domain", source.getFixedDomain()) .getResultList(); transaction.commit(); if (virtualUsers.size() > 0) { return MappingsImpl.fromRawString(virtualUsers.get(0).getTargetAddress()); } return MappingsImpl.empty(); } catch (PersistenceException e) { LOGGER.debug("Failed to get user domain mappings", e); if (transaction.isActive()) { transaction.rollback(); } throw new RecipientRewriteTableException("Error while retrieve mappings", e); } finally { entityManager.close(); } }
/** * 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(); } }
private Optional<Mappings> retrieveMappings(MappingSource source) { List<String> mappings = executor.execute(retrieveMappingStatement.bind() .setString(USER, source.getFixedUser()) .setString(DOMAIN, source.getFixedDomain())) .thenApply(resultSet -> cassandraUtils.convertToStream(resultSet) .map(row -> row.getString(MAPPING)) .collect(Guavate.toImmutableList())) .join(); return MappingsImpl.fromCollection(mappings).toOptional(); }
@Override public void addMapping(MappingSource source, Mapping mapping) { executor.executeVoid(insertStatement.bind() .setString(USER, source.getFixedUser()) .setString(DOMAIN, source.getFixedDomain()) .setString(MAPPING, mapping.asString())) .join(); }
@Override public void removeMapping(MappingSource source, Mapping mapping) { executor.executeVoid(deleteStatement.bind() .setString(USER, source.getFixedUser()) .setString(DOMAIN, source.getFixedDomain()) .setString(MAPPING, mapping.asString())) .join(); }
private void removeMappingsFromConfig(MappingSource source, Mappings mappings) { List<String> stored = new ArrayList<>(); for (String c : defaultConfiguration.getStringArray("mapping")) { String mapping = source.getFixedUser() + "@" + source.getFixedDomain() + "=" + mappings.serialize(); if (!c.equalsIgnoreCase(mapping)) { stored.add(c); } } // clear old values defaultConfiguration.clear(); // add stored mappings for (String aStored : stored) { defaultConfiguration.addProperty("mapping", aStored); } } }
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); } } }