[jboss-svn-commits] JBL Code SVN: r19545 - in labs/jbossrules/trunk/drools-repository/src: main/java/org/drools/repository/remoteapi and 2 other directories.
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Mon Apr 14 04:50:15 EDT 2008
Author: michael.neale at jboss.com
Date: 2008-04-14 04:50:15 -0400 (Mon, 14 Apr 2008)
New Revision: 19545
Added:
labs/jbossrules/trunk/drools-repository/src/main/java/org/drools/repository/remoteapi/
labs/jbossrules/trunk/drools-repository/src/main/java/org/drools/repository/remoteapi/Response.java
labs/jbossrules/trunk/drools-repository/src/main/java/org/drools/repository/remoteapi/RestAPI.java
labs/jbossrules/trunk/drools-repository/src/test/java/org/drools/repository/remoteapi/
labs/jbossrules/trunk/drools-repository/src/test/java/org/drools/repository/remoteapi/RestAPITest.java
Modified:
labs/jbossrules/trunk/drools-repository/src/main/java/org/drools/repository/AssetItem.java
labs/jbossrules/trunk/drools-repository/src/main/java/org/drools/repository/AssetItemIterator.java
labs/jbossrules/trunk/drools-repository/src/main/java/org/drools/repository/PackageItem.java
labs/jbossrules/trunk/drools-repository/src/test/java/org/drools/repository/AssetItemTest.java
Log:
remote REST api start
Modified: labs/jbossrules/trunk/drools-repository/src/main/java/org/drools/repository/AssetItem.java
===================================================================
--- labs/jbossrules/trunk/drools-repository/src/main/java/org/drools/repository/AssetItem.java 2008-04-14 08:05:00 UTC (rev 19544)
+++ labs/jbossrules/trunk/drools-repository/src/main/java/org/drools/repository/AssetItem.java 2008-04-14 08:50:15 UTC (rev 19545)
@@ -14,7 +14,7 @@
/**
* The RuleItem class is used to abstract away the details of the underlying JCR repository.
* It is used to pass information about rules stored in the repository.
- *
+ *
* @author btruitt
*/
public class AssetItem extends CategorisableItem {
@@ -27,7 +27,7 @@
public static final String CONTENT_PROPERTY_NAME = "drools:content";
public static final String CONTENT_PROPERTY_BINARY_NAME = "drools:binaryContent";
public static final String CONTENT_PROPERTY_ATTACHMENT_FILENAME = "drools:attachmentFileName";
-
+
/**
* The name of the date effective property on the rule node type
*/
@@ -39,15 +39,15 @@
public static final String DATE_EXPIRED_PROPERTY_NAME = "drools:dateExpired";
public static final String PACKAGE_NAME_PROPERTY = "drools:packageName";
-
-
+
+
/**
* Constructs a RuleItem object, setting its node attribute to the specified node.
- *
+ *
* @param rulesRepository the rulesRepository that instantiated this object
* @param node the node in the repository that this RuleItem corresponds to
- * @throws RulesRepositoryException
+ * @throws RulesRepositoryException
*/
public AssetItem(RulesRepository rulesRepository,
Node node) throws RulesRepositoryException {
@@ -55,7 +55,7 @@
node );
try {
- //make sure this node is a rule node
+ //make sure this node is a rule node
if ( !(this.node.getPrimaryNodeType().getName().equals( RULE_NODE_TYPE_NAME ) || isHistoricalVersion()) ) {
String message = this.node.getName() + " is not a node of type " + RULE_NODE_TYPE_NAME + " nor nt:version. It is a node of type: " + this.node.getPrimaryNodeType().getName();
log.error( message );
@@ -67,7 +67,7 @@
throw new RulesRepositoryException( e );
}
}
-
+
public AssetItem() {
super(null, null);
}
@@ -92,9 +92,22 @@
throw new RulesRepositoryException( e );
}
}
-
+
/**
- * If this asset contains binary data, this is how you return it.
+ * True if this is a binary asset (or has binary content).
+ */
+ public boolean isBinary() {
+ try {
+ Node ruleNode = getVersionContentNode();
+ return ruleNode.hasProperty( CONTENT_PROPERTY_BINARY_NAME );
+ } catch (RepositoryException e) {
+ log.error(e);
+ throw new RulesRepositoryException(e);
+ }
+ }
+
+ /**
+ * If this asset contains binary data, this is how you return it.
* Otherwise it will return null.
*/
public InputStream getBinaryContentAttachment() {
@@ -110,14 +123,14 @@
log.error( "Caught Exception",
e );
throw new RulesRepositoryException( e );
- }
+ }
}
-
+
/** Get the name of the "file" attachment, if one is set. Null otherwise */
public String getBinaryContentAttachmentFileName() {
return getStringProperty( CONTENT_PROPERTY_ATTACHMENT_FILENAME );
}
-
+
/**
* This is a convenience method for returning the binary data as a byte array.
*/
@@ -127,10 +140,10 @@
if ( ruleNode.hasProperty( CONTENT_PROPERTY_BINARY_NAME ) ) {
Property data = ruleNode.getProperty( CONTENT_PROPERTY_BINARY_NAME );
InputStream in = data.getStream();
-
+
// Create the byte array to hold the data
byte[] bytes = new byte[(int) data.getLength()];
-
+
// Read in the bytes
int offset = 0;
int numRead = 0;
@@ -138,14 +151,14 @@
&& (numRead=in.read(bytes, offset, bytes.length-offset)) >= 0) {
offset += numRead;
}
-
+
// Ensure all the bytes have been read in
if (offset < bytes.length) {
throw new RulesRepositoryException("Could not completely read asset "+ getName());
}
-
+
// Close the input stream and return bytes
- in.close();
+ in.close();
return bytes;
} else {
return null;
@@ -154,7 +167,7 @@
log.error( e );
if (e instanceof RuntimeException) throw (RuntimeException) e;
throw new RulesRepositoryException( e );
- }
+ }
}
@@ -180,9 +193,9 @@
/**
* Creates a new version of this object's rule node, updating the effective date for the
- * rule node.
- *
- * @param newDateEffective the new effective date for the rule
+ * rule node.
+ *
+ * @param newDateEffective the new effective date for the rule
* @throws RulesRepositoryException
*/
public void updateDateEffective(Calendar newDateEffective) throws RulesRepositoryException {
@@ -220,9 +233,9 @@
/**
* Creates a new version of this object's rule node, updating the expired date for the
- * rule node.
- *
- * @param newDateExpired the new expired date for the rule
+ * rule node.
+ *
+ * @param newDateExpired the new expired date for the rule
* @throws RulesRepositoryException
*/
public void updateDateExpired(Calendar newDateExpired) throws RulesRepositoryException {
@@ -240,10 +253,10 @@
/**
* 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 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.
+ * instead.
*/
public AssetItem updateContent(String newRuleContent) throws RulesRepositoryException {
checkout();
@@ -256,7 +269,7 @@
throw new RulesRepositoryException( e );
}
}
-
+
/**
* If the asset is a binary asset, then use this to update the content
* (do NOT use text).
@@ -264,14 +277,14 @@
public AssetItem updateBinaryContentAttachment(InputStream data) {
checkout();
try {
- this.node.setProperty( CONTENT_PROPERTY_BINARY_NAME, data );
+ this.node.setProperty( CONTENT_PROPERTY_BINARY_NAME, data );
return this;
} catch (RepositoryException e ) {
log.error( "Unable to update the assets binary content", e );
throw new RulesRepositoryException( e );
}
}
-
+
/**
* Optionally set the filename to be associated with the binary content.
*/
@@ -303,11 +316,11 @@
returnString.append( "Content of rule item named '" + this.getName() + "':\n" );
returnString.append( "Content: " + this.getContent() + "\n" );
returnString.append( "------\n" );
-
+
returnString.append( "Archived: " + this.isArchived() + "\n" );
returnString.append( "------\n" );
-
+
returnString.append( "Date Effective: " + this.getDateEffective() + "\n" );
returnString.append( "Date Expired: " + this.getDateExpired() + "\n" );
returnString.append( "------\n" );
@@ -367,7 +380,7 @@
/**
* Get the name of the enclosing package.
- * As assets are stored in versionable subfolders, this means walking up 2 levels in the
+ * As assets are stored in versionable subfolders, this means walking up 2 levels in the
* hierarchy to get to the enclosing "package" node.
*/
public String getPackageName() {
@@ -382,9 +395,9 @@
}
/**
- * This will remove the item.
+ * This will remove the item.
* The repository will need to be saved for this to take effect.
- * Typically the package that contains this should be versioned before removing this,
+ * Typically the package that contains this should be versioned before removing this,
* to make it easy to roll back.
*/
public void remove() {
@@ -400,16 +413,16 @@
throw new RulesRepositoryException( e );
}
}
-
+
/**
- *
+ *
* @return An iterator over the nodes history.
*/
public AssetHistoryIterator getHistory() {
return new AssetHistoryIterator(this.rulesRepository, this.node);
}
-
+
/**
* This will get the package an asset item belongs to.
*/
Modified: labs/jbossrules/trunk/drools-repository/src/main/java/org/drools/repository/AssetItemIterator.java
===================================================================
--- labs/jbossrules/trunk/drools-repository/src/main/java/org/drools/repository/AssetItemIterator.java 2008-04-14 08:05:00 UTC (rev 19544)
+++ labs/jbossrules/trunk/drools-repository/src/main/java/org/drools/repository/AssetItemIterator.java 2008-04-14 08:50:15 UTC (rev 19545)
@@ -17,7 +17,7 @@
*/
public class AssetItemIterator
implements
- Iterator {
+ Iterator<AssetItem> {
private NodeIterator it;
private RulesRepository rulesRepository;
@@ -32,7 +32,7 @@
return it.hasNext();
}
- public Object next() {
+ public AssetItem next() {
return new AssetItem( rulesRepository,
(Node) it.next() );
}
Modified: labs/jbossrules/trunk/drools-repository/src/main/java/org/drools/repository/PackageItem.java
===================================================================
--- labs/jbossrules/trunk/drools-repository/src/main/java/org/drools/repository/PackageItem.java 2008-04-14 08:05:00 UTC (rev 19544)
+++ labs/jbossrules/trunk/drools-repository/src/main/java/org/drools/repository/PackageItem.java 2008-04-14 08:50:15 UTC (rev 19545)
@@ -410,12 +410,11 @@
// }
/** Return an iterator for the rules in this package */
- public Iterator getAssets() {
+ public Iterator<AssetItem> getAssets() {
try {
Node content = getVersionContentNode();
- AssetItemIterator it = new AssetItemIterator( content.getNode( ASSET_FOLDER_NAME ).getNodes(),
+ return new AssetItemIterator( content.getNode( ASSET_FOLDER_NAME ).getNodes(),
this.rulesRepository );
- return it;
} catch ( RepositoryException e ) {
throw new RulesRepositoryException( e );
}
Added: labs/jbossrules/trunk/drools-repository/src/main/java/org/drools/repository/remoteapi/Response.java
===================================================================
--- labs/jbossrules/trunk/drools-repository/src/main/java/org/drools/repository/remoteapi/Response.java (rev 0)
+++ labs/jbossrules/trunk/drools-repository/src/main/java/org/drools/repository/remoteapi/Response.java 2008-04-14 08:50:15 UTC (rev 19545)
@@ -0,0 +1,52 @@
+package org.drools.repository.remoteapi;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.util.Calendar;
+
+
+
+public abstract class Response {
+ abstract String getContentType();
+ abstract void writeData(OutputStream out) throws IOException ;
+ public Calendar lastModified;
+
+
+ public static class Text extends Response {
+ String data;
+
+ @Override
+ String getContentType() {
+ return "text/plain";
+ }
+
+ @Override
+ void writeData(OutputStream out) throws IOException {
+ out.write(data.getBytes());
+ }
+ }
+
+ public static class Binary extends Response {
+
+ InputStream stream;
+
+ @Override
+ String getContentType() {
+ return "application/octet-stream";
+ }
+
+
+ @Override
+ void writeData(OutputStream out) {
+
+ // TODO Auto-generated method stub
+
+ }
+
+ }
+
+
+}
+
+
Property changes on: labs/jbossrules/trunk/drools-repository/src/main/java/org/drools/repository/remoteapi/Response.java
___________________________________________________________________
Name: svn:eol-style
+ native
Added: labs/jbossrules/trunk/drools-repository/src/main/java/org/drools/repository/remoteapi/RestAPI.java
===================================================================
--- labs/jbossrules/trunk/drools-repository/src/main/java/org/drools/repository/remoteapi/RestAPI.java (rev 0)
+++ labs/jbossrules/trunk/drools-repository/src/main/java/org/drools/repository/remoteapi/RestAPI.java 2008-04-14 08:50:15 UTC (rev 19545)
@@ -0,0 +1,106 @@
+package org.drools.repository.remoteapi;
+
+import java.io.InputStream;
+import java.io.UnsupportedEncodingException;
+import java.net.URLDecoder;
+import java.text.DateFormat;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+import java.util.Iterator;
+
+import org.drools.repository.AssetItem;
+import org.drools.repository.PackageItem;
+import org.drools.repository.RulesRepository;
+import org.drools.repository.remoteapi.Response.Binary;
+import org.drools.repository.remoteapi.Response.Text;
+
+/**
+ * This provides a simple REST style remote friendly API.
+ *
+ * @author Michael Neale
+ *
+ */
+public class RestAPI {
+
+ private final RulesRepository repo;
+
+ public RestAPI(RulesRepository repo) {
+ this.repo = repo;
+ }
+
+ public Response get(String path) throws UnsupportedEncodingException {
+ String[] bits = path.split("/");
+ if (bits[0].equals("packages")) {
+ String pkgName = bits[1];
+ if (bits.length == 2) {
+ return listPackage(pkgName);
+ } else {
+ String resourceFile = bits[2];
+ return loadContent(pkgName, resourceFile);
+ }
+ } else {
+ throw new IllegalArgumentException("Unable to deal with " + path);
+ }
+
+ }
+
+ private Response loadContent(String pkgName, String resourceFile) throws UnsupportedEncodingException {
+ PackageItem pkg = repo.loadPackage(pkgName);
+ if (resourceFile.equals(".package")) {
+ Text r = new Response.Text();
+ r.lastModified = pkg.getLastModified();
+ r.data = pkg.getHeader();
+ return r;
+ } else {
+ String assetName = URLDecoder.decode(resourceFile, "UTF-8").split("\\.")[0];
+
+ AssetItem asset = pkg.loadAsset(assetName);
+ if (asset.isBinary()) {
+ Binary r = new Response.Binary();
+ r.lastModified = asset.getLastModified();
+ r.stream = asset.getBinaryContentAttachment();
+ return r;
+ } else {
+ Text r = new Response.Text();
+ r.lastModified = pkg.getLastModified();
+ r.data = asset.getContent();
+ return r;
+ }
+
+ }
+
+ }
+
+ private Response listPackage(String pkgName) throws UnsupportedEncodingException {
+ PackageItem pkg = repo.loadPackage(URLDecoder.decode(pkgName, "UTF-8"));
+ StringBuilder sb = new StringBuilder();
+ Iterator<AssetItem> it = pkg.getAssets();
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
+
+ while (it.hasNext()) {
+ AssetItem a = it.next();
+ sb.append(a.getName() + "." + a.getFormat() + "=" + sdf.format(a.getLastModified().getTime()));
+ sb.append('\n');
+ }
+
+ Text r = new Response.Text();
+ r.lastModified = pkg.getLastModified();
+ r.data = sb.toString();
+ return r;
+ }
+
+ public String post(String path, InputStream in) {
+ return null;
+ }
+
+ public String put(String path, Date lastModified, InputStream in) {
+ return null;
+ }
+
+ public String delete(String path) {
+ return null;
+ }
+
+
+
+}
Property changes on: labs/jbossrules/trunk/drools-repository/src/main/java/org/drools/repository/remoteapi/RestAPI.java
___________________________________________________________________
Name: svn:eol-style
+ native
Modified: labs/jbossrules/trunk/drools-repository/src/test/java/org/drools/repository/AssetItemTest.java
===================================================================
--- labs/jbossrules/trunk/drools-repository/src/test/java/org/drools/repository/AssetItemTest.java 2008-04-14 08:05:00 UTC (rev 19544)
+++ labs/jbossrules/trunk/drools-repository/src/test/java/org/drools/repository/AssetItemTest.java 2008-04-14 08:50:15 UTC (rev 19545)
@@ -79,6 +79,8 @@
assertNotNull(ruleItem1);
assertNotNull(ruleItem1.getNode());
assertEquals("test content", ruleItem1.getContent());
+
+ assertFalse(ruleItem1.isBinary());
}
@@ -726,8 +728,10 @@
assertTrue(item.getNode().hasProperty( AssetItem.CONTENT_PROPERTY_BINARY_NAME ));
item.checkin( "lalalala" );
+ assertTrue(item.isBinary());
+
item = getRepo().loadDefaultPackage().loadAsset( "testBinaryAsset" );
InputStream in2 = item.getBinaryContentAttachment();
assertNotNull(in2);
@@ -735,6 +739,7 @@
byte[] data2 = item.getBinaryContentAsBytes();
assertEquals(data, new String(data2));
assertEquals("x.x", item.getBinaryContentAttachmentFileName());
+ assertTrue(item.isBinary());
}
Added: labs/jbossrules/trunk/drools-repository/src/test/java/org/drools/repository/remoteapi/RestAPITest.java
===================================================================
--- labs/jbossrules/trunk/drools-repository/src/test/java/org/drools/repository/remoteapi/RestAPITest.java (rev 0)
+++ labs/jbossrules/trunk/drools-repository/src/test/java/org/drools/repository/remoteapi/RestAPITest.java 2008-04-14 08:50:15 UTC (rev 19545)
@@ -0,0 +1,123 @@
+package org.drools.repository.remoteapi;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.File;
+import java.io.InputStream;
+import java.net.URL;
+import java.net.URLEncoder;
+import java.text.SimpleDateFormat;
+import java.util.Calendar;
+import java.util.Date;
+import java.util.Properties;
+
+import org.drools.repository.AssetItem;
+import org.drools.repository.PackageItem;
+import org.drools.repository.RepositorySessionUtil;
+import org.drools.repository.RulesRepository;
+import org.drools.repository.remoteapi.Response.Binary;
+import org.drools.repository.remoteapi.Response.Text;
+
+import junit.framework.TestCase;
+
+public class RestAPITest extends TestCase {
+
+
+ public void testGetBasics() throws Exception {
+ String someAsset = "packages/SomeName/SomeFile.drl";
+ String getAList = "packages/SomeName"; //will show a list
+ String getPackageConfig = "packages/SomeName/.package"; //should load package config
+
+
+ RulesRepository repo = RepositorySessionUtil.getRepository();
+ PackageItem pkg = repo.createPackage("testRestGetBasics", "");
+ pkg.updateHeader("This is some header");
+ repo.save();
+
+
+ AssetItem asset1 = pkg.addAsset("asset1", "");
+ asset1.updateContent("this is content");
+ asset1.updateFormat("drl");
+ asset1.checkin("");
+
+ AssetItem asset2 = pkg.addAsset("asset2", "");
+ asset2.updateContent("this is content");
+ asset2.updateFormat("xml");
+ asset2.checkin("");
+
+ AssetItem asset3 = pkg.addAsset("asset3", "");
+ ByteArrayInputStream in = new ByteArrayInputStream("a b c".getBytes());
+ asset3.updateBinaryContentAttachment(in);
+ asset3.updateFormat("xls");
+ asset3.checkin("");
+
+ assertTrue(asset3.isBinary());
+ assertFalse(asset1.isBinary());
+
+ RestAPI api = new RestAPI(repo);
+
+ //this should get us the package configuration
+
+ String url = "packages/testRestGetBasics/.package";
+ Response res = api.get(url);
+ assertEquals("text/plain", res.getContentType());
+ assertNotNull(res.lastModified);
+ assertEquals(pkg.getLastModified(), res.lastModified);
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ res.writeData(out);
+
+ String dotPackage = new String(out.toByteArray());
+ assertEquals(pkg.getHeader(), dotPackage);
+
+ res = api.get("packages/testRestGetBasics");
+ assertTrue(res instanceof Text);
+ assertNotNull(res.lastModified);
+ out = new ByteArrayOutputStream();
+ res.writeData(out);
+ String listing = new String(out.toByteArray());
+ assertNotNull(listing);
+
+ Properties p = new Properties();
+ p.load(new ByteArrayInputStream(out.toByteArray()));
+ assertEquals(3, p.size());
+
+ assertTrue(p.containsKey("asset1.drl"));
+ assertTrue(p.containsKey("asset2.xml"));
+ assertTrue(p.containsKey("asset3.xls"));
+
+ assertNotNull(p.getProperty("asset1.drl"));
+ String dt = p.getProperty("asset1.drl");
+ System.err.println(dt);
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
+ Date d= sdf.parse(dt);
+ assertNotNull(d);
+
+ assertEquals(sdf.format(asset1.getLastModified().getTime()), dt);
+
+
+ //try text
+ res = api.get("packages/testRestGetBasics/asset1.drl");
+ assertTrue(res instanceof Text);
+ out = new ByteArrayOutputStream();
+ assertNotNull(res.lastModified);
+ assertTrue(res.lastModified.getTime().after(sdf.parse("2000-04-14T18:36:37")));
+ res.writeData(out);
+
+ String s = new String(out.toByteArray());
+ assertEquals(asset1.getContent(), s);
+
+
+ //now binary
+ res = api.get("packages/testRestGetBasics/asset3.xls");
+ assertTrue(res instanceof Binary);
+ out = new ByteArrayOutputStream();
+ assertNotNull(res.lastModified);
+ assertTrue(res.lastModified.getTime().after(sdf.parse("2000-04-14T18:36:37")));
+ res.writeData(out);
+
+ byte[] data = out.toByteArray();
+ assertNotNull(data);
+
+
+ }
+}
Property changes on: labs/jbossrules/trunk/drools-repository/src/test/java/org/drools/repository/remoteapi/RestAPITest.java
___________________________________________________________________
Name: svn:eol-style
+ native
More information about the jboss-svn-commits
mailing list