* @see org.wymiwyg.rwcf.Handler#handle(org.wymiwyg.wrhapi.Request,
* org.wymiwyg.rwcf.Response, org.wymiwyg.rwcf.HandlerChain)
*/
public void handle(Request request, Response response, HandlerChain chain)
throws HandlerException {
EnhancedRequest ehRequest = new EnhancedRequest(request);
MultiPartBody body;
try {
body = (MultiPartBody) request.getBody();
} catch (ClassCastException ex) {
throw new HandlerException(ResponseStatus.BAD_REQUEST, "CommentPostHandler invoked with a wrong request-body format");
}
String serializedModel = body.getParameter("model");
if (serializedModel == null) {
throw new HandlerException(ResponseStatus.BAD_REQUEST,
"No serialized model found. Is JavaScript enabled?");
}
Model importing = ModelFactory.createDefaultModel();
importing.read(new StringReader(serializedModel), ehRequest
.getRequestURLWithoutParams().toString(), "RDF/XML");
Resource[] roots = JenaUtil.getRoots(importing);
if (roots.length != 1) {
throw new HandlerException(
"Imported model must have exactly one root");
}
Resource relation = roots[0];
/*
* if (!relation.hasProperty(RDF.type, KNOBOT.CommentRelation)) { throw
* new HandlerException("Imported resource must be of type " +
* KNOBOT.CommentRelation); }
*/
Resource comment = null;
Resource source = null;
model.enterCriticalSection(ModelLock.WRITE);
try {
comment = relation.getProperty(KNOBOT.target).getResource();
if (!comment.hasProperty(DC.date)) {
Literal currentDate = importing.createTypedLiteral(
new W3CDateFormat().format(new Date()),
XSDDatatype.XSDdateTime);
comment.addProperty(DC.date, currentDate);
}
Statement makerStmt = comment.getProperty(FOAF.maker);
if (makerStmt != null) {
Resource maker = makerStmt.getResource();
Statement mboxStmt = maker.getProperty(FOAF.mbox);
if (mboxStmt != null) {
Resource mbox = mboxStmt.getResource();
Resource user = VirtuserHandler.getSubject();
;
ResIterator existingAgents = model
.listSubjectsWithProperty(FOAF.mbox, mbox);
if (existingAgents.hasNext()) {
Resource agent = existingAgents.nextResource();
if (agent.hasProperty(AUTHORIZATION.pass_sha1sum)
&& !agent.equals(user)) {
throw new AccessControlException(
"Posting comment with exiting users mailbox");
}
// remove statements othe than mbox:
Collection removingStmt = new ArrayList();
StmtIterator makerProperties = maker.listProperties();
while (makerProperties.hasNext()) {
Statement current = (Statement) makerProperties
.nextStatement();
if (current.getPredicate().equals(FOAF.mbox)
&& current.getObject().equals(mbox)) {
continue;
}
if (current.getPredicate().equals(FOAF.name)) {
RDFNode currentNameObject = current.getObject();
if (!agent.hasProperty(FOAF.name,
currentNameObject)) {
comment.addProperty(DC.creator,
currentNameObject);
}
}
removingStmt.add(current);
}
makerProperties.close();
for (Iterator iter = removingStmt.iterator(); iter
.hasNext();) {
Statement current = (Statement) iter.next();
current.remove();
}
}
if (existingAgents.hasNext()) {
log.warn("duplicate agent with " + mbox);
}
existingAgents.close();
}
}
relation.addProperty(KNOBOT.strength, model.createTypedLiteral(1f));
relation.addProperty(KNOBOT.childStrength, model.createTypedLiteral(0.5f));
Literal effectiveDateLiteral = model.createTypedLiteral(
new W3CDateFormat().format(new Date()),
XSDDatatype.XSDdateTime);
relation.addProperty(KNOBOT.effectiveDate, effectiveDateLiteral);
relation.addProperty(KNOBOT.strengthReduction, model.createTypedLiteral(DefaultValuesHandler
.getDefaultStrengthReduction(model)));
Statement titleStatement = comment.getProperty(RSS.title);
if (titleStatement == null) {
throw new BadRequestException("Title is required");
}
Resource namedItem = (Resource) NamedResourceGenerator
.createNewResource(model, ehRequest.getRootURL(),
titleStatement.getString()).inModel(importing);
JenaUtil.replace(importing, comment, namedItem);
comment = namedItem;
htmlize(comment);
comment.addProperty(RDF.type, RWCF.AuthoritativelyServedResource);
comment.addProperty(RDF.type, KNOBOT.Commentable);
source = (Resource) relation.getProperty(KNOBOT.source)
.getResource().inModel(model);
if (!source.hasProperty(RDF.type, KNOBOT.Commentable)) {
AccessController.checkPermission(new RPermission(
AUTHORIZATION.edit));
}
// add same required permissions as for commented item
addRequiredPermissions(source, comment);
addNotificationsTargets(source, comment);
InsertionIFPResolver.process(model, importing);
model.add(importing);
RelationManager.addRelation((Resource) relation.inModel(model));
} finally {
model.leaveCriticalSection();
}
sendNotification((Resource) source.inModel(model), (Resource) comment
.inModel(model), ehRequest.getAcceptLanguages(), ehRequest
.getRootURL());
String go = body.getParameter("go");
if (go == null) {
go = source.getURI();
}