zoom: log Torus record.
[metaproxy-moved-to-github.git] / src / torus.cpp
1 /* This file is part of Metaproxy.
2    Copyright (C) 2005-2011 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(std::string url_template, const std::string &db,
31                            const std::string &realm,
32                            const std::string &proxy)
33 {
34     // http://newmk2.indexdata.com/torus2/searchable.ebsco/records/?query=udb=aberdeenUni
35     xmlDoc *doc = 0;
36     size_t found;
37
38     found = url_template.find("%db");
39     if (found != std::string::npos)
40         url_template.replace(found, 3, mp::util::uri_encode(db));
41
42     found = url_template.find("%realm");
43     if (found != std::string::npos)
44         url_template.replace(found, 6, mp::util::uri_encode(realm));
45
46     Z_HTTP_Header *http_headers = 0;
47     mp::odr odr;
48     
49     z_HTTP_header_add(odr, &http_headers, "Accept","application/xml");
50
51     yaz_url_t url_p = yaz_url_create();
52     if (proxy.length())
53         yaz_url_set_proxy(url_p, proxy.c_str());
54
55     Z_HTTP_Response *http_response = yaz_url_exec(url_p,
56                                                   url_template.c_str(),
57                                                   "GET",
58                                                   http_headers,
59                                                   0, /* content buf */
60                                                   0  /* content_len */
61         );
62     if (http_response && http_response->code == 200 && 
63         http_response->content_buf)
64     {
65         yaz_log(YLOG_LOG, "Torus: %s OK", url_template.c_str());
66         doc = xmlParseMemory(http_response->content_buf,
67                              http_response->content_len);
68         
69     }
70     else
71     {
72         yaz_log(YLOG_WARN, "Torus: %s FAIL", url_template.c_str());
73         if (http_response)
74         {
75             yaz_log(YLOG_LOG, "HTTP code: %d", http_response->code);
76         }
77     }
78
79     if (http_response && http_response->content_buf)
80     {
81         yaz_log(YLOG_LOG, "HTTP content:\n%.*s",
82                 (int) http_response->content_len,
83                 http_response->content_buf);
84     }
85     yaz_url_destroy(url_p);
86     return doc;
87 }
88
89 /*
90  * Local variables:
91  * c-basic-offset: 4
92  * c-file-style: "Stroustrup"
93  * indent-tabs-mode: nil
94  * End:
95  * vim: shiftwidth=4 tabstop=8 expandtab
96  */
97