AbstractHttp.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.utils.transport.http;

  21. import java.io.InputStream;
  22. import java.net.URLEncoder;
  23. import java.nio.charset.StandardCharsets;
  24. import java.util.ArrayList;
  25. import java.util.Collections;
  26. import java.util.Enumeration;
  27. import java.util.HashMap;
  28. import java.util.List;
  29. import java.util.Map;
  30. import java.util.stream.Collectors;

  31. import org.openspcoop2.utils.transport.TransportUtils;

  32. /**
  33.  * AbstractHttp
  34.  *
  35.  * @author Poli Andrea (apoli@link.it)
  36.  * @author $Author$
  37.  * @version $Rev$, $Date$
  38.  */
  39. public abstract class AbstractHttp {

  40.     private String contentType;
  41.     private byte[] content;
  42.     private Map<String, List<String>> headers = new HashMap<>();
  43.     private Map<String, String> params = new HashMap<>();
  44.    
  45.     public byte[] getContent() {
  46.         return this.content;
  47.     }
  48.     public void setContent(byte[] content) {
  49.         this.content = content;
  50.     }
  51.    
  52.     private InputStream contentStream;
  53.     public InputStream getContentStream() {
  54.         return this.contentStream;
  55.     }
  56.     public void setContentStream(InputStream contentStream) {
  57.         this.contentStream = contentStream;
  58.     }
  59.    
  60.     public void addHeader(String key,String value){
  61.         List<String> l = this.headers.remove(key);
  62.         if(l==null) {
  63.             l = new ArrayList<>();
  64.         }
  65.         l.add(value);
  66.         this.headers.put(key, l);
  67.     }
  68.     public void addHeader(String key,List<String> values){
  69.         List<String> l = this.headers.remove(key);
  70.         if(l==null) {
  71.             l = new ArrayList<>();
  72.         }
  73.         l.addAll(values);
  74.         this.headers.put(key, l);
  75.     }
  76.    
  77.     @Deprecated
  78.     public Map<String, String> getHeaders() {
  79.         return TransportUtils.convertToMapSingleValue(this.headers);
  80.     }
  81.     public Map<String, List<String>> getHeadersValues() {
  82.         return this.headers;
  83.     }
  84.    
  85.     @Deprecated
  86.     public String getHeader(String header) {
  87.         return TransportUtils.getObjectAsString(this.headers, header);
  88.     }
  89.     public String getHeader_compactMultipleValues(String name){
  90.         return TransportUtils.getObjectAsString(this.headers, name);
  91.     }
  92.     public String getHeaderFirstValue(String name){
  93.         List<String> l = getHeaderValues(name);
  94.         if(l!=null && !l.isEmpty()) {
  95.             return l.get(0);
  96.         }
  97.         return null;
  98.     }
  99.     public List<String> getHeaderValues(String header) {
  100.         return TransportUtils.getRawObject(this.headers, header);
  101.     }
  102.    
  103.     public String getContentType() {
  104.         return this.contentType;
  105.     }
  106.     public void setContentType(String contentType) {
  107.         this.contentType = contentType;
  108.     }
  109.    
  110.    
  111.     public Enumeration<String> getParamsName() {
  112.         return Collections.enumeration(this.params.keySet());
  113.     }
  114.    
  115.     public void removeParam(String key) {
  116.         this.params.remove(key);
  117.     }
  118.    
  119.     public String getParam(String key) {
  120.         return this.params.get(key);
  121.     }
  122.    
  123.     public void addParam(String key, String value) {
  124.         this.params.put(key, value);
  125.     }
  126.    
  127.     protected String getQueryString() {
  128.         return this.params.entrySet()
  129.                 .stream()
  130.                 .map(e -> URLEncoder.encode(e.getKey(), StandardCharsets.UTF_8) + "=" + URLEncoder.encode(e.getValue(), StandardCharsets.UTF_8))
  131.                 .collect(Collectors.joining("&"));
  132.     }
  133. }