/** * @param solrDocument the Solr document where to add the attachments data * @param locale the locale for which to index the attachments * @param originalDocument the original document, that should be used to access the attachments */ private void setAttachments(SolrInputDocument solrDocument, Locale locale, XWikiDocument originalDocument) { for (XWikiAttachment attachment : originalDocument.getAttachmentList()) { setAttachment(solrDocument, locale, attachment); } }
public XWikiAttachment getAttachment(String filename) { for (XWikiAttachment attach : getAttachmentList()) { if (attach.getFilename().equals(filename)) { return attach; } } for (XWikiAttachment attach : getAttachmentList()) { if (attach.getFilename().startsWith(filename + ".")) { return attach; } } return null; }
/** * Clone attachments from another document. This implementation expects that this document is the same as the other * document and thus attachments will be saved in the database in the same place as the ones which they are cloning. * * @param sourceDocument an XWikiDocument to copy attachments from */ private void cloneAttachments(final XWikiDocument sourceDocument) { this.getAttachmentList().clear(); for (XWikiAttachment attach : sourceDocument.getAttachmentList()) { XWikiAttachment newAttach = (XWikiAttachment) attach.clone(); // Document is set to this because if this document is renamed then the attachment will have a new id // and be saved somewhere different. newAttach.setDoc(this); this.getAttachmentList().add(newAttach); } }
private void replaceAttachmentInPlace(XWikiDocument doc, XWikiAttachment attachment) { for (ListIterator<XWikiAttachment> it = doc.getAttachmentList().listIterator(); it.hasNext();) { if (StringUtils.equals(it.next().getFilename(), attachment.getFilename())) { it.remove(); it.add(attachment); break; } } }
/** * @param document the document * @param result the list to add reference to */ private void addAttachmentsReferences(XWikiDocument document, List<EntityReference> result) { List<XWikiAttachment> attachments = document.getAttachmentList(); for (XWikiAttachment attachment : attachments) { AttachmentReference attachmentReference = attachment.getReference(); try { Iterables.addAll(result, this.attachmentResolverProvider.get().getReferences(attachmentReference)); } catch (Exception e) { this.logger.error("Failed to resolve references for attachment [" + attachmentReference + "]", e); } } }
public int queueAttachments(XWikiDocument document, XWikiContext context) { int retval = 0; final List<XWikiAttachment> attachmentList = document.getAttachmentList(); retval += attachmentList.size(); for (XWikiAttachment attachment : attachmentList) { try { queueAttachment(attachment, context, false); } catch (Exception e) { LOGGER.error("Failed to retrieve attachment [{}] of document [{}]", new Object[] {attachment.getFilename(), document, e}); } } return retval; }
public List<Attachment> getAttachmentList() { List<Attachment> apis = new ArrayList<Attachment>(); for (XWikiAttachment attachment : this.getDoc().getAttachmentList()) { apis.add(new Attachment(this, attachment, getXWikiContext())); } return apis; }
public void loadAttachments(XWikiContext context) throws XWikiException { for (XWikiAttachment attachment : getAttachmentList()) { attachment.loadContent(context); attachment.loadArchive(context); } }
/** * Copy attachments from one document to another. This implementation expects that you are copying the attachment * from one document to another and thus it should be saved seperately from the original in the database. * * @param sourceDocument an XWikiDocument to copy attachments from */ public void copyAttachments(XWikiDocument sourceDocument) { getAttachmentList().clear(); Iterator<XWikiAttachment> attit = sourceDocument.getAttachmentList().iterator(); while (attit.hasNext()) { XWikiAttachment attachment = attit.next(); XWikiAttachment newattachment = (XWikiAttachment) attachment.clone(); newattachment.setDoc(this); // TODO: Why must attachment content must be set dirty --cjdelisle if (newattachment.getAttachment_content() != null) { newattachment.getAttachment_content().setContentDirty(true); } getAttachmentList().add(newattachment); } setContentDirty(true); }
public XWikiPageClassLoader(String jarWikiPage, ClassLoader parent, XWikiContext context) throws XWikiException { super(new URL[0], parent); XWikiDocument doc = context.getWiki().getDocument(jarWikiPage, context); if (!doc.isNew()) { List attachList = doc.getAttachmentList(); for (int i=0;i<attachList.size();i++) { XWikiAttachment attach = (XWikiAttachment) attachList.get(i); String filename = attach.getFilename(); if (filename.endsWith(".jar")) { String downloadURL = doc.getExternalAttachmentURL(filename, "download", context); try{ addURL(new URL(downloadURL)); if (LOG.isDebugEnabled()) { LOG.debug("Adding [" + downloadURL + "] JAR from page [" + jarWikiPage + "] to Groovy classloader"); } } catch (Exception e) { LOG.warn("Failed to add [" + downloadURL + "] JAR from page [" + jarWikiPage + "], ignoring it."); } } } } } }
/** * {@inheritDoc} * * @see DocumentAccessBridge#getAttachmentReferences(org.xwiki.model.reference.DocumentReference) */ public List<AttachmentReference> getAttachmentReferences(DocumentReference documentReference) throws Exception { List<AttachmentReference> attachmentReferences = new ArrayList<AttachmentReference>(); XWikiContext xcontext = getContext(); DocumentReference resolvedReference = documentReference; List<XWikiAttachment> attachments = xcontext.getWiki().getDocument(resolvedReference, xcontext).getAttachmentList(); for (XWikiAttachment attachment : attachments) { attachmentReferences.add(new AttachmentReference(attachment.getFilename(), resolvedReference)); } return attachmentReferences; }
@Override protected void onRun() throws Exception { // TODO: When the rest of storage is rewritten using TransactionRunnable, // this method should be disolved. final Session session = this.context.getWiki().getHibernateStore().getSession(this.context); // Delete the content from the attachment. // In case it was stored in the database by XWikiHibernateAttachmentStore. session.delete(new XWikiAttachmentContent(this.attachment)); // Update the document if required. if (this.updateDocument) { final String filename = this.attachment.getFilename(); final List<XWikiAttachment> list = attachment.getDoc().getAttachmentList(); for (int i = 0; i < list.size(); i++) { if (filename.equals(list.get(i).getFilename())) { list.remove(i); break; } } this.context.getWiki().getStore().saveXWikiDoc(this.attachment.getDoc(), this.context, false); } // Delete the attachment metadata. session.delete(this.attachment); } }
public XWikiAttachment addAttachment(String fileName, byte[] data, XWikiContext context) throws XWikiException { int i = fileName.indexOf('\\'); if (i == -1) { i = fileName.indexOf('/'); } String filename = fileName.substring(i + 1); // TODO : avoid name clearing when encoding problems will be solved // JIRA : http://jira.xwiki.org/jira/browse/XWIKI-94 filename = context.getWiki().clearName(filename, false, true, context); XWikiAttachment attachment = getAttachment(filename); if (attachment == null) { attachment = new XWikiAttachment(); // TODO: Review this code and understand why it's needed. // Add the attachment in the current doc getAttachmentList().add(attachment); } attachment.setContent(data); attachment.setFilename(filename); attachment.setAuthor(context.getUser()); // Add the attachment to the document attachment.setDoc(this); return attachment; }
@Override protected void onRun() throws Exception { // TODO: When the rest of storage is rewritten using TransactionRunnable, // this method should be disolved. final Session session = this.context.getWiki().getHibernateStore().getSession(this.context); // Delete the content from the attachment. // In case it was stored in the database by XWikiHibernateAttachmentStore. session.delete(new XWikiAttachmentContent(this.attachment)); // Update the document if required. if (this.updateDocument) { final String filename = this.attachment.getFilename(); final List<XWikiAttachment> list = attachment.getDoc().getAttachmentList(); for (int i = 0; i < list.size(); i++) { if (filename.equals(list.get(i).getFilename())) { list.remove(i); break; } } this.context.getWiki().getStore().saveXWikiDoc(this.attachment.getDoc(), this.context, false); } // Delete the attachment metadata. session.delete(this.attachment); } }
for (XWikiAttachment origAttach : fromDoc.getAttachmentList()) { String fileName = origAttach.getFilename(); XWikiAttachment newAttach = toDoc.getAttachment(fileName); for (XWikiAttachment newAttach : toDoc.getAttachmentList()) { String fileName = newAttach.getFilename(); XWikiAttachment origAttach = fromDoc.getAttachment(fileName);
} else if (request.getParameter("id") != null) { int id = Integer.parseInt(request.getParameter("id")); attachment = (XWikiAttachment) doc.getAttachmentList().get(id); } else { attachment = doc.getAttachment(filename);
destinationDoc.getAttachmentList().add(attachment); attachment.setDoc(destinationDoc);
List<XWikiAttachment> list = doc.getAttachmentList(); for (XWikiAttachment attachment : list) { attachment.setDoc(doc);
/** * {@inheritDoc} * * @see DocumentAccessBridge#setAttachmentContent(String, String, byte[]) */ @Deprecated public void setAttachmentContent(String documentReference, String attachmentFilename, byte[] attachmentData) throws Exception { XWikiContext xcontext = getContext(); XWikiDocument doc = xcontext.getWiki().getDocument(documentReference, xcontext); XWikiAttachment attachment = doc.getAttachment(attachmentFilename); if (attachment == null) { attachment = new XWikiAttachment(); doc.getAttachmentList().add(attachment); doc.setComment("Add new attachment " + attachmentFilename); } else { doc.setComment("Update attachment " + attachmentFilename); } attachment.setContent(attachmentData); attachment.setFilename(attachmentFilename); attachment.setAuthor(getCurrentUser()); attachment.setDoc(doc); doc.setAuthor(getCurrentUser()); if (doc.isNew()) { doc.setCreator(getCurrentUser()); } doc.saveAttachmentContent(attachment, xcontext); }
/** * {@inheritDoc} * * @see org.xwiki.bridge.DocumentAccessBridge#setAttachmentContent(org.xwiki.model.reference.AttachmentReference, * byte[]) */ public void setAttachmentContent(AttachmentReference attachmentReference, byte[] attachmentData) throws Exception { XWikiContext xcontext = getContext(); XWikiDocument doc = xcontext.getWiki().getDocument(attachmentReference.getDocumentReference(), xcontext); XWikiAttachment attachment = doc.getAttachment(attachmentReference.getName()); if (attachment == null) { attachment = new XWikiAttachment(); doc.getAttachmentList().add(attachment); doc.setComment("Add new attachment " + attachmentReference.getName()); } else { doc.setComment("Update attachment " + attachmentReference.getName()); } attachment.setContent(attachmentData); attachment.setFilename(attachmentReference.getName()); attachment.setAuthor(getCurrentUser()); attachment.setDoc(doc); doc.setAuthor(getCurrentUser()); if (doc.isNew()) { doc.setCreator(getCurrentUser()); } doc.saveAttachmentContent(attachment, xcontext); }