public void updateDocumentPreview(String originalDocumentId){ //Get preview node id of source document to update previewNodeId = "...."; //Get input stream of file to write in OAK InputStream pdfInputStream = ....; //Now update the document updateDocumentContent(previewNodeId, pdfInputStream); } //Here the used method stack: /** * Updates the contents of a versioned file in the content repository * * @param documentId The identifier of the file that is being updated * @param fileContents The new contents of the file * @return A document containing information about the newly created version * @throws RuntimeRepositoryException In case any error with the JCR occurs */ public Document updateDocumentContent(String documentId, InputStream fileContents) { try { Binary binary = jcrSession.getValueFactory().createBinary(fileContents); try { Node file = jcrSession.getNodeByIdentifier(documentId); Version updatedVersion = versionFile(file, binary); return toDocument(updatedVersion); } finally { binary.dispose(); } } catch (ItemNotFoundException e) { throw new RuntimeItemNotFoundException(e); } catch (RepositoryException e) { throw new RuntimeRepositoryException(e); } } /** * Creates a version of the given file node, with the given fileContent * binary object as its contents. Also marks that version with a label, specifying * that it is the last (current) version of that file. * * @param file The file node in the CR that is being versioned * @param fileContent The contents of the file for that version * @return A new version of the given file, with new contents */ private Version versionFile(Node file, Binary fileContent) { try { VersionManager versionManager = file.getSession().getWorkspace().getVersionManager(); versionManager.checkout(file.getPath()); file.getNode(Property.JCR_CONTENT).setProperty(Property.JCR_LAST_MODIFIED, Calendar.getInstance()); file.getNode(Property.JCR_CONTENT).setProperty(Property.JCR_LAST_MODIFIED_BY, SecurityUtil.getCurrentUserLogin()); file.getNode(Property.JCR_CONTENT).setProperty(Property.JCR_DATA, fileContent); jcrSession.save(); Version version = versionManager.checkin(file.getPath()); versionManager.getVersionHistory(file.getPath()).addVersionLabel(version.getName(), NOAH_LAST_VERSION_LABEL, DO_MOVE_LABEL); return version; } catch (RepositoryException e) { throw new RuntimeRepositoryException(e); } } //INSIDE OF THIS METHOD THE EXCEPTION IS HAPPENING /** * Converts a file from the CR to a document. * Note: Converts also a version of a file from the CR to a document * if the node is not versioned. * * @param documentNode The version object - if not versioned, then the unversioned node. * @return A document filled with metadata from the particular node or version of the node * @throws RuntimeRepositoryException In case any error with the JCR occurs */ private Document toDocument(Node documentNode) { try { Node file = null; String identifier = null; // flag to show if current node is already versioned boolean isVersioned = true; if( documentNode instanceof Version ){ //Versioned node file = documentNode.getNode(Node.JCR_FROZEN_NODE); identifier = JcrUtil.getStringProperty(file, Property.JCR_FROZEN_UUID); }else{ //unversioned node, for example empty preview document file = documentNode; isVersioned = false; identifier = documentNode.getIdentifier(); } Node fileContent = file.getNode(Property.JCR_CONTENT); String versionName = documentNode.getName(); ZonedDateTime creationDate = DateUtil.toZonedDateTime(file.getProperty(Property.JCR_CREATED).getDate()); ZonedDateTime lastModificationDate = DateUtil.toZonedDateTime(JcrUtils.getLastModified(fileContent)); String createdBy = JcrUtil.getStringProperty(file, CustomProperty.NOAH_CREATED_BY); String lastModifiedBy = JcrUtil.getStringProperty(fileContent, Property.JCR_LAST_MODIFIED_BY); String title = JcrUtils.getStringProperty(file, Property.JCR_TITLE, null); String description = JcrUtils.getStringProperty(file, Property.JCR_DESCRIPTION, null); String entityUrl = JcrUtil.getStringProperty(file, CustomProperty.NOAH_ENTITY_URL); String documentTypeValue = JcrUtils.getStringProperty(file, CustomProperty.NOAH_DOCUMENT_TYPE_VALUE, null); String documentTypeLabel = JcrUtils.getStringProperty(file, CustomProperty.NOAH_DOCUMENT_TYPE_LABEL, null); ValueListEntry documentType = isNoneEmpty(documentTypeValue, documentTypeLabel) ? new ValueListEntry(documentTypeValue, documentTypeLabel) : null; String contentType = JcrUtil.getStringProperty(fileContent, Property.JCR_MIMETYPE); long fileSize = fileContent.getProperty(Property.JCR_DATA).getLength(); String previewDocId = JcrUtils.getStringProperty(file, CustomProperty.NOAH_PREVIEW_DOC_ID, null); String previewImageId = JcrUtils.getStringProperty(file, CustomProperty.NOAH_PREVIEW_IMAGE_ID, null); // -------------------------------------------------------------------- // Assign previews id in the right version (from frozen node if versioned): // We do this in a try block,because of the fact,that the generation process // of the previews is separated from the generating process of the document // or the document content update process is also separated from the // preview generation process which always happens afterward. If this method // was called during a document content update or new document creation, // then the preview node still has not the same version name as the // original document because the original document is created first. But // as soon as the preview node is created it is also accessable through // the same version name. // -------------------------------------------------------------------- //TODO: Unfortunately at the moment it seems that there is no method to check if in the version history a node has a certain version name (That is why this is in a try block). try { if (isVersioned && previewDocId != null) { Node previewNode = jcrSession.getNodeByIdentifier(previewDocId); VersionManager versionManager = jcrSession.getWorkspace().getVersionManager(); VersionHistory history = versionManager.getVersionHistory(previewNode.getPath()); if (history.getVersionLabels().length > 0) { Version previewVersion = history.getVersion(versionName); Node previewFrozenNode = previewVersion.getNode(Node.JCR_FROZEN_NODE); previewDocId = JcrUtil.getStringProperty(previewFrozenNode, Property.JCR_FROZEN_UUID); } } if (isVersioned && previewImageId != null) { Node previewImageNode = jcrSession.getNodeByIdentifier(previewImageId); VersionManager versionManager = jcrSession.getWorkspace().getVersionManager(); VersionHistory history = versionManager.getVersionHistory(previewImageNode.getPath()); if (history.getVersionLabels().length > 0) { Version previewVersion = history.getVersion(versionName); Node previewFrozenNode = previewVersion.getNode(Node.JCR_FROZEN_NODE); previewImageId = JcrUtil.getStringProperty(previewFrozenNode, Property.JCR_FROZEN_UUID); } } }catch (Exception e){ // That's ok if a preview node still has not the same version name (it is in generation-happens only one time for each version) } return new Document(identifier,previewDocId, previewImageId, versionName, creationDate, lastModificationDate, createdBy, lastModifiedBy, title, description, entityUrl, documentType, contentType, fileSize); } catch (RepositoryException e) { throw new RuntimeRepositoryException(e); } }