CORSWrappedHttpServletResponse.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;

  21. import java.util.HashMap;
  22. import java.util.List;
  23. import java.util.Map;

  24. import org.openspcoop2.message.ForcedResponseMessage;
  25. import org.openspcoop2.message.OpenSPCoop2Message;
  26. import org.openspcoop2.message.OpenSPCoop2MessageFactory;
  27. import org.openspcoop2.message.constants.MessageRole;
  28. import org.openspcoop2.message.constants.MessageType;
  29. import org.openspcoop2.pdd.config.OpenSPCoop2Properties;
  30. import org.openspcoop2.utils.transport.TransportUtils;
  31. import org.openspcoop2.utils.transport.http.WrappedHttpServletResponse;

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

  40.     private Map<String, List<String>> properties = new HashMap<>();
  41.     private boolean portaApplicativa;
  42.    
  43.     public CORSWrappedHttpServletResponse(boolean portaApplicativa) {
  44.         super(null);
  45.         this.portaApplicativa = portaApplicativa;
  46.     }

  47.     @Override
  48.     public void setHeader(String key, String value) {
  49.         TransportUtils.setHeader(this.properties,key, value);
  50.     }
  51.     @Override
  52.     public void addHeader(String key, String value) {
  53.         TransportUtils.addHeader(this.properties,key, value);
  54.     }
  55.    
  56.    
  57.     private OpenSPCoop2Message message;
  58.     private int status;
  59.    
  60.     public OpenSPCoop2Message buildMessage() {
  61.        
  62.         this.message = OpenSPCoop2MessageFactory.getDefaultMessageFactory().createEmptyMessage(MessageType.BINARY, MessageRole.RESPONSE);
  63.         ForcedResponseMessage forcedResponseMessage = new ForcedResponseMessage();
  64.         forcedResponseMessage.setContent(null); // vuoto
  65.         forcedResponseMessage.setContentType(null); // vuoto
  66.         if(this.portaApplicativa) {
  67.             this.status = OpenSPCoop2Properties.getInstance().getGestioneCORS_returnCode_ricezioneBuste();
  68.         }
  69.         else {
  70.             this.status = OpenSPCoop2Properties.getInstance().getGestioneCORS_returnCode_ricezioneContenutiApplicativi();
  71.         }
  72.         forcedResponseMessage.setResponseCode(this.status+"");
  73.         forcedResponseMessage.setHeadersValues(this.properties);
  74.         this.message.forceResponse(forcedResponseMessage);
  75.        
  76.         return this.message;
  77.     }

  78.     @Override
  79.     public int getStatus() {
  80.         return this.status;
  81.     }
  82.     public Map<String, List<String>> getHeadersValues() {
  83.         return this.properties;
  84.     }
  85. }