* 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 = (MultiPartBody) request.getBody();
String serializedModel = body.getParameter("model");
Model importing = ModelFactory.createDefaultModel();
importing.read(new StringReader(serializedModel), ehRequest
.getRequestURLWithoutParams().toString(), "RDF/XML");
ResIterator transactionIter = model.listSubjectsWithProperty(RDF.type,
TRANSACTION.Transaction);
Resource agentProfileDocument;
String primaryResource = body.getParameter("primaryResource");
if (primaryResource != null) {
agentProfileDocument = importing.getResource(primaryResource);
} else {
if (transactionIter.hasNext()) {
Resource transaction;
transaction = transactionIter.nextResource();
agentProfileDocument = transaction.getProperty(
TRANSACTION.mainResponseResource).getResource();
} else {
agentProfileDocument = JenaUtil.getRoots(importing)[0];
}
}
transactionIter.close();
// normalization of inserion so that permission so that exiting
// permission
// stmts are recognized by InsertPermissionChecker
addInverse(importing);
addSeeAlsoAndPPD(importing);
InsertionIFPResolver.process(model, importing);
// check insertion before deleting staments in model
InsertionPermissionChecker.check(importing, model);
// TODO check if user has the right to remove statements
FoafUtil.addSha1Mbox(importing);
// import all agents (knows), removing only properties of the Main
Resource agent = agentProfileDocument.getProperty(FOAF.primaryTopic)
.getResource();
try {
AccessController
.checkPermission(new RPermission(AUTHORIZATION.edit));
} catch (AccessControlException e) {
Resource user = VirtuserHandler.getSubject();
if (agent.equals(user)) {
} else {
throw e;
}
}
if (agentProfileDocument.isAnon()) {
String name;
try {
name = agent.getProperty(FOAF.name).getString();
if (name.equals("")) {
name = Util.createRandomString(8);
}
} catch (NullPointerException ex) {
name = Util.createRandomString(8);
}
Resource namedResource;
namedResource = (Resource) NamedResourceGenerator
.createNewResource(model, ehRequest.getRootURL(), name)
.inModel(importing);
/*
* namedResource = new ContactHandler(importing, ehRequest
* .getRootURL()).createContact();
*/
JenaUtil.replace(importing, agentProfileDocument, namedResource);
agentProfileDocument = namedResource;
} else {
Resource existingAgentDoc = (Resource) agentProfileDocument
.inModel(model);
if (existingAgentDoc.hasProperty(FOAF.primaryTopic)) {
Resource existingAgent = existingAgentDoc.getProperty(
FOAF.primaryTopic).getResource();
existingAgentDoc.removeProperties();
StmtIterator mboxes = existingAgent.listProperties(FOAF.mbox);
while (mboxes.hasNext()) {
Statement current = mboxes.nextStatement();
current.getResource().removeProperties();
}
mboxes.close();
existingAgent.removeProperties();
// Remove properties of mboxes
// no need, as this has already been done by IFPPResolver
// JenaUtil.replace(importing, agent, existingAgent);
agent = (Resource) existingAgent.inModel(importing);
addParticipants(existingAgent); // reconstruct
// the
// hasParticipant
// properties
removeParticipations(existingAgent, importing);
}
}
try {
// change references with pseudo-url "formfile:"
FormModelPost.handleFormFiles(
new URL(agentProfileDocument.getURI()), importing, request,
model, hashStore);
} catch (MalformedURLException e) {
throw new HandlerException("Not an URL for PPD "
+ agentProfileDocument.getURI(), e);
}
agentProfileDocument.addProperty(RDF.type, FOAFEX.AgentProfileDocument);
agentProfileDocument.addProperty(RDF.type,
RWCF.AuthoritativelyServedResource);
agent.addProperty(RDF.type, FOAF.Agent);
// agent.addProperty(FOAF.isPrimaryTopicOf, agentProfileDocument);
// verifyAgentNew(agent);
addKnows(ehRequest, agent);
Set participationSet = new HashSet();
/*
* StmtIterator debugIter = agentProfileDocument.listProperties(); while
* (debugIter.hasNext()) { Statement element = (Statement)
* debugIter.next(); log.info(element); }
*/
StmtIterator participationStmts = agent
.listProperties(ROLE.participatesIn);
while (participationStmts.hasNext()) {
Resource currentParticipation = participationStmts.nextStatement()
.getResource();
participationSet.add(currentParticipation);
}
participationStmts.close();
for (Iterator iter = participationSet.iterator(); iter.hasNext();) {
Resource currentParticipation = (Resource) iter.next();
Resource currentParticipationInModel = getExistingAgent(currentParticipation);
if (currentParticipationInModel != null) {
if (!currentParticipationInModel.hasProperty(RDF.type,
ROLE.Participation)) {
throw new RuntimeException(
"Incompatible agent exists for email of participation");
}
Resource namedResource = (Resource) currentParticipationInModel
.inModel(importing);
JenaUtil
.replace(importing, currentParticipation, namedResource);
currentParticipation = namedResource;
} else {
Resource participationAgentResource = new ContactHandler(
importing, ehRequest.getRootURL()).createContact();
JenaUtil.replace(importing, currentParticipation,
participationAgentResource);
currentParticipation = participationAgentResource;
}
currentParticipation.addProperty(RDF.type,
RWCF.AuthoritativelyServedResource);
// de-anonymise role:group
Resource group = currentParticipation.getProperty(ROLE.group)
.getResource();
Resource existingGroup = getExistingAgent(group);
if (existingGroup != null) {
// don't change existing group
group.removeProperties();
JenaUtil.replace(importing, group, (Resource) existingGroup
.inModel(importing));
group = existingGroup;
} else {
Resource namedGroup = new ContactHandler(importing, ehRequest
.getRootURL()).createContact();
JenaUtil.replace(importing, group, namedGroup);
group.addProperty(RDF.type, RWCF.AuthoritativelyServedResource);
}
StmtIterator actsInRoleStmts = currentParticipation
.listProperties(ROLE.actsInRole);
while (actsInRoleStmts.hasNext()) {
Resource currentRole = actsInRoleStmts.nextStatement()
.getResource();
Resource existingRole = getExistingAgent(currentRole, group);
if (existingRole != null) {
JenaUtil.replace(importing, currentRole, existingRole);
} else {
currentRole.addProperty(ROLE.group, group);
group.addProperty(ROLE.providesRole, currentRole);
}
}
actsInRoleStmts.close();
}
model.add(importing);
EnhancedResponse ehResponse = new EnhancedResponse(response);
if (body.getParameter("action_aggregate") != null) {
model.leaveCriticalSection();
try {
Download.download(model, body.getParameter("aggregate"),
new URL(ehRequest.getRequestURLWithoutParams(), "/"));
} catch (MalformedURLException e) {
throw new RuntimeException(e);
} finally {
model.enterCriticalSection(ModelLock.WRITE);
}