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

Resolve context path issue when deploying to servlet container #59

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 += "/";
}
}

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

// base.ftl.html
data.put("assetsPath", "assets/");
data.put("homePath", (contextPath != null ? contextPath : ""));
data.put("assetsPath", contextPath + "assets/");
data.put("header", datasource.getTitle());
data.put("date", new Date());

Expand Down Expand Up @@ -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());
Expand All @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions src/main/resources/views/base.ftl.html
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/views/index.ftl.html
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down