Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add MARC download to API #1748

Merged
merged 2 commits into from May 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion web/app/controllers/resources/Accept.java
Expand Up @@ -25,7 +25,8 @@ enum Format {
RDF_XML("rdf", "application/rdf+xml", "application/xml", "text/xml"), //
N_TRIPLE("nt", "application/n-triples", "text/plain"), //
TURTLE("ttl", "text/turtle", "application/x-turtle"), //
RSS("rss", "application/rss+xml");
RSS("rss", "application/rss+xml"), //
MARC_XML("mrcx", "application/marcxml+xml");

String[] types;
String queryParamString;
Expand Down
20 changes: 17 additions & 3 deletions web/app/controllers/resources/Application.java
Expand Up @@ -2,10 +2,11 @@

package controllers.resources;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.text.Collator;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
Expand Down Expand Up @@ -101,6 +102,8 @@ public class Application extends Controller {
new File("conf/resources.conf") : new File("conf/resources.conf_template") ;
public final static Config CONFIG =
ConfigFactory.parseFile(RESOURCES_CONF).resolve();
public final static String MARC_XML_API = CONFIG.getString("mrcx.api");


static Form<String> queryForm = Form.form(String.class);

Expand Down Expand Up @@ -483,6 +486,17 @@ public static Promise<Result> resource(final String id, String format) {
? ok(details.render(CONFIG, result.toString(), id))
: notFound(details.render(CONFIG, "", id));
}
boolean marcxmlRequested =
responseFormat.equals(Accept.Format.MARC_XML.queryParamString);
if (marcxmlRequested) {
URLConnection conn = new URL(MARC_XML_API + id).openConnection();
String marcxml;
try (BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8))) {
marcxml = reader.lines().collect(Collectors.joining("\n"));
}
return marcxml.isEmpty() ? internalServerError("No content")
: ok(marcxml).as(Format.MARC_XML.types[0] + "; charset=utf-8");
}
return result != null ? responseFor(result, responseFormat)
: notFound("\"Not found: " + id + "\"");
});
Expand Down
4 changes: 2 additions & 2 deletions web/app/views/details.scala.html
Expand Up @@ -33,7 +33,7 @@ <h1>@((doc\"title").asOpt[String].getOrElse(q))@for(corporateBodyForTitle <- (do
@defining((doc\"almaMmsId").asOpt[String].getOrElse((doc\"hbzId").asOpt[String].getOrElse(q))){ id =>
<dt>@tags.star_button(id) Titeldetails:
<small style='float:right'>
<span class="source-link"><a title="Quelldaten anzeigen" href='@if((doc\"almaMmsId").asOpt[String].isDefined){@Application.CONFIG.getString("mrcx.api")/@id}else{@Application.CONFIG.getString("hbz01.api")/@id}'><small>&#12296;M&#12297;</small></a></span>
<a title="Quelldaten anzeigen" href='@if((doc\"almaMmsId").asOpt[String].isDefined){@Application.CONFIG.getString("mrcx.api")@id}else{@Application.CONFIG.getString("hbz01.api")/@id}'><small>&#12296;M&#12297;</small></a>
<a title="JSON-LD-Indexdaten anzeigen" href='@resources.routes.Application.resourceDotFormat(id, "json")'><img class='json-ld-icon' src='@routes.Assets.at("images/json-ld.png")'></a>
</small>
</dt>
Expand Down Expand Up @@ -83,4 +83,4 @@ <h1>@((doc\"title").asOpt[String].getOrElse(q))@for(corporateBodyForTitle <- (do
@if(!q.trim.isEmpty) {@map_credits()}
}
}
}
}
2 changes: 1 addition & 1 deletion web/conf/resources.conf_template
@@ -1,6 +1,6 @@
host="http://lobid.org"
hbz01.api="http://lobid.org/hbz01"
mrcx.api="https://alma.lobid.org/marcxml"
mrcx.api="https://alma.lobid.org/marcxml/"
orgs.api="http://lobid.org/organisations/"

isil2opac_hbzid = "https://raw.githubusercontent.com/hbz/link-templates/master/isil2opac_hbzid.json"
Expand Down
7 changes: 0 additions & 7 deletions web/public/stylesheets/resources.css
Expand Up @@ -310,10 +310,3 @@ h5 {
height: 1.4em
}

.source-link a {
visibility:hidden;
}
.source-link:hover a {
visibility:visible;
text-decoration: none;
}
3 changes: 3 additions & 0 deletions web/test/tests/AcceptIntegrationTest.java
Expand Up @@ -58,10 +58,12 @@ public static Collection<Object[]> data() {
{ fakeRequest(GET, "/resources/990210781980206441").header("Accept", "application/pdf"), /*->*/ "application/json" },
// get, other formats as query param:
{ fakeRequest(GET, "/resources/990210781980206441?format=html"), /*->*/ "text/html" },
{ fakeRequest(GET, "/resources/990210781980206441?format=mrcx"), /*->*/ "application/marcxml+xml" },
{ fakeRequest(GET, "/resources/990210781980206441?format=rdf"), /*->*/ "application/rdf+xml" },
{ fakeRequest(GET, "/resources/990210781980206441?format=ttl"), /*->*/ "text/turtle" },
{ fakeRequest(GET, "/resources/990210781980206441?format=nt"), /*->*/ "application/n-triples" },
// get, formats as URL path elem:
{ fakeRequest(GET, "/resources/990210781980206441.mrcx"), /*->*/ "application/marcxml+xml" },
{ fakeRequest(GET, "/resources/990210781980206441.html"), /*->*/ "text/html" },
{ fakeRequest(GET, "/resources/990210781980206441.json"), /*->*/ "application/json" },
{ fakeRequest(GET, "/resources/990210781980206441.rdf"), /*->*/ "application/rdf+xml" },
Expand All @@ -71,6 +73,7 @@ public static Collection<Object[]> data() {
{ fakeRequest(GET, "/resources/990210781980206441").header("Accept", "application/json"), /*->*/ "application/json" },
{ fakeRequest(GET, "/resources/990210781980206441").header("Accept", "text/html"), /*->*/ "text/html" },
{ fakeRequest(GET, "/resources/990210781980206441").header("Accept", "text/xml"), /*->*/ "application/rdf+xml" },
{ fakeRequest(GET, "/resources/990210781980206441").header("Accept", "application/marcxml+xml"), /*->*/ "application/marcxml+xml" },
{ fakeRequest(GET, "/resources/990210781980206441").header("Accept", "application/xml"), /*->*/ "application/rdf+xml" },
{ fakeRequest(GET, "/resources/990210781980206441").header("Accept", "application/rdf+xml"), /*->*/ "application/rdf+xml" },
{ fakeRequest(GET, "/resources/990210781980206441").header("Accept", "text/turtle"), /*->*/ "text/turtle" },
Expand Down