Search in sources :

Example 16 with WebConfiguration

use of com.sun.faces.config.WebConfiguration in project mojarra by eclipse-ee4j.

the class AbstractConfigProcessor method getProjectStage.

private ProjectStage getProjectStage(ServletContext sc, FacesContext facesContext) {
    final String projectStageKey = AbstractConfigProcessor.class.getName() + ".PROJECTSTAGE";
    ProjectStage projectStage = (ProjectStage) sc.getAttribute(projectStageKey);
    if (projectStage == null) {
        WebConfiguration webConfig = WebConfiguration.getInstance(facesContext.getExternalContext());
        String value = webConfig.getEnvironmentEntry(WebConfiguration.WebEnvironmentEntry.ProjectStage);
        if (value != null) {
            if (LOGGER.isLoggable(FINE)) {
                LOGGER.log(FINE, "ProjectStage configured via JNDI: {0}", value);
            }
        } else {
            value = webConfig.getOptionValue(JakartaFacesProjectStage);
            if (value != null) {
                if (LOGGER.isLoggable(FINE)) {
                    LOGGER.log(FINE, "ProjectStage configured via servlet context init parameter: {0}", value);
                }
            }
        }
        if (value != null) {
            try {
                projectStage = ProjectStage.valueOf(value);
            } catch (IllegalArgumentException iae) {
                if (LOGGER.isLoggable(INFO)) {
                    LOGGER.log(INFO, "Unable to discern ProjectStage for value {0}.", value);
                }
            }
        }
        if (projectStage == null) {
            projectStage = Production;
        }
        sc.setAttribute(projectStageKey, projectStage);
    }
    return projectStage;
}
Also used : ProjectStage(jakarta.faces.application.ProjectStage) JakartaFacesProjectStage(com.sun.faces.config.WebConfiguration.WebContextInitParameter.JakartaFacesProjectStage) WebConfiguration(com.sun.faces.config.WebConfiguration)

Example 17 with WebConfiguration

use of com.sun.faces.config.WebConfiguration in project mojarra by eclipse-ee4j.

the class FilterClassesFromFacesInitializerAnnotationProvider method createAnnotatedMap.

// ---------------------------------------------------------- Private Methods
/**
 * Go over the annotated set and converter it to a hash map.
 *
 * @param annotatedMap
 * @param annotatedSet
 */
private Map<Class<? extends Annotation>, Set<Class<?>>> createAnnotatedMap(Set<Class<?>> annotatedSet) {
    HashMap<Class<? extends Annotation>, Set<Class<?>>> annotatedMap = new HashMap<>();
    if (isEmpty(annotatedSet)) {
        return annotatedMap;
    }
    WebConfiguration webConfig = WebConfiguration.getInstance();
    boolean annotationScanPackagesSet = webConfig.isSet(AnnotationScanPackages);
    String[] annotationScanPackages = annotationScanPackagesSet ? webConfig.getOptionValue(AnnotationScanPackages).split("\\s+") : null;
    Iterator<Class<?>> iterator = annotatedSet.iterator();
    while (iterator.hasNext()) {
        try {
            Class<?> clazz = iterator.next();
            stream(clazz.getAnnotations()).map(annotation -> annotation.annotationType()).filter(annotationType -> FACES_ANNOTATION_TYPE.contains(annotationType)).forEach(annotationType -> {
                Set<Class<?>> classes = annotatedMap.computeIfAbsent(annotationType, e -> new HashSet<>());
                if (annotationScanPackagesSet) {
                    if (matchesAnnotationScanPackages(clazz, annotationScanPackages)) {
                        classes.add(clazz);
                    }
                } else {
                    classes.add(clazz);
                }
            });
        } catch (NoClassDefFoundError ncdfe) {
        }
    }
    return annotatedMap;
}
Also used : Util.isEmpty(com.sun.faces.util.Util.isEmpty) Iterator(java.util.Iterator) WebConfiguration(com.sun.faces.config.WebConfiguration) ANNOTATED_CLASSES(com.sun.faces.RIConstants.ANNOTATED_CLASSES) AnnotationProvider(com.sun.faces.spi.AnnotationProvider) Set(java.util.Set) HashMap(java.util.HashMap) AnnotationScanPackages(com.sun.faces.config.WebConfiguration.WebContextInitParameter.AnnotationScanPackages) HashSet(java.util.HashSet) FACES_ANNOTATION_TYPE(com.sun.faces.config.manager.spi.AnnotationScanner.FACES_ANNOTATION_TYPE) Map(java.util.Map) Annotation(java.lang.annotation.Annotation) URI(java.net.URI) ServletContext(jakarta.servlet.ServletContext) Arrays.stream(java.util.Arrays.stream) Set(java.util.Set) HashSet(java.util.HashSet) HashMap(java.util.HashMap) Annotation(java.lang.annotation.Annotation) WebConfiguration(com.sun.faces.config.WebConfiguration)

Example 18 with WebConfiguration

use of com.sun.faces.config.WebConfiguration in project mojarra by eclipse-ee4j.

the class BaseWebConfigResourceProvider method getResources.

// ------------------------------ Methods from ConfigurationResourceProvider
@Override
public Collection<URI> getResources(ServletContext context) {
    WebConfiguration webConfig = WebConfiguration.getInstance(context);
    String paths = webConfig.getOptionValue(getParameter());
    Set<URI> urls = new LinkedHashSet<>(6);
    if (paths != null) {
        for (String token : split(context, paths.trim(), getSeparatorRegex())) {
            String path = token.trim();
            if (!isExcluded(path) && path.length() != 0) {
                URI u = getContextURLForPath(context, path);
                if (u != null) {
                    urls.add(u);
                } else {
                    if (LOGGER.isLoggable(WARNING)) {
                        LOGGER.log(WARNING, "faces.config.web_resource_not_found", new Object[] { path, JakartaFacesConfigFiles.getQualifiedName() });
                    }
                }
            }
        }
    }
    return urls;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) URI(java.net.URI) WebConfiguration(com.sun.faces.config.WebConfiguration)

Example 19 with WebConfiguration

use of com.sun.faces.config.WebConfiguration in project mojarra by eclipse-ee4j.

the class MetaInfFacesConfigResourceProvider method getResources.

// ------------------------------ Methods From ConfigurationResourceProvider
/**
 * @see com.sun.faces.spi.ConfigurationResourceProvider#getResources(jakarta.servlet.ServletContext)
 */
@Override
public Collection<URI> getResources(ServletContext context) {
    WebConfiguration webConfig = WebConfiguration.getInstance(context);
    String duplicateJarPattern = webConfig.getOptionValue(WebConfiguration.WebContextInitParameter.DuplicateJARPattern);
    Pattern duplicatePattern = null;
    if (duplicateJarPattern != null) {
        duplicatePattern = Pattern.compile(duplicateJarPattern);
    }
    SortedMap<String, Set<URI>> sortedJarMap = new TreeMap<>();
    // noinspection CollectionWithoutInitialCapacity
    List<URI> unsortedResourceList = new ArrayList<>();
    try {
        for (URI uri : loadURLs(context)) {
            String jarUrl = uri.toString();
            String jarName = null;
            Matcher m = JAR_PATTERN.matcher(jarUrl);
            if (m.matches()) {
                jarName = m.group(1);
            }
            if (jarName != null) {
                if (duplicatePattern != null) {
                    m = duplicatePattern.matcher(jarName);
                    if (m.matches()) {
                        jarName = m.group(1);
                    }
                }
                Set<URI> uris = sortedJarMap.get(jarName);
                if (uris == null) {
                    uris = new HashSet<>();
                    sortedJarMap.put(jarName, uris);
                }
                uris.add(uri);
            } else {
                unsortedResourceList.add(0, uri);
            }
        }
    } catch (IOException e) {
        throw new FacesException(e);
    }
    // Load the sorted resources first:
    List<URI> result = new ArrayList<>(sortedJarMap.size() + unsortedResourceList.size());
    for (Map.Entry<String, Set<URI>> entry : sortedJarMap.entrySet()) {
        result.addAll(entry.getValue());
    }
    // Then load the unsorted resources
    result.addAll(unsortedResourceList);
    return result;
}
Also used : Pattern(java.util.regex.Pattern) Set(java.util.Set) HashSet(java.util.HashSet) Matcher(java.util.regex.Matcher) ArrayList(java.util.ArrayList) IOException(java.io.IOException) TreeMap(java.util.TreeMap) URI(java.net.URI) FacesException(jakarta.faces.FacesException) TreeMap(java.util.TreeMap) Map(java.util.Map) SortedMap(java.util.SortedMap) WebConfiguration(com.sun.faces.config.WebConfiguration)

Example 20 with WebConfiguration

use of com.sun.faces.config.WebConfiguration in project mojarra by eclipse-ee4j.

the class ScriptStyleBaseRenderer method encodeEnd.

@Override
public void encodeEnd(FacesContext context, UIComponent component) throws IOException {
    Map<String, Object> attributes = component.getAttributes();
    String name = (String) attributes.get("name");
    if (null == name) {
        return;
    }
    // Special case of resource names that have query strings.
    // These resources actually use their query strings internally, not externally, so we don't need the resource to know
    // about them.
    int queryPos = name.indexOf("?");
    String query = null;
    if (queryPos > -1 && name.length() > queryPos) {
        query = name.substring(queryPos + 1);
        name = name.substring(0, queryPos);
    }
    String library = (String) attributes.get("library");
    // Ensure this resource is not rendered more than once per request.
    ResourceHandler resourceHandler = context.getApplication().getResourceHandler();
    if (resourceHandler.isResourceRendered(context, name, library)) {
        return;
    }
    Resource resource = resourceHandler.createResource(name, library);
    String resourceUrl = "RES_NOT_FOUND";
    ResponseWriter writer = context.getResponseWriter();
    startExternalElement(context, writer, component);
    WebConfiguration webConfig = WebConfiguration.getInstance();
    if (library == null && name != null && name.startsWith(webConfig.getOptionValue(WebConfiguration.WebContextInitParameter.WebAppContractsDirectory))) {
        if (context.isProjectStage(ProjectStage.Development)) {
            String msg = "Illegal path, direct contract references are not allowed: " + name;
            context.addMessage(component.getClientId(context), new FacesMessage(FacesMessage.SEVERITY_ERROR, msg, msg));
        }
        resource = null;
    }
    if (resource == null) {
        if (context.isProjectStage(ProjectStage.Development)) {
            String msg = "Unable to find resource " + (library == null ? "" : library + ", ") + name;
            context.addMessage(component.getClientId(context), new FacesMessage(FacesMessage.SEVERITY_ERROR, msg, msg));
        }
    } else {
        resourceUrl = resource.getRequestPath();
        if (query != null) {
            resourceUrl = resourceUrl + (resourceUrl.indexOf("?") > -1 ? "&amp;" : "?") + query;
        }
        resourceUrl = context.getExternalContext().encodeResourceURL(resourceUrl);
    }
    endExternalElement(writer, component, resourceUrl);
    resourceHandler.markResourceRendered(context, name, library);
    // Remove the key to prevent issues with state saving.
    String ccID = (String) component.getAttributes().get(COMP_KEY);
    if (ccID != null) {
        // the first pop maps to the component we're rendering.
        // the second pop maps to the composite component that was pushed
        // in this renderer's encodeBegin implementation.
        // re-push the current component to reset the original context
        component.popComponentFromEL(context);
        component.popComponentFromEL(context);
        component.pushComponentToEL(context, component);
    }
}
Also used : ResponseWriter(jakarta.faces.context.ResponseWriter) Resource(jakarta.faces.application.Resource) ResourceHandler(jakarta.faces.application.ResourceHandler) FacesMessage(jakarta.faces.application.FacesMessage) WebConfiguration(com.sun.faces.config.WebConfiguration)

Aggregations

WebConfiguration (com.sun.faces.config.WebConfiguration)25 IOException (java.io.IOException)4 URI (java.net.URI)4 VariableMapperWrapper (com.sun.faces.facelets.el.VariableMapperWrapper)3 VariableMapper (jakarta.el.VariableMapper)3 TagAttributeException (jakarta.faces.view.facelets.TagAttributeException)3 FaceletContextImplBase (com.sun.faces.facelets.FaceletContextImplBase)2 FacesMessage (jakarta.faces.application.FacesMessage)2 Resource (jakarta.faces.application.Resource)2 ResourceHandler (jakarta.faces.application.ResourceHandler)2 ExternalContext (jakarta.faces.context.ExternalContext)2 FacesContext (jakarta.faces.context.FacesContext)2 HashSet (java.util.HashSet)2 Map (java.util.Map)2 Set (java.util.Set)2 Matcher (java.util.regex.Matcher)2 NodeList (org.w3c.dom.NodeList)2 ANNOTATED_CLASSES (com.sun.faces.RIConstants.ANNOTATED_CLASSES)1 FaceletsConfiguration (com.sun.faces.config.FaceletsConfiguration)1 AnnotationScanPackages (com.sun.faces.config.WebConfiguration.WebContextInitParameter.AnnotationScanPackages)1