public static Node copyNode(Node node, Graph targetGraph, CopyStrategy copyStrategy) { if (node instanceof ContextNode) { return copyContextNode((ContextNode) node, targetGraph, copyStrategy); } else if (node instanceof LiteralNode) { return copyLiteralNode((LiteralNode) node, targetGraph, copyStrategy); } else { throw new IllegalArgumentException("Invalid node: " + node); } }
/** * Creates a clone of the given message with the same contents. * @param message The message to clone. * @return The cloned message. */ public static Message cloneMessage(Message message, boolean newId) { MessageCollection clonedMessageCollection = new MessageEnvelope().getMessageCollection(message.getSenderXDIAddress(), true); XdiEntity xdiEntity = message.getXdiEntity(); ContextNode copiedContextNode; if (newId) { if (xdiEntity instanceof XdiEntityInstanceUnordered) { copiedContextNode = clonedMessageCollection.getXdiEntityCollection().setXdiInstanceUnordered(true, false).getContextNode(); CopyUtil.copyContextNodeContents(message.getContextNode(), copiedContextNode, null); } else if (xdiEntity instanceof XdiEntityInstanceOrdered) { copiedContextNode = clonedMessageCollection.getXdiEntityCollection().setXdiInstanceOrdered(false, true).getContextNode(); CopyUtil.copyContextNodeContents(message.getContextNode(), copiedContextNode, null); } else { throw new Xdi2RuntimeException("Unexpected message entity: " + xdiEntity + " (" + xdiEntity.getClass().getSimpleName() + ")"); } } else { copiedContextNode = CopyUtil.copyContextNode(message.getContextNode(), clonedMessageCollection.getContextNode(), null); } XdiEntity clonedXdiEntity = XdiAbstractEntity.fromContextNode(copiedContextNode); Message clonedMessage = Message.fromMessageCollectionAndXdiEntity(clonedMessageCollection, clonedXdiEntity); if (log.isTraceEnabled()) log.trace("Cloned message: " + clonedMessage.getMessageEnvelope()); return clonedMessage; }
public static Node copyNode(Node node, ContextNode targetContextNode, CopyStrategy copyStrategy) { if (node instanceof ContextNode) { return copyContextNode((ContextNode) node, targetContextNode, copyStrategy); } else if (node instanceof LiteralNode) { return copyLiteralNode((LiteralNode) node, targetContextNode, copyStrategy); } else { throw new IllegalArgumentException("Invalid node: " + node); } }
/** * Creates a clone of the given context node with the same contents. * @param contextNode The context node to clone. * @return The cloned context node. */ public static ContextNode cloneContextNode(ContextNode contextNode) { Graph newGraph = graphFactory.openGraph(); ContextNode newContextNode = CopyUtil.copyContextNode(contextNode, newGraph, null); return newContextNode; } }
/** * Copies all context nodes of a context node into a target context node. * @param contextNode A context node from any graph. * @param targetContextNode The target context node. * @param copyStrategy The strategy to determine what to copy. * @return The copied context nodes in the target graph. */ public static Iterator<ContextNode> copyContextNodes(ContextNode contextNode, ContextNode targetContextNode, CopyStrategy copyStrategy) { if (contextNode == null) throw new NullPointerException(); if (targetContextNode == null) throw new NullPointerException(); if (copyStrategy == null) copyStrategy = DEFAULT_COPY_STRATEGY; for (Iterator<ContextNode> innerContextNodes = contextNode.getContextNodes(); innerContextNodes.hasNext(); ) { ContextNode innerContextNode = innerContextNodes.next(); copyContextNode(innerContextNode, targetContextNode, copyStrategy); } return targetContextNode.getContextNodes(); }
/** * Creates a clone of the given graph with the same contents. * @param graph The graph to clone. * @return The cloned graph. */ public static Graph cloneGraph(Graph graph) { Graph newGraph = graphFactory.openGraph(); CopyUtil.copyContextNode(graph.getRootContextNode(true), newGraph, null); return newGraph; }
@Override public Writer write(ContextNode contextNode, Writer writer) throws IOException { Graph subGraph = MemoryGraphFactory.getInstance().openGraph(); CopyUtil.copyContextNode(contextNode, subGraph, null); return this.write(subGraph, writer); }
@Override public OutputStream write(ContextNode contextNode, OutputStream stream) throws IOException { Graph subGraph = MemoryGraphFactory.getInstance().openGraph(); CopyUtil.copyContextNode(contextNode, subGraph, null); return this.write(subGraph, stream); }
@Override public void executeGetOnAddress(XDIAddress targetXDIAddress, GetOperation operation, Graph operationResultGraph, ExecutionContext executionContext) throws Xdi2MessagingException { Node node = this.getGraph().getDeepNode(targetXDIAddress, true); if (node == null) return; if (node instanceof ContextNode) { CopyUtil.copyContextNode((ContextNode) node, operationResultGraph, null); } else if (node instanceof LiteralNode) { CopyUtil.copyLiteralNode((LiteralNode) node, operationResultGraph, null); } }
/** * Returns the normalized serialization string of a context node, to be used * e.g. for signatures and encryptions. */ public static String serialize(ContextNode contextNode, CopyStrategy copyStrategy) { Graph tempGraph; tempGraph = MemoryGraphFactory.getInstance().openGraph(); CopyUtil.copyContextNode(contextNode, tempGraph, copyStrategy); StringWriter buffer = new StringWriter(); String string; try { XDIWRITER.write(tempGraph, buffer); string = buffer.toString(); } catch (IOException ex) { throw new Xdi2RuntimeException("Cannot serialize " + contextNode + ": " + ex.getMessage(), ex); } finally { try { buffer.close(); } catch (Exception ex) { } tempGraph.close(); } if (log.isDebugEnabled()) log.debug("Normalized context node " + contextNode.getXDIAddress() + ": " + string); return string; }
@Override public void execute(MessageEnvelope messageEnvelope, ExecutionContext executionContext, ExecutionResult executionResult) throws Xdi2MessagingException { for (Operation operation : messageEnvelope.getOperations()) { Graph operationResultGraph = executionResult.createOperationResultGraph(operation); CopyUtil.copyContextNode(operation.getMessage().getContextNode(), operationResultGraph, null); } executionResult.finish(); }
@Override public InterceptorResult before(Operation operation, Graph operationResultGraph, ExecutionContext executionContext) throws Xdi2MessagingException { // check parameters if (! (operation instanceof DoOperation)) return InterceptorResult.DEFAULT; if (! XDI_ADD_DO_SIG.equals(operation.getOperationXDIAddress())) return InterceptorResult.DEFAULT; if (DeferResultInterceptor.hasDeferResult(executionContext, operation.getMessage())) return InterceptorResult.DEFAULT; if (operation.getTargetXdiInnerRoot() == null) return InterceptorResult.DEFAULT; // get the inner graph Graph innerGraph = operation.getTargetXdiInnerRoot().getInnerGraph(); // sign inner graph Signature signature; try { signature = this.getSignatureCreator().createSignature(innerGraph.getRootContextNode()); } catch (GeneralSecurityException ex) { throw new Xdi2MessagingException("Could not create signature for operation " + operation + " via " + this.getSignatureCreator().getClass().getSimpleName() + ": " + ex.getMessage(), ex, executionContext); } // result graph CopyUtil.copyContextNode(signature.getBaseContextNode(), operationResultGraph, null); // done return InterceptorResult.SKIP_MESSAGING_CONTAINER; }
private void processPush(Message pushedMessage, Operation operation, Graph operationResultGraph, ExecutionContext executionContext) throws Xdi2MessagingException { if (log.isDebugEnabled()) log.debug("Preparing to process pushed message " + pushedMessage); // TODO: how exactly is the $push message a response, and to what? FullMessagingResponse pushedMessagingResponse = FullMessagingResponse.fromMessageEnvelope(operation.getMessage().getMessageEnvelope()); // TODO: correctly store statements of $set operation? Graph resultGraph = ((TransportMessagingResponse) pushedMessagingResponse).getResultGraph(); if (resultGraph != null) { MessageEnvelope innerMe = MessageEnvelope.fromGraph(operation.getTargetXdiInnerRoot().getInnerGraph()); Message innerM = innerMe == null ? null : innerMe.getMessages().next(); Iterator<SetOperation> innerSOs = innerM == null ? new EmptyIterator<SetOperation> () : innerM.getSetOperations(); SetOperation innerSO = innerSOs.hasNext() ? innerSOs.next() : null; XdiInnerRoot innerIR = innerSO == null ? null : innerSO.getTargetXdiInnerRoot(); if (innerIR != null) { CopyUtil.copyGraph(innerIR.getInnerGraph(), this.getTargetGraph(executionContext), null); } } // TODO: correctly store any contracts we got? // see comment in pushedMessagingResponse.getLinkContracts() if (log.isDebugEnabled()) log.debug("Looking for link contracts from result graph of pushed messaging response " + pushedMessagingResponse.getResultGraph()); for (LinkContract pushedLinkContract : TransportMessagingResponse.getLinkContracts(pushedMessagingResponse)) { if (log.isDebugEnabled()) log.debug("Obtained link contract from result graph of pushed messaging response " + pushedLinkContract); // first delete link contract in target graph if it exists already ContextNode linkContractContextNode = this.getTargetGraph(executionContext).getDeepContextNode(pushedLinkContract.getContextNode().getXDIAddress()); if (linkContractContextNode != null) linkContractContextNode.delete(); // write link contract and index into target graph CopyUtil.copyContextNode(pushedLinkContract.getContextNode(), this.getTargetGraph(executionContext), null); XdiEntityCollection xdiLinkContractIndex = Index.getEntityIndex(this.getTargetGraph(executionContext), XDILinkContractConstants.XDI_ARC_CONTRACT, true); Index.setEntityIndexAggregation(xdiLinkContractIndex, pushedLinkContract.getXdiEntity().getXDIAddress()); } }
CopyUtil.copyContextNode(pushLinkContract.getContextNode(), graphMessagingContainer.getGraph(), null); XdiEntityCollection xdiLinkContractIndex = Index.getEntityIndex(graphMessagingContainer.getGraph(), XDILinkContractConstants.XDI_ARC_CONTRACT, true); Index.setEntityIndexAggregation(xdiLinkContractIndex, pushLinkContract.getXdiEntity().getXDIAddress()); CopyUtil.copyContextNode(linkContract.getContextNode(), graphMessagingContainer.getGraph(), null); XdiEntityCollection xdiLinkContractIndex = Index.getEntityIndex(graphMessagingContainer.getGraph(), XDILinkContractConstants.XDI_ARC_CONTRACT, true); Index.setEntityIndexAggregation(xdiLinkContractIndex, linkContract.getXdiEntity().getXDIAddress());