Skip to content

Commit a66684e

Browse files
author
Andrew Woods
committed
Resolve context path issue when deploying to servlet container
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
1 parent 8052ca2 commit a66684e

File tree

4 files changed

+38
-6
lines changed

4 files changed

+38
-6
lines changed

src/main/java/org/linkeddatafragments/servlet/LinkedDataFragmentServlet.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.io.File;
55
import java.io.FileReader;
66
import java.io.IOException;
7+
import java.io.InputStream;
78
import java.nio.charset.StandardCharsets;
89
import java.util.ArrayList;
910
import java.util.Collection;
@@ -15,10 +16,10 @@
1516
import javax.servlet.http.HttpServletRequest;
1617
import javax.servlet.http.HttpServletResponse;
1718

19+
import org.apache.commons.io.IOUtils;
1820
import org.apache.http.HttpHeaders;
1921
import org.apache.jena.query.ARQ;
2022
import org.apache.jena.riot.Lang;
21-
import org.apache.jena.sys.JenaSystem;
2223
import org.linkeddatafragments.config.ConfigReader;
2324
import org.linkeddatafragments.datasource.DataSourceFactory;
2425
import org.linkeddatafragments.datasource.DataSourceTypesRegistry;
@@ -30,6 +31,7 @@
3031
import org.linkeddatafragments.fragments.ILinkedDataFragment;
3132
import org.linkeddatafragments.fragments.ILinkedDataFragmentRequest;
3233
import org.linkeddatafragments.util.MIMEParse;
34+
import org.linkeddatafragments.views.HtmlTriplePatternFragmentWriterImpl;
3335
import org.linkeddatafragments.views.ILinkedDataFragmentWriter;
3436
import org.linkeddatafragments.views.LinkedDataFragmentWriterFactory;
3537

@@ -106,6 +108,8 @@ public void init(ServletConfig servletConfig) throws ServletException {
106108
MIMEParse.register(Lang.NTRIPLES.getHeaderString());
107109
MIMEParse.register(Lang.JSONLD.getHeaderString());
108110
MIMEParse.register(Lang.TTL.getHeaderString());
111+
112+
HtmlTriplePatternFragmentWriterImpl.setContextPath(servletConfig.getServletContext().getContextPath());
109113
} catch (Exception e) {
110114
throw new ServletException(e);
111115
}
@@ -163,6 +167,22 @@ private IDataSource getDataSource(HttpServletRequest request) throws DataSourceN
163167
*/
164168
@Override
165169
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException {
170+
171+
// Ensure that 'assets' (favicon, css) resolve
172+
int fileNamePos = request.getRequestURI().toLowerCase().lastIndexOf("assets/");
173+
if (fileNamePos > 0) {
174+
try {
175+
String fileName = request.getRequestURI().substring(fileNamePos - 1);
176+
InputStream in = LinkedDataFragmentServlet.class.getResourceAsStream(fileName);
177+
if (in != null) {
178+
IOUtils.copy(in, response.getOutputStream());
179+
}
180+
return;
181+
} catch (IOException ioe) {
182+
log("Should never happen", ioe);
183+
}
184+
}
185+
166186
ILinkedDataFragment fragment = null;
167187
try {
168188
// do conneg

src/main/java/org/linkeddatafragments/views/HtmlTriplePatternFragmentWriterImpl.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,16 @@ public class HtmlTriplePatternFragmentWriterImpl extends TriplePatternFragmentWr
3636
private final Template notfoundTemplate;
3737
private final Template errorTemplate;
3838

39-
private final String HYDRA = "http://www.w3.org/ns/hydra/core#";
39+
private final String HYDRA = "http://www.w3.org/ns/hydra/core#";
40+
41+
private static String contextPath;
42+
43+
public static void setContextPath(String path) {
44+
contextPath = path;
45+
if (!contextPath.endsWith("/")) {
46+
contextPath += "/";
47+
}
48+
}
4049

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

7483
// base.ftl.html
84+
data.put("homePath", (contextPath != null ? contextPath : ""));
7585
data.put("assetsPath", "assets/");
7686
data.put("header", datasource.getTitle());
7787
data.put("date", new Date());
@@ -124,6 +134,7 @@ public void writeFragment(ServletOutputStream outputStream, IDataSource datasour
124134
@Override
125135
public void writeNotFound(ServletOutputStream outputStream, HttpServletRequest request) throws Exception {
126136
Map data = new HashMap();
137+
data.put("homePath", (contextPath != null ? contextPath : ""));
127138
data.put("assetsPath", "assets/");
128139
data.put("datasources", getDatasources());
129140
data.put("date", new Date());
@@ -135,6 +146,7 @@ public void writeNotFound(ServletOutputStream outputStream, HttpServletRequest r
135146
@Override
136147
public void writeError(ServletOutputStream outputStream, Exception ex) throws Exception {
137148
Map data = new HashMap();
149+
data.put("homePath", (contextPath != null ? contextPath : ""));
138150
data.put("assetsPath", "assets/");
139151
data.put("date", new Date());
140152
data.put("error", ex);

src/main/resources/views/base.ftl.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@
55
<head>
66
<meta charset="utf-8">
77
<title>${ title!header!"Linked Data Fragments Server" }</title>
8-
<link rel="shortcut icon" href="assets/favicon.ico">
8+
<link rel="shortcut icon" href="${ assetsPath }favicon.ico">
99
<link rel="stylesheet" href="${ assetsPath }style.css" />
1010
<link rel="stylesheet" href="//fonts.googleapis.com/css?family=Open+Sans:700italic,400,700|Droid+Sans+Mono" type="text/css" />
1111
<meta name="viewport" content="width=device-width,minimum-scale=1,maximum-scale=1">
1212
</head>
1313
<body>
1414
<header>
15-
<h1><a href="/">${header!"Linked Data Fragments Server"}</a></h1>
15+
<h1><a href="${homePath}">Linked Data Fragments Server</a></h1>
1616
<figure class="logo">
17-
<a href="http://linkeddatafragments.org/"><img src="${ assetsPath }logo.svg" alt="Linked Data Fragments" /></a>
17+
<a href="http://linkeddatafragments.org/"><img src="https://linkeddatafragments.org/images/logo.svg" alt="Linked Data Fragments" /></a>
1818
</figure>
1919
</header>
2020
<main>

src/main/resources/views/index.ftl.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ <h2>Available datasets</h2>
77
<dl class="datasets">
88
<#if datasources??>
99
<#list datasources?keys as datasourceName>
10-
<dt><a href="${datasourceName}">${datasources[datasourceName].getTitle() }</a></dt>
10+
<dt><a href="${homePath}${datasourceName}">${datasources[datasourceName].getTitle() }</a></dt>
1111
<dd>${ datasources[datasourceName].getDescription()!"" }</dd>
1212
</#list>
1313
</#if>

0 commit comments

Comments
 (0)