forked from OpenRefine/OpenRefine
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Wikibase: proxy manifest requests (OpenRefine#6136)
* Wikibase: proxy manifest requests * Set encoding before writing to body * Java formatting * Import sorting --------- Co-authored-by: Antonin Delpeuch <[email protected]>
- Loading branch information
Showing
3 changed files
with
47 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
extensions/wikibase/src/org/openrefine/wikibase/commands/FetchManifestCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |