All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jboss.ws.undertow_httpspi.UndertowHeaderMap Maven / Gradle / Ivy

/*
 * JBoss, Home of Professional Open Source.
 * Copyright 2014, Red Hat Middleware LLC, and individual contributors
 * as indicated by the @author tags. See the copyright.txt file in the
 * distribution for a full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.jboss.ws.undertow_httpspi;

import io.undertow.util.HeaderMap;
import io.undertow.util.HeaderValues;
import io.undertow.util.HttpString;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;



public class UndertowHeaderMap implements Map>
{
   private final HeaderMap headerMap;

   public UndertowHeaderMap(HeaderMap headerMap) {
      this.headerMap = headerMap;
   }

   @Override
   public int size()
   {
      return headerMap.size();
   }

   @Override
   public boolean isEmpty()
   {
      return headerMap.size() > 0;
   }

   @Override
   public boolean containsKey(Object key)
   {
      return headerMap.contains(key.toString());
   }

   @Override
   public boolean containsValue(Object value)
   {
      Iterator ite = headerMap.iterator();
      while (ite.hasNext())
      {
         HeaderValues values = ite.next();
         if (values.contains(value))
         {
            return true;
         }
      }
      return false;
   }

   @Override
   public List get(Object key)
   {
      HeaderValues values = headerMap.get(key.toString());
      List result = new ArrayList();
      if (values != null)
      {
         for (String value : values.toArray())
         {
            result.add(value);
         }
      }
      return result;
   }

   @Override
   public List put(String key, List value)
   {
      List previous = get(key);
      if (previous.isEmpty())
      {
         previous = null;
      }
      headerMap.addAll(new HttpString(key), value);
      return previous;

   }

   @Override
   public List remove(Object key)
   {
      List previous = get(key);
      if (previous.isEmpty())
      {
         previous = null;
      }
      headerMap.remove(key.toString());
      return previous;
   }

   @Override
   public void putAll(Map> m)
   {
      for (String key : m.keySet())
      {
         headerMap.putAll(new HttpString(key), m.get(key));
      }

   }

   @Override
   public void clear()
   {
      headerMap.clear();

   }

   @Override
   public Set keySet()
   {
      Set result = new HashSet();
      for (HeaderValues value : headerMap)
      {
         result.add(value.getHeaderName().toString());
      }
      return result;
   }

   @Override
   public Collection> values()
   {
      List> collections = new ArrayList>();
      for (HeaderValues value : headerMap)
      {
         List values = new ArrayList();
         for (String headerValue : value)
         {
            values.add(headerValue);
         }
         collections.add(values);
      }
      return collections;
   }

   @Override
   public Set>> entrySet()
   {
      Set>> result = new HashSet>>();
      for (HeaderValues headerValues : headerMap)
      {
         final String key = headerValues.getHeaderName().toString();
         final List headerValueList = new ArrayList();
         for (String value : headerValues)
         {
            headerValueList.add(value);
         }
         result.add(new Entry>() {

            @Override
            public String getKey()
            {
               return key;
            }

            @Override
            public List getValue()
            {
               return headerValueList;
            }

            @Override
            public List setValue(List value)
            {
               List previous = headerMap.get(key);
               if (previous.isEmpty())
               {
                  previous = null;
               }
               headerMap.addAll(new HttpString(key), value);

               return previous;

            }
         });
      }
      return result;
   }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy