/** * This will update the asset's content (checking it out if it is not * already). This will not save the session or create a new version of the * node (this has to be done seperately, as several properties may change as * part of one edit). This is only used if the asset is a textual asset. For * binary, use the updateBinaryContent method instead. */ public AssetItem updateContent(String newRuleContent) throws RulesRepositoryException { checkout(); try { if ( this.isBinary() ) { this.updateBinaryContentAttachment( new ByteArrayInputStream( newRuleContent.getBytes() ) ); } this.node.setProperty( CONTENT_PROPERTY_NAME, newRuleContent ); return this; } catch ( RepositoryException e ) { log.error( "Unable to update the asset content", e ); throw new RulesRepositoryException( e ); } }
/** * This will update the asset's content (checking it out if it is not * already). This will not save the session or create a new version of the * node (this has to be done seperately, as several properties may change as * part of one edit). This is only used if the asset is a textual asset. For * binary, use the updateBinaryContent method instead. */ public AssetItem updateContent(String newRuleContent) throws RulesRepositoryException { checkout(); try { if ( this.isBinary() ) { this.updateBinaryContentAttachment( new ByteArrayInputStream( newRuleContent.getBytes() ) ); } this.node.setProperty( CONTENT_PROPERTY_NAME, newRuleContent ); return this; } catch ( RepositoryException e ) { log.error( "Unable to update the asset content", e ); throw new RulesRepositoryException( e ); } }
/** * Only for use in the StorageEventManager, for passing the fromRepo * parameter * * returns the string contents of the asset node. If this is a binary asset, * this will return null (use getBinaryContent instead). */ //REVISIT: This return the binary data as a byte array if its binary asset. //Return null is better? public String getContent(Boolean fromRepo) throws RulesRepositoryException { try { if ( StorageEventManager.hasLoadEvent() && !fromRepo ) { return IOUtils.toString( StorageEventManager.getLoadEvent().loadContent( this ) ); } if ( isBinary() ) { return new String( this.getBinaryContentAsBytes() ); } Node ruleNode = getVersionContentNode(); if ( ruleNode.hasProperty( CONTENT_PROPERTY_NAME ) ) { Property data = ruleNode.getProperty( CONTENT_PROPERTY_NAME ); return data.getValue().getString(); } else { return ""; } } catch ( Exception e ) { log.error( "Caught Exception", e ); throw new RulesRepositoryException( e ); } }
/** * Only for use in the StorageEventManager, for passing the fromRepo * parameter * * returns the string contents of the asset node. If this is a binary asset, * this will return null (use getBinaryContent instead). */ //REVISIT: This return the binary data as a byte array if its binary asset. //Return null is better? public String getContent(Boolean fromRepo) throws RulesRepositoryException { try { if ( StorageEventManager.hasLoadEvent() && !fromRepo ) { return IOUtils.toString( StorageEventManager.getLoadEvent().loadContent( this ) ); } if ( isBinary() ) { return new String( this.getBinaryContentAsBytes() ); } Node ruleNode = getVersionContentNode(); if ( ruleNode.hasProperty( CONTENT_PROPERTY_NAME ) ) { Property data = ruleNode.getProperty( CONTENT_PROPERTY_NAME ); return data.getValue().getString(); } else { return ""; } } catch ( Exception e ) { log.error( "Caught Exception", e ); throw new RulesRepositoryException( e ); } }
return IOUtils.toByteArray( StorageEventManager.getLoadEvent().loadContent( this ) ); if ( isBinary() ) { Property data = assetNode.getProperty( CONTENT_PROPERTY_BINARY_NAME ); InputStream in = data.getBinary().getStream();
return IOUtils.toByteArray( StorageEventManager.getLoadEvent().loadContent( this ) ); if ( isBinary() ) { Property data = assetNode.getProperty( CONTENT_PROPERTY_BINARY_NAME ); InputStream in = data.getBinary().getStream();
/** * This will restore the historical version, save, and check it in as a new * version with the given comment. * * @param versionToRestore * @param headVersion * @param comment */ public void restoreHistoricalAsset(AssetItem versionToRestore, AssetItem headVersion, String comment) { headVersion.checkout(); if (versionToRestore.isBinary()) { headVersion.updateBinaryContentAttachment(versionToRestore.getBinaryContentAttachment()); } else { headVersion.updateContent(versionToRestore.getContent()); } headVersion.checkin(comment); }
/** * This will restore the historical version, save, and check it in as a new * version with the given comment. * * @param versionToRestore * @param headVersion * @param comment */ public void restoreHistoricalAsset(AssetItem versionToRestore, AssetItem headVersion, String comment) { headVersion.checkout(); if (versionToRestore.isBinary()) { headVersion.updateBinaryContentAttachment(versionToRestore.getBinaryContentAttachment()); } else { headVersion.updateContent(versionToRestore.getContent()); } headVersion.checkin(comment); }
@Test public void testBinaryAsset() throws Exception { AssetItem item = getRepo().loadDefaultModule().addAsset( "testBinaryAsset", "yeah" ); String data = "abc 123"; ByteArrayInputStream in = new ByteArrayInputStream(data.getBytes()); item.updateBinaryContentAttachment( in ); item.updateBinaryContentAttachmentFileName( "x.x" ); in.close(); assertEquals(data, item.getContent()); assertFalse(item.getNode().hasProperty( AssetItem.CONTENT_PROPERTY_NAME )); assertTrue(item.getNode().hasProperty( AssetItem.CONTENT_PROPERTY_BINARY_NAME )); item.checkin( "lalalala" ); assertTrue(item.isBinary()); item = getRepo().loadDefaultModule().loadAsset( "testBinaryAsset" ); InputStream in2 = item.getBinaryContentAttachment(); assertNotNull(in2); byte[] data2 = item.getBinaryContentAsBytes(); assertEquals(data, new String(data2)); assertEquals("x.x", item.getBinaryContentAttachmentFileName()); assertTrue(item.isBinary()); item.updateContent("qed"); item.checkin(""); item = getRepo().loadAssetByUUID(item.getUUID()); assertEquals("qed", item.getContent()); }
@Test public void testPost() throws Exception { RulesRepository repo = RepositorySessionUtil.getRepository(); ModuleItem pkg = repo.createModule("testRestPost", "", ModuleItem.MODULE_FORMAT); pkg.updateStringProperty("This is some header", ModuleItem.HEADER_PROPERTY_NAME); repo.save(); RestAPI api = new RestAPI(repo); api.setAssetValidator(new AssetValidator()); ByteArrayInputStream in = new ByteArrayInputStream("abc".getBytes()); api.post("/packages/testRestPost/asset1.drl", in, "a comment"); AssetItem a = pkg.loadAsset("asset1"); assertFalse(a.isBinary()); assertEquals("drl", a.getFormat()); assertEquals("abc", a.getContent()); assertEquals("a comment", a.getCheckinComment()); in = new ByteArrayInputStream("qed".getBytes()); api.post("/packages/testRestPost/asset2.xls", in, "a comment"); a = pkg.loadAsset("asset2"); assertTrue(a.isBinary()); String s = new String(a.getBinaryContentAsBytes()); assertEquals("qed", s); assertEquals("a comment", a.getCheckinComment()); assertEquals("xls", a.getFormat()); List<AssetItem> assets = RulesRepositoryTest.iteratorToList(repo.loadModule("testRestPost").listAssetsByFormat(new String[] {"drl", "xls"})); assertEquals(2, assets.size()); }
@Test public void testBinaryAssetForShareableAsset() throws Exception { AssetItem asset = getRepo().loadGlobalArea().addAsset("testBinaryAssetForShareableAsset", "desc"); asset.checkin("version0"); AssetItem linkedAsset = getDefaultPackage().addAssetImportedFromGlobalArea(asset.getName()); String data = "abc 123"; ByteArrayInputStream in = new ByteArrayInputStream(data.getBytes()); linkedAsset.updateBinaryContentAttachment(in); linkedAsset.updateBinaryContentAttachmentFileName("x.x"); in.close(); assertEquals(data, linkedAsset.getContent()); assertFalse(linkedAsset.getNode().hasProperty(AssetItem.CONTENT_PROPERTY_NAME)); assertTrue(linkedAsset.getNode().hasProperty(AssetItem.CONTENT_PROPERTY_BINARY_NAME)); linkedAsset.checkin("lalalala"); assertTrue(linkedAsset.isBinary()); asset = getRepo().loadGlobalArea().loadAsset("testBinaryAssetForShareableAsset"); InputStream in2 = asset.getBinaryContentAttachment(); assertNotNull(in2); byte[] data2 = asset.getBinaryContentAsBytes(); assertEquals(data, new String(data2)); assertEquals("x.x", asset.getBinaryContentAttachmentFileName()); assertTrue(asset.isBinary()); linkedAsset.updateContent("qed"); linkedAsset.checkin(""); linkedAsset = getRepo().loadAssetByUUID(linkedAsset.getUUID()); assertEquals("qed", linkedAsset.getContent()); }
@Test public void testGetContentForShareableAsset() { AssetItem asset = getRepo().loadGlobalArea().addAsset("testGetContentForShareableAsset", "test content"); AssetItem linkedAsset = getRepo().loadDefaultModule().addAssetImportedFromGlobalArea(asset.getName()); linkedAsset.updateContent("test content"); linkedAsset.updateFormat("drl"); assertNotNull(linkedAsset.getNode()); assertEquals("test content", linkedAsset.getContent()); assertEquals("test content", asset.getContent()); assertFalse(linkedAsset.isBinary()); assertFalse(asset.isBinary()); assertNotNull(linkedAsset.getBinaryContentAsBytes()); assertNotNull(linkedAsset.getBinaryContentAttachment()); String content = new String(linkedAsset.getBinaryContentAsBytes()); assertNotNull(content); content = new String(asset.getBinaryContentAsBytes()); assertNotNull(content); }
@Test public void testGetContent() { AssetItem ruleItem1 = getRepo().loadDefaultModule().addAsset("testGetContent", "test content"); ruleItem1.updateContent( "test content" ); ruleItem1.updateFormat("drl"); assertNotNull(ruleItem1); assertNotNull(ruleItem1.getNode()); assertEquals("test content", ruleItem1.getContent()); assertFalse(ruleItem1.isBinary()); assertNotNull(ruleItem1.getBinaryContentAsBytes()); assertNotNull(ruleItem1.getBinaryContentAttachment()); String content = new String(ruleItem1.getBinaryContentAsBytes()); assertNotNull(content); }