<commit_before>/*
 *  Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) 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 org.wso2.carbon.uuf;

import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import org.osgi.service.component.annotations.ReferenceCardinality;
import org.osgi.service.component.annotations.ReferencePolicy;
import org.slf4j.MDC;
import org.wso2.carbon.uuf.core.create.AppCreator;
import org.wso2.carbon.uuf.core.create.RenderableCreator;
import org.wso2.carbon.uuf.fileio.ArtifactResolver;
import org.wso2.carbon.uuf.fileio.BundleClassLoaderProvider;
import org.wso2.carbon.uuf.fileio.HttpRequest;
import org.wso2.carbon.uuf.fileio.StaticResolver;
import org.wso2.msf4j.Microservice;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;
import java.util.HashSet;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * UUF Main Service.
 */
@Component(name = "org.wso2.carbon.uuf.UUFService",
        service = Microservice.class,
        immediate = true)
@Path("/")
public class UUFService implements Microservice {

    private static final Set<RenderableCreator> RENDERABLE_CREATORS = new HashSet<>();

    private UUFRegistry registry;
    private final AtomicInteger count = new AtomicInteger(0);

    @SuppressWarnings("unused")
    public UUFService() {
        // We need an empty constructor for running in OSGi mode.
        this(createRegistry());
    }

    public UUFService(UUFRegistry server) {
        // Used in the fat-jar mode.
        this.registry = server;
    }

    private static UUFRegistry createRegistry() {
        ArtifactResolver appResolver = new ArtifactResolver();
        StaticResolver staticResolver = new StaticResolver();
        AppCreator appCreator = new AppCreator(RENDERABLE_CREATORS, new BundleClassLoaderProvider());
        return new UUFRegistry(appCreator, Optional.empty(), appResolver, staticResolver);
    }

    @GET
    @Path(".*")
    @Produces({"text/plain"})
    public Response get(@Context io.netty.handler.codec.http.HttpRequest request) {
        try {
            MDC.put("uuf-request", String.valueOf(count.incrementAndGet()));
            Response.ResponseBuilder response = registry.serve(new HttpRequest(request));
            return response.build();
        } finally {
            try {<commit_msg>Adding form post handling to uuf service<commit_after>/*
 *  Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) 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 org.wso2.carbon.uuf;

import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import org.osgi.service.component.annotations.ReferenceCardinality;
import org.osgi.service.component.annotations.ReferencePolicy;
import org.slf4j.MDC;
import org.wso2.carbon.uuf.core.create.AppCreator;
import org.wso2.carbon.uuf.core.create.RenderableCreator;
import org.wso2.carbon.uuf.fileio.ArtifactResolver;
import org.wso2.carbon.uuf.fileio.BundleClassLoaderProvider;
import org.wso2.carbon.uuf.fileio.HttpRequest;
import org.wso2.carbon.uuf.fileio.StaticResolver;
import org.wso2.msf4j.Microservice;

import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;
import java.util.HashSet;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * UUF Main Service.
 */
@Component(name = "org.wso2.carbon.uuf.UUFService",
        service = Microservice.class,
        immediate = true)
@Path("/")
public class UUFService implements Microservice {

    private static final Set<RenderableCreator> RENDERABLE_CREATORS = new HashSet<>();

    private UUFRegistry registry;
    private final AtomicInteger count = new AtomicInteger(0);

    @SuppressWarnings("unused")
    public UUFService() {
        // We need an empty constructor for running in OSGi mode.
        this(createRegistry());
    }

    public UUFService(UUFRegistry server) {
        // Used in the fat-jar mode.
        this.registry = server;
    }

    private static UUFRegistry createRegistry() {
        ArtifactResolver appResolver = new ArtifactResolver();
        StaticResolver staticResolver = new StaticResolver();
        AppCreator appCreator = new AppCreator(RENDERABLE_CREATORS, new BundleClassLoaderProvider());
        return new UUFRegistry(appCreator, Optional.empty(), appResolver, staticResolver);
    }

    @GET
    @Path(".*")
    @Produces({"text/plain"})
    public Response get(@Context io.netty.handler.codec.http.HttpRequest request) {
        return execute(new HttpRequest(request));
    }

    @POST
    @Path(".*")
    @Produces({"text/plain"})
    public Response post(@Context io.netty.handler.codec.http.HttpRequest request) {
        return execute(new HttpRequest(request));
    }

    private Response execute(HttpRequest request){
        try {
            MDC.put("uuf-request", String.valueOf(count.incrementAndGet()));
            Response.ResponseBuilder response = registry.serve(request);
            return response.build();
        } finally {
            try {
{'fraction_non_alphanumeric': 0.10549245371090711, 'fraction_numerical': 0.006846117939940875, 'mean_word_length': 3.674909090909091, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5393626', 'n_tokens_mistral': 1943, 'n_tokens_neox': 1899, 'n_words': 485}