package com.atlassian.stash.internal.rest.filter;

import com.atlassian.bitbucket.rest.enrich.AvatarEnricher;
import com.atlassian.bitbucket.rest.enrich.LinkEnricher;
import com.atlassian.bitbucket.rest.util.RestUtils;
import com.sun.jersey.spi.container.ContainerRequest;
import com.sun.jersey.spi.container.ContainerResponse;
import com.sun.jersey.spi.container.ContainerResponseFilter;

/**
 * Provides a {@code ContentResponseFilter} which enriches the entity graph with avatar URLs and self links.
 */
public class EnricherResponseFilter implements ContainerResponseFilter {

    private final AvatarEnricher avatarEnricher;
    private final LinkEnricher linkEnricher;

    public EnricherResponseFilter(AvatarEnricher avatarEnricher, LinkEnricher linkEnricher) {
        this.avatarEnricher = avatarEnricher;
        this.linkEnricher = linkEnricher;
    }

    /**
     * Applies link and avatar enrichers to the response.
     *
     * @param request  the container request
     * @param response the response being filtered
     * @return {@code response}
     */
    @Override
    public ContainerResponse filter(ContainerRequest request, ContainerResponse response) {
        linkEnricher.enrich(response.getEntity());
        avatarEnricher.enrich(response.getEntity(), RestUtils.makeAvatarRequest(request));
        return response;
    }
}
