/** * Initializes with data from a JSON-serialized attachment. * * @param json the attachment to wrap * @throws IllegalArgumentException if the JSON does not contain all the needed fields */ Attachment(JSONObject json, DocumentReferenceResolver<String> userResolver, EntityReferenceSerializer<String> userSerializer, Provider<XWikiContext> contextProvider) throws IllegalArgumentException { this.attachment = new XWikiAttachment(); this.userResolver = userResolver; this.userSerializer = userSerializer; this.contextProvider = contextProvider; readJSON(json); }
@Override public XWikiAttachment next() { Object[] item = this.data.next(); try { XWikiDocument doc = new XWikiDocument(HibernateAttachmentsReader.this.resolver.resolve(String.valueOf(item[0]))); XWikiAttachment att = new XWikiAttachment(doc, String.valueOf(item[1])); HibernateAttachmentsReader.this.store.loadAttachmentContent(att, HibernateAttachmentsReader.this.context.get(), true); HibernateAttachmentsReader.this.archiveStore.loadArchive(att, HibernateAttachmentsReader.this.context.get(), true); HibernateAttachmentsReader.this.logger.debug("Loaded [{}] from the database", att.getReference()); return att; } catch (Exception ex) { HibernateAttachmentsReader.this.logger.error("Failed to read attachment from the database store: {}", ex.getMessage(), ex); } return null; }
@Override public XWikiAttachment next() { Object[] item = this.data.next(); try { XWikiDocument doc = new XWikiDocument(HibernateAttachmentsReader.this.resolver.resolve(String.valueOf(item[0]))); XWikiAttachment att = new XWikiAttachment(doc, String.valueOf(item[1])); HibernateAttachmentsReader.this.store.loadAttachmentContent(att, HibernateAttachmentsReader.this.context.get(), true); HibernateAttachmentsReader.this.archiveStore.loadArchive(att, HibernateAttachmentsReader.this.context.get(), true); HibernateAttachmentsReader.this.logger.debug("Loaded [{}] from the database", att.getReference()); return att; } catch (Exception ex) { HibernateAttachmentsReader.this.logger.error("Failed to read attachment from the database store: {}", ex.getMessage(), ex); } return null; }
@Override public XWikiAttachment parse(final Element docel) throws IOException { if (!ROOT_ELEMENT_NAME.equals(docel.getName())) { throw new IOException("XML not recognizable as attachment metadata, expecting <attachment> tag"); } if (docel.attribute(SERIALIZER_PARAM) == null || !THIS_SERIALIZER.equals(docel.attribute(SERIALIZER_PARAM).getValue())) { throw new IOException("Cannot parse this attachment metadata, it was saved with a different " + "serializer."); } final XWikiAttachment out = new XWikiAttachment(); out.setFilename(docel.element(FILENAME).getText()); out.setFilesize(Integer.parseInt(docel.element(FILESIZE).getText())); out.setAuthor(docel.element(AUTHOR).getText()); out.setVersion(docel.element(VERSION).getText()); out.setComment(docel.element(COMMENT).getText()); final String sdate = docel.element(DATE).getText(); final Date date = new Date(Long.parseLong(sdate)); out.setDate(date); return out; }
@Override public XWikiAttachment parse(final Element docel) throws IOException { if (!ROOT_ELEMENT_NAME.equals(docel.getName())) { throw new IOException("XML not recognizable as attachment metadata, expecting <attachment> tag"); } if (docel.attribute(SERIALIZER_PARAM) == null || !THIS_SERIALIZER.equals(docel.attribute(SERIALIZER_PARAM).getValue())) { throw new IOException("Cannot parse this attachment metadata, it was saved with a different " + "serializer."); } final XWikiAttachment out = new XWikiAttachment(); out.setFilename(docel.element(FILENAME).getText()); out.setLongSize(Long.parseLong(docel.element(FILESIZE).getText())); out.setAuthor(docel.element(AUTHOR).getText()); out.setVersion(docel.element(VERSION).getText()); out.setComment(docel.element(COMMENT).getText()); final String sdate = docel.element(DATE).getText(); final Date date = new Date(Long.parseLong(sdate)); out.setDate(date); return out; }
@Override public boolean storeEntity(XWikiAttachment entity) { if (entity == null) { return true; } XWikiAttachment existing = new XWikiAttachment(entity.getDoc(), entity.getFilename()); try { this.store.loadAttachmentContent(existing, this.context.get(), false); // If loading succeeded, then the attachment already exists on the filesystem; // keep using the existing attachment version and discard the database one this.logger.debug("Skipped importing already existing attachment [{}]", entity.getReference()); return true; } catch (XWikiException e) { // No such attachment on the filesystem, continue storing it } try { this.store.saveAttachmentContent(entity, false, this.context.get(), false); // The archive is also automatically stored by the call above, no need to explicitly store the archive this.logger.debug("Imported attachment [{}] into the filesystem store", entity.getReference()); return true; } catch (XWikiException ex) { this.logger.error("Failed to store attachment into the filesystem store: {}", ex.getMessage(), ex); return false; } } }
@Override public boolean storeEntity(XWikiAttachment entity) { if (entity == null) { return true; } XWikiAttachment existing = new XWikiAttachment(entity.getDoc(), entity.getFilename()); try { this.store.loadAttachmentContent(existing, this.context.get(), false); // If loading succeeded, then the attachment already exists on the filesystem; // keep using the existing attachment version and discard the database one this.logger.debug("Skipped importing already existing attachment [{}]", entity.getReference()); return true; } catch (XWikiException e) { // No such attachment on the filesystem, continue storing it } try { this.store.saveAttachmentContent(entity, false, this.context.get(), false); // The archive is also automatically stored by the call above, no need to explicitly store the archive this.logger.debug("Imported attachment [{}] into the filesystem store", entity.getReference()); return true; } catch (XWikiException ex) { this.logger.error("Failed to store attachment into the filesystem store: {}", ex.getMessage(), ex); return false; } } }
/** * @param rcsArchive the RCS archive to import. * @throws Exception if getting a revision from the RCS archive or deserializing an attachment from XML fails */ private void fromRCS(final Archive rcsArchive) throws Exception { if (rcsArchive == null) { return; } final Node[] nodes = rcsArchive.changeLog(); for (int i = nodes.length - 1; i > -1; i--) { final Object[] lines = rcsArchive.getRevision(nodes[i].getVersion()); final StringBuilder content = new StringBuilder(); for (int j = 0; j < lines.length; j++) { String line = lines[j].toString(); content.append(line); if (j != lines.length - 1) { content.append("\n"); } } final XWikiAttachment rev = new XWikiAttachment(); rev.fromXML(content.toString()); rev.setDoc(getAttachment().getDoc()); rev.setAttachment_archive(this); // this should not be necessary, keeping to maintain behavior. rev.setVersion(nodes[i].getVersion().toString()); revisions.add(rev); } }
/** * @param rcsArchive the RCS archive to import. * @throws Exception if getting a revision from the RCS archive or deserializing an attachment from XML fails */ private void fromRCS(final Archive rcsArchive) throws Exception { if (rcsArchive == null) { return; } final Node[] nodes = rcsArchive.changeLog(); for (int i = nodes.length - 1; i > -1; i--) { final Object[] lines = rcsArchive.getRevision(nodes[i].getVersion()); final StringBuilder content = new StringBuilder(); for (int j = 0; j < lines.length; j++) { String line = lines[j].toString(); content.append(line); if (j != lines.length - 1) { content.append("\n"); } } final XWikiAttachment rev = new XWikiAttachment(); rev.fromXML(content.toString()); rev.setDoc(this.getAttachment().getDoc()); rev.setAttachment_archive(this); // this should not be necessary, keeping to maintain behavior. rev.setVersion(nodes[i].getVersion().toString()); revisions.add(rev); } }
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; }
/** * Retrieve all the deleted attachments that belonged to a certain document and had the specified name. Multiple * versions can be returned since the same file can be uploaded and deleted several times, creating different * instances in the trash. Note that this does not distinguish between different incarnations of a document name, * and it does not require that the document still exists, it returns all the attachments that at the time of their * deletion had a document with the specified name as their owner. * * @param docName the {@link DeletedAttachment#getDocName() name of the document} the attachment belonged to * @param filename the {@link DeletedAttachment#getFilename() name} of the attachment to search for * @param context the current request context * @return A list with all the deleted attachments which belonged to the specified document and had the specified * filename. If no such attachments are found in the trash, an empty list is returned. * @throws XWikiException if an error occurs while loading the attachments */ public List<DeletedAttachment> getDeletedAttachments(String docName, String filename, XWikiContext context) throws XWikiException { if (hasAttachmentRecycleBin(context)) { XWikiDocument doc = new XWikiDocument(this.currentMixedDocumentReferenceResolver.resolve(docName)); XWikiAttachment attachment = new XWikiAttachment(doc, filename); return getAttachmentRecycleBinStore().getAllDeletedAttachments(attachment, context, true); } return null; }
attachment = new XWikiAttachment(); doc.getAttachmentList().add(attachment);
/** * Restore a {@link XWikiAttachment} from a {@link DeletedAttachment}. Note that this method does not actually * restore the attachment to its owner document, it simply recomposes an {@link XWikiAttachment} object from the * saved data. * * @return restored attachment * @param attachment optional object where to put the attachment data, if not <code>null</code> * @param context the current {@link XWikiContext context} * @throws XWikiException If an exception occurs while the Attachment is restored from the XML. See * {@link XWikiAttachment#fromXML(String)}. */ public XWikiAttachment restoreAttachment(XWikiAttachment attachment, XWikiContext context) throws XWikiException { XWikiAttachment result = attachment; if (result == null) { result = new XWikiAttachment(); } result.fromXML(getXml()); if (result.getDoc() == null || !(this.getDocName().equals(result.getDoc().getFullName()))) { result.setDoc(context.getWiki().getDocument(this.getDocName(), context)); } return result; } }
&& request.getParameter("rid") != null) { int recycleId = Integer.parseInt(request.getParameter("rid")); attachment = new XWikiAttachment(doc, filename); attachment = (XWikiAttachment) context.getWiki().getAttachmentRecycleBinStore()
xattachment = new XWikiAttachment(doc, report.getFilename()); doc.addAttachment(xattachment);
xattachment = new XWikiAttachment(doc, file.getFilename()); doc.addAttachment(xattachment);
/** * {@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); }
throws WikiStreamException XWikiAttachment attachment = new XWikiAttachment(this.document, name);
/** * {@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); }
XWikiAttachment xwikiAttachment = xwikiDocument.getAttachment(attachmentName); if (xwikiAttachment == null) { xwikiAttachment = new XWikiAttachment(); xwikiDocument.getAttachmentList().add(xwikiAttachment); } else {