001/*
002 * Copyright 2012-2018 the original author or authors.
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 */
016
017package org.springframework.boot.actuate.endpoint.web;
018
019import java.util.Collections;
020import java.util.LinkedHashMap;
021import java.util.Map;
022
023import javax.servlet.Servlet;
024
025import org.springframework.beans.BeanUtils;
026import org.springframework.util.Assert;
027import org.springframework.util.StringUtils;
028
029/**
030 * Contains details of a servlet that is exposed as an actuator endpoint.
031 *
032 * @author Phillip Webb
033 */
034public final class EndpointServlet {
035
036        private final Servlet servlet;
037
038        private final Map<String, String> initParameters;
039
040        public EndpointServlet(Class<? extends Servlet> servlet) {
041                Assert.notNull(servlet, "Servlet must not be null");
042                this.servlet = BeanUtils.instantiateClass(servlet);
043                this.initParameters = Collections.emptyMap();
044        }
045
046        public EndpointServlet(Servlet servlet) {
047                Assert.notNull(servlet, "Servlet must not be null");
048                this.servlet = servlet;
049                this.initParameters = Collections.emptyMap();
050        }
051
052        private EndpointServlet(Servlet servlet, Map<String, String> initParameters) {
053                this.servlet = servlet;
054                this.initParameters = Collections.unmodifiableMap(initParameters);
055        }
056
057        public EndpointServlet withInitParameter(String name, String value) {
058                Assert.hasText(name, "Name must not be empty");
059                return withInitParameters(Collections.singletonMap(name, value));
060        }
061
062        public EndpointServlet withInitParameters(Map<String, String> initParameters) {
063                Assert.notNull(initParameters, "InitParameters must not be null");
064                boolean hasEmptyName = initParameters.keySet().stream()
065                                .anyMatch((name) -> !StringUtils.hasText(name));
066                Assert.isTrue(!hasEmptyName, "InitParameters must not contain empty names");
067                Map<String, String> mergedInitParameters = new LinkedHashMap<>(
068                                this.initParameters);
069                mergedInitParameters.putAll(initParameters);
070                return new EndpointServlet(this.servlet, mergedInitParameters);
071        }
072
073        Servlet getServlet() {
074                return this.servlet;
075        }
076
077        Map<String, String> getInitParameters() {
078                return this.initParameters;
079        }
080
081}