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