/** * Metodo estico que retorna el array de bytes de un content * * @param content * @return * @throws IOException */ public static byte[] getByteContent(Content content) throws Exception { ContentFormat format = content.getFormat(); String mimeType = format.getMimetype(); String ext = mimeType.split("/")[1]; File fileTemp; byte[] contentBytes = null; try { fileTemp = File.createTempFile(AlfrescoKeys.FILE_TEMP, "." + ext); // Se convierte el contenido en un fichero temporal ContentUtils.copyContentToFile(content, fileTemp); // Se recupera el inputStream del fichero temporal y se retornan los byte[] InputStream viewStream = new FileInputStream(fileTemp); contentBytes = ContentUtils.convertToByteArray(viewStream); // Se borra el fichero temproal boolean a = fileTemp.delete(); } catch (IOException e) { log.error("Error en el fichero", e); throw e; } catch (Exception e) { // TODO Auto-generated catch block log.error(e); throw e; } return contentBytes; }
/** * Gets the content from Alfresco DMS. * * @param reference not null {@link Reference} * @param store the store. * @return not null {@link Document} * @throws RemoteException If the {@link Reference} is not equivalent to an existing {@link Node}. * @throws DMSException if an error occurs in DMS back end */ private Document getContent(Reference reference, Store store) throws RemoteException, DMSException { Validate.notNull(reference); Validate.notNull(store); /** Get the content service. */ final ContentServiceSoapBindingStub contentService = this.getContentService(); Document result = null; /** Read the content from the respository. */ Content[] readResult; readResult = contentService.read( new Predicate(new Reference[] {reference}, store, null), Constants.PROP_CONTENT); Content content = readResult[0]; if (readResult != null && content != null && content.getUrl() != null) { final String fileName = this.getName(content.getUrl()); File tempFile = new File(ResourcesLocator.getResourcesTempDir() + fileName); /** Get the content from the download servlet using the URL and display it. */ /** Copy content in a temporary direcoty. */ ContentUtils.copyContentToFile(content, tempFile); try { FileInputStream fin; fin = new FileInputStream(tempFile); int bytesCount = (int) tempFile.length(); byte[] contentBytes = new byte[bytesCount]; fin.read(contentBytes); result = new Document(fileName, content.getFormat().getMimetype(), contentBytes); fin.close(); this.deleteTempFile(tempFile); } catch (IOException e) { String message = "IO Error: "; LOG.error(message, e); DMSFault dmsFault = new DMSFault(); dmsFault.setMessage(message); throw new DMSException(message, dmsFault, e); } } return result; }
/** * Tests the checkinExternal service method * * @throws Exception */ public void testCheckinExternal() throws Exception { // First we need to check a document out Reference workingCopy = doCheckOut(); // Check in with external content NamedValue[] comments = getVersionComments(); ContentFormat contentFormat = new ContentFormat(Constants.MIMETYPE_TEXT_PLAIN, "UTF-8"); Reference origionalNode = this.authoringService.checkinExternal( workingCopy, comments, false, contentFormat, SECOND_VERSION_CONTENT.getBytes()); // Check the origianl Node assertNotNull(origionalNode); Content[] contents = this.contentService.read( new Predicate(new Reference[] {origionalNode}, BaseWebServiceSystemTest.store, null), Constants.PROP_CONTENT.toString()); Content readResult = contents[0]; assertNotNull(readResult); String checkedInContent = ContentUtils.getContentAsString(readResult); assertNotNull(checkedInContent); assertEquals(SECOND_VERSION_CONTENT, checkedInContent); }
/** * Tests the createVersion service method * * @throws Exception */ public void testVersionMethods() throws Exception { Reference reference = createContentAtRoot("create_version_test.txt", INITIAL_VERSION_CONTENT); Predicate predicate = convertToPredicate(reference); // Get the version history (before its been versioned) VersionHistory emptyVersionHistory = this.authoringService.getVersionHistory(reference); assertNotNull(emptyVersionHistory); assertNull(emptyVersionHistory.getVersions()); // Create the version VersionResult result = this.authoringService.createVersion(predicate, getVersionComments(), false); assertNotNull(result); assertEquals(1, result.getNodes().length); assertEquals(1, result.getVersions().length); Version version = result.getVersions()[0]; assertEquals("1.0", version.getLabel()); // TODO check commentaries // TODO check creator // Get the version history this.authoringService.createVersion(predicate, getVersionComments(), false); VersionHistory versionHistory = this.authoringService.getVersionHistory(reference); assertNotNull(versionHistory); assertEquals(2, versionHistory.getVersions().length); // TODO some more tests ... // Update the content this.contentService.write( reference, Constants.PROP_CONTENT, SECOND_VERSION_CONTENT.getBytes(), null); // Create another version VersionResult versionResult2 = this.authoringService.createVersion(predicate, getVersionComments(), false); assertNotNull(versionResult2); assertEquals(1, versionResult2.getNodes().length); assertEquals(1, versionResult2.getVersions().length); Version version2 = versionResult2.getVersions()[0]; assertEquals("1.3", version2.getLabel()); // TODO check commentaries // TODO check creator // Check the version history VersionHistory versionHistory2 = this.authoringService.getVersionHistory(reference); assertNotNull(versionHistory2); assertEquals(4, versionHistory2.getVersions().length); // TODO some more tests ... // Create a major version NamedValue versionVal = Utils.createNamedValue("versionType", "MAJOR"); NamedValue descriptionVal = Utils.createNamedValue("description", "new description"); NamedValue[] comments = new NamedValue[] {versionVal, descriptionVal}; VersionResult result5 = this.authoringService.createVersion(predicate, comments, false); assertNotNull(result5); assertEquals(1, result5.getNodes().length); assertEquals(1, result5.getVersions().length); Version version5 = result5.getVersions()[0]; assertEquals("2.0", version5.getLabel()); // Confirm the current content of the node Content[] contents = this.contentService.read( new Predicate(new Reference[] {reference}, BaseWebServiceSystemTest.store, null), Constants.PROP_CONTENT.toString()); Content readResult1 = contents[0]; String content1 = ContentUtils.getContentAsString(readResult1); assertEquals(SECOND_VERSION_CONTENT, content1); // Revert the node to the first version this.authoringService.revertVersion(reference, "1.0"); // Confirm that the state of the node has been reverted Content[] contents2 = this.contentService.read( new Predicate(new Reference[] {reference}, BaseWebServiceSystemTest.store, null), Constants.PROP_CONTENT.toString()); Content readResult2 = contents2[0]; String content2 = ContentUtils.getContentAsString(readResult2); assertEquals(INITIAL_VERSION_CONTENT, content2); // Now delete the version history VersionHistory deletedVersionHistory = this.authoringService.deleteAllVersions(reference); assertNotNull(deletedVersionHistory); assertNull(deletedVersionHistory.getVersions()); // Check the version history VersionHistory versionHistory3 = this.authoringService.getVersionHistory(reference); assertNotNull(versionHistory3); assertNull(versionHistory3.getVersions()); }