package edu.neu.ccs.task.rdf;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletResponse;
import webframe.BasicUserManager;
import webframe.Configuration;
import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.query.ResultSetFormatter;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.RDFWriter;
import com.hp.hpl.jena.sdb.StoreDesc;
import com.hp.hpl.jena.update.UpdateAction;
public class RdfUserManager extends BasicUserManager {
protected final StoreDesc storeDesc;
public RdfUserManager(Configuration c) {
super(c);
// need to make sure we have RDF support:
c.requireInstance(AgentWithRdfFactory.class, "dtask.agent-factory");
storeDesc = new StoreDesc(
c.getString("dtask.rdf.db-persistence.layout", "layout2"),
c.getString("dtask.rdf.db-persistence.db-type", "MySQL"));
}
@Override
public List<String> statusTypes() {
return Arrays.asList("rdf", "sparql");
}
@Override
public boolean status(HttpServletResponse resp, int id, String type,
Map<String,String[]> params)
throws ServletException, IOException {
if ("rdf".equals(type)) {
rdfStatus(resp, "usermodel:"+id);
return true;
} else if ("sparql".equals(type)) {
sparqlStatus(resp, "usermodel:"+id, params);
return true;
} else
return false;
}
private void rdfStatus(HttpServletResponse resp, String model)
throws ServletException, IOException {
resp.setContentType("application/rdf+xml");
resp.setCharacterEncoding("UTF-8");
final PrintWriter out = resp.getWriter();
SdbUtil.runWrap(ds, storeDesc, model, new SdbQuery() {
public void exec(Model sdbModel) {
RDFWriter w = sdbModel.getWriter();
w.setProperty("allowBadURIs", true);
w.write(sdbModel, out, null);
}
});
out.flush();
}
private void sparqlStatus(HttpServletResponse resp, String model,
Map<String,String[]> params)
throws ServletException, IOException {
String[] qparam = params.get("query");
if ((qparam==null) || (qparam.length!=1))
throw new ServletException("bad query parameter");
final String query = qparam[0];
resp.setContentType("application/sparql-results+xml");
resp.setCharacterEncoding("UTF-8");
final OutputStream out = resp.getOutputStream();
SdbUtil.runWrap(ds, storeDesc, model, new SdbQuery() {
public void exec(Model sdbModel) {
QueryExecution qexec = QueryExecutionFactory.create(query, sdbModel);
try {
ResultSet rs = qexec.execSelect();
ResultSetFormatter.outputAsXML(out, rs);
} finally {
qexec.close();
}
}
});
out.flush();
}
@Override
public List<String> actionTypes() {
List<String> actions = new ArrayList<String>();
actions.addAll(super.actionTypes());
actions.add("sparqlu");
return actions;
}
@Override
public boolean action(int id, String type, Map<String, String[]> params)
throws ServletException, IOException {
if ("sparqlu".equals(type)) {
sparqluAction("usermodel:"+id, params);
return true;
} else
return super.action(id, type, params);
}
private void sparqluAction(String model, Map<String, String[]> params)
throws ServletException, IOException {
String[] stmntParam = params.get("statement");
if ((stmntParam==null) || (stmntParam.length!=1))
throw new ServletException("bad statement parameter");
final String stmnt = stmntParam[0];
SdbUtil.runWrap(ds, storeDesc, model, new SdbQuery() {
public void exec(Model sdbModel) {
UpdateAction.parseExecute(stmnt, sdbModel);
}
});
}
}