Exemple #1
0
        private bool IsExistingPrinter(string printer)
        {
            var  printers = RawPrinterHelper.GetPrinters();
            bool response = false;

            foreach (Dictionary <string, object> p in printers)
            {
                if (p["Name"].ToString() == printer)
                {
                    response = true;
                    break;
                }
            }

            return(response);
        }
Exemple #2
0
        public Stream Command(System.IO.Stream pStream)
        {
            var jss = new JavaScriptSerializer();

            string value        = "";
            string response     = "";
            string command      = "";
            string printer      = "";
            string jobId        = "*";
            string documentName = "*";

            using (var reader = new StreamReader(pStream, Encoding.UTF8))
            {
                value = reader.ReadToEnd();
            }

            var commandData = jss.Deserialize <Dictionary <string, string> >(value);

            // Make sure required command paramters are complete
            if (!commandData.ContainsKey("Command") && !commandData.ContainsKey("Printer"))
            {
                response = "Command requests requires a 'Command' and 'Printer' parameters.";
            }
            else
            {
                command = commandData["Command"];
                printer = commandData["Printer"];

                // Check for command validity
                if (!IsValidCommand(command))
                {
                    response = "'" + command.ToUpper().Trim() + "' command is not supported.";
                }
                else if (!IsExistingPrinter(printer))
                {
                    response = "'" + printer.ToUpper().Trim() + "' is not found in the Cameleon Printer Service server.";
                }
                else
                {
                    // Get specified JobId or DocumentName
                    if (commandData.ContainsKey("JobId"))
                    {
                        jobId = commandData["JobId"];
                    }
                    else if (commandData.ContainsKey("DocumentName"))
                    {
                        documentName = commandData["DocumentName"];
                        if (jobId.Trim() == "*" && !string.IsNullOrEmpty(documentName.Trim()))
                        {
                            jobId = "";
                        }
                    }

                    // Check if printer parameter is valid
                    if (printer.Trim() == "")
                    {
                        response = "Command requires a 'Printer' parameter to have a value.";
                    }
                    else
                    {
                        int totalAffected = RawPrinterHelper.SendCommandToPrinter(command, printer, jobId, documentName);
                        WebOperationContext.Current.OutgoingResponse.ContentType = "application/json; charset=utf-8";

                        response = totalAffected + " print jobs are affected by '" + command.ToUpper() + "' on '" +
                                   printer.ToUpper() + "'.";
                    }
                }
            }

            return(new MemoryStream(Encoding.UTF8.GetBytes(@"{""Response"" : " + @"""" + response + @"""}")));
        }
Exemple #3
0
        public Stream Print(System.IO.Stream pStream)
        {
            var jss = new JavaScriptSerializer();

            string value           = "";
            string response        = "";
            string dataType        = "RAW";
            string defaultDataType = "RAW";
            string printer         = "";
            string name            = "";
            string data            = "";

            using (var reader = new StreamReader(pStream, Encoding.UTF8))
            {
                value = reader.ReadToEnd();
            }

            var printData = jss.Deserialize <Dictionary <string, string> >(value);

            if (printData.ContainsKey("Printer") && printData.ContainsKey("Name") && printData.ContainsKey("Data"))
            {
                printer = printData["Printer"];
                name    = printData["Name"];
                data    = printData["Data"];

                //Check for Printer Name validity and DataType default

                if (printData.ContainsKey("DataType"))
                {
                    dataType = printData["DataType"].Trim();
                }

                var  printers       = RawPrinterHelper.GetPrinters();
                bool isValidPrinter = false;
                foreach (Dictionary <string, object> p in printers)
                {
                    if (p["Name"].ToString() == printer)
                    {
                        defaultDataType = p["PrintJobDataType"].ToString();
                        isValidPrinter  = true;
                        break;
                    }
                }

                if (isValidPrinter)
                {
                    if (!IsValidDataType(dataType))
                    {
                        dataType = defaultDataType;
                    }
                    bool successPrint = RawPrinterHelper.SendStringToPrinter(printer, name, data, dataType);
                    if (successPrint)
                    {
                        response = "Print request sent to " + printer + " as " + dataType + " data.";
                    }
                    else
                    {
                        response = "An error occured during print request: " + Marshal.GetLastWin32Error();
                    }
                }
                else
                {
                    response = printer + " is not found in the Cameleon Printer Service server.";
                }
            }
            else
            {
                response = "Print requests requires a 'Printer', 'Name' and 'Data' parameters.";
            }

            return(new MemoryStream(Encoding.UTF8.GetBytes(@"{""Response"" : " + @"""" + response + @"""}")));
        }