Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Resolve context path issue when deploying to servlet container
Browse files Browse the repository at this point in the history
Prior to this update, when deploying to a servlet container:
- the style.css does not resolve
- the favicon.ico does not resolve
- the "Linked Data Fragments" image does not resolve
- the contexts of datasource do not resolve

Resolves: LinkedDataFragments#52
Andrew Woods committed Oct 25, 2019
1 parent 8052ca2 commit 7759b9f
Showing 4 changed files with 38 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -4,6 +4,7 @@
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collection;
@@ -15,10 +16,10 @@
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.IOUtils;
import org.apache.http.HttpHeaders;
import org.apache.jena.query.ARQ;
import org.apache.jena.riot.Lang;
import org.apache.jena.sys.JenaSystem;
import org.linkeddatafragments.config.ConfigReader;
import org.linkeddatafragments.datasource.DataSourceFactory;
import org.linkeddatafragments.datasource.DataSourceTypesRegistry;
@@ -30,6 +31,7 @@
import org.linkeddatafragments.fragments.ILinkedDataFragment;
import org.linkeddatafragments.fragments.ILinkedDataFragmentRequest;
import org.linkeddatafragments.util.MIMEParse;
import org.linkeddatafragments.views.HtmlTriplePatternFragmentWriterImpl;
import org.linkeddatafragments.views.ILinkedDataFragmentWriter;
import org.linkeddatafragments.views.LinkedDataFragmentWriterFactory;

@@ -106,6 +108,8 @@ public void init(ServletConfig servletConfig) throws ServletException {
MIMEParse.register(Lang.NTRIPLES.getHeaderString());
MIMEParse.register(Lang.JSONLD.getHeaderString());
MIMEParse.register(Lang.TTL.getHeaderString());

HtmlTriplePatternFragmentWriterImpl.setContextPath(servletConfig.getServletContext().getContextPath());
} catch (Exception e) {
throw new ServletException(e);
}
@@ -163,6 +167,22 @@ private IDataSource getDataSource(HttpServletRequest request) throws DataSourceN
*/
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException {

// Ensure that 'assets' (favicon, css) resolve
int fileNamePos = request.getRequestURI().toLowerCase().lastIndexOf("assets/");
if (fileNamePos > 0) {
try {
String fileName = request.getRequestURI().substring(fileNamePos - 1);
InputStream in = LinkedDataFragmentServlet.class.getResourceAsStream(fileName);
if (in != null) {
IOUtils.copy(in, response.getOutputStream());
}
return;
} catch (IOException ioe) {
log("Should never happen", ioe);
}
}

ILinkedDataFragment fragment = null;
try {
// do conneg
Original file line number Diff line number Diff line change
@@ -36,7 +36,16 @@ public class HtmlTriplePatternFragmentWriterImpl extends TriplePatternFragmentWr
private final Template notfoundTemplate;
private final Template errorTemplate;

private final String HYDRA = "http://www.w3.org/ns/hydra/core#";
private final String HYDRA = "http://www.w3.org/ns/hydra/core#";

private static String contextPath;

public static void setContextPath(String path) {
contextPath = path;
if (!contextPath.endsWith("/")) {
contextPath += "/";
}
}

/**
*
@@ -72,6 +81,7 @@ public void writeFragment(ServletOutputStream outputStream, IDataSource datasour
Map data = new HashMap();

// base.ftl.html
data.put("homePath", (contextPath != null ? contextPath : ""));
data.put("assetsPath", "assets/");
data.put("header", datasource.getTitle());
data.put("date", new Date());
@@ -124,6 +134,7 @@ public void writeFragment(ServletOutputStream outputStream, IDataSource datasour
@Override
public void writeNotFound(ServletOutputStream outputStream, HttpServletRequest request) throws Exception {
Map data = new HashMap();
data.put("homePath", (contextPath != null ? contextPath : ""));
data.put("assetsPath", "assets/");
data.put("datasources", getDatasources());
data.put("date", new Date());
@@ -135,6 +146,7 @@ public void writeNotFound(ServletOutputStream outputStream, HttpServletRequest r
@Override
public void writeError(ServletOutputStream outputStream, Exception ex) throws Exception {
Map data = new HashMap();
data.put("homePath", (contextPath != null ? contextPath : ""));
data.put("assetsPath", "assets/");
data.put("date", new Date());
data.put("error", ex);
6 changes: 3 additions & 3 deletions src/main/resources/views/base.ftl.html
Original file line number Diff line number Diff line change
@@ -5,16 +5,16 @@
<head>
<meta charset="utf-8">
<title>${ title!header!"Linked Data Fragments Server" }</title>
<link rel="shortcut icon" href="assets/favicon.ico">
<link rel="shortcut icon" href="${ assetsPath }favicon.ico">
<link rel="stylesheet" href="${ assetsPath }style.css" />
<link rel="stylesheet" href="//fonts.googleapis.com/css?family=Open+Sans:700italic,400,700|Droid+Sans+Mono" type="text/css" />
<meta name="viewport" content="width=device-width,minimum-scale=1,maximum-scale=1">
</head>
<body>
<header>
<h1><a href="/">${header!"Linked Data Fragments Server"}</a></h1>
<h1><a href="${homePath}">Linked Data Fragments Server</a></h1>
<figure class="logo">
<a href="http://linkeddatafragments.org/"><img src="${ assetsPath }logo.svg" alt="Linked Data Fragments" /></a>
<a href="http://linkeddatafragments.org/"><img src="https://linkeddatafragments.org/images/logo.svg" alt="Linked Data Fragments" /></a>
</figure>
</header>
<main>
2 changes: 1 addition & 1 deletion src/main/resources/views/index.ftl.html
Original file line number Diff line number Diff line change
@@ -7,7 +7,7 @@ <h2>Available datasets</h2>
<dl class="datasets">
<#if datasources??>
<#list datasources?keys as datasourceName>
<dt><a href="${datasourceName}">${datasources[datasourceName].getTitle() }</a></dt>
<dt><a href="${homePath}${datasourceName}">${datasources[datasourceName].getTitle() }</a></dt>
<dd>${ datasources[datasourceName].getDescription()!"" }</dd>
</#list>
</#if>

0 comments on commit 7759b9f

Please sign in to comment.