HeaderMap.java

  1. /*
  2.  * GovWay - A customizable API Gateway
  3.  * https://govway.org
  4.  *
  5.  * Copyright (c) 2005-2025 Link.it srl (https://link.it).
  6.  *
  7.  * This program is free software: you can redistribute it and/or modify
  8.  * it under the terms of the GNU General Public License version 3, as published by
  9.  * the Free Software Foundation.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  18.  *
  19.  */
  20. package org.openspcoop2.pdd.core.integrazione.peer;

  21. import java.util.Collection;
  22. import java.util.HashMap;
  23. import java.util.List;
  24. import java.util.Map;
  25. import java.util.Objects;

  26. /**
  27. * Classe di utilita, utilizzata per salvare header e
  28. * ottenere gli headers peer da una lista di header della richiesta http
  29. *
  30. * @author Tommaso Burlon (tommaso.burlon@link.it)
  31. * @author $Author$
  32. * @version $Rev$, $Date$
  33. */
  34. public class HeaderMap extends HashMap<String, String> {
  35.     private static final long serialVersionUID = 5811004934063754049L;
  36.    
  37.     public static HeaderMap computeFromHeaders(Collection<PeerHeaderDescriptor> descs, Map<String, List<String>> headers) {
  38.         HeaderMap rv = new HeaderMap();    
  39.         descs.forEach(desc -> {
  40.             if (headers == null) {
  41.                 return;
  42.             }
  43.             Map<String, String> entry = desc.computeHeaders(headers.keySet());
  44.             if (entry == null)
  45.                 return;
  46.            
  47.             entry.forEach((newHdr, oldHdr) -> {
  48.                 if (oldHdr != null) {
  49.                     String value = String.join(",", Objects.requireNonNullElse(headers.get(oldHdr), List.of()));
  50.                     rv.put(newHdr, value);
  51.                 }
  52.             });
  53.         });
  54.        
  55.         return rv;
  56.     }

  57. }