001/**
002 * Copyright 2015 DuraSpace, Inc.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.fcrepo.http.commons.webxml;
017
018import static java.util.Collections.unmodifiableList;
019
020import java.util.ArrayList;
021import java.util.Collection;
022import java.util.List;
023import java.util.function.Predicate;
024import java.util.stream.Collectors;
025
026
027import javax.xml.bind.annotation.XmlElement;
028import javax.xml.bind.annotation.XmlElements;
029import javax.xml.bind.annotation.XmlRootElement;
030
031import org.fcrepo.http.commons.webxml.bind.ContextParam;
032import org.fcrepo.http.commons.webxml.bind.Displayable;
033import org.fcrepo.http.commons.webxml.bind.Filter;
034import org.fcrepo.http.commons.webxml.bind.FilterMapping;
035import org.fcrepo.http.commons.webxml.bind.Listener;
036import org.fcrepo.http.commons.webxml.bind.Servlet;
037import org.fcrepo.http.commons.webxml.bind.ServletMapping;
038
039/**
040 * <p>WebAppConfig class.</p>
041 *
042 * @author awoods
043 */
044@XmlRootElement(namespace = "http://java.sun.com/xml/ns/javaee",
045        name = "web-app")
046public class WebAppConfig extends Displayable {
047
048    @XmlElements(value = {@XmlElement(
049            namespace = "http://java.sun.com/xml/ns/javaee",
050            name = "context-param")})
051    List<ContextParam> contextParams;
052
053    @XmlElements(
054            value = {@XmlElement(
055                    namespace = "http://java.sun.com/xml/ns/javaee",
056                    name = "listener")})
057    List<Listener> listeners;
058
059    @XmlElements(value = {@XmlElement(
060            namespace = "http://java.sun.com/xml/ns/javaee", name = "servlet")})
061    List<Servlet> servlets;
062
063    @XmlElements(value = {@XmlElement(
064            namespace = "http://java.sun.com/xml/ns/javaee", name = "filter")})
065    List<Filter> filters;
066
067    @XmlElements(value = {@XmlElement(
068            namespace = "http://java.sun.com/xml/ns/javaee",
069            name = "servlet-mapping")})
070    List<ServletMapping> servletMappings;
071
072    @XmlElements(value = {@XmlElement(
073            namespace = "http://java.sun.com/xml/ns/javaee",
074            name = "filter-mapping")})
075    List<FilterMapping> filterMappings;
076
077    public Collection<ServletMapping> servletMappings(final String servletName) {
078        return servletMappings.stream().filter(new SMapFilter(servletName)).collect(Collectors.toList());
079    }
080
081    public Collection<FilterMapping> filterMappings(final String filterName) {
082        return filterMappings.stream().filter(new FMapFilter(filterName)).collect(Collectors.toList());
083    }
084
085    private static List<ContextParam> NO_CP = unmodifiableList(new ArrayList<ContextParam>(0));
086
087    public Collection<ContextParam> contextParams() {
088        return (contextParams != null) ? contextParams : NO_CP;
089    }
090
091    private static List<Servlet> NO_S = unmodifiableList(new ArrayList<Servlet>(0));
092
093    public Collection<Servlet> servlets() {
094        return (servlets != null) ? servlets : NO_S;
095    }
096
097    private static List<Filter> NO_F = unmodifiableList(new ArrayList<Filter>(0));
098
099    public Collection<Filter> filters() {
100        return (filters != null) ? filters : NO_F;
101    }
102
103    private static List<Listener> NO_L =
104        unmodifiableList(new ArrayList<Listener>(0));
105
106    public Collection<Listener> listeners() {
107        return (listeners != null) ? listeners : NO_L;
108    }
109
110    private static class SMapFilter implements Predicate<ServletMapping> {
111
112        String servletName;
113
114        SMapFilter(final String sName) {
115            servletName = sName;
116        }
117
118        @Override
119        public boolean test(final ServletMapping input) {
120            return (servletName == null) ? input.servletName() == null
121                    : servletName.equals(input.servletName());
122        }
123
124    }
125
126    private static class FMapFilter implements Predicate<FilterMapping> {
127
128        String filterName;
129
130        FMapFilter(final String sName) {
131            filterName = sName;
132        }
133
134        @Override
135        public boolean test(final FilterMapping input) {
136            return (filterName == null) ? input.filterName() == null
137                    : filterName.equals(input.filterName());
138        }
139
140    }
141}