Skip to content

Commit

Permalink
Wikibase: proxy manifest requests (OpenRefine#6136)
Browse files Browse the repository at this point in the history
* Wikibase: proxy manifest requests

* Set encoding before writing to body

* Java formatting

* Import sorting

---------

Co-authored-by: Antonin Delpeuch <[email protected]>
  • Loading branch information
Abbe98 and wetneb authored Nov 23, 2024
1 parent 557839b commit 3cf0c40
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
1 change: 1 addition & 0 deletions extensions/wikibase/module/MOD-INF/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion extensions/wikibase/module/scripts/wikibase-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
}

0 comments on commit 3cf0c40

Please sign in to comment.