Menu

[r2337]: / trunk / Src / Web.UDBDownloadMgr.pas  Maximize  Restore  History

Download this file

441 lines (398 with data), 16.5 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
{
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at http://mozilla.org/MPL/2.0/
*
* Copyright (C) 2005-2012, Peter Johnson (www.delphidabbler.com).
*
* $Rev$
* $Date$
*
* Implements a class that interfaces with a web service to update the database.
}
unit Web.UDBDownloadMgr;
interface
uses
// Delphi
Classes,
// Project
UEncodings, UURIParams, Web.UExceptions, Web.UStdWebService;
type
{
TDBDownloadMgr:
Manages downloads of updates to database from web service via HTTP.
}
TDBDownloadMgr = class sealed(TStdWebService)
strict private
procedure HandleException(const E: EWebError);
{Converts EWebError exceptions into EDBDownloadMgr exceptions with both
a long and a short description.
@param E [in] EWebError or descendant exception to be converted.
@except Raises EDBDownloadMgr exceptions based on given exception.
}
procedure GetStdParams(const Params: TURIParams);
procedure PostStdCommand(const Cmd: string; const Response: TStrings);
{Sends command to server that includes standard parameters that are sent
with all commands, i.e. "progid" and "version".
@param Cmd [in] Command to be sent.
@param Response [in] String list to receive web server response.
@except EDBDownloadMgr Raised if EWebError exception or sub class
detected.
}
procedure SafePostCommand(const Cmd: string; const Params: TURIParams;
const Response: TStrings);
{Sends a command to server and returns data component of response in a
string list. Converts recognised exceptions to EDBDownloadMgr.
@param Cmd [in] Command to be executed. Passed to server as Cmd=CmdName
parameter.
@param Params [in] List of parameters in form ParamName=ParamValue. []
if no parameters.
@param Response [in] Server's response as string list where each line of
response is a line of string list.
@except EDBDownloadMgr Raised if EWebError exception or sub class
detected.
}
public
procedure LogOn(const WantProgress: Boolean = False);
{Logs on to web service.
@param WantProgresss [in] Flag true if OnProgress event to be triggered
for download.
}
procedure LogOff(const WantProgress: Boolean = False);
{Logs off from web server.
@param WantProgresss [in] Flag true if OnProgress event to be triggered
for download.
}
function LastUpdate(const WantProgress: Boolean = False): string;
{Gets date of last update to code snippets database on web server.
@param WantProgresss [in] Flag true if OnProgress event to be triggered
for download.
@return String containing last update date as a Unix date stamp.
}
function FileCount(const WantProgress: Boolean = False): Integer;
{Gets count of files in code snippets database on web server.
@param WantProgresss [in] Flag true if OnProgress event to be triggered
for download.
@return File count.
}
function GetDatabase(const WantProgress: Boolean = False): TEncodedData;
{Gets whole code snippets database from web server.
@param WantProgresss [in] Flag true if OnProgress event to be triggered
for download.
@return Downloaded data with encoding info.
}
constructor Create;
{Class constructor. Initialises service.
}
end;
{
EDBDownloadMgr:
Class of exception raised by TDBDownloadMgr. Contains standard full
description of exception and adds an abbreviated description in ShortMsg
property.
}
EDBDownloadMgr = class(EWebService)
strict private
fShortMsg: string;
{Value of ShortMsg property}
public
constructor Create(const ShortMsg, Msg: string); overload;
{Class constructor. Creates exception that records abbreviated and full
descriptions.
@param ShortMsg [in] Abbreviated message for ShortMsg property.
@param Msg [in] Standard exception Message property value.
}
constructor CreateFmt(const ShortMsg, Fmt: string;
const Args: array of const); overload;
{Class constructor. Creates exception that records abbreviated and full
descriptions. Builds full description from format string and parameters.
@param ShortMsg [in] Abbreviated message for ShortMsg property.
@param Fmt [in] Format string for standard Message property.
@param Args [in] Parameters for format string.
}
property ShortMsg: string read fShortMsg;
{Abbreviated description of exception}
end;
implementation
uses
// Delphi
SysUtils,
// Project
UAppInfo, UConsts, UStrUtils, USystemInfo, Web.UCharEncodings, Web.UInfo;
{
Web service notes: codesnip-updt.php v5
=======================================
This web service enables CodeSnip to check if updated files are available in
on-line database and to update local database accordingly.
The service accepts POSTed commands which must each have form:
cmd=<command> [<params>]
where <params> is a list of parameters in form param-name=param-value.
All responses from the v5 service have two parts:
+ Successful responses have '0' on first line followed by optional lines of
data resulting from command.
+ Error responses have +ve error code on first line and error message on 2nd
line.
The web service expects a user agent of "DelphiDabbler-CodeSnip-Updater-v5"
and will return a 403 "Forbidden" error if this is not provided.
Table of Commands
+----------------------------------------------------------------------------+
|Command |Queries|Values |Response |
+-----------+-------|----------------------+---------------------------------|
|logon |cmd |"logon" |OK: Stream of news items which |
| |progid |unique id of program |is ignored |
| |version|program version number|ERROR: CSUPDT_ERR_STDPARAMS if |
| |os |operating system info |required params not provided |
| |browser|version of IE browser | |
+-----------+-------+----------------------+---------------------------------+
|filecount |cmd |"filecount" |OK: Integer indication number of |
| |progid |unique id of program |files in remote database |
| |version|program version number|ERROR: CSUPDT_ERR_STDPARAMS if |
| | | |required params not provided |
| | | |ERROR: CSUPDT_ERR_LIST if can't |
| | | |list files in database |
+-----------+-------+----------------------+---------------------------------+
|lastupdate |cmd |"lastupdate" |OK: Unix time stamp representing |
| |progid |unique id of program |date on which online database was|
| |version|program version number|last updated |
| | | |ERROR: CSUPDT_ERR_STDPARAMS if |
| | | |required params not provided |
| | | |ERROR: CSUPDT_ERR_LIST if can't |
| | | |list files in database |
| | | |ERROR: CSUPDT_ERR_NOTFOUND if |
| | | |can't access a file in database |
+-----------+-------+----------------------+---------------------------------+
|getdatabase|cmd |"getdatabase" |OK: All files from on-line |
| |progid |unique id of program |database combined in single |
| |version|program version number|stream of data. |
| | | |ERROR: CSUPDT_ERR_STDPARAMS if |
| | | |required params not provided |
| | | |ERROR: CSUPDT_ERR_LIST if can't |
| | | |list files in database |
| | | |ERROR: CSUPDT_ERR_NOTFOUND if |
| | | |can't access a file in database |
+-----------+-------+----------------------+---------------------------------+
|logoff |cmd |"logoff" |OK: No data |
| |progid |unique id of program |ERRORS: CSUPDT_ERR_STDPARAMS if | |
| |version|program version number|required params not provided |
+-----------+-------+----------------------+---------------------------------+
If any other command (or no command) is specified then error code
CSUPDT_ERR_CMD (1) is returned.
}
const
// Web service info
cScriptName = 'codesnip-updt.php'; // script name
cUserAgent = 'DelphiDabbler-CodeSnip-Updater-v5'; // user agent string
resourcestring
// Error messages
sShortHTTPError = 'HTTP Error';
sLongHTTPError = 'The web server returned the following error: "%0:s"';
sShortConnectionError = 'Connection Error';
sShortTransmissionError = 'Transmission Error';
sShortWebSvcFailure = 'Web Service Failure';
sLongWebSvcFailure = 'The database update web service failed with the '
+ 'following error:' + EOL + '%0:s';
sShortWebSvcError = 'Download Error';
sLongWebSvcError = 'The database update web service returned the following '
+ 'error' + EOL + 'Error %0:d: %1:s';
sBadFileCount = 'Web service returned an invalid value for the filecount '
+ 'command';
{ TDBDownloadMgr }
constructor TDBDownloadMgr.Create;
{Class constructor. Initialises service.
}
begin
inherited Create(TWebServiceInfo.Create(cScriptName, cUserAgent));
end;
function TDBDownloadMgr.FileCount(const WantProgress: Boolean): Integer;
{Gets count of files in code snippets database on web server.
@param WantProgresss [in] Flag true if OnProgress event to be triggered for
download.
@return File count.
}
var
Response: TStringList; // response from server
begin
Self.WantProgress := WantProgress;
Response := TStringList.Create;
try
PostStdCommand('filecount', Response);
if not TryStrToInt(StrTrim(Response.Text), Result) then
raise EWebServiceFailure.Create(sBadFileCount);
finally
Response.Free;
end;
end;
function TDBDownloadMgr.GetDatabase(const WantProgress: Boolean): TEncodedData;
{Gets whole code snippets database from web server.
@param WantProgresss [in] Flag true if OnProgress event to be triggered for
download.
@return Downloaded data with encoding info.
}
var
Response: TStringList; // response from server
begin
Self.WantProgress := WantProgress;
Response := TStringList.Create;
try
PostStdCommand('getdatabase', Response);
Result := TEncodedData.Create(
Response.Text, TWebCharEncodings.GetEncodingType(ResponseCharSet)
);
finally
Response.Free;
end;
end;
procedure TDBDownloadMgr.GetStdParams(const Params: TURIParams);
begin
Params.Add('progid', TAppInfo.ProgramKey);
Params.Add('version', TAppInfo.ProgramReleaseVersion);
end;
procedure TDBDownloadMgr.HandleException(const E: EWebError);
{Converts EWebError exceptions into EDBDownloadMgr exceptions with both
a long and a short description.
@param E [in] EWebError or descendant exception to be converted.
@except Raises EDBDownloadMgr exceptions based on given exception.
}
begin
if E is EHTTPError then
raise EDBDownloadMgr.CreateFmt(
sShortHTTPError, sLongHTTPError, [E.Message]
);
if E is EWebConnectionError then
raise EDBDownloadMgr.Create(sShortConnectionError, E.Message);
if E is EWebTransmissionError then
raise EDBDownloadMgr.Create(sShortTransmissionError, E.Message);
if E is EWebServiceFailure then
raise EDBDownloadMgr.CreateFmt(
sShortWebSvcFailure, sLongWebSvcFailure, [E.Message]
);
if E is EWebServiceError then
// Include web service error code in long description
raise EDBDownloadMgr.CreateFmt(
sShortWebSvcError,
sLongWebSvcError,
[(E as EWebServiceError).ErrorCode, E.Message]
);
end;
function TDBDownloadMgr.LastUpdate(const WantProgress: Boolean): string;
{Gets date of last update to code snippets database on web server.
@param WantProgresss [in] Flag true if OnProgress event to be triggered for
download.
@return String containing last update date as a Unix date stamp.
}
var
Response: TStringList; // response from server
begin
Self.WantProgress := WantProgress;
Response := TStringList.Create;
try
PostStdCommand('lastupdate', Response);
Result := StrTrim(Response.Text);
finally
Response.Free;
end;
end;
procedure TDBDownloadMgr.LogOff(const WantProgress: Boolean);
{Logs off from web server.
@param WantProgresss [in] Flag true if OnProgress event to be triggered for
download.
}
var
Response: TStringList; // response from server
begin
Self.WantProgress := WantProgress;
Response := TStringList.Create;
try
PostStdCommand('logoff', Response); // No response data expected
finally
Response.Free;
end;
end;
procedure TDBDownloadMgr.LogOn(const WantProgress: Boolean);
{Logs on to web service.
@param WantProgresss [in] Flag true if OnProgress event to be triggered for
download.
}
var
Response: TStringList; // response from server
Params: TURIParams; // parameters to send with command
begin
Self.WantProgress := WantProgress;
Response := TStringList.Create;
try
Params := TURIParams.Create;
try
GetStdParams(Params);
Params.Add('os', TOSInfo.Description);
Params.Add('browser', IntToStr(TOSInfo.BrowserVer));
SafePostCommand('logon', Params, Response);
finally
Params.Free;
end;
finally
Response.Free;
end;
end;
procedure TDBDownloadMgr.PostStdCommand(const Cmd: string;
const Response: TStrings);
{Sends command to server that includes standard parameters that are sent with
all commands, i.e. "progid" and "version".
@param Cmd [in] Command to be sent.
@param Response [in] String list to receive web server response.
@except EDBDownloadMgr Raised if EWebError exception or sub class detected.
}
var
StdParams: TURIParams;
begin
StdParams := TURIParams.Create;
try
GetStdParams(StdParams);
SafePostCommand(Cmd, StdParams, Response);
finally
StdParams.Free;
end;
end;
procedure TDBDownloadMgr.SafePostCommand(const Cmd: string;
const Params: TURIParams; const Response: TStrings);
{Sends a command to server and returns data component of response in a
string list. Converts recognised exceptions to EDBDownloadMgr.
@param Cmd [in] Command to be executed. Passed to server as Cmd=CmdName
parameter.
@param Params [in] List of parameters in form ParamName=ParamValue. [] if no
parameters.
@param Response [in] Server's response as string list where each line of
response is a line of string list.
@except EDBDownloadMgr Raised if EWebError exception or sub class detected.
}
begin
try
PostCommand(Cmd, Params, Response);
except
on E: EWebError do
HandleException(E);
end;
end;
{ EDBDownloadMgr }
constructor EDBDownloadMgr.Create(const ShortMsg, Msg: string);
{Class constructor. Creates exception that records abbreviated and full
descriptions.
@param ShortMsg [in] Abbreviated message for ShortMsg property.
@param Msg [in] Standard exception Message property value.
}
begin
inherited Create(Msg);
fShortMsg := ShortMsg;
end;
constructor EDBDownloadMgr.CreateFmt(const ShortMsg, Fmt: string;
const Args: array of const);
{Class constructor: creates exception that records abbreviated and full
descriptions. Builds full description from format string and parameters.
@param ShortMsg [in] Abbreviated message for ShortMsg property.
@param Fmt [in] Format string for standard Message property.
@param Args [in] Parameters for format string.
}
begin
inherited CreateFmt(Fmt, Args);
fShortMsg := ShortMsg;
end;
end.
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.