@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()) )); }
@GET @Path(ROOT_PATH) @ApiOperation(value = "getting groups list") @ApiResponses(value = { @ApiResponse(code = HttpStatus.OK_200, message = "OK", response = List.class), @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500, message = "Internal server error - Something went bad on the server side.") }) public Set<String> listGroups(Request request, Response response) throws RecipientRewriteTableException { return Optional.ofNullable(recipientRewriteTable.getAllMappings()) .map(mappings -> mappings.entrySet().stream() .filter(e -> e.getValue().contains(Mapping.Type.Group)) .map(Map.Entry::getKey) .flatMap(source -> OptionalUtils.toStream(source.asMailAddress())) .map(MailAddress::asString) .collect(Guavate.toImmutableSortedSet())) .orElse(ImmutableSortedSet.of()); }
@GET @Path(ROOT_PATH) @ApiOperation(value = "getting forwards list") @ApiResponses(value = { @ApiResponse(code = HttpStatus.OK_200, message = "OK", response = List.class), @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500, message = "Internal server error - Something went bad on the server side.") }) public Set<String> listForwards(Request request, Response response) throws RecipientRewriteTableException { return Optional.ofNullable(recipientRewriteTable.getAllMappings()) .map(mappings -> mappings.entrySet().stream() .filter(e -> e.getValue().contains(Mapping.Type.Forward)) .map(Map.Entry::getKey) .flatMap(source -> OptionalUtils.toStream(source.asMailAddress())) .map(MailAddress::asString) .collect(Guavate.toImmutableSortedSet())) .orElse(ImmutableSortedSet.of()); }
private void checkDuplicateMapping(MappingSource source, Mapping mapping) throws RecipientRewriteTableException { Mappings mappings = getUserDomainMappings(source); if (mappings != null && mappings.contains(mapping)) { throw new MappingAlreadyExistsException("Mapping " + mapping + " for " + source.asString() + " already exist!"); } }
@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()); }