Hello,

Thanks Marc Mundt for your help,

1 We have tried with the following code which reads a String which represents an image in pRequest.imagen and we send it into a MIME:

Class Operaciones.REST.miSCS.miSCSSubirImagenJPGlocal Extends EnsLib.REST.Operation { Parameter INVOCATION = "Queue";

/// 📤 Subir la imagenSanitaria
Method SubirImagen(pRequest As Mensajes.Request.miSCS.SubirImagen, pResponse As Mensajes.Response.miSCS.SubirImagen) As %Library.Status
{
    
    Set httpRequest=##class(%Net.HttpRequest).%New()
    set tResponse = ##class(%Net.HttpResponse).%New()
    
    Set msg=##class(%Net.MIMEPart).%New()
     Set msg.Body=##class(%GlobalCharacterStream).%New()
    Do msg.Body.Write(pRequest.imagen)
    Set msg.ContentType="multipart/form-data"
    
    // create MIME writer; write root MIME message
    Set writer=##class(%Net.MIMEWriter).%New()

    // Prepare outputting to the HttpRequestStream
    Set status=writer.OutputToStream(httpRequest.EntityBody)
    if $$$ISERR(status) {do $SYSTEM.Status.DisplayError(status) Quit}

    // Now write down the content
    Set status=writer.WriteMIMEBody(msg)
    if $$$ISERR(status) {do $SYSTEM.Status.DisplayError(status) Quit}
    
    set url      = ##class(Util.TablasMaestras).getValorMaestra("MISCS.PARAMETRIZACIONES","url")
    set path     = ##class(Util.TablasMaestras).getValorMaestra("MISCS.PARAMETRIZACIONES","path")
    set servicio = ##class(Util.TablasMaestras).getValorMaestra("MISCS.PARAMETRIZACIONES","servicio")
    set recurso  = ##class(Util.TablasMaestras).getValorMaestra("MISCS.PARAMETRIZACIONES","subirImagen")
            
    set URL = "http://"_url_path_servicio_recurso
    $$$LOGINFO("URL: "_URL)

    set tFormVarNames = "expediente,imagen"
    set tData("expediente") = pRequest.expediente
    set tData("imagen") = pRequest.imagen

    set tSC = ..Adapter.SendFormDataArray(.tResponse,"POST",httpRequest,.tFormVarNames,.tData,URL)
    $$$LOGINFO("tSC: "_tSC)
    
    if $$$ISERR(tSC){
            $$$ThrowOnError(tSC)
    }
    if (tResponse.Data.AtEnd = 0) {
        $$$LOGINFO("En SubirImagen, tamaño de tResponse: "_tResponse.Data.Size)
        set linea = tResponse.Data.Read()
        $$$LOGINFO("Linea: "_linea)
    }
    
    set pResponse = ##class(Mensajes.Response.miSCS.SubirImagen).%New()
    do pResponse.return.Write(linea)

    Quit pResponse
}

However when we see it using Whireshark we observe that it is being sent as text/html

2 We have also used the following approach, to read a JPG directly and send it into the MIME:

Class Operaciones.REST.miSCS.miSCSSubirImagenJPGlocal Extends EnsLib.REST.Operation
{

Parameter INVOCATION = "Queue";

/// 📤 Subir la imagenSanitaria
Method SubirImagen(pRequest As Mensajes.Request.miSCS.SubirImagen, pResponse As Mensajes.Response.miSCS.SubirImagen) As %Library.Status
{
    
    //Crear Request y Response HTTP
    Set httpRequest=##class(%Net.HttpRequest).%New()
    set tResponse = ##class(%Net.HttpResponse).%New()
    
    //Crear MIME
    Set msg=##class(%Net.MIMEPart).%New()
     Set msg.Body=##class(%GlobalCharacterStream).%New()
     
    //Probamos a leer una imagen local y a enviarla
    Set stream=##class(%Stream.FileCharacter).%New()
      Set sc=stream.LinkToFile("C:\Users\ext-ymorjim\Pictures\miSCS.jpg")
      While 'stream.AtEnd {
          Set linea=stream.Read()
      }
      $$$LOGINFO("linea: "_linea)
      
      //Escribir la imagen en el mensaje MIME
     Do msg.Body.Write(linea)
    //Do msg.Body.Write(pRequest.imagen)
    Set msg.ContentType="multipart/form-data"
    
    // create MIME writer; write root MIME message
    Set writer=##class(%Net.MIMEWriter).%New()

    // Prepare outputting to the HttpRequestStream
    Set status=writer.OutputToStream(httpRequest.EntityBody)
    if $$$ISERR(status) {do $SYSTEM.Status.DisplayError(status) Quit}

    // Now write down the content
    Set status=writer.WriteMIMEBody(msg)
    if $$$ISERR(status) {do $SYSTEM.Status.DisplayError(status) Quit}
    

    set url      = ##class(Util.TablasMaestras).getValorMaestra("MISCS.PARAMETRIZACIONES","url")
    set path     = ##class(Util.TablasMaestras).getValorMaestra("MISCS.PARAMETRIZACIONES","path")
    set servicio = ##class(Util.TablasMaestras).getValorMaestra("MISCS.PARAMETRIZACIONES","servicio")
    set recurso  = ##class(Util.TablasMaestras).getValorMaestra("MISCS.PARAMETRIZACIONES","subirImagen")
            
    set URL = "http://"_url_path_servicio_recurso
    $$$LOGINFO("URL: "_URL)

    set tFormVarNames = "expediente,imagen"
    set tData("expediente") = pRequest.expediente
    set tData("imagen") = pRequest.imagen
    //set tData("imagen") = linea

    set tSC = ..Adapter.SendFormDataArray(.tResponse,"POST",httpRequest,.tFormVarNames,.tData,URL)
    $$$LOGINFO("tSC: "_tSC)
    
    if $$$ISERR(tSC){
            $$$ThrowOnError(tSC)
    }
    if (tResponse.Data.AtEnd = 0) {
        $$$LOGINFO("En SubirImagen, tamaño de tResponse: "_tResponse.Data.Size)
        set linea = tResponse.Data.Read()
        $$$LOGINFO("Linea: "_linea)
    }
    
    set pResponse = ##class(Mensajes.Response.miSCS.SubirImagen).%New()
    do pResponse.return.Write(linea)

    Quit pResponse
}

With the previous code we see that a text/html is being sent too:

In both cases, the external system gives us a HTTP code 400:

     ERROR <Ens>ErrException: <THROW>zSubirImagen+40^Operaciones.REST.miSCS.miSCS.1 *%Exception.StatusException ERROR <Ens>ErrHTTPStatus: Received non-OK status 400 from remote HTTP server: 'HTTP/1.1 400 ' -- logged as '-' number - @' Set sc=tSC Throw:('sc) ##class(%Exception.StatusException).ThrowIfInterrupt(sc)'

How could we achieve to send it in the correct way? as it is being sent when we use POSTMAN:

How could we send the image as multipart/form-data?

> We have read:

https://docs.intersystems.com/irisforhealthlatest/csp/docbook/DocBook.UI...

https://docs.intersystems.com/irisforhealthlatest/csp/docbook/Doc.View.c...

How could we send the image as multipart/form-data as it is being sent by Postman?

➡️ Could you point us to same code examples, please?

Hello Kevin,

Yes you were right we did not include "/opt/contenedor/Java/AppSeguimientoAnadidaReferenciaJARSexternos.jar"

We have just added it and the exception continues.

Hello Kevin,

We are using the following Java version:

java version "1.8.0_65"
Java(TM) SE Runtime Environment (build 1.8.0_65-b17)
Java HotSpot(TM) 64-Bit Server VM (build 25.65-b01, mixed mode)

If we execute the command with our jar (which includes the Java program and its dependencies):

jar -tf "AppSeguimientoAnadidaReferenciaJARSexternos.jar"

We see:

META-INF/MANIFEST.MF
org/
org/eclipse/
org/eclipse/jdt/
org/eclipse/jdt/internal/
org/eclipse/jdt/internal/jarinjarloader/
org/eclipse/jdt/internal/jarinjarloader/JIJConstants.class
org/eclipse/jdt/internal/jarinjarloader/JarRsrcLoader$ManifestInfo.class
org/eclipse/jdt/internal/jarinjarloader/JarRsrcLoader.class
org/eclipse/jdt/internal/jarinjarloader/RsrcURLConnection.class
org/eclipse/jdt/internal/jarinjarloader/RsrcURLStreamHandler.class
org/eclipse/jdt/internal/jarinjarloader/RsrcURLStreamHandlerFactory.class
radarCovidcrearToken/
radarCovidcrearToken/CCAATokenGeneratorTest.class
generated_private_base64.pem
aether-api-1.7.jar
aether-impl-1.7.jar
aether-spi-1.7.jar
aether-util-1.7.jar
asm-6.2.jar
bcpkix-jdk15on-1.58.jar
bcprov-jdk15on-1.58.jar
classworlds-1.1.jar
commons-cli-1.0.jar
commons-codec-1.14.jar
commons-io-2.5.jar
jackson-annotations-2.10.3.jar
jackson-core-2.10.3.jar
jackson-databind-2.10.3.jar
java-jwt-3.10.3.jar
junit-3.8.1.jar
plexus-build-api-0.0.4.jar
plexus-cipher-1.4.jar
plexus-classworlds-2.2.3.jar
plexus-compiler-api-2.8.4.jar
plexus-compiler-javac-2.8.4.jar
plexus-compiler-manager-2.8.4.jar
plexus-component-annotations-1.7.1.jar
plexus-container-default-1.0-alpha-9-stable-1.jar
plexus-interactivity-api-1.0-alpha-4.jar
plexus-interpolation-1.13.jar
plexus-interpolation-1.14.jar
plexus-java-0.9.10.jar
plexus-sec-dispatcher-1.3.jar
plexus-utils-2.0.4.jar
plexus-utils-2.0.5.jar
qdox-2.0-M9.jar
sisu-guice-2.1.7-noaop.jar
sisu-inject-bean-1.4.2.jar
sisu-inject-plexus-1.4.2.jar
java-jwt-3.10.3_2.jar
jackson-databind-2.10.3_2.jar
jackson-annotations-2.10.3_2.jar
jackson-core-2.10.3_2.jar
commons-codec-1.14_2.jar
bcpkix-jdk15on-1.58_2.jar
bcprov-jdk15on-1.58_2.jar

If we execute the command to locate the jar where is the class which generates the NoClassDefFoundError :

jar -tf "bcprov-jdk15on-1.58.jar"

We get:

...

org/bouncycastle/util/io/pem/
org/bouncycastle/util/io/pem/PemGenerationException.class
org/bouncycastle/util/io/pem/PemHeader.class
org/bouncycastle/util/io/pem/PemObject.class
org/bouncycastle/util/io/pem/PemObjectGenerator.class
org/bouncycastle/util/io/pem/PemObjectParser.class
org/bouncycastle/util/io/pem/PemReader.class
org/bouncycastle/util/io/pem/PemWriter.class
org/bouncycastle/util/test/

...

As you would notice it outputs the name of the missing class:

org/bouncycastle/util/io/pem/PemReader.class

What could we do?

Hello Eduard

We would need to be able to send a REST POST meesage with the following body:

{
"provider":"provider",
"group""group",
"location""location",
"data""data",
"checkin""checkin",
"client""client",
"admin""admin",
"apiUrl""apiUrl"
}
 
 

And get the previous body inside our Service.

We have written as a Service:

Class Servicios.REST.HistoriaClinica.Videoconsulta Extends Ens.BusinessService
{ Parameter ADAPTER = "EnsLib.HTTP.InboundAdapter"; Method OnProcessInput(pInput As %RegisteredObject, Output pOutput As %RegisteredObject) As %Status
{
do ..enviarCitaCrearSala(pInput,.pOutput)
quit $$$OK
} Method enviarCitaCrearSala(pInput As %Stream.Object, Output pOutput As %Stream.Object) As %Status
{
//TODO: Get body


set tSC = ..SendRequestSync("Videoconsulta",body,.objetoSalida)
set claseAux = ##class(%ZEN.Auxiliary.jsonProvider).%New()
set tSC = claseAux.%WriteJSONStreamFromObject(.pOutput,.objetoSalida,,,,"aeloqtuw")

Quit tSC
}

Inspired by Cristiano Silva's answer, we have found the following way to get the desired property:

..%Process.%CurrentResponseHeader.SourceConfigName

Helo Alexey, Eduard:

There will be dozens of files every day. We also think that we should do it through a linux shell script, which:

1º Detects the files modified (or created) on the last day, in a series of folders (which will be in the same directory)

2º Copy the files to the folder "internal to the server" (we need to copy them and not just move them, because the original file must be recorded because they are owned by a different system)

How would you approach the shell script? We appreciate your time

Hello Eduard Lebedyuk, thanks for your answer.

We have tried to send two async calls, and then use a <sync> to wait for both responses. We thought the BPL execution flow would stop at <sync> until it gets all responses from the async calls. However we observe that both sync calls are sent and then the execution flow continues beyond <sync>.

To give more context, we are calling for specialists appointments, then for primary appointments, after that we have the sync, and finally some transformation and code as follows:

Being the trace, the following, where we see the two asyn call being sent:

However because of the execution flow continues after <sync> without having both responses, we have in our context empty responses, so then the transformation fails:

Besides we do get the responses, however they arrive after they are needed, because of primary appointments return after the Transform attempt was made, and the specialists appointments are retrieved after BPL sends a response to the service (which means we could not use the specialists appointments anymore).

The question is: how could we achieve to wait and stop BPL execution flow, until we do get both async calls' responses?

Hello Tomáš Vaverka,

We have tried the following code:

set httpRequest.ContentType = "application/json"
  set httpRequest.ContentCharset = "UTF-8"
set httpRequest.ContentEncoding = "UTF-8"
set httpRequest.NoDefaultContentCharset = 0

And it does store the notification with the accented letters.

We appreciate that you have spent time and effort helping us in this task, Tomáš Vaverka.

 

Hello,  Tomáš Vaverka,

We have tried to rewind httpRequest.EntityBody

And we see it is being stored in the API with the strange character.

The code is:

while(httpRequest.EntityBody.AtEnd=0){
 set linea = httpRequest.EntityBody.Read()
}

Do httpRequest.EntityBody.Rewind()

Being the complete code:

Method CrearNotificacion(pRequest As Mensajes.Request.NotificacionesPUSH.CrearNotificacion, pResponse As Mensajes.Response.NotificacionesPUSH.CrearNotificacion) As %Library.Status
{
$$$LOGINFO("^data: "_^data) $$$LOGINFO("^idsDispositivos: "_^idsDispositivos) Set body = {
     "app_id"(pRequest.idApp),
     "headings"{"en":(pRequest.notificacion.titulo),"es":(pRequest.notificacion.titulo)},
     "subtitle"{"en":(pRequest.notificacion.subtitulo),"es":(pRequest.notificacion.subtitulo)},
     "contents"{"en":(pRequest.notificacion.mensaje),"es":(pRequest.notificacion.mensaje)},
     "data":(^data),
     "include_player_ids"(^idsDispositivos)
} //"headings": {"en":($ZCONVERT(pRequest.notificacion.titulo,"O","UTF8")),"es":(pRequest.notificacion.titulo)}, set httpRequest = ##class(%Net.HttpRequest).%New()
set tResponse = ##class(%Net.HttpResponse).%New()
set httpRequest.Server = "onesignal.com" set httpRequest.ContentCharset = "UTF-8"
set httpRequest.ContentType = "application/json"
set httpRequest.ContentEncoding = "UTF-8"
set httpRequest.NoDefaultContentCharset = 0
//set httpRequest.ContentType = "application/json;charset=utf-8"
//do httpRequest.SetHeader("Content-Type","application/json; charset=utf-8")
//set httpRequest.ContentCharset = "utf-8"
//do httpRequest.SetHeader("Content-Encoding","gzip") //set httpRequest.Authorization = "Basic "_##class(Util.TablasMaestras).getValorMaestra("NOTIFICACIONESPUSH.PARAMETRIZACIONES","onesignal_apikey")
set httpRequest.Authorization = "Basic "_##class(Util.TablasMaestras).getValorMaestra("NOTIFICACIONESPUSH.PARAMETRIZACIONES","ONESIGNAL_APIKEY_SCS")
$$$LOGINFO("httpRequest.Authorization: "_httpRequest.Authorization)
set httpRequest.Https = 1 set httpRequest.SSLConfiguration = "Certificado_SCS"
$$$LOGINFO("body.data: "_body.data)
set dataObject = ##class(%DynamicObject).%FromJSON(body.data)
$$$LOGINFO("dataObject: "_dataObject)
set body.data = dataObject $$$LOGINFO("body.'include_player_ids': "_body."include_player_ids")
set idsDispositivosObject = ##class(%DynamicObject).%FromJSON(body."include_player_ids")
$$$LOGINFO("idsDispositivosObject "_idsDispositivosObject)
set body."include_player_ids" = idsDispositivosObject //set status = ##class(%ZEN.Auxiliary.jsonProvider).%WriteJSONStreamFromObject(httpRequest.EntityBody, body,,,,"aeloqtuw")
Do httpRequest.EntityBody.Write(body.%ToJSON())
while(httpRequest.EntityBody.AtEnd=0){
set linea = httpRequest.EntityBody.Read()
} Do httpRequest.EntityBody.Rewind() $$$LOGINFO("linea: "_linea) set tSC = httpRequest.Post("https://onesignal.com/api/v1/notifications") set tResponse = httpRequest.HttpResponse
if $$$ISERR(tSC){
$$$ThrowOnError(tSC)
}
if (tResponse.Data.AtEnd = 0) {
//$$$LOGINFO("En AñadirDispositivo, tamaño de tResponse: "_tResponse.Data.Size)
set linea = tResponse.Data.Read()
//$$$LOGINFO("Linea: "_linea)
} set pResponse = ##class(Mensajes.Response.NotificacionesPUSH.CrearNotificacion).%New()
do pResponse.informacion.Write(linea)
while(pResponse.informacion.AtEnd=0){
set informacion = pResponse.informacion.Read()
}
$$$LOGINFO("informacion: "_informacion)
Do pResponse.informacion.Rewind() Quit pResponse
}
 

In addition, we observe that in our Operation the notification is:


{"app_id":"5cf57b56-c3b4-4a0d-8938-4ac4466f93af","headings":{"en":"Cita Atención Primaria","es":"Cita Atención Primaria"},"subtitle":{"en":"C.P. ISORA","es":"C.P. ISORA"},"contents":{"en":"Aqui el contenido del mensaje si aplicase","es":"Aqui el contenido del mensaje si aplicase"},"data":{"centro":"C.P. ISORA","fecha":"yyy/mm/dd","hora":"hh:mm","profesional":"nombre del profesional","nomUsuario":"nombre de usuario","codcita":"idCita","sepuedeborrar":"1"},"include_player_ids":["114a63e3-1da9-40fa-a3ec-e6677466e11a"]}  

And it is being stored with rare characters:


How could we debug this behaviour?
 

We observe:

 
linea: {"app_id":"5cf57b56-c3b4-4a0d-8938-4ac4466f93af","headings":{"en":"Cita Atención Primaria","es":"Cita Atención Primaria"},"subtitle":{"en":"C.P. ISORA","es":"C.P. ISORA"},"contents":{"en":"Aqui el contenido del mensaje si aplicase","es":"Aqui el contenido del mensaje si aplicase"},"data":{"centro":"C.P. ISORA","fecha":"yyy/mm/dd","hora":"hh:mm","profesional":"nombre del profesional","nomUsuario":"nombre de usuario","codcita":"idCita","sepuedeborrar":"1"},"include_player_ids":["114a63e3-1da9-40fa-a3ec-e6677466e11a"]}

 

Being the code:

Method CrearNotificacion(pRequest As Mensajes.Request.NotificacionesPUSH.CrearNotificacion, pResponse As Mensajes.Response.NotificacionesPUSH.CrearNotificacion) As %Library.Status
{
$$$LOGINFO("^data: "_^data) /*
Set body = {
     "app_id": (pRequest.idApp),
     "headings": {"en":(pRequest.notificacion.titulo),"es":(pRequest.notificacion.titulo)},
     "subtitle": {"en":(pRequest.notificacion.subtitulo),"es":(pRequest.notificacion.subtitulo)},
     "contents": {"en":(pRequest.notificacion.mensaje),"es":(pRequest.notificacion.mensaje)},
     "data":(^data),
     "include_player_ids": ["c2917a6f-6ecf-4f45-8b31-9b72538580fd"]
}
*/ $$$LOGINFO("^idsDispositivos: "_^idsDispositivos) Set body = {
     "app_id"(pRequest.idApp),
     "headings"{"en":(pRequest.notificacion.titulo),"es":(pRequest.notificacion.titulo)},
     "subtitle"{"en":(pRequest.notificacion.subtitulo),"es":(pRequest.notificacion.subtitulo)},
     "contents"{"en":(pRequest.notificacion.mensaje),"es":(pRequest.notificacion.mensaje)},
     "data":(^data),
     "include_player_ids"(^idsDispositivos)
} //"headings": {"en":($ZCONVERT(pRequest.notificacion.titulo,"O","UTF8")),"es":(pRequest.notificacion.titulo)}, set httpRequest = ##class(%Net.HttpRequest).%New()
set tResponse = ##class(%Net.HttpResponse).%New()
set httpRequest.Server = "onesignal.com" set httpRequest.ContentCharset = "UTF-8"
set httpRequest.ContentType = "application/json"
set httpRequest.ContentEncoding = "UTF-8"
set httpRequest.NoDefaultContentCharset = 0
//set httpRequest.ContentType = "application/json;charset=utf-8"
//do httpRequest.SetHeader("Content-Type","application/json; charset=utf-8")
//set httpRequest.ContentCharset = "utf-8"
//do httpRequest.SetHeader("Content-Encoding","gzip") //set httpRequest.Authorization = "Basic "_##class(Util.TablasMaestras).getValorMaestra("NOTIFICACIONESPUSH.PARAMETRIZACIONES","onesignal_apikey")
set httpRequest.Authorization = "Basic "_##class(Util.TablasMaestras).getValorMaestra("NOTIFICACIONESPUSH.PARAMETRIZACIONES","ONESIGNAL_APIKEY_SCS")
$$$LOGINFO("httpRequest.Authorization: "_httpRequest.Authorization)
set httpRequest.Https = 1 set httpRequest.SSLConfiguration = "Certificado_SCS"
$$$LOGINFO("body.data: "_body.data)
set dataObject = ##class(%DynamicObject).%FromJSON(body.data)
$$$LOGINFO("dataObject: "_dataObject)
set body.data = dataObject $$$LOGINFO("body.'include_player_ids': "_body."include_player_ids")
set idsDispositivosObject = ##class(%DynamicObject).%FromJSON(body."include_player_ids")
$$$LOGINFO("idsDispositivosObject "_idsDispositivosObject)
set body."include_player_ids" = idsDispositivosObject //set status = ##class(%ZEN.Auxiliary.jsonProvider).%WriteJSONStreamFromObject(httpRequest.EntityBody, body,,,,"aeloqtuw")
Do httpRequest.EntityBody.Write(body.%ToJSON())
while(httpRequest.EntityBody.AtEnd=0){
set linea = httpRequest.EntityBody.Read()
}
$$$LOGINFO("linea: "_linea) set tSC = httpRequest.Post("https://onesignal.com/api/v1/notifications") set tResponse = httpRequest.HttpResponse
if $$$ISERR(tSC){
$$$ThrowOnError(tSC)
}
if (tResponse.Data.AtEnd = 0) {
//$$$LOGINFO("En AñadirDispositivo, tamaño de tResponse: "_tResponse.Data.Size)
set linea = tResponse.Data.Read()
//$$$LOGINFO("Linea: "_linea)
} set pResponse = ##class(Mensajes.Response.NotificacionesPUSH.CrearNotificacion).%New()
do pResponse.informacion.Write(linea) Quit pResponse
}