5352b9ab1700ac563d335be1c440ff6339ba9217
[metaproxy-moved-to-github.git] / src / torus.cpp
1 /* This file is part of Metaproxy.
2    Copyright (C) Index Data
3
4 Metaproxy is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
7 version.
8
9 Metaproxy is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17 */
18
19 #include <metaproxy/xmlutil.hpp>
20
21 #include <string.h>
22 #include <yaz/wrbuf.h>
23 #include <yaz/log.h>
24 #include <yaz/url.h>
25 #include <metaproxy/util.hpp>
26 #include "torus.hpp"
27
28 namespace mp = metaproxy_1;
29
30 xmlDoc *mp::get_searchable(mp::Package &package,
31                            std::string url_template, const std::string &db,
32                            const std::string &query,
33                            const std::string &realm,
34                            const std::string &proxy)
35 {
36     // http://mk2.indexdata.com/torus2/searchable/records/?query=udb%3d%db
37     // or
38     // http://mk2.indexdata.com/torus2/searchable/records/?query=%query
39     xmlDoc *doc = 0;
40     size_t found;
41
42     found = url_template.find("%query");
43     if (found != std::string::npos)
44         url_template.replace(found, 6, mp::util::uri_encode(query));
45
46     found = url_template.find("%db");
47     if (found != std::string::npos)
48         url_template.replace(found, 3, mp::util::uri_encode(db));
49
50     found = url_template.find("%realm");
51     if (found != std::string::npos)
52         url_template.replace(found, 6, mp::util::uri_encode(realm));
53
54     Z_HTTP_Header *http_headers = 0;
55     mp::odr odr;
56
57     z_HTTP_header_add(odr, &http_headers, "Accept","application/xml");
58
59     yaz_url_t url_p = yaz_url_create();
60     if (proxy.length())
61         yaz_url_set_proxy(url_p, proxy.c_str());
62
63     Z_HTTP_Response *http_response = yaz_url_exec(url_p,
64                                                   url_template.c_str(),
65                                                   "GET",
66                                                   http_headers,
67                                                   0, /* content buf */
68                                                   0  /* content_len */
69         );
70     if (http_response && http_response->code == 200 &&
71         http_response->content_buf)
72     {
73         doc = xmlParseMemory(http_response->content_buf,
74                              http_response->content_len);
75         if (doc)
76             package.log("zoom", YLOG_LOG, "Torus: %s OK",
77                         url_template.c_str());
78         else
79             package.log("zoom", YLOG_WARN, "Torus: %s FAIL. XML parse failed",
80                         url_template.c_str());
81     }
82     else
83     {
84         if (http_response)
85         {
86             package.log("zoom", YLOG_WARN, "Torus: %s FAIL. HTTP code %d",
87                         url_template.c_str(), http_response->code);
88         }
89         else
90             package.log("zoom", YLOG_WARN, "Torus: %s FAIL. No HTTP response",
91                         url_template.c_str());
92     }
93
94     if (http_response && http_response->content_buf)
95     {
96         package.log("zoom", YLOG_LOG, "HTTP content");
97         package.log_write(http_response->content_buf,
98                           http_response->content_len);
99     }
100     yaz_url_destroy(url_p);
101     return doc;
102 }
103
104 /*
105  * Local variables:
106  * c-basic-offset: 4
107  * c-file-style: "Stroustrup"
108  * indent-tabs-mode: nil
109  * End:
110  * vim: shiftwidth=4 tabstop=8 expandtab
111  */
112