예제 #1
0
        public void InvokeUploadService(string authtoken, bool isServerMig, string filePath, string mimebuffer,
                                        string theDisposition, string theType, int mode, out string rsp)
        {
            using (LogBlock logblock = Log.NotTracing() ? null : new LogBlock(GetType() + "." + System.Reflection.MethodBase.GetCurrentMethod().Name))
            {
                // --------------------------------------------------
                // Is the data in file, or in string mimebuffer?
                // --------------------------------------------------
                bool bIsBuffer = false;
                if (mimebuffer.Length > 0)
                {
                    bIsBuffer = true;
                }

                // --------------------------------------------------
                // Cert callback
                // --------------------------------------------------
                ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(delegate(object sender2, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return(true); });

                // --------------------------------------------------
                // Create the request
                // --------------------------------------------------
                HttpWebRequest webReq = this.CreateWebRequestRaw(authtoken, isServerMig);

                // -------------------------------
                // Get preamble for the request
                // -------------------------------
                string ct;
                if (theType.Length == 0)
                {
                    ct = "Content-Type: " + "application/octet-stream";
                }
                else
                {
                    ct = "Content-Type: " + theType;
                }

                string boundary            = "--B-00=_" + DateTime.Now.Ticks.ToString("x");
                string endBoundary         = Environment.NewLine + "--" + boundary + "--" + Environment.NewLine; // FBS bug 73727 -- 5/29/12 -- removed extra "Environment.NewLine +"
                string contentDisposition1 = "--" + boundary + Environment.NewLine + "Content-Disposition: form-data; name=\"requestId\"" + Environment.NewLine + Environment.NewLine + "lsrpc32-client-id" + Environment.NewLine;
                string cd2 = (theDisposition.Length > 0) ? theDisposition : "Content-Disposition : form-data; name=\"lslib32\"; filename=\"lslib32.bin\"";
                string contentDisposition2 = "--" + boundary + Environment.NewLine + cd2;
                string contentType         = Environment.NewLine + ct + Environment.NewLine;
                string contentTransfer     = "Content-Transfer-Encoding: binary" + Environment.NewLine + Environment.NewLine;

                // -------------------------------
                // Write data into webReq's stream
                // -------------------------------
                webReq.ContentType = "multipart/form-data; boundary=" + boundary;

                if (mode == ZimbraAPI.STRING_MODE)  // easier -- all text in the request
                {
                    try
                    {
                        if (bIsBuffer)
                        {
                            // =============================================================================================
                            // STREAM FROM BUFFER
                            // =============================================================================================
                            string sDataToUpload = mimebuffer;
                            int    nDataLen      = sDataToUpload.Length; // for view in debugger

                            // --------------------------------------------------
                            // Build the request stream
                            // --------------------------------------------------
                            using (Stream stm = webReq.GetRequestStream())
                            {
                                using (StreamWriter stmw = new StreamWriter(stm, System.Text.Encoding.Default))
                                {
                                    stmw.Write(contentDisposition1);
                                    stmw.Write(contentDisposition2);
                                    stmw.Write(contentType);
                                    stmw.Write(contentTransfer);
                                    stmw.Write(sDataToUpload);
                                    stmw.Write(endBoundary);
                                    stmw.Close();
                                }
                            }
                        }
                        else
                        {
                            // =============================================================================================
                            // STREAM FROM FILE
                            // =============================================================================================


                            // -------------------------------
                            // Build the request stream
                            // -------------------------------
                            long lFileSize = new System.IO.FileInfo(filePath).Length;
                            using (FileStream fileStream = File.OpenRead(filePath))
                            {
                                // Send it off in chunks of 5MB
                                int    bufferSize = 5 * 1024 * 1024;
                                byte[] buffer     = new byte[bufferSize];


                                UTF8Encoding encoding         = new UTF8Encoding();
                                byte[]       bytecd1          = encoding.GetBytes(contentDisposition1);
                                byte[]       bytecd2          = encoding.GetBytes(contentDisposition2);
                                byte[]       byteContType     = encoding.GetBytes(contentType);
                                byte[]       byteContTransfer = encoding.GetBytes(contentTransfer);
                                byte[]       byteEndBoundary  = encoding.GetBytes(endBoundary);

                                long lContentLength = bytecd1.Length
                                                      + bytecd2.Length
                                                      + byteContType.Length
                                                      + byteContTransfer.Length
                                                      + lFileSize
                                                      + byteEndBoundary.Length;
                                Log.trace("Bytes to upload:" + lContentLength);

                                webReq.AllowWriteStreamBuffering = false; // Without this, the call to GetRequestStream will allocate ContentLength bytes   "Setting AllowWriteStreamBuffering to true might cause performance problems when uploading large datasets because the data buffer could use all available memory."    YES!

                                // If the AllowWriteStreamBuffering property of HttpWebRequest is set to false,the contentlength has to be set to length of data to be posted else Exception(411) is raised.
                                webReq.ContentLength = lContentLength;

                                using (Stream RqstStrm = webReq.GetRequestStream())
                                {
                                    // Write preamble
                                    RqstStrm.Write(bytecd1, 0, bytecd1.Length);
                                    RqstStrm.Write(bytecd2, 0, bytecd2.Length);
                                    RqstStrm.Write(byteContType, 0, byteContType.Length);
                                    RqstStrm.Write(byteContTransfer, 0, byteContTransfer.Length);


                                    // Write file contents
                                    long nDone  = 0;
                                    int  nBytes = 1;
                                    while (nBytes != 0)
                                    {
                                        nBytes = fileStream.Read(buffer, 0, bufferSize);
                                        if (nBytes > 0)
                                        {
                                            RqstStrm.Write(buffer, 0, nBytes);
                                            nDone += nBytes;
                                            Log.trace((100 * nDone / lContentLength) + "% uploaded " + nDone + " bytes");
                                        }
                                    }

                                    // Write end boundary
                                    RqstStrm.Write(byteEndBoundary, 0, byteEndBoundary.Length);

                                    // All done
                                    RqstStrm.Close();
                                }
                            }
                        }
                    }
                    catch (System.Net.WebException wex)
                    {
                        // catch (Exception ex)
                        Log.err(wex);
                        setErrors(wex);
                        rsp = "";
                        return;
                    }
                }
                else // CONTACT, APPT_VALUE, or APPT_EMB.  Not distinguishing yet, but we might later.
                {
                    try
                    {
                        // ----------------------------------------------------------------
                        // first get the bytes from the file -- this is the attachment data
                        // ----------------------------------------------------------------
                        byte[] buf     = null;
                        long   datalen = 0;
                        if (bIsBuffer)
                        {
                            datalen = mimebuffer.Length;
                            buf     = Encoding.ASCII.GetBytes(mimebuffer);
                        }
                        else
                        {
                            System.IO.FileStream   fileStream   = new System.IO.FileStream(filePath, System.IO.FileMode.Open, System.IO.FileAccess.Read);
                            System.IO.BinaryReader binaryReader = new System.IO.BinaryReader(fileStream);

                            datalen = new System.IO.FileInfo(filePath).Length;

                            buf = binaryReader.ReadBytes((Int32)datalen);
                            fileStream.Close();
                            fileStream.Dispose();
                            binaryReader.Close();
                        }

                        // ----------------------------------------------------------------
                        // now use a memory stream since we have mixed data
                        // ----------------------------------------------------------------
                        using (Stream memStream = new System.IO.MemoryStream())
                        {
                            // write the request data
                            byte[] cd1Bytes = System.Text.Encoding.UTF8.GetBytes(contentDisposition1);
                            memStream.Write(cd1Bytes, 0, cd1Bytes.Length);

                            byte[] cd2Bytes = System.Text.Encoding.UTF8.GetBytes(contentDisposition2);
                            memStream.Write(cd2Bytes, 0, cd2Bytes.Length);

                            byte[] cTypeBytes = System.Text.Encoding.UTF8.GetBytes(contentType);
                            memStream.Write(cTypeBytes, 0, cTypeBytes.Length);

                            byte[] cTransferBytes = System.Text.Encoding.UTF8.GetBytes(contentTransfer);
                            memStream.Write(cTransferBytes, 0, cTransferBytes.Length);
                            memStream.Write(buf, 0, (int)datalen);

                            byte[] cEndBoundaryBytes = System.Text.Encoding.UTF8.GetBytes(endBoundary);
                            memStream.Write(cEndBoundaryBytes, 0, cEndBoundaryBytes.Length);

                            // set up the web request to use our memory stream
                            webReq.ContentLength = memStream.Length;

                            memStream.Position = 0;
                            byte[] tempBuffer = new byte[memStream.Length];
                            memStream.Read(tempBuffer, 0, tempBuffer.Length);
                            memStream.Close();


                            // ----------------------------------------------------------------
                            // Send it to server
                            // ----------------------------------------------------------------
                            Stream requestStream = webReq.GetRequestStream();
                            requestStream.Write(tempBuffer, 0, tempBuffer.Length);
                            requestStream.Close();
                        }
                    }
                    catch (System.Net.WebException wex)
                    {
                        // catch (Exception ex)
                        setErrors(wex);
                        rsp = "";
                        return;
                    }
                }

                // =======================================================================
                // Get the response from the web service
                // =======================================================================
                WebResponse response = null;
                try
                {
                    Log.verbose(">GetResponse");
                    response = webReq.GetResponse();
                    Log.verbose("<GetResponse");
                }
                catch (System.Net.WebException wex)
                {
                    // catch (Exception ex)
                    setErrors(wex);
                    rsp = "";
                    return;
                }

                Stream       str = response.GetResponseStream();
                StreamReader sr  = new StreamReader(str);
                rsp    = sr.ReadToEnd();
                status = 0;
            }
        }
예제 #2
0
        static public string  checkPrereqs()
        {
            using (LogBlock logblock = Log.NotTracing()?null: new LogBlock(/*GetType() + "." + */ System.Reflection.MethodBase.GetCurrentMethod().Name))
            {
                string str          = "";
                string path         = Directory.GetDirectoryRoot(Directory.GetCurrentDirectory());
                string absolutepath = Path.GetFullPath("Exchange.dll");

                bool bitness = CompatibilityChk.UnmanagedDllIs64Bit(absolutepath).Value;

                string      registryValue = string.Empty;
                RegistryKey localKey      = null;
                if (Environment.Is64BitOperatingSystem)
                {
                    localKey = RegistryKey.OpenBaseKey(Microsoft.Win32.RegistryHive.LocalMachine, RegistryView.Registry64);
                }
                else
                {
                    localKey = RegistryKey.OpenBaseKey(Microsoft.Win32.RegistryHive.LocalMachine, RegistryView.Registry32);
                }

                try
                {
                    localKey = localKey.OpenSubKey(@"Software\\Microsoft\Office\\");
                    // registryValue = localKey.GetValue("TestKey").ToString();
                    if (localKey.SubKeyCount > 0)
                    {
                        if ((localKey.GetSubKeyNames()).Contains(@"Outlook"))
                        {
                        }
                        else
                        {
                            str = "Outlook is not installed on this system.Please Install Outlook"; // Still need this?
                            return(str);
                        }
                    }
                }
                catch (Exception e)
                {
                    str = "Execption in reading regsitry for outlook prereq " + e.Message;
                    return(str);
                }



                string InstallPath = (string)Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Office\14.0\Outlook", "Bitness", null);
                if (InstallPath == null)
                {
                    InstallPath = (string)Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Office\15.0\Outlook", "Bitness", null);
                }
                if (InstallPath == null)
                {
                    InstallPath = (string)Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Office\16.0\Outlook", "Bitness", null);
                }

                if (InstallPath != null)
                {
                    //its 64 bit outlook and 64 bit migration
                    if (bitness == true)
                    {
                        if (InstallPath == "x64")
                        {
                            //64 bit mapi and 64 bit migration perfect
                        }
                        else
                        {
                            if (InstallPath == "x86")
                            {
                                //32 bit mapi and 64 bit migration cannot continue
                                str = "The 64 bit Migration wizard is not compatible with MAPI 32 bit libraries.  Please run the 32 bit Migration wizard.";
                                return(str);
                            }
                        }
                    }
                    else
                    {
                        if (InstallPath == "x64")
                        {
                            //64 bit mapi and 32 bit bit migration cannot continue
                            str = "The 32 bit Migration wizard is not compatible with MAPI 64 bit libraries.  Please run the 64 bit Migration wizard.";
                            return(str);
                        }
                        else
                        {
                            if (InstallPath == "x86")
                            {
                                //32 bit mapi and 32 bit migration perfect
                            }
                        }
                    }
                }
                else
                {
                    if (bitness == true)
                    {
                        //32 bit mapi and 64 bitmigration
                        str = "Older versions of Outlook are not compatible with the 64 bit Migration wizard.  Please run the 32 bit Migration wizard.";
                        return(str);
                    }
                    else
                    {
                        //32 bit mapi and 32 bit migration.
                    }
                }
                return(str);
            }
        }