@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()) )); }
/** * 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(); } }
.setParameter("targetAddress", mapping) .setParameter("user", source.getFixedUser()) .setParameter("domain", source.getFixedDomain()) .executeUpdate(); transaction.commit();
@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(); } }
@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 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); } } }