Libgourou

Libgourou Git Source Tree

Root/src/fulfillment_item.cpp

1/*
2 Copyright 2021 Grégory Soutadé
3
4 This file is part of libgourou.
5
6 libgourou is free software: you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 libgourou is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with libgourou. If not, see <http://www.gnu.org/licenses/>.
18*/
19
20#include <cctype>
21#include <fulfillment_item.h>
22#include <libgourou_common.h>
23#include "user.h"
24
25namespace gourou
26{
27 FulfillmentItem::FulfillmentItem(pugi::xml_document& doc, User* user)
28: fulfillDoc(), loanToken(0)
29 {
30fulfillDoc.reset(doc); /* We must keep a copy */
31metadatas = fulfillDoc.select_node("//metadata").node();
32
33if (!metadatas)
34 EXCEPTION(FFI_INVALID_FULFILLMENT_DATA, "No metadata tag in document");
35
36pugi::xml_node node = doc.select_node("/envelope/fulfillmentResult/resourceItemInfo/src").node();
37downloadURL = node.first_child().value();
38
39if (downloadURL == "")
40 EXCEPTION(FFI_INVALID_FULFILLMENT_DATA, "No download URL in document");
41
42node = doc.select_node("/envelope/fulfillmentResult/resourceItemInfo/resource").node();
43resource = node.first_child().value();
44
45if (resource == "")
46 EXCEPTION(FFI_INVALID_FULFILLMENT_DATA, "No resource in document");
47
48pugi::xml_node licenseToken = doc.select_node("/envelope/fulfillmentResult/resourceItemInfo/licenseToken").node();
49
50if (!licenseToken)
51 EXCEPTION(FFI_INVALID_FULFILLMENT_DATA, "Any license token in document");
52
53buildRights(licenseToken, user);
54
55node = doc.select_node("/envelope/fulfillmentResult/returnable").node();
56try
57{
58 if (node && node.first_child().value() == std::string("true"))
59loanToken = new LoanToken(doc);
60}
61catch(std::exception& e)
62{
63 GOUROU_LOG(ERROR, "Book is returnable, but contains invalid loan token");
64 GOUROU_LOG(ERROR, e.what());
65}
66 }
67
68 FulfillmentItem::~FulfillmentItem()
69 {
70if (loanToken) delete loanToken;
71 }
72
73 void FulfillmentItem::buildRights(const pugi::xml_node& licenseToken, User* user)
74 {
75pugi::xml_node decl = rights.append_child(pugi::node_declaration);
76decl.append_attribute("version") = "1.0";
77
78pugi::xml_node root = rights.append_child("adept:rights");
79root.append_attribute("xmlns:adept") = ADOBE_ADEPT_NS;
80
81pugi::xml_node newLicenseToken = root.append_copy(licenseToken);
82if (!newLicenseToken.attribute("xmlns"))
83 newLicenseToken.append_attribute("xmlns") = ADOBE_ADEPT_NS;
84
85pugi::xml_node licenseServiceInfo = root.append_child("adept:licenseServiceInfo");
86pugi::xml_node licenseURL = licenseToken.select_node("licenseURL").node();
87licenseURL.set_name("adept:licenseURL");
88licenseServiceInfo.append_copy(licenseURL);
89pugi::xml_node certificate = licenseServiceInfo.append_child("adept:certificate");
90std::string certificateValue = user->getLicenseServiceCertificate(licenseURL.first_child().value());
91certificate.append_child(pugi::node_pcdata).set_value(certificateValue.c_str());
92 }
93
94 std::string FulfillmentItem::getMetadata(std::string name)
95 {
96// https://stackoverflow.com/questions/313970/how-to-convert-an-instance-of-stdstring-to-lower-case
97#if __STDC_VERSION__ >= 201112L
98std::transform(name.begin(), name.end(), name.begin(),
99 [](unsigned char c){ return std::tolower(c); });
100#else
101std::transform(name.begin(), name.end(), name.begin(), tolower);
102#endif
103name = std::string("dc:") + name;
104pugi::xpath_node path = metadatas.select_node(name.c_str());
105
106if (!path)
107 return "";
108
109return path.node().first_child().value();
110 }
111
112 std::string FulfillmentItem::getRights()
113 {
114StringXMLWriter xmlWriter;
115rights.save(xmlWriter, " ");
116return xmlWriter.getResult();
117 }
118
119 std::string FulfillmentItem::getDownloadURL()
120 {
121return downloadURL;
122 }
123
124 std::string FulfillmentItem::getResource()
125 {
126return resource;
127 }
128
129 LoanToken* FulfillmentItem::getLoanToken()
130 {
131return loanToken;
132 }
133}

Archive Download this file