Created
January 30, 2012 12:54
-
-
Save bblfish/1704255 to your computer and use it in GitHub Desktop.
This shows how one should be able to use Jena readers (re issue JENA-203)
https://issues.apache.org/jira/browse/JENA-203
but in fact this throws an exception
ERROR (WebFetcher.scala:59) : org.xml.sax.SAXParseException; systemId: http://bblfish.net/people
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright (c) 2012 Henry Story (bblfish.net) | |
* under the MIT licence defined at | |
* http://www.opensource.org/licenses/mit-license.html | |
* | |
* Permission is hereby granted, free of charge, to any person obtaining a copy of | |
* this software and associated documentation files (the "Software"), to deal in the | |
* Software without restriction, including without limitation the rights to use, copy, | |
* modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, | |
* and to permit persons to whom the Software is furnished to do so, subject to the | |
* following conditions: | |
* | |
* The above copyright notice and this permission notice shall be included in all | |
* copies or substantial portions of the Software. | |
* | |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, | |
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A | |
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT | |
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | |
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE | |
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
*/ | |
package org.w3c.readwriteweb.cache | |
import com.ning.http.client.AsyncHandler.STATE | |
import com.ning.http.client.AsyncHandler | |
import com.ning.http.client.AsyncHttpClient | |
import com.ning.http.client.HttpResponseBodyPart | |
import com.ning.http.client.HttpResponseHeaders | |
import com.ning.http.client.HttpResponseStatus | |
import java.net.URL | |
import org.w3.readwriteweb.Lang | |
import java.io._ | |
import com.weiglewilczek.slf4s.Logging | |
import com.hp.hpl.jena.rdf.model.{RDFReader, ModelFactory, Model} | |
import scalaz.Zero | |
import java.util.{Collections, ArrayList} | |
import com.hp.hpl.jena.sparql.function.library.e | |
/** | |
* @author bblfish | |
* @created 27/01/2012 | |
*/ | |
class URLFetcher(url: URL) extends AsyncHandler[Model]() with Logging { | |
import scala.collection.JavaConverters._ | |
var model: Model = _ | |
var reader: RDFReader = _ | |
var base: String = _ | |
var status: HttpResponseStatus = _ | |
var counter = 0 | |
def onThrowable(t: Throwable) { | |
logger.error(t.getMessage) | |
} | |
def onBodyPartReceived(bodyPart: HttpResponseBodyPart) = { | |
logger.info("body part n."+counter) | |
counter += 1 | |
reader.read(model,new ByteArrayInputStream(bodyPart.getBodyPartBytes),base) | |
STATE.CONTINUE | |
} | |
def onStatusReceived(responseStatus: HttpResponseStatus) = { | |
status = responseStatus | |
STATE.CONTINUE | |
} | |
def onHeadersReceived(headers: HttpResponseHeaders) = { | |
if (status.getStatusCode < 200 && status.getStatusCode > 204) { | |
STATE.CONTINUE | |
} else { | |
val typeHdr = nullSquisher( headers.getHeaders.get("Content-Type") ).asScala | |
logger.info("Content-Types ➤ " + typeHdr.mkString(" ➤ ")) | |
val mime = typeHdr.flatMap(mime => Lang(mime.split(";")(0))).headOption | |
val locHdr = nullSquisher ( headers.getHeaders.get("Content-Location")).asScala | |
logger.info("Content-Location ➤ " + locHdr.mkString(" ➤ ")) | |
val location = locHdr.headOption match { | |
case Some(loc) => new URL(url, loc) | |
case None => new URL(url.getProtocol, url.getAuthority, url.getPort, url.getPath) | |
} | |
base = location.toString | |
model = ModelFactory.createDefaultModel() | |
val lang = mime getOrElse Lang.default | |
reader = model.getReader(lang.jenaLang) | |
STATE.CONTINUE | |
} | |
} | |
def onCompleted() = model | |
def nullSquisher[T](body: => T)(implicit z: Zero[T]): T = | |
try { | |
val res = body; | |
if (res == null) z.zero else res | |
} catch { | |
case _ => { | |
logger.warn("cought ",e) | |
z.zero | |
} | |
} | |
implicit def JavaListZero[A]: Zero[java.util.List[A]] = new Zero[java.util.List[A]] { val zero = Collections.emptyList[A]() } | |
} | |
object ModelCache extends App { | |
val url = new URL("http://bblfish.net/people/henry/card.rdf") | |
val client = new AsyncHttpClient | |
val response = client.prepareGet(url.toString). | |
setFollowRedirects(true). | |
execute(new URLFetcher(url)).get() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment