1 | #include "resumptiontoken.h"
|
---|
2 | #include "oaitools.h"
|
---|
3 |
|
---|
4 | /**
|
---|
5 | * Generate an initial resumption token from some basic details.
|
---|
6 | *
|
---|
7 | * TODO: add optional argument to set the server name.
|
---|
8 | */
|
---|
9 | ResumptionToken::ResumptionToken(const text_t &collection, const text_t &node,
|
---|
10 | const text_t &buildDate)
|
---|
11 | { this->collection = collection;
|
---|
12 | this->browseNode = node;
|
---|
13 | this->buildDate = buildDate;
|
---|
14 | this->startItem = 0;
|
---|
15 | }
|
---|
16 |
|
---|
17 | /**
|
---|
18 | * Generate a resumption token from a URN-style format.
|
---|
19 | *
|
---|
20 | * See getToken() for details of the format.
|
---|
21 | *
|
---|
22 | * TODO: support inclusion of an optional server name.
|
---|
23 | */
|
---|
24 | ResumptionToken::ResumptionToken(const text_t &URN)
|
---|
25 | { text_t::const_iterator first = URN.begin();
|
---|
26 | text_t::const_iterator last = URN.end();
|
---|
27 |
|
---|
28 | this->collection = "";
|
---|
29 | this->browseNode = "";
|
---|
30 | this->startItem = -1;
|
---|
31 |
|
---|
32 | text_t::const_iterator here = findchar(first, last, ':');
|
---|
33 | if (here == first) {
|
---|
34 | return;
|
---|
35 | }
|
---|
36 |
|
---|
37 | text_t oainamespace = substr(first, here);
|
---|
38 | if (oainamespace != "gsdloai") {
|
---|
39 | return;
|
---|
40 | }
|
---|
41 |
|
---|
42 | // increment past the first colon to get the location
|
---|
43 | first = ++here;
|
---|
44 |
|
---|
45 | // get the collection
|
---|
46 | here = findchar(first, last, '.');
|
---|
47 | if (here == first) {
|
---|
48 | return;
|
---|
49 | }
|
---|
50 | this->collection = substr(first, here);
|
---|
51 |
|
---|
52 | first = ++here;
|
---|
53 |
|
---|
54 | // get the browse node
|
---|
55 | here = findchar(first, last, ',');
|
---|
56 |
|
---|
57 | // now, we *may* have truncated items where we start with the first
|
---|
58 | // item in a node (i.e. suppress the ',0' for one reason or another,
|
---|
59 | // let's be forgiving if we can...
|
---|
60 | if (here == first) {
|
---|
61 | this->startItem = 0;
|
---|
62 |
|
---|
63 | here = findchar(first, last, '-');
|
---|
64 |
|
---|
65 | // no build date - keep trying...
|
---|
66 | if (here == first) {
|
---|
67 | this->browseNode = substr(first, last);
|
---|
68 | }
|
---|
69 | else {
|
---|
70 | this->browseNode = substr(first, here);
|
---|
71 | this->buildDate = substr(++here, last);
|
---|
72 | }
|
---|
73 | }
|
---|
74 | // got a proper item indicator...
|
---|
75 | else {
|
---|
76 | // record the browse node
|
---|
77 | this->browseNode = substr(first, here);
|
---|
78 |
|
---|
79 | // get past the ','
|
---|
80 | first = ++here;
|
---|
81 |
|
---|
82 | // now find the build date marker
|
---|
83 | here = findchar(first, last, '-');
|
---|
84 | if (here == first) {
|
---|
85 | this->startItem = substr(first, last).getint();
|
---|
86 | }
|
---|
87 | else {
|
---|
88 | this->startItem = substr(first, here).getint();
|
---|
89 | this->buildDate = substr(++here, last);
|
---|
90 | }
|
---|
91 | }
|
---|
92 | }
|
---|
93 |
|
---|
94 | /**
|
---|
95 | * Get a resumption token in text_t format.
|
---|
96 | *
|
---|
97 | * Resumption tokens are in the format:
|
---|
98 | *
|
---|
99 | * gsdloai:<serverName>:collectionname.browseNode,startItem-BuildDate
|
---|
100 | *
|
---|
101 | * The resumption token format does not currently implement the use of
|
---|
102 | * the optional <serverName> item; it is taken to default to the name of
|
---|
103 | * the receiving server.
|
---|
104 | *
|
---|
105 | * TODO: add server identity as an optional argument; also change
|
---|
106 | * ResumptionToken(text_t &) accordingly.
|
---|
107 | */
|
---|
108 | text_t ResumptionToken::getToken()
|
---|
109 | { text_t reply = "gsdloai:";
|
---|
110 | reply = reply + this->collection;
|
---|
111 | if (this->browseNode != "") {
|
---|
112 | reply = reply + "." + this->browseNode;
|
---|
113 | }
|
---|
114 | reply = reply + ",";
|
---|
115 | reply.append(this->startItem);
|
---|
116 | reply = reply + "-" + buildDate;
|
---|
117 |
|
---|
118 | return reply;
|
---|
119 | }
|
---|
120 |
|
---|
121 | /**
|
---|
122 | * Update the position of an existing resumption token
|
---|
123 | */
|
---|
124 | void ResumptionToken::setPosition(const text_t &node, int startItem)
|
---|
125 | { this->browseNode = node;
|
---|
126 | this->startItem = startItem;
|
---|
127 | }
|
---|
128 |
|
---|
129 | /**
|
---|
130 | * Check if the resumption token is valid - only a very primitive
|
---|
131 | * check is done here; one ought to check for an existing collection
|
---|
132 | * and valid browse Node, build date and startItem
|
---|
133 | *
|
---|
134 | * TODO: implement improved validation checking.
|
---|
135 | */
|
---|
136 | bool ResumptionToken::isValid()
|
---|
137 | { return this->collection != "";
|
---|
138 | }
|
---|