metaproxy  1.13.0
torus.cpp
Go to the documentation of this file.
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  std::string &addinfo)
36 {
37  // http://mk2.indexdata.com/torus2/searchable/records/?query=udb%3d%db
38  // or
39  // http://mk2.indexdata.com/torus2/searchable/records/?query=%query
40  xmlDoc *doc = 0;
41  size_t found;
42 
43  found = url_template.find("%query");
44  if (found != std::string::npos)
45  url_template.replace(found, 6, mp::util::uri_encode(query));
46 
47  found = url_template.find("%db");
48  if (found != std::string::npos)
49  url_template.replace(found, 3, mp::util::uri_encode(db));
50 
51  found = url_template.find("%realm");
52  if (found != std::string::npos)
53  url_template.replace(found, 6, mp::util::uri_encode(realm));
54 
55  Z_HTTP_Header *http_headers = 0;
56  mp::odr odr;
57 
58  z_HTTP_header_add(odr, &http_headers, "Accept","application/xml");
59 
60  yaz_url_t url_p = yaz_url_create();
61  if (proxy.length())
62  yaz_url_set_proxy(url_p, proxy.c_str());
63 
64  Z_HTTP_Response *http_response = yaz_url_exec(url_p,
65  url_template.c_str(),
66  "GET",
67  http_headers,
68  0, /* content buf */
69  0 /* content_len */
70  );
71  if (http_response && http_response->code == 200 &&
72  http_response->content_buf)
73  {
74  doc = xmlParseMemory(http_response->content_buf,
75  http_response->content_len);
76  if (doc)
77  package.log("zoom", YLOG_LOG, "Torus: %s OK",
78  url_template.c_str());
79  else
80  {
81  package.log("zoom", YLOG_WARN, "Torus: %s FAIL. XML parse failed",
82  url_template.c_str());
83  addinfo = "Torus: XML parse failed";
84  }
85  }
86  else
87  {
88  addinfo = "Torus: ";
89  if (http_response)
90  {
91  package.log("zoom", YLOG_WARN, "Torus: %s FAIL. HTTP code %d",
92  url_template.c_str(), http_response->code);
93  addinfo += std::string(http_response->content_buf,
94  http_response->content_len);
95  }
96  else
97  {
98  addinfo += "unknown error";
99  package.log("zoom", YLOG_WARN, "Torus: %s FAIL. No HTTP response",
100  url_template.c_str());
101  }
102  }
103 
104  if (http_response && http_response->content_buf)
105  {
106  package.log("zoom", YLOG_LOG, "HTTP content");
107  package.log_write(http_response->content_buf,
108  http_response->content_len);
109  }
110  yaz_url_destroy(url_p);
111  return doc;
112 }
113 
114 /*
115  * Local variables:
116  * c-basic-offset: 4
117  * c-file-style: "Stroustrup"
118  * indent-tabs-mode: nil
119  * End:
120  * vim: shiftwidth=4 tabstop=8 expandtab
121  */
122 
xmlDoc * get_searchable(Package &package, std::string url_template, const std::string &db, const std::string &query, const std::string &realm, const std::string &proxy, std::string &addinfo)