/*
 * (c) Copyright 2019 Palantir Technologies Inc. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.palantir.atlasdb.http;

import com.google.common.annotations.VisibleForTesting;
import com.palantir.conjure.java.api.config.service.UserAgent;
import com.palantir.conjure.java.api.config.service.UserAgents;
import com.palantir.conjure.java.api.errors.QosException;
import com.palantir.logsafe.SafeArg;
import com.palantir.logsafe.exceptions.SafeIllegalStateException;
import com.palantir.logsafe.logger.SafeLogger;
import com.palantir.logsafe.logger.SafeLoggerFactory;
import java.util.List;
import java.util.Optional;
import javax.annotation.Nonnull;
import javax.inject.Inject;
import javax.inject.Provider;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
import org.glassfish.jersey.spi.ExceptionMappers;

abstract class ProtocolAwareExceptionMapper<E extends Exception> implements ExceptionMapper<E> {
    private static final SafeLogger log = SafeLoggerFactory.get(ProtocolAwareExceptionMapper.class);

    @Context
    private HttpHeaders httpHeaders;

    @Inject
    private Provider<ExceptionMappers> exceptionMappersProvider;

    @Override
    public Response toResponse(E exception) {
        AtlasDbHttpProtocolVersion version = getHttpProtocolVersion();
        switch (version) {
            case LEGACY_OR_UNKNOWN:
                return handleLegacyOrUnknownVersion(exception);
            case CONJURE_JAVA_RUNTIME:
                QosException qosException = handleConjureJavaRuntime(exception);
                return Optional.ofNullable(exceptionMappersProvider.get())
                        .map(provider -> provider.findMapping(qosException))
                        .map(mapper -> mapper.toResponse(qosException))
                        .orElseThrow(() -> new SafeIllegalStateException(
                                "Couldn't find QoS exception mapper." + " This is a product bug."));
            default:
                log.warn(
                        "Couldn't determine what to do with protocol version {}. This is a product bug.",
                        SafeArg.of("protocolVersion", version));
                throw new SafeIllegalStateException(
                        "Unrecognized protocol version in HttpProtocolAwareExceptionMapper",
                        SafeArg.of("protocolVersion", version));
        }
    }

    private AtlasDbHttpProtocolVersion getHttpProtocolVersion() {
        return AtlasDbHttpProtocolVersion.inferFromString(
                Optional.ofNullable(httpHeaders.getRequestHeader(HttpHeaders.USER_AGENT))
                        .flatMap(ProtocolAwareExceptionMapper::parseProtocolVersionFromUserAgentHeader));
    }

    @VisibleForTesting
    static Optional<String> parseProtocolVersionFromUserAgentHeader(@Nonnull List<String> userAgentHeader) {
        return userAgentHeader.stream()
                .map(UserAgents::tryParse)
                .map(UserAgent::informational)
                .flatMap(List::stream)
                .filter(agent -> agent.name().equals(AtlasDbRemotingConstants.ATLASDB_HTTP_CLIENT))
                .findFirst()
                .map(UserAgent.Agent::version);
    }

    abstract Response handleLegacyOrUnknownVersion(E exception);

    abstract QosException handleConjureJavaRuntime(E exception);
}

@@ -1110,16 +1110,87 @@
 actory;%0A
+import com.palantir.util.RateLimitedLogger;%0Aimport java.time.Duration;%0A
 import j
@@ -1714,16 +1714,175 @@
 .class);
+%0A    private static final RateLimitedLogger LEGACY_OR_UNKNOWN_VERSION_LOGGER =%0A            new RateLimitedLogger(log, 1. / Duration.ofMinutes(10).toSeconds());
 %0A%0A    @C
@@ -2198,16 +2198,68 @@
 NKNOWN:%0A
+                logLegacyProtocolUsageIfPossible();%0A
         
@@ -3295,313 +3295,703 @@
 ate 
-AtlasDbHttpProtocolVersion getHttpProtocolVersion() %7B%0A        return AtlasDbHttpProtocolVersion.inferFromString(%0A                Optional.ofNullable(httpHeaders.getRequestHeader(HttpHeaders.USER_AGENT))%0A                        .flatMap(ProtocolAwareExceptionMapper::parseProtocolVersionFromUserAgentHeader
+void logLegacyProtocolUsageIfPossible() %7B%0A        LEGACY_OR_UNKNOWN_VERSION_LOGGER.log(logger -%3E logger.info(%0A                %22Usage of the LEGACY_OR_UNKNOWN HTTP protocol version for AtlasDB was detected.%22,%0A                SafeArg.of(%22userAgent%22, parseUserAgentFromHeaders())));%0A    %7D%0A%0A    private AtlasDbHttpProtocolVersion getHttpProtocolVersion() %7B%0A        return AtlasDbHttpProtocolVersion.inferFromString(parseUserAgentFromHeaders()%0A                .flatMap(ProtocolAwareExceptionMapper::parseProtocolVersionFromUserAgentHeader));%0A    %7D%0A%0A    private Optional%3CList%3CString%3E%3E parseUserAgentFromHeaders() %7B%0A        return Optional.ofNullable(httpHeaders.getRequestHeader(HttpHeaders.USER_AGENT
 ));%0A

Check if people are still using non-Conjure clients to talk to AtlasDB (#5833)