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.util.ArrayList;
  23. import java.util.HashMap;
  24. import java.util.List;
  25. import java.util.Map;

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

  27. /**
  28.  * AbstractHttp
  29.  *
  30.  * @author Poli Andrea (apoli@link.it)
  31.  * @author $Author$
  32.  * @version $Rev$, $Date$
  33.  */
  34. public abstract class AbstractHttp {

  35.     private String contentType;
  36.     private byte[] content;
  37.     private Map<String, List<String>> headers = new HashMap<>();
  38.    
  39.     public byte[] getContent() {
  40.         return this.content;
  41.     }
  42.     public void setContent(byte[] content) {
  43.         this.content = content;
  44.     }
  45.    
  46.     private InputStream contentStream;
  47.     public InputStream getContentStream() {
  48.         return this.contentStream;
  49.     }
  50.     public void setContentStream(InputStream contentStream) {
  51.         this.contentStream = contentStream;
  52.     }
  53.    
  54.     public void addHeader(String key,String value){
  55.         List<String> l = this.headers.remove(key);
  56.         if(l==null) {
  57.             l = new ArrayList<>();
  58.         }
  59.         l.add(value);
  60.         this.headers.put(key, l);
  61.     }
  62.     public void addHeader(String key,List<String> values){
  63.         List<String> l = this.headers.remove(key);
  64.         if(l==null) {
  65.             l = new ArrayList<>();
  66.         }
  67.         l.addAll(values);
  68.         this.headers.put(key, l);
  69.     }
  70.    
  71.     @Deprecated
  72.     public Map<String, String> getHeaders() {
  73.         return TransportUtils.convertToMapSingleValue(this.headers);
  74.     }
  75.     public Map<String, List<String>> getHeadersValues() {
  76.         return this.headers;
  77.     }
  78.    
  79.     @Deprecated
  80.     public String getHeader(String header) {
  81.         return TransportUtils.getObjectAsString(this.headers, header);
  82.     }
  83.     public String getHeader_compactMultipleValues(String name){
  84.         return TransportUtils.getObjectAsString(this.headers, name);
  85.     }
  86.     public String getHeaderFirstValue(String name){
  87.         List<String> l = getHeaderValues(name);
  88.         if(l!=null && !l.isEmpty()) {
  89.             return l.get(0);
  90.         }
  91.         return null;
  92.     }
  93.     public List<String> getHeaderValues(String header) {
  94.         return TransportUtils.getRawObject(this.headers, header);
  95.     }
  96.    
  97.     public String getContentType() {
  98.         return this.contentType;
  99.     }
  100.     public void setContentType(String contentType) {
  101.         this.contentType = contentType;
  102.     }
  103.    
  104. }