diff --git a/extensions/wikibase/module/MOD-INF/controller.js b/extensions/wikibase/module/MOD-INF/controller.js index e78cf1dff9a1..76ca143f9fe3 100644 --- a/extensions/wikibase/module/MOD-INF/controller.js +++ b/extensions/wikibase/module/MOD-INF/controller.js @@ -48,6 +48,7 @@ function init() { RefineServlet.registerCommand(module, "perform-wikibase-edits", new PerformWikibaseEditsCommand()); RefineServlet.registerCommand(module, "parse-wikibase-schema", new ParseWikibaseSchemaCommand()); RefineServlet.registerCommand(module, "login", new LoginCommand()); + RefineServlet.registerCommand(module, "fetch-manifest", new FetchManifestCommand()); /* * GREL functions diff --git a/extensions/wikibase/module/scripts/wikibase-manager.js b/extensions/wikibase/module/scripts/wikibase-manager.js index 1bc03a26b33b..f6239ffc9225 100644 --- a/extensions/wikibase/module/scripts/wikibase-manager.js +++ b/extensions/wikibase/module/scripts/wikibase-manager.js @@ -308,7 +308,7 @@ WikibaseManager.fetchManifestFromURL = function (manifestURL, onSuccess, onError }; // The manifest host must support CORS. - $.ajax(manifestURL, { + $.ajax("/command/wikidata/fetch-manifest?url=" + manifestURL, { "dataType": "json", "timeout": 5000 }).done(function (data) { diff --git a/extensions/wikibase/src/org/openrefine/wikibase/commands/FetchManifestCommand.java b/extensions/wikibase/src/org/openrefine/wikibase/commands/FetchManifestCommand.java new file mode 100644 index 000000000000..91d89f014bed --- /dev/null +++ b/extensions/wikibase/src/org/openrefine/wikibase/commands/FetchManifestCommand.java @@ -0,0 +1,45 @@ + +package org.openrefine.wikibase.commands; + +import static org.openrefine.wikibase.commands.CommandUtilities.respondError; + +import java.io.IOException; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.Response; + +import com.google.refine.commands.Command; + +/** + * Proxies Wikibase manifests to allow the client to bypass CORS restrictions. + */ +public class FetchManifestCommand extends Command { + + @Override + public void doGet(HttpServletRequest request, HttpServletResponse response) + throws ServletException, IOException { + String url = request.getParameter("url"); + try { + if (url == null) { + respondError(response, "No URL provided."); + return; + } + + // fetch the contents at the url with a plain get request and return the response + OkHttpClient client = new OkHttpClient(); + Request req = new Request.Builder().url(url).build(); + Response res = client.newCall(req).execute(); + response.setCharacterEncoding("UTF-8"); + response.setContentType("application/json"); + response.getWriter().write(res.body().string()); + response.setStatus(200); + } catch (Exception e) { + respondException(response, e); + } + } +}