<commit_before>package org.rakam.http;

import com.fasterxml.jackson.databind.node.ObjectNode;
import com.google.common.base.Optional;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.net.HostAndPort;
import com.google.inject.AbstractModule;
import com.google.inject.Singleton;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.epoll.Epoll;
import io.netty.channel.epoll.EpollEventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.swagger.models.*;
import io.swagger.models.Response;
import io.swagger.models.auth.ApiKeyAuthDefinition;
import io.swagger.models.auth.In;
import io.swagger.models.properties.RefProperty;
import io.swagger.util.PrimitiveType;
import org.apache.avro.generic.GenericRecord;
import org.rakam.Access;
import org.rakam.ServiceStarter;
import org.rakam.analysis.ApiKeyService;
import org.rakam.analysis.CustomParameter;
import org.rakam.analysis.RequestContext;
import org.rakam.analysis.RequestPreProcessorItem;
import org.rakam.server.http.*;
import org.rakam.server.http.HttpServerBuilder.IRequestParameterFactory;
import org.rakam.server.http.annotations.Api;
import org.rakam.server.http.annotations.ApiOperation;
import org.rakam.server.http.annotations.Authorization;
import org.rakam.util.*;

import javax.inject.Inject;
import java.lang.reflect.Method;
import java.time.ZoneId;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.Set;

import static io.netty.handler.codec.http.HttpHeaders.Names.ACCESS_CONTROL_ALLOW_CREDENTIALS;
import static io.netty.handler.codec.http.HttpResponseStatus.FORBIDDEN;

@Singleton
public class WebServiceModule
        extends AbstractModule {
    private final Set<WebSocketService> webSocketServices;
    private final Set<HttpService> httpServices;
    private final HttpServerConfig config;
    private final Set<Tag> tags;
    private final Set<CustomParameter> customParameters;
    private final Set<RequestPreProcessorItem> requestPreProcessorItems;
    private final HttpRequestHandler requestHandler;

    @Inject
    public WebServiceModule(Set<HttpService> httpServices,
                            Set<Tag> tags,
                            Set<CustomParameter> customParameters,
                            Set<RequestPreProcessorItem> requestPreProcessorItems,
                            Set<WebSocketService> webSocketServices,
                            @NotFoundHandler Optional<HttpRequestHandler> requestHandler,
                            HttpServerConfig config) {
        this.httpServices = httpServices;
        this.webSocketServices = webSocketServices;
        this.requestPreProcessorItems = requestPreProcessorItems;
        this.config = config;
        this.tags = tags;
        this.customParameters = customParameters;
        this.requestHandler = requestHandler.orNull();
    }

    @Override
    protected void configure() {
        Info info = new Info()
                .title("Rakam API Documentation")
                .version(ServiceStarter.RAKAM_VERSION)
                .description("An analytics platform API that lets you create your own analytics services.")
                .contact(new Contact().email("<EMAIL>"))
                .license(new License()
                        .name("Apache License 2.0")
                        .url("http://www.apache.org/licenses/LICENSE-2.0.html"));

        Swagger swagger = new Swagger().info(info)
                .basePath("/")
                .tags(ImmutableList.copyOf(tags))
                .securityDefinition("write_key", new ApiKeyAuthDefinition().in(In.HEADER).name("write_key"))
                .securityDefinition("master_key", new ApiKeyAuthDefinition().in(In.HEADER).name("master_key"));

        EventLoopGroup eventExecutors;
        if (Epoll.isAvailable()) {
            eventExecutors = new EpollEventLoopGroup();
        } else {
            eventExecutors = new NioEventLoopGroup();
        }

        HttpServerBuilder httpServer = new HttpServerBuilder()
                .setHttpServices(httpServices)
                .setWebsocketServices(webSocketServices)
                .setSwagger(swagger)
                .setEventLoopGroup(eventExecutors)
                .setSwaggerOperationProcessor((method, operation) -> {
                    ApiOperation annotation = method.getAnnotation(ApiOperation.class);
                    if (annotation != null && annotation.authorizations() != null && annotation.authorizations().length > 0) {
                        String value = annotation.authorizations()[0].value();
                        if (value != null && !value.isEmpty()) {
                            operation.response(FORBIDDEN.code(), new Response()
                                    .schema(new RefProperty("ErrorMessage"))
                                    .description(value + " is invalid"));
                        }
                    }
                })
                .setMapper(JsonHelper.getMapper())
                .setProxyProtocol(config.getProxyProtocol())
                .setExceptionHandler((request, ex) -> {
                    if (ex instanceof RakamException) {
                        LogUtil.logException(request, (RakamException) ex);
                    }
                    if (!(ex instanceof HttpRequestException)) {
                        LogUtil.logException(request, ex);
                    }
                })
                .setOverridenMappings(ImmutableMap.of(GenericRecord.class, PrimitiveType.OBJECT, ZoneId.class, PrimitiveType.STRING));

        requestPreProcessorItems.forEach(i -> httpServer.addJsonPreprocessor(i.processor, i.predicate));

        ImmutableMap.Builder<String, IRequestParameterFactory> builder = ImmutableMap.<String, IRequestParameterFactory>builder();

        for (CustomParameter customParameter : customParameters) {
            builder.put(customParameter.parameterName, customParameter.factory);
        }

        httpServer.setCustomRequestParameters(builder.build());
        httpServer.setMaximumBody(config.getMaximumRequestSize());

        HttpServer build = httpServer.build();

        if (requestHandler != null) {
            build.setNotFoundHandler(requestHandler);
        }
<commit_msg>Update WebServiceModule.java<commit_after>package org.rakam.http;

import com.fasterxml.jackson.databind.node.ObjectNode;
import com.google.common.base.Optional;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.net.HostAndPort;
import com.google.inject.AbstractModule;
import com.google.inject.Singleton;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.epoll.Epoll;
import io.netty.channel.epoll.EpollEventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.swagger.models.*;
import io.swagger.models.Response;
import io.swagger.models.auth.ApiKeyAuthDefinition;
import io.swagger.models.auth.In;
import io.swagger.models.properties.RefProperty;
import io.swagger.util.PrimitiveType;
import org.apache.avro.generic.GenericRecord;
import org.rakam.Access;
import org.rakam.ServiceStarter;
import org.rakam.analysis.ApiKeyService;
import org.rakam.analysis.CustomParameter;
import org.rakam.analysis.RequestContext;
import org.rakam.analysis.RequestPreProcessorItem;
import org.rakam.server.http.*;
import org.rakam.server.http.HttpServerBuilder.IRequestParameterFactory;
import org.rakam.server.http.annotations.Api;
import org.rakam.server.http.annotations.ApiOperation;
import org.rakam.server.http.annotations.Authorization;
import org.rakam.util.*;

import javax.inject.Inject;
import java.lang.reflect.Method;
import java.time.ZoneId;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.Set;

import static io.netty.handler.codec.http.HttpHeaders.Names.ACCESS_CONTROL_ALLOW_CREDENTIALS;
import static io.netty.handler.codec.http.HttpResponseStatus.FORBIDDEN;

@Singleton
public class WebServiceModule
        extends AbstractModule {
    private final Set<WebSocketService> webSocketServices;
    private final Set<HttpService> httpServices;
    private final HttpServerConfig config;
    private final Set<Tag> tags;
    private final Set<CustomParameter> customParameters;
    private final Set<RequestPreProcessorItem> requestPreProcessorItems;
    private final HttpRequestHandler requestHandler;

    @Inject
    public WebServiceModule(Set<HttpService> httpServices,
                            Set<Tag> tags,
                            Set<CustomParameter> customParameters,
                            Set<RequestPreProcessorItem> requestPreProcessorItems,
                            Set<WebSocketService> webSocketServices,
                            @NotFoundHandler Optional<HttpRequestHandler> requestHandler,
                            HttpServerConfig config) {
        this.httpServices = httpServices;
        this.webSocketServices = webSocketServices;
        this.requestPreProcessorItems = requestPreProcessorItems;
        this.config = config;
        this.tags = tags;
        this.customParameters = customParameters;
        this.requestHandler = requestHandler.orNull();
    }

    @Override
    protected void configure() {
        Info info = new Info()
                .title("Rakam API Documentation")
                .version(ServiceStarter.RAKAM_VERSION)
                .description("An analytics platform API that lets you create your own analytics services.")
                .contact(new Contact().email("<EMAIL>"))
                .license(new License()
                        .name("Apache License 2.0")
                        .url("http://www.apache.org/licenses/LICENSE-2.0.html"));

        Swagger swagger = new Swagger().info(info)
                .basePath("/")
                .tags(ImmutableList.copyOf(tags))
                .securityDefinition("write_key", new ApiKeyAuthDefinition().in(In.HEADER).name("write_key"))
                .securityDefinition("master_key", new ApiKeyAuthDefinition().in(In.HEADER).name("master_key"));

        EventLoopGroup eventExecutors;
        if (Epoll.isAvailable()) {
            eventExecutors = new EpollEventLoopGroup();
        } else {
            eventExecutors = new NioEventLoopGroup();
        }

        HttpServerBuilder httpServer = new HttpServerBuilder()
                .setHttpServices(httpServices)
                .setWebsocketServices(webSocketServices)
                .setSwagger(swagger)
                .setEventLoopGroup(eventExecutors)
                .setSwaggerOperationProcessor((method, operation) -> {
                    ApiOperation annotation = method.getAnnotation(ApiOperation.class);
                    if (annotation != null && annotation.authorizations() != null && annotation.authorizations().length > 0) {
                        String value = annotation.authorizations()[0].value();
                        if (value != null && !value.isEmpty()) {
                            operation.response(FORBIDDEN.code(), new Response()
                                    .schema(new RefProperty("ErrorMessage"))
                                    .description(value + " is invalid"));
                        }
                    }
                })
                .setMapper(JsonHelper.getMapper())
                .setProxyProtocol(config.getProxyProtocol())
                .setExceptionHandler((request, ex) -> {
                    if (ex instanceof RakamException) {
                        RakamException rakamEx = (RakamException) ex;
                        if(rakamEx.getStatusCode() != FORBIDDEN) {
                            LogUtil.logException(request, rakamEx);
                        }
                    }
                    if (!(ex instanceof HttpRequestException)) {
                        LogUtil.logException(request, ex);
                    }
                })
                .setOverridenMappings(ImmutableMap.of(GenericRecord.class, PrimitiveType.OBJECT, ZoneId.class, PrimitiveType.STRING));

        requestPreProcessorItems.forEach(i -> httpServer.addJsonPreprocessor(i.processor, i.predicate));

        ImmutableMap.Builder<String, IRequestParameterFactory> builder = ImmutableMap.<String, IRequestParameterFactory>builder();

        for (CustomParameter customParameter : customParameters) {
            builder.put(customParameter.parameterName, customParameter.factory);
        }

        httpServer.setCustomRequestParameters(builder.build());
        httpServer.setMaximumBody(config.getMaximumRequestSize());

        HttpServer build = httpServer.build();

        if (requestHandler != null) {
            build.setNotFoundHandler(requestHandler);
        }

{'fraction_non_alphanumeric': 0.09552845528455285, 'fraction_numerical': 0.0009380863039399625, 'mean_word_length': 2.5369090406414156, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3311733', 'n_tokens_mistral': 3310, 'n_tokens_neox': 3174, 'n_words': 568}