MessageUtilities.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.message.utils;

  21. import org.openspcoop2.message.OpenSPCoop2Message;
  22. import org.openspcoop2.message.OpenSPCoop2MessageFactory;
  23. import org.openspcoop2.message.constants.MessageRole;
  24. import org.openspcoop2.message.constants.MessageType;
  25. import org.openspcoop2.message.exception.MessageException;
  26. import org.openspcoop2.utils.transport.http.HttpConstants;


  27. /**
  28.  * ContentTypeUtilities
  29.  *
  30.  * @author Andrea Poli (apoli@link.it)
  31.  * @author $Author$
  32.  * @version $Rev$, $Date$
  33.  */
  34. public class MessageUtilities {
  35.    
  36.     public static String getDefaultContentType(MessageType messageType){
  37.         String contentType = null;
  38.         switch (messageType) {
  39.         case SOAP_11:
  40.             contentType = HttpConstants.CONTENT_TYPE_SOAP_1_1;
  41.             break;
  42.         case SOAP_12:      
  43.             contentType = HttpConstants.CONTENT_TYPE_SOAP_1_2;
  44.             break;
  45.         case XML:
  46.             contentType = HttpConstants.CONTENT_TYPE_XML;
  47.             break;
  48.         case JSON:      
  49.             contentType = HttpConstants.CONTENT_TYPE_JSON;
  50.             break;
  51.         case BINARY:        
  52.             contentType = HttpConstants.CONTENT_TYPE_APPLICATION_OCTET_STREAM;
  53.             break;
  54.         case MIME_MULTIPART:    // e' sicuro REST  
  55.             contentType = HttpConstants.CONTENT_TYPE_MULTIPART_FORM_DATA;
  56.             break;
  57.         }
  58.         return contentType;
  59.     }
  60.    
  61.     public static void checkType(MessageType messageType,OpenSPCoop2Message msg) throws MessageException{
  62.         switch (messageType) {
  63.             case SOAP_11:
  64.                 msg.castAsSoap(); // check
  65.                 break;
  66.             case SOAP_12:
  67.                 msg.castAsSoap(); // check
  68.                 break;
  69.             case XML:
  70.                 msg.castAsRestXml(); // check
  71.                 break;
  72.             case JSON:
  73.                 msg.castAsRestJson(); // check
  74.                 break;
  75.             case BINARY:
  76.                 msg.castAsRestBinary(); // check
  77.                 break;
  78.             case MIME_MULTIPART:
  79.                 msg.castAsRestMimeMultipart(); // check
  80.                 break;
  81.         }
  82.     }
  83.    
  84.    
  85.     public static OpenSPCoop2Message buildEmptyMessage(OpenSPCoop2MessageFactory messageFactory, MessageType messageType,MessageRole messageRole) {

  86.         try{
  87.             OpenSPCoop2Message msg = messageFactory.createEmptyMessage(messageType,messageRole);
  88.             return msg;
  89.         } catch(Exception e) {
  90.             return null;
  91.         }
  92.     }
  93. }