Author: fguillaume
Date: Wed Dec 23 00:59:25 2009
New Revision: 893366
URL: http://svn.apache.org/viewvc?rev=893366&view=rev
Log:
Implemented SPI.createFolder & createDocument for AtomPub client
Modified:
incubator/chemistry/trunk/chemistry/chemistry-atompub-client/src/main/java/org/apache/chemistry/atompub/client/APPConnection.java
incubator/chemistry/trunk/chemistry/chemistry-atompub-client/src/main/java/org/apache/chemistry/atompub/client/stax/ObjectEntryWriter.java
incubator/chemistry/trunk/chemistry/chemistry-tests/src/main/java/org/apache/chemistry/test/BasicTestCase.java
Modified:
incubator/chemistry/trunk/chemistry/chemistry-atompub-client/src/main/java/org/apache/chemistry/atompub/client/APPConnection.java
URL:
http://svn.apache.org/viewvc/incubator/chemistry/trunk/chemistry/chemistry-atompub-client/src/main/java/org/apache/chemistry/atompub/client/APPConnection.java?rev=893366&r1=893365&r2=893366&view=diff
==============================================================================
---
incubator/chemistry/trunk/chemistry/chemistry-atompub-client/src/main/java/org/apache/chemistry/atompub/client/APPConnection.java
(original)
+++
incubator/chemistry/trunk/chemistry/chemistry-atompub-client/src/main/java/org/apache/chemistry/atompub/client/APPConnection.java
Wed Dec 23 00:59:25 2009
@@ -28,6 +28,7 @@
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
+import java.util.Map.Entry;
import javax.xml.namespace.QName;
@@ -63,9 +64,7 @@
import org.apache.chemistry.atompub.client.stax.ReadContext;
import org.apache.chemistry.atompub.client.stax.XmlProperty;
import org.apache.chemistry.impl.simple.SimpleContentStream;
-import org.apache.chemistry.impl.simple.SimpleFolder;
import org.apache.chemistry.impl.simple.SimpleListPage;
-import org.apache.chemistry.impl.simple.SimpleObjectEntry;
import org.apache.chemistry.impl.simple.SimpleObjectId;
/**
@@ -331,17 +330,82 @@
}
}
+ protected String getPostHref(ObjectId parentId) {
+ APPObjectEntry parentEntry = getObjectEntry(parentId);
+ String href = parentEntry.getLink(AtomPub.LINK_DOWN,
+ AtomPub.MEDIA_TYPE_ATOM_FEED);
+ if (href == null) {
+ href = parentEntry.getLink(AtomPub.LINK_DOWN,
+ AtomPub.MEDIA_TYPE_ATOM);
+ if (href == null) {
+ throw new IllegalArgumentException(
+ "Cannot create entry: no 'down' link present");
+ }
+ }
+ return href;
+ }
+
+ protected APPObjectEntry createObject(String postHref,
+ Map<String, Serializable> properties, ContentStream contentStream,
+ BaseType baseType) {
+ String typeId = (String) properties.get(Property.TYPE_ID);
+ if (typeId == null) {
+ throw new IllegalArgumentException("Missing object type id");
+ }
+ Type type = repository.getType(typeId);
+ if (type == null || type.getBaseType() != baseType) {
+ throw new IllegalArgumentException(typeId);
+ }
+ APPObjectEntry entry = newObjectEntry(typeId);
+ for (Entry<String, Serializable> en : properties.entrySet()) {
+ entry._setValue(en.getKey(), en.getValue());
+ }
+ if (contentStream != null) {
+ entry.setContentStream(contentStream);
+ }
+
+ Request req = new Request(postHref);
+ req.setHeader("Content-Type", AtomPub.MEDIA_TYPE_ATOM_ENTRY);
+ Response resp = connector.postObject(req, entry);
+ if (resp.getStatusCode() != 201) { // Created
+ throw new ContentManagerException(
+ "Remote server returned error code: "
+ + resp.getStatusCode());
+ }
+ ReadContext ctx = new ReadContext(this);
+ APPObjectEntry newEntry = (APPObjectEntry) resp.getObject(ctx);
+ // newEntry SHOULD be returned (AtomPub 9.2)...
+ String loc = resp.getHeader("Location");
+ if (loc == null) {
+ throw new ContentManagerException(
+ "Remote server failed to return a Location header");
+ }
+ if (newEntry == null ||
!loc.equals(resp.getHeader("Content-Location"))) {
+ // (Content-Location defined by AtomPub 9.2)
+ // fetch actual new entry from Location header
+ // TODO could fetch only a subset of the properties, if deemed ok
+ newEntry = (APPObjectEntry) connector.getObject(ctx, loc);
+ if (newEntry == null) {
+ throw new ContentManagerException(
+ "Remote server failed to return an entry for Location:
"
+ + loc);
+ }
+ }
+ return newEntry;
+ }
+
public ObjectId createDocument(Map<String, Serializable> properties,
ObjectId folder, ContentStream contentStream,
VersioningState versioningState) {
- // TODO Auto-generated method stub
- throw new UnsupportedOperationException();
+ // TODO versioningState
+ return createObject(getPostHref(folder), properties, contentStream,
+ BaseType.DOCUMENT);
}
public ObjectId createFolder(Map<String, Serializable> properties,
ObjectId folder) {
- // TODO Auto-generated method stub
- throw new UnsupportedOperationException();
+ return createObject(getPostHref(folder), properties, null,
+ BaseType.FOLDER);
}
public ObjectId createRelationship(Map<String, Serializable> properties) {
Modified:
incubator/chemistry/trunk/chemistry/chemistry-atompub-client/src/main/java/org/apache/chemistry/atompub/client/stax/ObjectEntryWriter.java
URL:
http://svn.apache.org/viewvc/incubator/chemistry/trunk/chemistry/chemistry-atompub-client/src/main/java/org/apache/chemistry/atompub/client/stax/ObjectEntryWriter.java?rev=893366&r1=893365&r2=893366&view=diff
==============================================================================
---
incubator/chemistry/trunk/chemistry/chemistry-atompub-client/src/main/java/org/apache/chemistry/atompub/client/stax/ObjectEntryWriter.java
(original)
+++
incubator/chemistry/trunk/chemistry/chemistry-atompub-client/src/main/java/org/apache/chemistry/atompub/client/stax/ObjectEntryWriter.java
Wed Dec 23 00:59:25 2009
@@ -48,7 +48,8 @@
xw.start();
// atom requires an ID to be set even on new created entries ..
xw.element("id").content("urn:uuid:" + object.getId());
- xw.element("title").content((String)
object.getValue(Property.NAME));
+ String title = (String) object.getValue(Property.NAME);
+ xw.element("title").content(title == null ? "" : title);
xw.element("updated").content(new Date());
writeContent(object, xw);
writeCmisObject(object, xw);
Modified:
incubator/chemistry/trunk/chemistry/chemistry-tests/src/main/java/org/apache/chemistry/test/BasicTestCase.java
URL:
http://svn.apache.org/viewvc/incubator/chemistry/trunk/chemistry/chemistry-tests/src/main/java/org/apache/chemistry/test/BasicTestCase.java?rev=893366&r1=893365&r2=893366&view=diff
==============================================================================
---
incubator/chemistry/trunk/chemistry/chemistry-tests/src/main/java/org/apache/chemistry/test/BasicTestCase.java
(original)
+++
incubator/chemistry/trunk/chemistry/chemistry-tests/src/main/java/org/apache/chemistry/test/BasicTestCase.java
Wed Dec 23 00:59:25 2009
@@ -222,8 +222,27 @@
}
}
+ public void testCreateSPI() throws Exception {
+ Map<String, Serializable> properties = new HashMap<String,
Serializable>();
+ properties.put(Property.TYPE_ID, "fold");
+ properties.put("description", "some descr");
+ ObjectId folderId = spi.createFolder(properties,
+ repository.getInfo().getRootFolderId());
+ assertNotNull(folderId);
+ Folder folder = (Folder) conn.getObject(folderId);
+ assertEquals("some descr", folder.getValue("description"));
+
+ properties = new HashMap<String, Serializable>();
+ properties.put(Property.TYPE_ID, "doc");
+ properties.put("title", "some title");
+ ObjectId docId = spi.createDocument(properties,
+ repository.getInfo().getRootFolderId(), null, null);
+ assertNotNull(docId);
+ Document doc = (Document) conn.getObject(docId);
+ assertEquals("some title", doc.getValue("title"));
+ }
+
public void testQuery() {
- Connection conn = repository.getConnection(null);
Collection<CMISObject> res = conn.query("SELECT * FROM doc", false);
assertNotNull(res);
assertEquals(4, res.size());