/*
 * JBoss, Home of Professional Open Source.
 * Copyright 2012 Red Hat, Inc., and individual contributors
 * as indicated by the @author tags.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.undertow.servlet.spec;

import java.util.Collection;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import javax.servlet.MultipartConfigElement;
import javax.servlet.ServletRegistration;
import javax.servlet.ServletSecurityElement;

import io.undertow.servlet.api.DeploymentInfo;
import io.undertow.servlet.api.ServletInfo;

/**
 * @author Stuart Douglas
 */
public class ServletRegistrationImpl implements ServletRegistration, ServletRegistration.Dynamic {

    private final ServletInfo servletInfo;
    private final DeploymentInfo deploymentInfo;

    public ServletRegistrationImpl(final ServletInfo servletInfo, final DeploymentInfo deploymentInfo) {
        this.servletInfo = servletInfo;
        this.deploymentInfo = deploymentInfo;
    }

    @Override
    public void setLoadOnStartup(final int loadOnStartup) {
        servletInfo.setLoadOnStartup(loadOnStartup);
    }

    @Override
    public Set<String> setServletSecurity(final ServletSecurityElement constraint) {
        return null;
    }

    @Override
    public void setMultipartConfig(final MultipartConfigElement multipartConfig) {
        servletInfo.setMultipartConfig(multipartConfig);
    }

    @Override
    public void setRunAsRole(final String roleName) {
        servletInfo.setRunAs(roleName);
    }

    @Override
    public void setAsyncSupported(final boolean isAsyncSupported) {
        servletInfo.setAsyncSupported(isAsyncSupported);
    }

    @Override
    public Set<String> addMapping(final String... urlPatterns) {
        final Set<String> ret = new HashSet<String>();
        final Set<String> existing = new HashSet<String>();
        for(ServletInfo s : deploymentInfo.getServlets().values()){
            existing.addAll(s.getMappings());
        }
        for(String pattern : urlPatterns) {
            if(!existing.contains(pattern)) {
                ret.add(pattern);
                servletInfo.addMapping(pattern);
            }
        }
        return ret;
    }

    @Override
    public Collection<String> getMappings() {
        return servletInfo.getMappings();
    }

    @Override
    public String getRunAsRole() {
        return servletInfo.getRunAs();
    }

    @Override
    public String getName() {
        return servletInfo.getName();
    }

    @Override
    public String getClassName() {
        return servletInfo.getServletClass().getName();
    }


    @Override
    public boolean setInitParameter(final String name, final String value) {
        if (servletInfo.getInitParams().containsKey(name)) {
            return false;
        }
        servletInfo.addInitParam(name, value);
        return true;
    }

    @Override
    public String getInitParameter(final String name) {
        return servletInfo.getInitParams().get(name);
    }

    @Override
    public Set<String> setInitParameters(final Map<String, String> initParameters) {
        final Set<String> ret = new HashSet<String>();
        for (Map.Entry<String, String> entry : initParameters.entrySet()) {
            if (!setInitParameter(entry.getKey(), entry.getValue())) {
                ret.add(entry.getKey());
            }
        }
        return ret;
    }

    @Override
    public Map<String, String> getInitParameters() {
        return servletInfo.getInitParams();
    }
}

@@ -2374,16 +2374,17 @@
      for
+ 
 (Servlet
@@ -2430,18 +2430,84 @@
 alues())
+ 
 %7B%0A
+            if(!s.getName().equals(servletInfo.getName())) %7B%0A    
         
@@ -2540,24 +2540,38 @@
 appings());%0A
+            %7D%0A
         %7D%0A  
@@ -2579,16 +2579,17 @@
      for
+ 
 (String 
@@ -2627,18 +2627,18 @@
       if
+ 
 (
-!
 existing
@@ -2685,32 +2685,256 @@
 t.add(pattern);%0A
+            %7D%0A        %7D%0A        //only update if no changes have been made%0A        if (ret.isEmpty()) %7B%0A            for (String pattern : urlPatterns) %7B%0A                if(!servletInfo.getMappings().contains(pattern)) %7B%0A    
                 
@@ -2958,32 +2958,50 @@
 pping(pattern);%0A
+                %7D%0A
             %7D%0A  

Fix servlet registration bug