001/*
002 * The contents of this file are subject to the license and copyright
003 * detailed in the LICENSE and NOTICE files at the root of the source
004 * tree.
005 */
006package org.fcrepo.http.commons.webxml;
007
008import static java.util.Collections.unmodifiableList;
009
010import java.util.ArrayList;
011import java.util.Collection;
012import java.util.List;
013import java.util.function.Predicate;
014import java.util.stream.Collectors;
015
016
017import javax.xml.bind.annotation.XmlElement;
018import javax.xml.bind.annotation.XmlElements;
019import javax.xml.bind.annotation.XmlRootElement;
020
021import org.fcrepo.http.commons.webxml.bind.ContextParam;
022import org.fcrepo.http.commons.webxml.bind.Displayable;
023import org.fcrepo.http.commons.webxml.bind.Filter;
024import org.fcrepo.http.commons.webxml.bind.FilterMapping;
025import org.fcrepo.http.commons.webxml.bind.Listener;
026import org.fcrepo.http.commons.webxml.bind.Servlet;
027import org.fcrepo.http.commons.webxml.bind.ServletMapping;
028
029/**
030 * <p>WebAppConfig class.</p>
031 *
032 * @author awoods
033 */
034@XmlRootElement(namespace = "http://java.sun.com/xml/ns/javaee",
035        name = "web-app")
036public class WebAppConfig extends Displayable {
037
038    @XmlElements(value = {@XmlElement(
039            namespace = "http://java.sun.com/xml/ns/javaee",
040            name = "context-param")})
041    List<ContextParam> contextParams;
042
043    @XmlElements(
044            value = {@XmlElement(
045                    namespace = "http://java.sun.com/xml/ns/javaee",
046                    name = "listener")})
047    List<Listener> listeners;
048
049    @XmlElements(value = {@XmlElement(
050            namespace = "http://java.sun.com/xml/ns/javaee", name = "servlet")})
051    List<Servlet> servlets;
052
053    @XmlElements(value = {@XmlElement(
054            namespace = "http://java.sun.com/xml/ns/javaee", name = "filter")})
055    List<Filter> filters;
056
057    @XmlElements(value = {@XmlElement(
058            namespace = "http://java.sun.com/xml/ns/javaee",
059            name = "servlet-mapping")})
060    List<ServletMapping> servletMappings;
061
062    @XmlElements(value = {@XmlElement(
063            namespace = "http://java.sun.com/xml/ns/javaee",
064            name = "filter-mapping")})
065    List<FilterMapping> filterMappings;
066
067    public Collection<ServletMapping> servletMappings(final String servletName) {
068        return servletMappings.stream().filter(new SMapFilter(servletName)).collect(Collectors.toList());
069    }
070
071    public Collection<FilterMapping> filterMappings(final String filterName) {
072        return filterMappings.stream().filter(new FMapFilter(filterName)).collect(Collectors.toList());
073    }
074
075    private static final List<ContextParam> NO_CP = unmodifiableList(new ArrayList<>(0));
076
077    public Collection<ContextParam> contextParams() {
078        return (contextParams != null) ? contextParams : NO_CP;
079    }
080
081    private static final List<Servlet> NO_S = unmodifiableList(new ArrayList<>(0));
082
083    public Collection<Servlet> servlets() {
084        return (servlets != null) ? servlets : NO_S;
085    }
086
087    private static final List<Filter> NO_F = unmodifiableList(new ArrayList<>(0));
088
089    public Collection<Filter> filters() {
090        return (filters != null) ? filters : NO_F;
091    }
092
093    private static final List<Listener> NO_L = unmodifiableList(new ArrayList<>(0));
094
095    public Collection<Listener> listeners() {
096        return (listeners != null) ? listeners : NO_L;
097    }
098
099    private static class SMapFilter implements Predicate<ServletMapping> {
100
101        final String servletName;
102
103        SMapFilter(final String sName) {
104            servletName = sName;
105        }
106
107        @Override
108        public boolean test(final ServletMapping input) {
109            return (servletName == null) ? input.servletName() == null
110                    : servletName.equals(input.servletName());
111        }
112
113    }
114
115    private static class FMapFilter implements Predicate<FilterMapping> {
116
117        final String filterName;
118
119        FMapFilter(final String sName) {
120            filterName = sName;
121        }
122
123        @Override
124        public boolean test(final FilterMapping input) {
125            return (filterName == null) ? input.filterName() == null
126                    : filterName.equals(input.filterName());
127        }
128
129    }
130}