<commit_before>package org.wso2.carbon.uuf.core;

import org.wso2.carbon.uuf.core.auth.SessionRegistry;

import javax.ws.rs.core.Response;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;

public class App {
    private static final String ROOT_COMPONENT_CONTEXT = "/root";
    private final String context;
    private final Map<String, Component> components;
    private final Component rootComponent;
    private final SessionRegistry sessionRegistry;

    public App(String context, Set<Component> components, SessionRegistry sessionRegistry) {
        if (!context.startsWith("/")) {
            throw new IllegalArgumentException("App context must start with a '/'.");
        }

        this.context = context;
        this.components = components.stream().collect(Collectors.toMap(Component::getContext, Function.identity()));
        this.rootComponent = this.components.get(ROOT_COMPONENT_CONTEXT);
        this.sessionRegistry = sessionRegistry;
    }

    @Deprecated
    public App(String context, Set<Component> components) {
        if (!context.startsWith("/")) {
            throw new IllegalArgumentException("app context must start with a '/'");
        }

        this.context = context;
        this.components = components.stream().collect(Collectors.toMap(Component::getContext, Function.identity()));
        this.rootComponent = this.components.get("/root");

        this.sessionRegistry = null;
    }

    public String renderPage(String uri, RequestLookup requestLookup) {
        String uriWithoutContext = uri.substring(requestLookup.getAppContext().length());
        // First try to render the page with root component
        Optional<String> output = rootComponent.renderPage(uriWithoutContext, requestLookup);
        if (output.isPresent()) {
            return output.get();
        }

        // Since root components doesn't have the page, try with other components
        for (Map.Entry<String, Component> entry : components.entrySet()) {
            if (uriWithoutContext.startsWith(entry.getKey())) {
                Component component = entry.getValue();
                String pageUri = uriWithoutContext.substring(component.getContext().length());
                output = component.renderPage(pageUri, requestLookup);
                if (output.isPresent()) {
                    return output.get();
                }
                break;
            }
        }
        throw new UUFException("Requested page '" + uri + "' does not exists.", Response.Status.NOT_FOUND);
    }

    @Deprecated
    public String renderPage(String uriUpToContext, String uriAfterContext) {
        // First try to render the page with root component
        Optional<String> output = rootComponent.renderPage(uriUpToContext, uriAfterContext);
        if (output.isPresent()) {
            return output.get();
        }

        // Since root components doesn't have the page, try with other components
        int firstSlash = uriAfterContext.indexOf('/', 1);
        if (firstSlash > 0) {
            String componentContext = uriAfterContext.substring(0, firstSlash);
            Component component = components.get(componentContext);
            if (component != null) {
                output = component.renderPage(uriUpToContext, uriAfterContext.substring(firstSlash));
                if (output.isPresent()) {
                    return output.get();
                }
            }
        }
        throw new UUFException("Requested page '" + uriAfterContext + "' does not exists.", Response.Status.NOT_FOUND);
    }<commit_msg>added a constructor which accepts context, components, & SessionRegistry as parameters and changed parameters of 'renderPage' method<commit_after>package org.wso2.carbon.uuf.core;

import org.wso2.carbon.uuf.core.auth.SessionRegistry;

import javax.ws.rs.core.Response;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

public class App {
    @Deprecated
    private static final String ROOT_COMPONENT_CONTEXT = "/root";
    private final String context;
    private final Map<String, Component> components;
    private final Component rootComponent;
    private final SessionRegistry sessionRegistry;
    private final API api;

    public App(String context, Set<Component> components, SessionRegistry sessionRegistry) {
        if (!context.startsWith("/")) {
            throw new IllegalArgumentException("App context must start with a '/'.");
        }

        this.context = context;
        this.components = components.stream().collect(Collectors.toMap(Component::getContext, cmp -> cmp));
        this.rootComponent = this.components.get(ROOT_COMPONENT_CONTEXT);
        this.sessionRegistry = sessionRegistry;
        this.api = new API(sessionRegistry);
    }

    @Deprecated
    public App(String context, Set<Component> components) {
        this.context = context;
        this.components = null;
        this.rootComponent = null;
        this.sessionRegistry = null;
        this.api = null;
    }

    public String renderPage(String uriWithoutContext, RequestLookup requestLookup) {
        // First try to render the page with root component
        Optional<String> output = rootComponent.renderPage(uriWithoutContext, requestLookup, api);
        if (output.isPresent()) {
            return output.get();
        }

        // Since root components doesn't have the page, try with other components
        for (Map.Entry<String, Component> entry : components.entrySet()) {
            if (uriWithoutContext.startsWith(entry.getKey())) {
                Component component = entry.getValue();
                String pageUri = uriWithoutContext.substring(component.getName().length());
                output = component.renderPage(pageUri, requestLookup, api);
                if (output.isPresent()) {
                    return output.get();
                }
                break;
            }
        }
        throw new UUFException("Requested page '" + uriWithoutContext + "' does not exists.",
                               Response.Status.NOT_FOUND);
    }

    @Deprecated
    public String renderPage(String uriUpToContext, String uriAfterContext) {
        return "";
    }
{'fraction_non_alphanumeric': 0.09941612750512861, 'fraction_numerical': 0.0011046236389458735, 'mean_word_length': 2.6530259365994238, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '5281440', 'n_tokens_mistral': 1575, 'n_tokens_neox': 1541, 'n_words': 408}