Skip to content
This repository has been archived by the owner on Jun 27, 2023. It is now read-only.

Commit

Permalink
close #6: subdomains now work. (JS bugs remain)
Browse files Browse the repository at this point in the history
  • Loading branch information
bblfish committed Nov 24, 2013
1 parent 625a303 commit 8270ec2
Show file tree
Hide file tree
Showing 15 changed files with 96 additions and 93 deletions.
11 changes: 6 additions & 5 deletions app/Global.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Date: 24/11/2013
*/

import controllers.routes
import controllers.{RwwConfiguration, routes}
import play.api._
import mvc.RequestHeader
import java.net._
Expand All @@ -14,16 +14,17 @@ object Global extends GlobalSettings {

val uri = req.getAbsoluteURI



if (uri.getHost != "localhost") {
if (uri.getPath.startsWith("/assets/") ||
uri.getPath.startsWith("/srv/")) {
super.onRouteRequest(req)
} else if (uri.getHost != RwwConfiguration.hostName) {
req.method match {
case "GET" => Some(controllers.ReadWriteWebApp.get(req.path))
case "POST" => Some(controllers.ReadWriteWebApp.post(req.path))
case "PATCH" => Some(controllers.ReadWriteWebApp.patch(req.path))
case "MKCOL" => Some(controllers.ReadWriteWebApp.mkcol(req.path))
case "HEAD" => Some(controllers.ReadWriteWebApp.head(req.path))
case "DELETE" => Some(controllers.ReadWriteWebApp.delete(req.path))

}
} else super.onRouteRequest(req)
}
Expand Down
6 changes: 5 additions & 1 deletion app/controllers/RwwConfiguration.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ object RwwConfiguration {

val RdfViewerHtmlTemplatePathKey = "rww.rdf.html.viewer.template.path"
val RootContainerPathKey = "rww.root.container.path"

val baseHostnameKey = "http.hostname"
/**
* we check the existence of the file because Resource.fromFile creates the file if it doesn't exist
* (the doc says it raises an exception but it's not the case)
Expand Down Expand Up @@ -39,5 +39,9 @@ object RwwConfiguration {
file.toPath.toAbsolutePath
}

val hostName: String = {
Play.current.configuration.getString(baseHostnameKey).getOrElse("localhost")
}


}
1 change: 0 additions & 1 deletion app/rww/ldp/RWWActor.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ object RWWebActor {
def cleanDots(path: String) = {
val split = path.split('/').toIndexedSeq

println(s"split($split).length="+split.length)
val sections = if (split.length>0 && split(0).isEmpty) split.tail else split
val fileName = sections.lastOption.getOrElse("")
var idot = fileName.indexOf('.')
Expand Down
36 changes: 16 additions & 20 deletions app/rww/play/ReadWriteWeb.scala
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ trait ReadWriteWeb[Rdf <: RDF] {
namedRes match {
case ldpr: LDPR[Rdf] => {
if (isStupidBrowser(request)) {
//SeeOther(controllers.routes.RDFViewer.htmlFor(request.path).toString())
SeeOther("https://localhost:8443/srv/rdfViewer?url="+URLEncoder.encode(uri.toString))
SeeOther(controllers.routes.RDFViewer.htmlFor(uri.toString).toString())
// SeeOther("https://localhost:8443/srv/rdfViewer?url="+URLEncoder.encode(uri.toString))
} else {
writerFor[Rdf#Graph](request).map { wr =>
result(200, wr, Map("Access-Control-Allow-Origin"-> "*") ++ link)(ldpr.relativeGraph)
Expand Down Expand Up @@ -144,15 +144,14 @@ trait ReadWriteWeb[Rdf <: RDF] {
* @param path
* @return
*/
def mkcol(path: String) = Action.async(rwwBodyParser) { request =>
def mkcol(path: String) = Action.async(rwwBodyParser) { implicit request =>
val correctedPath = if (!request.path.endsWith("/")) request.path else request.path.substring(0, request.path.length - 1)
val pathUri = new java.net.URI(correctedPath)
val coll = pathUri.resolve(".")
implicit val uri = request.getAbsoluteURI

def mk(graph: Option[Rdf#Graph]): Future[SimpleResult] = {
val path = correctedPath.toString.substring(coll.toString.length)
for (answer <- rwwActor.makeCollection(request, coll.toString, Some(path), graph))
for (answer <- rwwActor.makeCollection(coll.toString, Some(path), graph))
yield {
val res = Created("Created Collection at " + answer)
if (request.path == correctedPath) res
Expand All @@ -172,10 +171,10 @@ trait ReadWriteWeb[Rdf <: RDF] {
}
}

def put(path: String) = Action.async(rwwBodyParser) { request =>
implicit val uri = request.getAbsoluteURI
def put(path: String) = Action.async(rwwBodyParser) { implicit request =>

val future = for {
answer <- rwwActor.put(request, request.body)
answer <- rwwActor.put(request.body)
} yield {
Ok("Succeeded")
}
Expand All @@ -185,10 +184,9 @@ trait ReadWriteWeb[Rdf <: RDF] {
}
}

def patch(path: String) = Action.async(rwwBodyParser) { request =>
implicit val uri = request.getAbsoluteURI
def patch(path: String) = Action.async(rwwBodyParser) {implicit request =>
val future = for {
_ <- rwwActor.patch(request, request.body)
_ <- rwwActor.patch( request.body)
} yield {
Ok("Succeeded")
}
Expand All @@ -199,12 +197,11 @@ trait ReadWriteWeb[Rdf <: RDF] {
}


def post(path: String) = Action.async(rwwBodyParser) { request =>
implicit val uri = request.getAbsoluteURI
def post(path: String) = Action.async(rwwBodyParser) { implicit request =>

def postGraph(request: PlayApi.mvc.Request[RwwContent], rwwGraph: Option[Rdf#Graph]): Future[SimpleResult] = {
def postGraph(rwwGraph: Option[Rdf#Graph])(implicit request: PlayApi.mvc.Request[RwwContent]): Future[SimpleResult] = {
for {
location <- rwwActor.postGraph(request,
location <- rwwActor.postGraph(
request.headers.get("Slug").map(t => URLDecoder.decode(t, "UTF-8")),
rwwGraph
)
Expand All @@ -215,11 +212,11 @@ trait ReadWriteWeb[Rdf <: RDF] {

val future = request.body match {
case rwwGraph: GraphRwwContent[Rdf] => {
postGraph(request, Some(rwwGraph.graph))
postGraph(Some(rwwGraph.graph))
}
case rwwQuery: QueryRwwContent[Rdf] => {
for {
answer <- rwwActor.postQuery(request, request.path, rwwQuery, uri)
answer <- rwwActor.postQuery(request.path, rwwQuery)
} yield {
answer.fold(
graph =>
Expand All @@ -240,7 +237,7 @@ trait ReadWriteWeb[Rdf <: RDF] {
}
case BinaryRwwContent(file: TemporaryFile, mime: String) => {
for {
location <- rwwActor.postBinary(request, request.path,
location <- rwwActor.postBinary(request.path,
request.headers.get("Slug").map(t => URLDecoder.decode(t, "UTF-8")),
file,
MimeType(mime))
Expand All @@ -249,7 +246,7 @@ trait ReadWriteWeb[Rdf <: RDF] {
}
}
case emptyContent => {
postGraph(request, None)
postGraph(None)
}
// case _ => Ok("received content")
}
Expand All @@ -266,7 +263,6 @@ trait ReadWriteWeb[Rdf <: RDF] {
}

def delete(path: String) = Action.async { implicit request =>
implicit val uri = request.getAbsoluteURI
val future = for {
_ <- rwwActor.delete(request)
} yield {
Expand Down
51 changes: 26 additions & 25 deletions app/rww/play/ResourceManager.scala
Original file line number Diff line number Diff line change
Expand Up @@ -59,29 +59,29 @@ class ResourceMgr[Rdf <: RDF](base: URL, rww: RWW[Rdf], authn: AuthN, authz: WAC
else (path.substring(0, i + 1), path.substring(i + 1, path.length))
}

def patch(request: PlayRequestHeader, content: RwwContent)(implicit uri : java.net.URI): Future[Boolean] = {
def patch(content: RwwContent)(implicit request: PlayRequestHeader): Future[Boolean] = {
val path = request.path
content match {
case updatedQuery: PatchRwwContent[Rdf] => for {
_ <- auth(request, uri.toString, Method.write)
_ <- auth(request, request.getAbsoluteURI.toString, Method.write)
x <- rww.execute(patchLDPR(URI(path), updatedQuery.query, Map()))
} yield x
case _ => Future.failed(new Exception("PATCH requires application/sparql-update message content"))
}
}


def put(request: PlayRequestHeader, content: RwwContent)(implicit uri : java.net.URI) : Future[Rdf#URI] = {
def put(content: RwwContent)(implicit request: PlayRequestHeader) : Future[Rdf#URI] = {
val path = request.path
val (collection, file) = split(path)
if ("" == file) Future.failed(new Exception("Cannot do a PUT on a collection"))
else for {
_ <- auth(request, uri.toString, Method.write)
_ <- auth(request, request.getAbsoluteURI.toString, Method.write)
f <- content match {
//todo: arbitrarily for the moment we only allow a PUT of same type things graphs on graphs, and other on other
case grc: GraphRwwContent[Rdf] => {
rww.execute(for {
resrc <- getResource(URI(uri.toString))
resrc <- getResource(URI(request.getAbsoluteURI.toString))
x <- resrc match {
case ldpr: LDPR[Rdf] => updateLDPR(ldpr.location,remove=Seq((ANY,ANY,ANY)),add=grc.graph.toIterable)
case _ => throw new Error("yoyo")
Expand All @@ -91,7 +91,7 @@ class ResourceMgr[Rdf <: RDF](base: URL, rww: RWW[Rdf], authn: AuthN, authz: WAC
case BinaryRwwContent(tmpFile, mime) => {
rww.execute(
for {
resrc <- getResource(URI(uri.toString))
resrc <- getResource(URI(request.getAbsoluteURI.toString))
// if (resrc.isInstanceOf[BinaryResource[Rdf]])
} yield {
val b = resrc.asInstanceOf[BinaryResource[Rdf]]
Expand Down Expand Up @@ -158,10 +158,11 @@ class ResourceMgr[Rdf <: RDF](base: URL, rww: RWW[Rdf], authn: AuthN, authz: WAC
} yield x
}

def makeLDPR(request: PlayRequestHeader, collectionPath: String, content: Rdf#Graph, slug: Option[String])(implicit uri : java.net.URI): Future[Rdf#URI] = {
val uric: Rdf#URI = URI(collectionPath)
def makeLDPR( collectionPath: String, content: Rdf#Graph, slug: Option[String])
(implicit request: PlayRequestHeader): Future[Rdf#URI] = {
val uric: Rdf#URI = URI(request.getAbsoluteURI.toString)
for {
_ <- auth(request, uri.toString, Method.write)
_ <- auth(request, request.getAbsoluteURI.toString, Method.write)
x <- rww.execute(
for {
r <- createLDPR(uric, slug, content)
Expand All @@ -187,27 +188,28 @@ class ResourceMgr[Rdf <: RDF](base: URL, rww: RWW[Rdf], authn: AuthN, authz: WAC
* @param content
* @return
*/
def postGraph(request: PlayRequestHeader, slug: Option[String], content: Option[Rdf#Graph])( implicit uri : java.net.URI): Future[Rdf#URI] = {
def postGraph(slug: Option[String], content: Option[Rdf#Graph])(implicit request: PlayRequestHeader): Future[Rdf#URI] = {

// just on RWWPlay we can adopt the convention that if the object ends in a "/"
// then it is a collection, anything else is not a collection
val (collection, file) = split(request.path)
val g = content.getOrElse(emptyGraph)
if ("" == file) {
//todo: do we still need both createLDPR and createContainer if the type of action is determined by the content?
if (find(g, URI(""), rdf.typ, ldp.Container).hasNext)
makeCollection(request, collection, slug, content)
else makeLDPR(request, collection, g, slug)
makeCollection(collection, slug, content)
else makeLDPR(collection, g, slug)
} else {
Future.failed(WrongTypeException("POSTing on a LDPR that is not an LDPC is not defined")) //
}
}

def makeCollection(request: PlayRequestHeader, coll: String, slug: Option[String], content: Option[Rdf#Graph])( implicit uri : java.net.URI): Future[Rdf#URI] = {
def makeCollection(coll: String, slug: Option[String], content: Option[Rdf#Graph])(implicit request: PlayRequestHeader): Future[Rdf#URI] = {
for {
_ <- auth(request, uri.toString, Method.write)
_ <- auth(request, request.getAbsoluteURI.toString, Method.write)
x <- rww.execute {
for {
c <- createContainer(URI(coll), slug,
c <- createContainer(URI(request.getAbsoluteURI.toString), slug,
content.getOrElse(Graph.empty) union Graph(Triple(URI(""), rdf.typ, ldp.Container))
)
meta <- getMeta(c)
Expand All @@ -222,16 +224,17 @@ class ResourceMgr[Rdf <: RDF](base: URL, rww: RWW[Rdf], authn: AuthN, authz: WAC
} yield x
}

def postBinary(request: PlayRequestHeader, path: String, slug: Option[String], tmpFile: TemporaryFile, mime: MimeType)( implicit uri : java.net.URI): Future[Rdf#URI] = {
def postBinary(path: String, slug: Option[String], tmpFile: TemporaryFile, mime: MimeType)
( implicit request: PlayRequestHeader ): Future[Rdf#URI] = {
val (collection, file) = split(path)
val ldpc = URI(collection)
if ("" != file) Future.failed(WrongTypeException("Can only POST binary on a Collection"))
else
for {
_ <- auth(request, uri.toString, Method.write)
_ <- auth(request, request.getAbsoluteURI.toString, Method.write)
x <- rww.execute {
for {
b <- createBinary(URI(uri.toString), slug, mime)
b <- createBinary(URI(request.getAbsoluteURI.toString), slug, mime)
} yield {
Enumerator.fromFile(tmpFile.file)(ec)(b.write)
b.location
Expand All @@ -242,12 +245,10 @@ class ResourceMgr[Rdf <: RDF](base: URL, rww: RWW[Rdf], authn: AuthN, authz: WAC
}


def delete(request: PlayRequestHeader)( implicit uri : java.net.URI): Future[Unit] = {
val path = request.path
val (collection, file) = split(path)
def delete(implicit request: PlayRequestHeader): Future[Unit] = {
for {
_ <- auth(request, uri.toString, Method.write)
e <- rww.execute(deleteResource(URI(uri.toString)))
_ <- auth(request, request.getAbsoluteURI.toString, Method.write)
e <- rww.execute(deleteResource(URI(request.getAbsoluteURI.toString)))
} yield e
// rww.execute{
// for {
Expand All @@ -259,13 +260,13 @@ class ResourceMgr[Rdf <: RDF](base: URL, rww: RWW[Rdf], authn: AuthN, authz: WAC
// } yield { Unit }
}

def postQuery(implicit request: PlayRequestHeader, path: String, query: QueryRwwContent[Rdf], uri : java.net.URI): Future[Either3[Rdf#Graph, Rdf#Solutions, Boolean]] = {
def postQuery( path: String, query: QueryRwwContent[Rdf])(implicit request: PlayRequestHeader): Future[Either3[Rdf#Graph, Rdf#Solutions, Boolean]] = {
val (collection, file) = split(path)

import sparqlOps._
//clearly the queries could be simplified here.
for {
_ <- auth(request,uri.toString, Method.write)
_ <- auth(request,request.getAbsoluteURI.toString, Method.write)
e <-rww.execute {
if ("" != file)
fold(query.query)(
Expand Down
16 changes: 8 additions & 8 deletions public/ldp/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

<head>
<title>Primary Html file.</title>
<base href="https://localhost:8443/assets/ldp/">
<script type="text/javascript" src="lib/jquery-1.7.1.min.js"></script>
<script type="text/javascript" src="lib/underscore.js"></script>
<script type="text/javascript" src="lib/rdflib.js"></script>
<base href="/assets/ldp/">
<script type="text/javascript" src="/assets/ldp/lib/jquery-1.7.1.min.js"></script>
<script type="text/javascript" src="/assets/ldp/lib/underscore.js"></script>
<script type="text/javascript" src="/assets/ldp/lib/rdflib.js"></script>
<script type="text/javascript">
var LDP = $rdf.Namespace("http://www.w3.org/ns/ldp#");
var RDF = $rdf.Namespace("http://www.w3.org/1999/02/22-rdf-syntax-ns#");
Expand All @@ -16,12 +16,12 @@
// Get the graph related to Base Uri.
graphsCache = {};
var baseUri = window.location.href;
var viewerUri = 'https://localhost:8443/assets/ldp/viewer.ttl';
var viewerUri = '/assets/ldp/viewer.ttl';
var graph = graphsCache[baseUri] = new $rdf.IndexedFormula();
var fetch = $rdf.fetcher(graph);
fetch.nowOrWhenFetched(baseUri, undefined, function () {
// Load the main view.
loadScript("https://localhost:8443/assets/ldp/js/bodyViewer.js", null);
loadScript("/assets/ldp/js/bodyViewer.js", null);

// Get ressource type.
$rdf.types = graph.each( $rdf.sym(baseUri), RDF("type"));
Expand All @@ -42,13 +42,13 @@

function loadScript () {
var scriptLoad = document.createElement("script");
scriptLoad.src = "js/loadScript.js";
scriptLoad.src = "/assets/ldp/js/loadScript.js";
document.head.appendChild(scriptLoad);
}

function loadCSS () {
var cssLoad = document.createElement("script");
cssLoad.src = "js/loadCSS.js";
cssLoad.src = "/assets/ldp/js/loadCSS.js";
document.head.appendChild(cssLoad);
}

Expand Down
16 changes: 8 additions & 8 deletions public/ldp/js/bodyViewer.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
var templateURI = "https://localhost:8443/assets/ldp/templates/bodyTemplate.html";
var templateURI = "/assets/ldp/templates/bodyTemplate.html";
var tab = {};
$.get(templateURI, function(data) {
// Load related CSS.
loadCSS("css/blueprint.css");
loadCSS("css/common.css");
loadCSS("css/font-awesome.min.css");
loadCSS("css/buttons.css");
loadCSS("css/style.css");
loadCSS("/assets/ldp/css/blueprint.css");
loadCSS("/assets/ldp/css/common.css");
loadCSS("/assets/ldp/css/font-awesome.min.css");
loadCSS("/assets/ldp/css/buttons.css");
loadCSS("/assets/ldp/css/style.css");

// Load Html.
var template = _.template(data, tab);
Expand All @@ -15,8 +15,8 @@ $.get(templateURI, function(data) {
$('body').append(template);

// Load the menu.
loadScript("https://localhost:8443/assets/ldp/js/menuViewer.js", null);
loadScript("/assets/ldp/js/menuViewer.js", null);
// Load utils js.
loadScript("https://localhost:8443/assets/ldp/js/utils.js", null);
loadScript("/assets/ldp/js/utils.js", null);

}, 'html');

0 comments on commit 8270ec2

Please sign in to comment.