/*
Copyright (c) Tom Morris, 2008. Licenced under the GNU General Public License.
This is the code that was running the FOAF part of the SemanticCamp signup routine.
It is a Java servlet that I set up to run on Tomcat to basically run SPARQL queries
over FOAF documents and return back an XML file containing the results, which the
PHP application would then use.
It worked reasonably well - during the week that it was online, there were two
errors - either because the FOAF document wasn't found or because the queries
failed.
It doesn't work perfectly - the queries were written to give back no result rather
than a wrong result - that is why the SELECT has a isURI filter on it - because
bnodes were causing havoc.
I'll release the hCard code shortly.
The reason I'm releasing this code is because I want to prompt the RDF community into
improving it and making simple, off-the-shelf solutions to common "portable social
networks"/"data portability" use cases (and I want the same to happen with microformats
and other approaches). Running, tested, open source code over specifications.
This code was made with NetBeans 6, so has the usual Java IDE fluffiness.
*/
/*
* SemanticCampProfileDiscoveryServlet.java
*
* Created on December 30, 2007, 11:23 AM
*/
package SemanticCamp;
import java.io.*;
import java.net.*;
import javax.servlet.*;
import javax.servlet.http.*;
import com.hp.hpl.jena.query.*;
import com.hp.hpl.jena.rdf.model.*;
/**
*
* @author tom
* @version
*/
public class SemanticCampProfileDiscoveryServlet extends HttpServlet {
/** Processes requests for both HTTP GET and POST methods.
* @param request servlet request
* @param response servlet response
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/plain;charset=UTF-8");
PrintWriter out = response.getWriter();
String model = request.getParameter("url");
String subject = request.getParameter("subject");
Model r = ModelFactory.createDefaultModel(ModelFactory.Standard);
Model r2 = ModelFactory.createDefaultModel(ModelFactory.Standard);
r.read(model);
String describeQueryString;
if (subject == null) {
describeQueryString = "DESCRIBE ?node WHERE { " +
"?x ?node . " +
"?node a . " +
"}";
} else {
describeQueryString = "DESCRIBE <" + subject + ">";
}
Query describeQuery = QueryFactory.create(describeQueryString);
QueryExecution describeQe = QueryExecutionFactory.create(describeQuery, r);
r2.add(describeQe.execDescribe());
describeQe.close();
String selectQueryString = "PREFIX foaf: " +
"PREFIX rdf: " +
"SELECT ?name ?url ?email " +
"WHERE {" +
"?person rdf:type foaf:Person . " +
"OPTIONAL {" +
"?person foaf:name ?name ." +
"}" +
"OPTIONAL {" +
"?person foaf:homepage ?url . " +
"} " +
"OPTIONAL {" +
"?person foaf:mbox ?email . " +
"}" +
"FILTER(isURI(?person)) " +
"} LIMIT 1";
Query selectQuery = QueryFactory.create(selectQueryString);
QueryExecution selectQe = QueryExecutionFactory.create(selectQuery, r2);
ResultSet selectResults = selectQe.execSelect();
out.append(ResultSetFormatter.asXMLString(selectResults));
selectQe.close();
}
//
/** Handles the HTTP GET method.
* @param request servlet request
* @param response servlet response
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/** Handles the HTTP POST method.
* @param request servlet request
* @param response servlet response
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/** Returns a short description of the servlet.
*/
public String getServletInfo() {
return "Short description";
}
//
}