Search in sources :

Example 6 with FilterException

use of org.xwiki.filter.FilterException in project xwiki-platform by xwiki.

the class XAROutputFilterStream method onWikiAttachment.

@Override
public void onWikiAttachment(String name, InputStream content, Long size, FilterEventParameters parameters) throws FilterException {
    checkXMLWriter();
    try {
        this.writer.writeStartElement(XARAttachmentModel.ELEMENT_ATTACHMENT);
        this.writer.writeElement(XARAttachmentModel.ELEMENT_NAME, name);
        if (this.properties.isPreserveVersion() && parameters.containsKey(XWikiWikiAttachmentFilter.PARAMETER_JRCSREVISIONS)) {
            this.writer.writeElement(XARAttachmentModel.ELEMENT_REVISIONS, (String) parameters.get(XWikiWikiAttachmentFilter.PARAMETER_JRCSREVISIONS));
        }
        if (parameters.containsKey(XWikiWikiAttachmentFilter.PARAMETER_MIMETYPE)) {
            this.writer.writeElement(XARAttachmentModel.ELEMENT_MIMETYPE, (String) parameters.get(XWikiWikiAttachmentFilter.PARAMETER_MIMETYPE));
        }
        if (parameters.containsKey(XWikiWikiAttachmentFilter.PARAMETER_REVISION_AUTHOR)) {
            this.writer.writeElement(XARAttachmentModel.ELEMENT_REVISION_AUTHOR, (String) parameters.get(XWikiWikiAttachmentFilter.PARAMETER_REVISION_AUTHOR));
        }
        if (parameters.containsKey(XWikiWikiAttachmentFilter.PARAMETER_REVISION_DATE)) {
            this.writer.writeElement(XARAttachmentModel.ELEMENT_REVISION_DATE, toString((Date) parameters.get(XWikiWikiAttachmentFilter.PARAMETER_REVISION_DATE)));
        }
        if (parameters.containsKey(XWikiWikiAttachmentFilter.PARAMETER_REVISION)) {
            this.writer.writeElement(XARAttachmentModel.ELEMENT_REVISION, (String) parameters.get(XWikiWikiAttachmentFilter.PARAMETER_REVISION));
        }
        if (parameters.containsKey(XWikiWikiAttachmentFilter.PARAMETER_REVISION_COMMENT)) {
            this.writer.writeElement(XARAttachmentModel.ELEMENT_REVISION_COMMENT, (String) parameters.get(XWikiWikiAttachmentFilter.PARAMETER_REVISION_COMMENT));
        }
        if (content != null) {
            long contentSize = 0;
            this.writer.writeStartElement(XARAttachmentModel.ELEMENT_CONTENT);
            byte[] buffer = new byte[ATTACHMENT_BUFFER_CHUNK_SIZE];
            int readSize;
            do {
                try {
                    readSize = content.read(buffer, 0, ATTACHMENT_BUFFER_CHUNK_SIZE);
                } catch (IOException e) {
                    throw new FilterException("Failed to read content stream", e);
                }
                if (readSize > 0) {
                    String chunk;
                    if (readSize == ATTACHMENT_BUFFER_CHUNK_SIZE) {
                        chunk = Base64.encodeBase64String(buffer);
                    } else {
                        chunk = Base64.encodeBase64String(ArrayUtils.subarray(buffer, 0, readSize));
                    }
                    this.writer.writeCharacters(chunk);
                    contentSize += readSize;
                }
            } while (readSize == ATTACHMENT_BUFFER_CHUNK_SIZE);
            this.writer.writeEndElement();
            this.writer.writeElement(XARAttachmentModel.ELEMENT_CONTENT_SIZE, toString(contentSize));
        }
        this.writer.writeEndElement();
    } catch (Exception e) {
        throw new FilterException(String.format("Failed to write attachment [%s] from document [%s] with version [%s]", name, this.currentDocumentReference, this.currentDocumentVersion), e);
    }
}
Also used : FilterException(org.xwiki.filter.FilterException) IOException(java.io.IOException) Date(java.util.Date) IOException(java.io.IOException) FilterException(org.xwiki.filter.FilterException)

Example 7 with FilterException

use of org.xwiki.filter.FilterException in project xwiki-platform by xwiki.

the class XARWikiWriter method newEntry.

public OutputStream newEntry(LocalDocumentReference reference) throws FilterException {
    StringBuilder path = new StringBuilder();
    // Add space path
    addSpacePath(path, reference.getParent());
    // Add document name
    path.append(escapeXARPath(reference.getName()));
    // Add language
    if (reference.getLocale() != null && !reference.getLocale().equals(Locale.ROOT)) {
        path.append('.');
        path.append(reference.getLocale());
    }
    // Add extension
    path.append(".xml");
    String entryName = path.toString();
    ZipArchiveEntry zipentry = new ZipArchiveEntry(entryName);
    try {
        this.zipStream.putArchiveEntry(zipentry);
    } catch (IOException e) {
        throw new FilterException("Failed to add a new zip entry for [" + path + "]", e);
    }
    this.xarPackage.addEntry(reference, entryName);
    return this.zipStream;
}
Also used : ZipArchiveEntry(org.apache.commons.compress.archivers.zip.ZipArchiveEntry) FilterException(org.xwiki.filter.FilterException) IOException(java.io.IOException)

Example 8 with FilterException

use of org.xwiki.filter.FilterException in project xwiki-platform by xwiki.

the class AbstractWikiObjectPropertyReader method readObjectProperty.

public WikiObjectProperty readObjectProperty(XMLStreamReader xmlReader, XARInputProperties properties, WikiClass wikiClass) throws XMLStreamException, FilterException {
    xmlReader.nextTag();
    WikiObjectProperty property = new WikiObjectProperty();
    property.name = xmlReader.getLocalName();
    String type;
    if (wikiClass != null) {
        WikiClassProperty classProperty = wikiClass.properties.get(property.name);
        type = classProperty != null ? classProperty.type : null;
    } else {
        type = properties.getObjectPropertyType();
    }
    try {
        property.value = this.propertySerializerManager.getPropertySerializer(type).read(xmlReader);
    } catch (ComponentLookupException e) {
        throw new FilterException("Failed to get a property parser", e);
    }
    property.parameters.put(WikiObjectPropertyFilter.PARAMETER_TYPE, type);
    xmlReader.nextTag();
    return property;
}
Also used : ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) FilterException(org.xwiki.filter.FilterException) WikiClassProperty(org.xwiki.filter.xar.internal.input.ClassPropertyReader.WikiClassProperty)

Example 9 with FilterException

use of org.xwiki.filter.FilterException in project xwiki-platform by xwiki.

the class WikiReader method read.

public void read(Object filter, XARInputFilter proxyFilter) throws IOException, FilterException {
    InputStream stream;
    InputSource source = this.properties.getSource();
    if (source instanceof InputStreamInputSource) {
        stream = ((InputStreamInputSource) source).getInputStream();
    } else {
        throw new FilterException("Unsupported source type [" + source.getClass() + "]");
    }
    read(stream, filter, proxyFilter);
    // Close remaining opened spaces
    if (this.documentReader.getSentSpaceReference() != null) {
        for (EntityReference space = this.documentReader.getSentSpaceReference(); space != null; space = space.getParent()) {
            proxyFilter.endWikiSpace(space.getName(), FilterEventParameters.EMPTY);
        }
    }
    // Send extension event
    if (this.xarPackage.getPackageExtensionId() != null) {
        proxyFilter.beginExtension(this.xarPackage.getPackageExtensionId(), this.xarPackage.getPackageVersion(), FilterEventParameters.EMPTY);
        proxyFilter.endExtension(this.xarPackage.getPackageExtensionId(), this.xarPackage.getPackageVersion(), FilterEventParameters.EMPTY);
    }
}
Also used : InputStreamInputSource(org.xwiki.filter.input.InputStreamInputSource) InputSource(org.xwiki.filter.input.InputSource) ZipArchiveInputStream(org.apache.commons.compress.archivers.zip.ZipArchiveInputStream) InputStream(java.io.InputStream) EntityReference(org.xwiki.model.reference.EntityReference) FilterException(org.xwiki.filter.FilterException) InputStreamInputSource(org.xwiki.filter.input.InputStreamInputSource)

Example 10 with FilterException

use of org.xwiki.filter.FilterException in project xwiki-platform by xwiki.

the class DocumentInstanceInputEventGenerator method setWikiDocumentParameters.

@Override
public void setWikiDocumentParameters(String name, FilterEventParameters documentParameters) throws FilterException {
    DocumentReference reference = new DocumentReference(name, new SpaceReference(this.currentReference));
    XWikiContext xcontext = this.xcontextProvider.get();
    XWikiDocument defaultDocument;
    try {
        defaultDocument = xcontext.getWiki().getDocument(reference, xcontext);
    } catch (XWikiException e) {
        throw new FilterException("Failed to get document [" + reference + "]", e);
    }
    documentParameters.put(WikiDocumentFilter.PARAMETER_LOCALE, defaultDocument.getDefaultLocale());
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) SpaceReference(org.xwiki.model.reference.SpaceReference) XWikiContext(com.xpn.xwiki.XWikiContext) FilterException(org.xwiki.filter.FilterException) DocumentReference(org.xwiki.model.reference.DocumentReference) XWikiException(com.xpn.xwiki.XWikiException)

Aggregations

FilterException (org.xwiki.filter.FilterException)35 IOException (java.io.IOException)17 XWikiException (com.xpn.xwiki.XWikiException)10 XWikiContext (com.xpn.xwiki.XWikiContext)7 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)6 Date (java.util.Date)6 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)6 DocumentReference (org.xwiki.model.reference.DocumentReference)5 ArrayList (java.util.ArrayList)4 InputFilterStream (org.xwiki.filter.input.InputFilterStream)3 OutputFilterStream (org.xwiki.filter.output.OutputFilterStream)3 WikiReference (org.xwiki.model.reference.WikiReference)3 QueryException (org.xwiki.query.QueryException)3 XWikiAttachment (com.xpn.xwiki.doc.XWikiAttachment)2 BaseObject (com.xpn.xwiki.objects.BaseObject)2 BaseClass (com.xpn.xwiki.objects.classes.BaseClass)2 InputStream (java.io.InputStream)2 URL (java.net.URL)2 HashSet (java.util.HashSet)2 ComponentManager (org.xwiki.component.manager.ComponentManager)2