private List<String> toDomainList(Mappings mappings) { return mappings .select(Mapping.Type.Domain) .asStream() .map(Mapping::asString) .map(Mapping.Type.Domain::withoutPrefix) .collect(Guavate.toImmutableList()); } }
@GET @Path(ROOT_PATH + "/{" + GROUP_ADDRESS + "}") @ApiOperation(value = "listing group members") @ApiImplicitParams({ @ApiImplicitParam(required = true, dataType = "string", name = GROUP_ADDRESS, paramType = "path") }) @ApiResponses(value = { @ApiResponse(code = HttpStatus.OK_200, message = "OK", response = List.class), @ApiResponse(code = HttpStatus.BAD_REQUEST_400, message = "The group is not an address"), @ApiResponse(code = HttpStatus.NOT_FOUND_404, message = "The group does not exist"), @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500, message = "Internal server error - Something went bad on the server side.") }) public ImmutableSortedSet<String> listGroupMembers(Request request, Response response) throws RecipientRewriteTableException { MailAddress groupAddress = parseMailAddress(request.params(GROUP_ADDRESS)); Mappings mappings = Optional.ofNullable(recipientRewriteTable.getUserDomainMappings(MappingSource.fromMailAddress(groupAddress))) .orElse(MappingsImpl.empty()) .select(Mapping.Type.Group); ensureNonEmptyMappings(mappings); return mappings .asStream() .map(Mapping::asMailAddress) .flatMap(OptionalUtils::toStream) .map(MailAddress::asString) .collect(Guavate.toImmutableSortedSet()); }
@GET @Path(ROOT_PATH + "/{" + FORWARD_BASE_ADDRESS + "}") @ApiOperation(value = "listing forward destinations") @ApiImplicitParams({ @ApiImplicitParam(required = true, dataType = "string", name = FORWARD_BASE_ADDRESS, paramType = "path") }) @ApiResponses(value = { @ApiResponse(code = HttpStatus.OK_200, message = "OK", response = List.class), @ApiResponse(code = HttpStatus.BAD_REQUEST_400, message = "The forward is not an address"), @ApiResponse(code = HttpStatus.NOT_FOUND_404, message = "The forward does not exist"), @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500, message = "Internal server error - Something went bad on the server side.") }) public ImmutableSet<ForwardDestinationResponse> listForwardDestinations(Request request, Response response) throws RecipientRewriteTableException { MailAddress baseAddress = parseMailAddress(request.params(FORWARD_BASE_ADDRESS)); Mappings mappings = Optional.ofNullable(recipientRewriteTable.getUserDomainMappings(MappingSource.fromMailAddress(baseAddress))) .orElse(MappingsImpl.empty()) .select(Mapping.Type.Forward); ensureNonEmptyMappings(mappings); return mappings.asStream() .map(mapping -> mapping.asMailAddress() .orElseThrow(() -> new IllegalStateException(String.format("Can not compute address for mapping %s", mapping.asString())))) .map(MailAddress::asString) .sorted() .map(ForwardDestinationResponse::new) .collect(Guavate.toImmutableSet()); }
@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()); }