Compose XML for HTTP POST using file in hot folder

jan_suhr
Advanced member
Posts: 608
Joined: Fri Nov 04, 2011 1:12 pm
Location: Nyköping, Sweden

Re: Compose XML for HTTP POST using file in hot folder

Post by jan_suhr »

The idea we try to get you to understand is that this folder should not have an outgoing connection. Then the file stored in that folder will be without the prefix. For your script to be able to pickup the file you will set the folder to a path where your script can get the file.

In the property of the folder it is by default set as Automanaged but you can change it to a specific location that your Switch can reach, either local or on the network.

This folder should branch of in your flow right before your script. This will save you a copy on disc and the other copy will trigger your script to pickup the other copy from the local folder.
Jan Suhr
Color Consult AB
Sweden
=============
Check out my apps
jan_suhr
Advanced member
Posts: 608
Joined: Fri Nov 04, 2011 1:12 pm
Location: Nyköping, Sweden

Re: Compose XML for HTTP POST using file in hot folder

Post by jan_suhr »

Snag_965ea3e.png
Snag_965ea3e.png (6.5 KiB) Viewed 201 times
Snag_96731e1.png
Snag_96731e1.png (9.32 KiB) Viewed 200 times
Something like this
Jan Suhr
Color Consult AB
Sweden
=============
Check out my apps
teetomterrific
Member
Posts: 25
Joined: Mon Mar 09, 2020 10:22 pm

Re: Compose XML for HTTP POST using file in hot folder

Post by teetomterrific »

First of all your example shows using HTTP Request which I'm not using because it doesn't work for me. The POST is in my script for various reasons not the least of which is the decision making process for the URL and the calculations that needs to be made in code using regular expressions to pull data out of the file name. The script does everything I need except for stripping off the pesky unnecessary unique ID.

Secondly you told me there could be no outgoing connections and yet your example has outgoing connections. That is not confusing and contradictory at all.

Third, the script isn't picked up from a folder. It is a SwitchScript object which you have not shown in your example. The HTTP POST must be made at the end of the Script not in some other portion of the flow that is unrelated as you have illustrated.

I keep telling you that I have two flow elements: 1. a hot folder/input folder element and 2. the Switch script flow element, and the flow ends with the obligatory empty folder. The example you are showing bears no relation to my existing flow. Perhaps you should start with what I've got to create an example because I can't figure out how to take the contradictory directions and strip off the unique ID.

https://www.dropbox.com/scl/fi/jrnd525d ... 47757&dl=0
jan_suhr
Advanced member
Posts: 608
Joined: Fri Nov 04, 2011 1:12 pm
Location: Nyköping, Sweden

Re: Compose XML for HTTP POST using file in hot folder

Post by jan_suhr »

Well this was an example on how to get you a file to use in what ever thing you want where you need to pick up a file from disc to use.

You can replace HTTP request with your script and write it to pick up the file from the folder I specified with a path location.
Jan Suhr
Color Consult AB
Sweden
=============
Check out my apps
teetomterrific
Member
Posts: 25
Joined: Mon Mar 09, 2020 10:22 pm

Re: Compose XML for HTTP POST using file in hot folder

Post by teetomterrific »

I put a folder between the input folder and the script and used it to move the file to another location in a different file share as you suggest:

1. The file moved and the prefix was stripped off of the file name in the new location

2. job.getPath returns the correct share path for the move in the script with the exception that the the path still shows the file name with the prefix even though it has been stripped off in the new location.

3. Moving the file was a failure since Switch does not respect the name change from the move and return it correctly.

That's a pretty bad bug that Switch did not correct the file name after stripping the prefix in the move.

The stupid prefix has become the Bain of my existence. It is a crippling limitation to not have an effective way to disable or eliminate it. Hard coding a path change in the script is a bad idea. It would make more sense to remove the prefix at the destination with RBA and not move the file in the flow since the move is subject to the path bug I have identified.

I appreciate all the help you have provided but unfortunately Switch has a big problem I am unable to work around within the Switch environment. There needs to be a prefix off switch
jan_suhr
Advanced member
Posts: 608
Joined: Fri Nov 04, 2011 1:12 pm
Location: Nyköping, Sweden

Re: Compose XML for HTTP POST using file in hot folder

Post by jan_suhr »

1. That is what we want.

2. You are not going to use job.getPath since it will give you the path to the file that enters your script. The file we put in the folder, point 1 above, is now forgotten by Switch. Since this location is static you have to write that path in the script and then use the variable job.Name.
Like this C:\\Mydisc\myFolderpath\[job.Name]
This will give you the proper file in to your script without any prefix.

3. Do as above, in point 2, and it will work.

This is how you will use Switch the proper way for this kind of case.
Jan Suhr
Color Consult AB
Sweden
=============
Check out my apps
User avatar
magnussandstrom
Advanced member
Posts: 377
Joined: Thu Jul 30, 2020 6:34 pm
Location: Sweden
Contact:

Re: Compose XML for HTTP POST using file in hot folder

Post by magnussandstrom »

teetomterrific wrote: Tue May 07, 2024 3:39 pm The script does more than send the Post. It also determines which Server out of 5 the POST needs to be sent to and formats the server URL by using Regular Expressions to pull the last digit of the Job name to a select case for the server.
I understand your frustration, but we are just trying to help you to find a solution to your problem.

I'm pretty sure that it's possible to determine the URL without a script, using PrivateData and PrivateData Magician instead. Could you please give an example how you determine the URL?
teetomterrific
Member
Posts: 25
Joined: Mon Mar 09, 2020 10:22 pm

Re: Compose XML for HTTP POST using file in hot folder

Post by teetomterrific »

magnussandstrom wrote: Tue May 07, 2024 7:17 pm Could you please give an example how you determine the URL?
Here you go:

Code: Select all

Option Explicit

' --------------------------------------------------------------------------------------------------
' Sendremotetrigger
'
' Sends a remote trigger to Prinergy with file paths
' --------------------------------------------------------------------------------------------------
'
' Is invoked each time a new job arrives in one of the input folders for the flow element.
' The newly arrived job is passed as the second parameter.
'
Function jobArrived(s, job)

    Dim theFilePath
    theFilePath = Replace(job.getPath(), "/", "\")

    Dim theFileName
    theFileName = job.getNameProper()

    Dim inputVariable, regexPattern, regex, match, singleDigit, thePrinergyServer, jobName

    ' Input variable
    inputVariable = theFileName

    ' Create regular expression objects
    Set regex = New RegExp
    regex.Global = False
    regex.MultiLine = True
    regex.IgnoreCase = True

    ' Match input variable with the first regex pattern
    regex.Pattern = "^(\d{6}-\d{2}).*$"
    Set match = regex.Execute(inputVariable)

    If match.Count > 0 Then
        ' Get the matched capture group
        = match(0).SubMatches(0)
       
        ' Get the single digit from jobName
        regex.Pattern = "^\d{5}(\d{1})-.*"
        Set match = regex.Execute(jobName)

        If match.Count > 0 Then
            singleDigit = match(0).SubMatches(0)
            
            ' Select the Prinergy server based on the single digit
            Select Case singleDigit
                Case "0", "1"
                    thePrinergyServer = "http://clkswjmpg101:61235/ExtAutomation/RaiseExternalEventImp.aspx?XmlVersion=1"
                Case "2", "3"
                    thePrinergyServer = "http://clkswjmpg201:61235/ExtAutomation/RaiseExternalEventImp.aspx?XmlVersion=1"
                Case "4", "5"
                    thePrinergyServer = "http://clkswjmpg301:61235/ExtAutomation/RaiseExternalEventImp.aspx?XmlVersion=1"
                Case "6", "7"
                    thePrinergyServer = "http://clkswjmpg401:61235/ExtAutomation/RaiseExternalEventImp.aspx?XmlVersion=1"
                Case "8", "9"
                    thePrinergyServer = "http://clkswjmpg501:61235/ExtAutomation/RaiseExternalEventImp.aspx?XmlVersion=1"
            End Select
        End If
    End If

    Dim objHTTP, xmlData

    ' XML body
    xmlData = "<?xml version=""1.0""?>" & vbCrLf &_
              "<RaiseExternalEvent xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns=""http://creo.com/pws/automation/"">" & _
              "<RuleSetPath>!Development/Prinergy/AddInputFile</RuleSetPath>" & vbCrLf &_
              "<Source>Switch Test01</Source>" & vbCrLf &_
              "<SourceMachine>clkswjtenf01</SourceMachine>" & vbCrLf &_
              "<SourceMachineIp>10.41.145.24</SourceMachineIp>" & vbCrLf &_
              "<Args>" & vbCrLf &_
              "  <string>" & jobName & "</string>" & vbCrLf &_
              "  <string>" & theFilePath & "</string>" & vbCrLf &_
              "</Args>" & vbCrLf &_
              "<FileUNCPaths />" & vbCrLf &_
              "</RaiseExternalEvent>"

    ' Create an HTTP object
    Set objHTTP = CreateObject("MSXML2.ServerXMLHTTP")

    ' Open an asynchronous POST request to the specified URL
    objHTTP.Open "POST", thePrinergyServer", False

    ' Set request headers
    objHTTP.setRequestHeader "Content-Type", "text/xml"

    ' Send the request with XML body
    objHTTP.send xmlData

    ' Check if request was successful
    Dim objFSO, outFile, logFilePath

    ' Specify the log file path
    logFilePath = "C:\VBscrptLogs\" & theFileName & ".output.txt"

    ' Create a FileSystemObject
    Set objFSO = CreateObject("Scripting.FileSystemObject")

    ' Create or open the text file for writing
    Set outFile = objFSO.CreateTextFile(logFilePath, True)

    ' Check the status and write to the log file
    If objHTTP.Status = 200 Then
        outFile.WriteLine "HTTP POST request successful." & vbCrLf & xmlData
    Else
        outFile.WriteLine "HTTP POST request failed with status: " & objHTTP.Status & " " & objHTTP.statusText & vbCrLf & xmlData
    End If

    ' Close the text file and release resources
    outFile.Close
    Set outFile = Nothing
    Set objFSO = Nothing

    ' Clean up
    Set objHTTP = Nothing

End Function
teetomterrific
Member
Posts: 25
Joined: Mon Mar 09, 2020 10:22 pm

Re: Compose XML for HTTP POST using file in hot folder

Post by teetomterrific »

jan_suhr wrote: Tue May 07, 2024 7:02 pm 2. You are not going to use job.getPath since it will give you the path to the file that enters your script. The file we put in the folder, point 1 above, is now forgotten by Switch. Since this location is static you have to write that path in the script and then use the variable job.Name.
Like this C:\\Mydisc\myFolderpath\[job.Name]
This will give you the proper file in to your script without any prefix.

3. Do as above, in point 2, and it will work.

This is how you will use Switch the proper way for this kind of case.
It did not give the path to the file that enters the flow. It gave the correct path for where the file was moved, which indicates it is a bug that it didn't also correct the file name too at the same time.

I disagree that your method of building a path in the script is "proper". Hard coding part of the full path instead of retrieving programmatically is the surest way to break your code in the future. Unfortunately Switch's ability to correctly retrieve the path is broken. I could just as easily remove the the prefix from the filename in the script rather than hard coding the path, but both options are not a best practice when writing code. The method you advocate as proper is nothing more than a work around due to the inflexibility of the Unique ID prefix that cannot be turned off when not needed.

Again, thank you for your assistance. I really appreciate it. I mostly write and use custom applications for workflows. Switch has some benefits for integrating certain things like PitStop and PDFtoolbox, but it's limitations are sometime truly mind blowing that other users just seem to put up with. I'm just not that forgiving.
User avatar
magnussandstrom
Advanced member
Posts: 377
Joined: Thu Jul 30, 2020 6:34 pm
Location: Sweden
Contact:

Re: Compose XML for HTTP POST using file in hot folder

Post by magnussandstrom »

teetomterrific wrote: Tue May 07, 2024 8:12 pm Here you go
Thanks, could you also provide a couple of example jobnames?
freddyp
Advanced member
Posts: 1031
Joined: Thu Feb 09, 2012 3:53 pm

Re: Compose XML for HTTP POST using file in hot folder

Post by freddyp »

I must say, teetomterrific, that the tone of some of your posts is not the style of this forum. This is a place where we meet to help each other to the best of our abilities, not to sling mud.

I do not quite understand your rant against the unique id. It is something Switch does when creating a job and that will not change. With a correct implementation it does not bother, believe me, I have written one or two scripts.

I also do not understand the problem you have with hard coding something. I fully agree that hard coding is not good practice but in your script you hard code the path to which you write the log file, you hard code the values for SourceMachine and SourceMachineIp, and you hard code the URLs to which the XML has to be posted. What is the problem with one more hard-coded value?

All this being said, there is an easy solution for not being bothered by the unique id. Instead of using a jobArrived entry point you use a timerFired entry point. Add a property to the script that allows you to choose the folder that is being watched in timerFired so it is not hard coded. The script reads the file list of the folder directly without Switch doing this and adding a stupid unique id. To avoid processing the same files multiple times you have to store the list of files that were already processed. Switch offers global data to do that.
teetomterrific
Member
Posts: 25
Joined: Mon Mar 09, 2020 10:22 pm

Re: Compose XML for HTTP POST using file in hot folder

Post by teetomterrific »

magnussandstrom wrote: Tue May 07, 2024 9:48 pm Thanks, could you also provide a couple of example jobnames?
This is an example of a file name that would be dropped into the hot folder that contains a job name:

037984-24_001_8-C-Y.pdf

The Job Name is 037984-24
The Job number is 037984 and the production year is 24 for 2024
the last digit of the Job number determines which server it will be homed to as the POST destination.
0,1 = server 1, 2, 3 = server 2, 4,5 = server 3, 6,7 = server 4 and 8,9 = server 5
User avatar
magnussandstrom
Advanced member
Posts: 377
Joined: Thu Jul 30, 2020 6:34 pm
Location: Sweden
Contact:

Re: Compose XML for HTTP POST using file in hot folder

Post by magnussandstrom »

By looking at the script I guess you are doing the following:

1. Fetch a job number containing 6 digits followed by a dash and two more digits from filename
2. Fetch the 6th digit in that job number as case
3. Use that case digit to set the URL

If that's correct - here is a flow that does that using private data:

URL_test.zip
(13.44 KiB) Downloaded 3 times
url.png
url.png (26.88 KiB) Viewed 144 times
teetomterrific
Member
Posts: 25
Joined: Mon Mar 09, 2020 10:22 pm

Re: Compose XML for HTTP POST using file in hot folder

Post by teetomterrific »

freddyp wrote: Tue May 07, 2024 9:57 pm I must say, teetomterrific, that the tone of some of your posts is not the style of this forum. This is a place where we meet to help each other to the best of our abilities, not to sling mud.
Mud? So you would rather hear a dishonest assessment of Switch's inadequacies? Clearly my frustration with it isn't rosy prose, but I would not characterize it as slinging mud.
I do not quite understand your rant against the unique id. It is something Switch does when creating a job and that will not change. With a correct implementation it does not bother, believe me, I have written one or two scripts.
The unique ID is not always necessary so I consider it a design flaw to not allow disabling it. My 2 cents.
I also do not understand the problem you have with hard coding something. I fully agree that hard coding is not good practice but in your script you hard code the path to which you write the log file, you hard code the values for SourceMachine and SourceMachineIp, and you hard code the URLs to which the XML has to be posted. What is the problem with one more hard-coded value?
I have an aversion to hard coding paths because Switch messes with them. The other items you mentioned are more immutable in the current context, but I would prefer a method for setting them as preferences variables than hard coding in the script. I never bothered with attempting that because I can't seem to get past the unique ID conundrum.

I also have an issue with a lack of a debugger in Switch for the scripting languages and a viable way to step through a flow and debug the actions. It is very cumbersome to debug without a proper tool available to do so.
jan_suhr
Advanced member
Posts: 608
Joined: Fri Nov 04, 2011 1:12 pm
Location: Nyköping, Sweden

Re: Compose XML for HTTP POST using file in hot folder

Post by jan_suhr »

The unique id prefix is necessary for Switch to know where all copies of the same file are in the flow.

It's how it works!
Jan Suhr
Color Consult AB
Sweden
=============
Check out my apps
Post Reply