All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jboss.resteasy.reactive.common.model.ResourceClass Maven / Gradle / Ivy

There is a newer version: 3.17.5
Show newest version
package org.jboss.resteasy.reactive.common.model;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Supplier;

import jakarta.ws.rs.ext.ExceptionMapper;

import org.jboss.resteasy.reactive.common.core.LazyUnmanagedBeanFactory;
import org.jboss.resteasy.reactive.common.util.URLUtils;
import org.jboss.resteasy.reactive.spi.BeanFactory;

public class ResourceClass {

    /**
     * The class name of the resource class
     */
    private String className;

    /**
     * The class path, will be null if this is a sub resource
     */
    private String path;

    /**
     * The resource methods
     */
    private final List methods = new ArrayList<>();

    private BeanFactory factory;
    private boolean perRequestResource;

    private boolean isFormParamRequired;

    private Set pathParameters = new HashSet<>();

    /**
     * Contains class level exception mappers
     * The key is the exception type and the value is the exception mapper class
     */
    private Map classLevelExceptionMappers = new HashMap<>();

    private Supplier isDisabled;

    public String getClassName() {
        return className;
    }

    public ResourceClass setClassName(String className) {
        this.className = className;
        return this;
    }

    public String getPath() {
        return path;
    }

    public BeanFactory getFactory() {
        return factory;
    }

    public ResourceClass setFactory(BeanFactory factory) {
        this.factory = factory;
        return this;
    }

    public ResourceClass setPath(String path) {
        this.path = path;
        if (path != null) {
            pathParameters.clear();
            URLUtils.parsePathParameters(path, pathParameters);
        }
        return this;
    }

    public List getMethods() {
        return methods;
    }

    public boolean isPerRequestResource() {
        return perRequestResource;
    }

    public void setPerRequestResource(boolean perRequestResource) {
        this.perRequestResource = perRequestResource;
    }

    public boolean isFormParamRequired() {
        return isFormParamRequired;
    }

    public ResourceClass setFormParamRequired(boolean isFormParamRequired) {
        this.isFormParamRequired = isFormParamRequired;
        return this;
    }

    public Set getPathParameters() {
        return pathParameters;
    }

    public ResourceClass setPathParameters(Set pathParameters) {
        this.pathParameters = pathParameters;
        return this;
    }

    public Map getClassLevelExceptionMappers() {
        return classLevelExceptionMappers;
    }

    public void setClassLevelExceptionMappers(Map classLevelExceptionMappers) {
        this.classLevelExceptionMappers = classLevelExceptionMappers;
    }

    public Supplier getIsDisabled() {
        return isDisabled;
    }

    public void setIsDisabled(Supplier isDisabled) {
        this.isDisabled = isDisabled;
    }

    @SuppressWarnings({ "rawtypes", "unchecked" })
    public Map, ResourceExceptionMapper> resourceExceptionMapper() {
        if (classLevelExceptionMappers.isEmpty()) {
            return Collections.emptyMap();
        }
        Map, ResourceExceptionMapper> result = new HashMap<>(
                classLevelExceptionMappers.size());
        for (Map.Entry entry : classLevelExceptionMappers.entrySet()) {
            ResourceExceptionMapper mapper = new ResourceExceptionMapper();
            // TODO: consider not using reflection to create these
            mapper.setFactory(new LazyUnmanagedBeanFactory<>(new Supplier>() {
                @Override
                public ExceptionMapper get() {
                    try {
                        return (ExceptionMapper) loadClass(entry.getValue()).getConstructor().newInstance();
                    } catch (Exception e) {
                        throw new RuntimeException(e);
                    }
                }
            }));
            result.put((Class) loadClass(entry.getKey()), mapper);
        }
        return result;
    }

    private static Class loadClass(String className) {
        try {
            return Class.forName(className, false, Thread.currentThread().getContextClassLoader());
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }
}