package com.atlassian.stash.internal.scm.git.protocol.http;

import com.atlassian.event.api.EventPublisher;
import com.atlassian.bitbucket.i18n.I18nService;
import com.atlassian.stash.internal.scm.git.GitWriteTracker;
import com.atlassian.bitbucket.repository.Repository;
import com.atlassian.bitbucket.request.RequestContext;
import com.atlassian.bitbucket.scm.git.command.GitCommandBuilderFactory;
import com.atlassian.bitbucket.scm.git.GitScmConfig;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

abstract class GitSmartWriteRequest extends GitSmartRequest {

    private final GitWriteTracker writeTracker;

    public GitSmartWriteRequest(@Nonnull Repository repository, @Nonnull GitCommandBuilderFactory builderFactory,
                                @Nonnull GitScmConfig config, @Nonnull EventPublisher eventPublisher,
                                @Nonnull I18nService i18nService, @Nonnull HttpServletRequest request,
                                @Nonnull HttpServletResponse response, @Nonnull ServletContext servletContext,
                                @Nonnull GitWriteTracker writeTracker, @Nonnull RequestContext requestContext) {
        super(repository, true, builderFactory, config, eventPublisher, i18nService, request, response, servletContext,
                requestContext);

        this.writeTracker = writeTracker;
    }

    @Override
    public void handleRequest() throws IOException {
        writeTracker.onWriteStart();
        try {
            super.handleRequest();
        } finally {
            writeTracker.onWriteEnd();
        }
    }
}
