'ECGridOSMailboxIO by Loren Data Corp.
'This public domain code demonstrates how to use ECGridOS to upload and download
'files from ECGrid(r)
'See ECGridOS documentation at http://ecgridos.net
'Request ECGridOS credentials at http://ecgridos.com
'The following ECGridOS calls are used:
' Login()
' Logout()
' ParcelUpload()
' ParcelUploadGZip()
' ParcelDownload()
' ParcelDownloadGZip()
' ParcelDownloadConfirm()
' ParcelInBox()
'The syntax of this function is:
'ECGridOSMailboxIO <UserID> <Password> <directory> <-upload|-download> [-gzip]
' UserID - must be previously registered on ECGridOS
' Password - must be previously registered on ECGridOS
' directory -
the local directory - if upload, all files in the
directory
' will be sent
' -upload|-download - direction of file transfer
' -gzip - if present, then GZIP compression is used.
'ECGrid is a registered service mark of Loren Data Corp.
'ECGridOS is a service mark of Loren Data Corp.
Imports System.Web.Services.Protocols
Imports System.Xml
Imports System.IO
Module ECGridOSMailboxIO
'ECGRIDOS: Set up an Object for the Web Service;
' this requires a Web Reference
to ' https://ecgridos.net/v2.1/prod/ecgridosv0201.asmx
Public ecgridos As New net.ecgridos.ECGridOSAPIv2
'ECGRIDOS: Have a place to store the SessionID
Public SessionID As String
Sub Main()
If My.Application.CommandLineArgs.Count < 4 Then
Syntax()
Else
Dim UserID As String = My.Application.CommandLineArgs(0)
Dim Password As String = My.Application.CommandLineArgs(1)
Dim FileDir As String = My.Application.CommandLineArgs(2)
Dim Upload As Boolean = My.Application.CommandLineArgs(3).ToLower
= _ "-upload"
Dim GZip As Boolean = False
If My.Application.CommandLineArgs.Count = 5 Then
GZip = My.Application.CommandLineArgs(4).ToLower = "-gzip"
End If
Console.WriteLine(My.Application.Info.ProductName & ": "
& _ FormatDateTime(Now, DateFormat.ShortDate))
Try
'ECGRIDOS: Always Login first
SessionID = ecgridos.Login(UserID, Password)
If Upload Then
ECGridOSUpload(FileDir, GZip)
Else
ECGridOSDownload(FileDir, GZip)
End If
Catch ex As SoapException
'There is good data in the InnerXML
'which can be parsed and used to processes specific exceptions
Dim doc As New XmlDocument
Dim Node As XmlNode
doc.LoadXml(ex.Detail.OuterXml)
Node = doc.DocumentElement.SelectSingleNode("ErrorInfo")
If Not Node Is Nothing Then
Console.WriteLine([String].Format(
_ "SOAP Exception: ({0}) {1}", _ Node.SelectSingleNode("ErrorCode").InnerText,
_ Node.SelectSingleNode("ErrorString").InnerText))
Console.WriteLine([String].Format(" Error Item: {0}",
_
Node.SelectSingleNode("ErrorItem").InnerText))
Console.WriteLine([String].Format(" Msg: {0}",
_
Node.SelectSingleNode("ErrorMessage").InnerText))
Else
Console.WriteLine("ERROR: " & ex.Message.ToString)
End If
Catch ex As Exception
Console.WriteLine("ERROR: " & ex.ToString)
Finally
'ECGRIDOS: Make sure to Logout when done.
ecgridos.Logout(SessionID)
End Try
End If
ecgridos = Nothing
End Sub
Private Sub Syntax()
Console.WriteLine("ECGridOSMailboxIO <UserID> <Password>"
& _ " <upload|download> <localdirectory>")
End Sub
Private Sub ECGridOSUpload(ByVal FileDir As String, _
ByVal GZip As Boolean)
Dim fs As IO.FileStream
Dim files As System.Collections.ObjectModel.ReadOnlyCollection(Of String)
Dim buffer() As Byte
Dim bytes As Long
Dim ParcelID As Integer
'Let's send all files in the specified directory
files = My.Computer.FileSystem.GetFiles(FileDir, _
FileIO.SearchOption.SearchTopLevelOnly, _
"*.*")
For Each FileName As String In files
Console.Write("Uploading: " & FileName & "...")
'Load the entire file into a string buffer
bytes = FileLen(FileName)
fs = New IO.FileStream(FileName, IO.FileMode.Open, IO.FileAccess.Read)
ReDim buffer(bytes - 1)
fs.Read(buffer, 0, bytes)
fs.Close()
If GZip Then
Dim ms As New MemoryStream
'Make sure that LeaveOpen:=true so that the underlying stream (ms)
'maintains control.
Dim gz As New GZipStream(ms, CompressionMode.Compress, True)
gz.Write(buffer, 0, buffer.Length)
'Be sure to close the GZipStream here or the buffer will not
'flush properly.
gz.Close()
ReDim buffer(ms.Length - 1)
ms.Position = 0
ms.Read(buffer, 0, ms.Length)
ms.Close()
'ECGRIDOS: ParcelUpload() posts a file to ECGrid
' The ParcelID can be used as a handle later to get more information
' about the specific files as it transits the system.
ParcelID = ecgridos.ParcelUploadGZip(SessionID,
_ Path.GetFileName(FileName), bytes, buffer)
Else
'ECGRIDOS: ParcelUpload() posts a file to ECGrid
' The ParcelID can be used as a handle later to get more information
' about the specific files as it transits the system.
ParcelID = ecgridos.ParcelUpload(SessionID,
_ Path.GetFileName(FileName), bytes, buffer)
End If
Console.WriteLine(ParcelID.ToString)
'Delete the file after a successful upload.
'Note that the Try/Catch from the calling routine will handle any errors
' and not delete the file if it doesn't upload
My.Computer.FileSystem.DeleteFile(FileName)
Next
End Sub
Private Sub ECGridOSDownload(ByVal FileDir As String, _
ByVal GZip As Boolean)
Dim parcels() As net.ecgridos.ParcelIDInfo
Dim ParcelID As Integer
'The FileInfo object holds all the info and payload of the downloaded file
Dim FileInfo As net.ecgridos.FileInfo
Dim fs As IO.FileStream
Dim buffer() As Byte
If Left(FileDir, 1) <> "\" Then FileDir = FileDir & "\"
'ECGRIDOS: ParcelInBox() is used to lists all pending files in the current
' Mailbox("In Box"). A collection/array of ParcelIDInfo objects is returned.
parcels = ecgridos.ParcelInBox(SessionID)
For Each parcel In parcels
Console.Write("Downloading: " & parcel.FileName & "...")
ParcelID = parcel.ParcelID
If GZip Then
'ECGRIDOS:
ParcelDownloadGZip() is used to download
the
' File info & content;
it returns the FileInfo object
with ' the Content GZip compressed.
FileInfo = ecgridos.ParcelDownloadGZip(SessionID, ParcelID)
Dim ms As New MemoryStream()
Dim os As New MemoryStream()
Dim buf() As Byte
Dim b As Integer
ms.Write(FileInfo.Content, 0, FileInfo.Content.Length)
ms.Position = 0
Dim gz As New GZipStream(ms, CompressionMode.Decompress, True)
'One would think you could do this in one shot, but GZipStream
'has it's own ideas. You won't even get a consistent 4096 bytes
'ever time you loop through.
buf = New Byte(4095) {}
Do
b = gz.Read(buf, 0, buf.Length)
os.Write(buf, 0, b)
If os.Length = FileInfo.Bytes Then Exit Do
If b < 1 Then Exit Do
Loop
ReDim FileInfo.Content(FileInfo.Bytes - 1)
os.Position = 0
os.Read(FileInfo.Content, 0, os.Length)
gz.Close()
ms.Close()
os.Close()
Else
'ECGRIDOS:
ParcelDownload() is used to download the
File
' info & content;
it returns the FileInfo object
FileInfo = ecgridos.ParcelDownload(SessionID, ParcelID)
End If
'Save the payload to a file as the same name as in the FileInfo object
' in the commandline specified directory.
buffer = FileInfo.Content fs =
New IO.FileStream(FileDir & FileInfo.FileName, _
IO.FileMode.Create, _
IO.FileAccess.Write)
fs.Write(buffer, 0, FileInfo.Bytes)
fs.Close()
'ECGRIDOS: ParcelDownloadConfirm is used to tell ECGrid to mark the file
' as downloaded and remove it from the InBox. A copy of the file remains
' in the Archive.
ecgridos.ParcelDownloadConfirm(SessionID, ParcelID)
Console.WriteLine("complete.")
Next
End Sub
End Module
|