Search in sources :

Example 1 with CDResourceFile

use of org.openntf.domino.nsfdata.structs.obj.CDResourceFile in project org.openntf.domino by OpenNTF.

the class AbstractDesignFileResource method setFileDataRaw.

protected void setFileDataRaw(final String itemName, final byte[] fileData) {
    // To set the file content, first clear out existing content
    // $NON-NLS-1$ //$NON-NLS-2$
    List<XMLNode> fileDataNodes = getDxl().selectNodes("//item[@name='" + XMLDocument.escapeXPathValue(itemName) + "']");
    for (int i = fileDataNodes.size() - 1; i >= 0; i--) {
        fileDataNodes.get(i).getParentNode().removeChild(fileDataNodes.get(i));
    }
    // Now create a CD record for the file data
    // CDResourceFile record = CDResourceFile.fromFileData(fileData, "");
    // byte[] reconData = record.getBytes();
    // $NON-NLS-1$
    CDResourceFile record = new CDResourceFile("");
    record.setFileData(fileData);
    byte[] reconData = record.getData().array();
    // Write out the first chunk
    int firstChunk = reconData.length > 20544 ? 20544 : reconData.length;
    String firstChunkData = printBase64Binary(Arrays.copyOfRange(reconData, 0, firstChunk));
    // $NON-NLS-1$
    XMLNode documentNode = getDxl().selectSingleNode("//note");
    // $NON-NLS-1$
    XMLNode fileDataNode = documentNode.addChildElement("item");
    // $NON-NLS-1$
    fileDataNode.setAttribute("name", itemName);
    // $NON-NLS-1$
    fileDataNode = fileDataNode.addChildElement("rawitemdata");
    // $NON-NLS-1$ //$NON-NLS-2$
    fileDataNode.setAttribute("type", "1");
    fileDataNode.setText(firstChunkData);
    // Write out any remaining chunks
    int remaining = reconData.length - firstChunk;
    int chunks = remaining / 20516;
    if (remaining % 20516 > 0) {
        chunks++;
    }
    int offset = firstChunk;
    for (int i = 0; i < chunks; i++) {
        int chunkSize = remaining > 20516 ? 20516 : remaining;
        String chunkData = printBase64Binary(Arrays.copyOfRange(reconData, offset, offset + chunkSize));
        // $NON-NLS-1$
        fileDataNode = documentNode.addChildElement("item");
        // $NON-NLS-1$
        fileDataNode.setAttribute("name", itemName);
        // $NON-NLS-1$
        fileDataNode = fileDataNode.addChildElement("rawitemdata");
        // $NON-NLS-1$ //$NON-NLS-2$
        fileDataNode.setAttribute("type", "1");
        fileDataNode.setText(chunkData);
        remaining -= 20516;
        offset += chunkSize;
    }
    // Also set the file size if we're setting the main field
    if (DEFAULT_FILEDATA_FIELD.equals(itemName)) {
        // $NON-NLS-1$
        setItemValue("$FileSize", String.valueOf(fileData.length), FLAG_SIGN_SUMMARY);
    }
}
Also used : XMLNode(org.openntf.domino.utils.xml.XMLNode) CDResourceFile(org.openntf.domino.nsfdata.structs.obj.CDResourceFile)

Example 2 with CDResourceFile

use of org.openntf.domino.nsfdata.structs.obj.CDResourceFile in project org.openntf.domino by OpenNTF.

the class AbstractDesignFileResource method getFileDataRaw.

/**
 * Reads a FileData Item in RAW-mode
 *
 * @param itemName
 * @return
 */
protected byte[] getFileDataRaw(final String itemName) {
    try {
        ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
        for (XMLNode rawitemdata : // 
        getDxl().selectNodes("//item[@name='" + XMLDocument.escapeXPathValue(itemName) + "']/rawitemdata")) {
            // $NON-NLS-1$ //$NON-NLS-2$
            String rawData = rawitemdata.getText();
            byte[] thisData = parseBase64Binary(rawData);
            byteStream.write(thisData);
        }
        if (byteStream.size() > 0) {
            byte[] data = byteStream.toByteArray();
            CData cdata = new CData(data);
            CDObject obj = CDObject.create(cdata);
            // Files may be attached either as FILE or as EVENT... (ssjs for example) Damn. This makes everything quite complex
            if (obj instanceof CDResourceFile)
                return ((CDResourceFile) obj).getFileData();
            if (obj instanceof CDResourceEvent)
                return ((CDResourceEvent) obj).getFileData();
            // $NON-NLS-1$
            throw new IllegalStateException("Cannot decode " + obj.getClass().getName());
        } else {
            byteStream = new ByteArrayOutputStream();
            for (XMLNode rawitemdata : // 
            getDxl().selectNodes("//file[@name='" + XMLDocument.escapeXPathValue(itemName) + "']/filedata")) {
                // $NON-NLS-1$ //$NON-NLS-2$
                String rawData = rawitemdata.getText();
                byte[] thisData = parseBase64Binary(rawData);
                byteStream.write(thisData);
            }
            return byteStream.toByteArray();
        }
    } catch (IOException ioe) {
        DominoUtils.handleException(ioe);
        return null;
    }
}
Also used : CDResourceEvent(org.openntf.domino.nsfdata.structs.obj.CDResourceEvent) XMLNode(org.openntf.domino.utils.xml.XMLNode) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) CDObject(org.openntf.domino.nsfdata.structs.obj.CDObject) CData(org.openntf.domino.nsfdata.structs.cd.CData) CDResourceFile(org.openntf.domino.nsfdata.structs.obj.CDResourceFile)

Aggregations

CDResourceFile (org.openntf.domino.nsfdata.structs.obj.CDResourceFile)2 XMLNode (org.openntf.domino.utils.xml.XMLNode)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 CData (org.openntf.domino.nsfdata.structs.cd.CData)1 CDObject (org.openntf.domino.nsfdata.structs.obj.CDObject)1 CDResourceEvent (org.openntf.domino.nsfdata.structs.obj.CDResourceEvent)1