forked from neodynamic/WebClientPrint-ASPNET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebClientPrintAPI.ashx
More file actions
123 lines (96 loc) · 5.02 KB
/
WebClientPrintAPI.ashx
File metadata and controls
123 lines (96 loc) · 5.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<%@ WebHandler Language="C#" Class="WebClientPrintAPI" %>
using System;
using System.Web;
using System.IO;
using Neodynamic.SDK.Web;
public class WebClientPrintAPI : IHttpHandler {
//*********************************
// IMPORTANT NOTE
// In this sample we store users related stuff (like
// the list of printers and whether they have the WCPP
// client utility installed) in the Application cache
// object part of ASP.NET BUT you can change it to
// another different storage (like a DB or file server)!
// which will be required in Load Balacing scenarios
//*********************************
public void ProcessRequest (HttpContext context) {
//get session ID
string sessionID = (context.Request["sid"] != null) ? context.Request["sid"].ToString() : null;
//get Query String
string queryString = context.Request.Url.Query;
try
{
//Determine and get the Type of Request
RequestType prType = WebClientPrint.GetProcessRequestType(queryString);
if (prType == RequestType.GenPrintScript ||
prType == RequestType.GenWcppDetectScript)
{
//Let WebClientPrint to generate the requested script
byte[] script = WebClientPrint.GenerateScript(context.Request.Url.AbsoluteUri.Replace(queryString, ""), queryString);
context.Response.ContentType = "text/javascript";
context.Response.BinaryWrite(script);
}
else if (prType == RequestType.ClientSetWcppVersion)
{
//This request is a ping from the WCPP utility
//so store the session ID indicating this user has the WCPP installed
//also store the WCPP Version if available
string wcppVersion = context.Request["wcppVer"];
if (string.IsNullOrEmpty(wcppVersion))
wcppVersion = "1.0.0.0";
context.Application.Set(sessionID + "wcppInstalled", wcppVersion);
}
else if (prType == RequestType.ClientSetInstalledPrinters)
{
//WCPP Utility is sending the installed printers at client side
//so store this info with the specified session ID
string printers = context.Request["printers"];
if (string.IsNullOrEmpty(printers) == false)
printers = System.Text.Encoding.UTF8.GetString(Convert.FromBase64String(printers));
context.Application.Set(sessionID + "printers", printers);
}
else if (prType == RequestType.ClientSetInstalledPrintersInfo)
{
//WCPP Utility is sending the client installed printers with detailed info
//so store this info with the specified session ID
//Printers Info is in JSON format
string printersInfo = context.Request.Params["printersInfoContent"];
if (string.IsNullOrEmpty(printersInfo) == false)
printersInfo = System.Text.Encoding.UTF8.GetString(Convert.FromBase64String(printersInfo));
context.Application.Set(sessionID + "printersInfo", printersInfo);
}
else if (prType == RequestType.ClientGetWcppVersion)
{
//return the WCPP version for the specified Session ID (sid) if any
bool sidWcppVersion = (context.Application[sessionID + "wcppInstalled"] != null);
context.Response.ContentType = "text/plain";
context.Response.Write(sidWcppVersion ? context.Application[sessionID + "wcppInstalled"] : "");
}
else if (prType == RequestType.ClientGetInstalledPrinters)
{
//return the installed printers for the specified Session ID (sid) if any
bool sidHasPrinters = (context.Application[sessionID + "printers"] != null);
context.Response.ContentType = "text/plain";
context.Response.Write(sidHasPrinters ? context.Application[sessionID + "printers"] : "");
}
else if (prType == RequestType.ClientGetInstalledPrintersInfo)
{
//return the installed printers with detailed info for the specified Session ID (sid) if any
bool sidHasPrinters = (context.Application[sessionID + "printersInfo"] != null);
context.Response.ContentType = "text/plain";
context.Response.Write(sidHasPrinters ? context.Application[sessionID + "printersInfo"] : "");
}
}
catch (Exception ex)
{
context.Response.StatusCode = 500;
context.Response.ContentType = "text/plain";
context.Response.Write(ex.Message + " - " + ex.StackTrace);
}
}
public bool IsReusable {
get {
return false;
}
}
}