@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());
}