Skip to content

Commit

Permalink
Merge pull request #56 from rightsstatements/develop
Browse files Browse the repository at this point in the history
Error documents for human readable representations
  • Loading branch information
anarchivist committed Apr 29, 2016
2 parents fbf9b9b + 9dff29f commit 1430efb
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 83 deletions.
47 changes: 47 additions & 0 deletions app/ErrorHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import com.google.inject.Inject;
import play.Configuration;
import play.Environment;
import play.Logger;
import play.api.OptionalSourceMapper;
import play.http.DefaultHttpErrorHandler;
import play.libs.F;
import play.mvc.Http;
import play.mvc.Result;
import play.mvc.Results;
import play.api.routing.Router;

import services.LayoutProvider;

import javax.inject.Provider;
import java.io.IOException;

/**
* Created by fo on 29.04.16.
*/
public class ErrorHandler extends DefaultHttpErrorHandler {

@Inject
private LayoutProvider layoutProvider;

@Inject
public ErrorHandler(Configuration configuration, Environment environment,
OptionalSourceMapper sourceMapper, Provider<Router> routes) {
super(configuration, environment, sourceMapper, routes);
}

@Override
public F.Promise<Result> onNotFound(Http.RequestHeader request, java.lang.String message) {

Result result;
try {
result = Results.notFound(layoutProvider.getTemplateLoader().sourceAt("en/404.html").content()).as("text/html");
} catch (IOException e) {
Logger.error(e.toString());
result = Results.notFound("Not Found");
}

return F.Promise.pure(result);

}

}
38 changes: 30 additions & 8 deletions app/controllers/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public Result getVocabData(String version, String extension) {
Model vocab = getVocabModel(version);

if (vocab.isEmpty()) {
return notFound();
return notFoundPage();
}

MimeType mimeType = getMimeType(request(), extension);
Expand All @@ -94,7 +94,7 @@ public Result getVocabPage(String version, String language) throws IOException {
Locale locale = getLocale(request(), language);

if (vocab.isEmpty()) {
return notFound();
return notFoundPage();
}

response().setHeader("Link", "<".concat(routes.Application.getVocabPage(version, null)
Expand All @@ -109,7 +109,7 @@ public Result getStatement(String id, String version) {

if (!request().queryString().isEmpty()) {
setAlternates(request(), id, version, true);
return status(406);
return notAcceptablePage();
} else if (request().accepts("text/html")) {
Locale locale = getLocale(request(), null);
return redirect(routes.Application.getStatementPage(id, version, locale.getLanguage()).absoluteURL(request()));
Expand All @@ -123,13 +123,13 @@ public Result getStatementData(String id, String version, String extension) {

if (!request().queryString().isEmpty()) {
setAlternates(request(), id, version, false);
return status(406);
return notAcceptablePage();
}

Model rightsStatement = getStatementModel(id, version);

if (rightsStatement.isEmpty()) {
return notFound();
return notFoundPage();
}

MimeType mimeType = getMimeType(request(), extension);
Expand All @@ -149,7 +149,7 @@ public Result getStatementPage(String id, String version, String language) throw
Locale locale = getLocale(request(), language);

if (rightsStatement.isEmpty()) {
return notFound();
return notFoundPage();
}

response().setHeader("Link", "<".concat(routes.Application.getStatementPage(id, version, null)
Expand All @@ -176,7 +176,7 @@ public Result getCollectionData(String id, String version, String extension) {
Model collection = getCollectionModel(id, version);

if (collection.isEmpty()) {
return notFound();
return notFoundPage();
}

MimeType mimeType = getMimeType(request(), extension);
Expand All @@ -196,7 +196,7 @@ public Result getCollectionPage(String id, String version, String language) thro
Locale locale = getLocale(request(), language);

if (collection.isEmpty()) {
return notFound();
return notFoundPage();
}

response().setHeader("Link", "<".concat(routes.Application.getCollectionPage(id, version, null)
Expand All @@ -208,6 +208,28 @@ public Result getCollectionPage(String id, String version, String language) thro

}

public Result notFoundPage() {

try {
return notFound(layoutProvider.getTemplateLoader().sourceAt("en/404.html").content()).as("text/html");
} catch (IOException e) {
Logger.error(e.toString());
return notFound("Not Found");
}

}

public Result notAcceptablePage() {

try {
return status(406, layoutProvider.getTemplateLoader().sourceAt("en/406.html").content()).as("text/html");
} catch (IOException e) {
Logger.error(e.toString());
return status(406, "Not Acceptable");
}

}

private Result getData(Model model, MimeType mimeType) {

OutputStream result = new ByteArrayOutputStream();
Expand Down
9 changes: 6 additions & 3 deletions app/services/GitLayoutProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

import com.github.jknack.handlebars.io.URLTemplateLoader;
import com.google.inject.Singleton;
import com.google.inject.Inject;
import org.apache.commons.io.FileUtils;
import org.apache.jena.atlas.RuntimeIOException;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import play.Configuration;
import play.Logger;
import play.Play;

import java.io.File;
import java.io.IOException;
Expand All @@ -21,7 +21,7 @@
@Singleton
public class GitLayoutProvider implements LayoutProvider {

private Configuration gitSource = Play.application().configuration().getConfig("source.site.git");
private Configuration gitSource;

public URLTemplateLoader getTemplateLoader() {
URLTemplateLoader urlTemplateLoader = new ResourceTemplateLoader();
Expand All @@ -42,7 +42,10 @@ protected URL getResource(final String location) throws IOException {

}

public GitLayoutProvider() {
@Inject
public GitLayoutProvider(Configuration configuration) {

gitSource = configuration.getConfig("source.site.git");

try {
Logger.debug("Checking out template branch ".concat(gitSource.getString("branch")));
Expand Down
7 changes: 4 additions & 3 deletions app/services/GitVocabProvider.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package services;

import com.google.inject.Inject;
import com.google.inject.Singleton;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
Expand All @@ -18,7 +19,6 @@
import org.eclipse.jgit.treewalk.filter.PathSuffixFilter;
import play.Configuration;
import play.Logger;
import play.Play;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
Expand All @@ -35,9 +35,10 @@ public class GitVocabProvider implements VocabProvider {

private Model vocab = ModelFactory.createDefaultModel();

public GitVocabProvider() {
@Inject
public GitVocabProvider(Configuration configuration) {

Configuration gitSource = Play.application().configuration().getConfig("source.data.git");
Configuration gitSource = configuration.getConfig("source.data.git");

try {

Expand Down
69 changes: 0 additions & 69 deletions public/handlebars/statement.hbs

This file was deleted.

0 comments on commit 1430efb

Please sign in to comment.