<commit_before>/*
 * Copyright 2014 Red Hat, Inc, and individual contributors.
 *
 * 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.projectodd.wunderboss.wildfly;

import io.undertow.server.HandlerWrapper;
import io.undertow.server.HttpHandler;
import io.undertow.server.HttpServerExchange;
import io.undertow.servlet.spec.ServletContextImpl;
import org.jboss.logging.Logger;
import org.projectodd.wunderboss.Options;
import org.projectodd.wunderboss.web.UndertowWeb;

import javax.servlet.Servlet;
import javax.servlet.ServletContext;
import javax.servlet.ServletRegistration;
import java.util.Map;

import static org.projectodd.wunderboss.web.Web.RegisterOption.PATH;
import static org.projectodd.wunderboss.web.Web.RegisterOption.SERVLET_NAME;

public class WildFlyWeb extends UndertowWeb {

    public WildFlyWeb(String name, ServletContext servletContext) {
        super(name, new Options<CreateOption>());
        this.servletContext = servletContext;
        if (servletContext != null) {
            addHandlerWrapper();
        }
    }

    @Override
    public boolean registerServlet(Servlet servlet, Map<RegisterOption, Object> opts) {
        final Options<RegisterOption> options = new Options<>(opts);
        final String context = options.getString(PATH);
        final String servletName = options.getString(SERVLET_NAME);
        // TODO: Take mapping instead of path for servlets?
        final String mapping = context.endsWith("/") ? context + "*" : context + "/*";

        Class servletClass = servlet.getClass();

        ServletRegistration.Dynamic servletRegistration = servletContext.addServlet(servletName != null ? servletName : servletClass.getSimpleName(), servlet);
        servletRegistration.addMapping(mapping);
        servletRegistration.setLoadOnStartup(1);
        servletRegistration.setAsyncSupported(true);

        HttpHandler handler = new HttpHandler() {
            @Override
            public void handleRequest(HttpServerExchange exchange) throws Exception {
                // Restore the relative and resolved paths we saved earlier
                // so that the Servlet mapping will match correctly
                final String originalRelativePath = exchange.getPathParameters().get(ORIGINAL_RELATIVE_PATH).remove();
                final String originalResolvedPath = exchange.getPathParameters().get(ORIGINAL_RESOLVED_PATH).remove();
                exchange.setRelativePath(originalRelativePath);
                exchange.setResolvedPath(originalResolvedPath);
                servletHandler.handleRequest(exchange);
            }
        };
        return registerHandler(handler, options);
    }

    @Override<commit_msg>Don't use the classname as the fallback servlet name.<commit_after>/*
 * Copyright 2014 Red Hat, Inc, and individual contributors.
 *
 * 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.projectodd.wunderboss.wildfly;

import io.undertow.server.HandlerWrapper;
import io.undertow.server.HttpHandler;
import io.undertow.server.HttpServerExchange;
import io.undertow.servlet.spec.ServletContextImpl;
import org.jboss.logging.Logger;
import org.projectodd.wunderboss.Options;
import org.projectodd.wunderboss.web.UndertowWeb;

import javax.servlet.Servlet;
import javax.servlet.ServletContext;
import javax.servlet.ServletRegistration;
import java.util.Map;

import static org.projectodd.wunderboss.web.Web.RegisterOption.PATH;
import static org.projectodd.wunderboss.web.Web.RegisterOption.SERVLET_NAME;

public class WildFlyWeb extends UndertowWeb {

    public WildFlyWeb(String name, ServletContext servletContext) {
        super(name, new Options<CreateOption>());
        this.servletContext = servletContext;
        if (servletContext != null) {
            addHandlerWrapper();
        }
    }

    @Override
    public boolean registerServlet(Servlet servlet, Map<RegisterOption, Object> opts) {
        final Options<RegisterOption> options = new Options<>(opts);
        final String context = options.getString(PATH);
        final String servletName = options.getString(SERVLET_NAME);
        // TODO: Take mapping instead of path for servlets?
        final String mapping = context.endsWith("/") ? context + "*" : context + "/*";

        Class servletClass = servlet.getClass();

        ServletRegistration.Dynamic servletRegistration = servletContext.addServlet(servletName != null ? servletName : context, servlet);
        servletRegistration.addMapping(mapping);
        servletRegistration.setLoadOnStartup(1);
        servletRegistration.setAsyncSupported(true);

        HttpHandler handler = new HttpHandler() {
            @Override
            public void handleRequest(HttpServerExchange exchange) throws Exception {
                // Restore the relative and resolved paths we saved earlier
                // so that the Servlet mapping will match correctly
                final String originalRelativePath = exchange.getPathParameters().get(ORIGINAL_RELATIVE_PATH).remove();
                final String originalResolvedPath = exchange.getPathParameters().get(ORIGINAL_RESOLVED_PATH).remove();
                exchange.setRelativePath(originalRelativePath);
                exchange.setResolvedPath(originalResolvedPath);
                servletHandler.handleRequest(exchange);
            }
        };
        return registerHandler(handler, options);
    }

    @Override
{'fraction_non_alphanumeric': 0.08332035508487774, 'fraction_numerical': 0.0028033016664071016, 'mean_word_length': 3.506666666666667, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '3112889', 'n_tokens_mistral': 1658, 'n_tokens_neox': 1586, 'n_words': 461}