StringPeerHeaderDescriptor.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.Arrays;
  22. import java.util.Collection;
  23. import java.util.HashMap;
  24. import java.util.List;
  25. import java.util.Map;
  26. import java.util.stream.Collectors;
  27. import java.util.stream.IntStream;

  28. /**
  29.  * Classe che serve a descrivere un headers peer di tipo string
  30.  *
  31.  *
  32.  * @author Tommaso Burlon (tommaso.burlon@link.it)
  33.  * @author $Author$
  34.  * @version $Rev$, $Date$
  35.  */
  36. public class StringPeerHeaderDescriptor implements PeerHeaderDescriptor {
  37.     private final Map<String, Integer> headerMap;
  38.     private final String headerName;
  39.    
  40.     public StringPeerHeaderDescriptor(String key, String value) {
  41.         this.headerName = key;
  42.        
  43.         List<String> headerList = Arrays.stream(value.split(","))
  44.                 .map(String::trim)
  45.                 .collect(Collectors.toList());
  46.         this.headerMap = IntStream.range(0, headerList.size())
  47.                 .boxed()
  48.                 .collect(Collectors.toMap(headerList::get, i -> i, Math::min, HashMap::new));
  49.     }
  50.    
  51.     @Override
  52.     public Map<String, String> computeHeaders(Collection<String> headers) {
  53.         Integer bestMatch = this.headerMap.size();
  54.         String name = this.headerName;
  55.         String match = null;
  56.         for (String header : headers) {
  57.             Integer priority = this.headerMap.get(header);
  58.            
  59.             if (priority != null && priority < bestMatch) {
  60.                 bestMatch = priority;
  61.                 match = header;
  62.             }
  63.         }
  64.        
  65.         if (match == null)
  66.             return Map.of();
  67.         return Map.of(name, match);
  68.     }

  69. }