You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@chemistry.apache.org by fm...@apache.org on 2010/02/23 16:13:35 UTC
svn commit: r915373 - in
/incubator/chemistry/trunk/opencmis/opencmis-server/opencmis-server-impl/src:
main/java/org/apache/opencmis/server/impl/atompub/ test/ test/java/
test/java/org/ test/java/org/apache/ test/java/org/apache/opencmis/
test/java/org...
Author: fmui
Date: Tue Feb 23 15:13:33 2010
New Revision: 915373
URL: http://svn.apache.org/viewvc?rev=915373&view=rev
Log:
added the missing support for AtomPub clients that transmit "/xml", "+xml" or "xhtml" content
Added:
incubator/chemistry/trunk/opencmis/opencmis-server/opencmis-server-impl/src/test/
incubator/chemistry/trunk/opencmis/opencmis-server/opencmis-server-impl/src/test/java/
incubator/chemistry/trunk/opencmis/opencmis-server/opencmis-server-impl/src/test/java/org/
incubator/chemistry/trunk/opencmis/opencmis-server/opencmis-server-impl/src/test/java/org/apache/
incubator/chemistry/trunk/opencmis/opencmis-server/opencmis-server-impl/src/test/java/org/apache/opencmis/
incubator/chemistry/trunk/opencmis/opencmis-server/opencmis-server-impl/src/test/java/org/apache/opencmis/server/
incubator/chemistry/trunk/opencmis/opencmis-server/opencmis-server-impl/src/test/java/org/apache/opencmis/server/impl/
incubator/chemistry/trunk/opencmis/opencmis-server/opencmis-server-impl/src/test/java/org/apache/opencmis/server/impl/AtomEntryParserTest.java (with props)
Modified:
incubator/chemistry/trunk/opencmis/opencmis-server/opencmis-server-impl/src/main/java/org/apache/opencmis/server/impl/atompub/AtomEntryParser.java
Modified: incubator/chemistry/trunk/opencmis/opencmis-server/opencmis-server-impl/src/main/java/org/apache/opencmis/server/impl/atompub/AtomEntryParser.java
URL: http://svn.apache.org/viewvc/incubator/chemistry/trunk/opencmis/opencmis-server/opencmis-server-impl/src/main/java/org/apache/opencmis/server/impl/atompub/AtomEntryParser.java?rev=915373&r1=915372&r2=915373&view=diff
==============================================================================
--- incubator/chemistry/trunk/opencmis/opencmis-server/opencmis-server-impl/src/main/java/org/apache/opencmis/server/impl/atompub/AtomEntryParser.java (original)
+++ incubator/chemistry/trunk/opencmis/opencmis-server/opencmis-server-impl/src/main/java/org/apache/opencmis/server/impl/atompub/AtomEntryParser.java Tue Feb 23 15:13:33 2010
@@ -19,7 +19,9 @@
package org.apache.opencmis.server.impl.atompub;
import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
import java.io.InputStream;
+import java.util.Iterator;
import java.util.List;
import java.util.Map;
@@ -27,7 +29,9 @@
import javax.xml.bind.Unmarshaller;
import javax.xml.namespace.QName;
import javax.xml.stream.XMLInputFactory;
+import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamReader;
+import javax.xml.stream.XMLStreamWriter;
import org.apache.commons.codec.binary.Base64;
import org.apache.opencmis.commons.PropertyIds;
@@ -257,8 +261,6 @@
}
}
- next(parser);
-
if (type.equals("text") || type.equals("html")) {
fAtomContentStream.setStream(new ByteArrayInputStream(readText(parser).getBytes("UTF-8")));
}
@@ -356,25 +358,138 @@
* Copies a subtree into a stream.
*/
private InputStream copy(XMLStreamReader parser) throws Exception {
- // ToDo: implement copy
+ // create a writer
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ XMLStreamWriter writer = XMLOutputFactory.newInstance().createXMLStreamWriter(out);
+ writer.writeStartDocument();
+
+ // copy subtree
int level = 1;
while (next(parser)) {
int event = parser.getEventType();
if (event == XMLStreamReader.START_ELEMENT) {
+ copyStartElement(parser, writer);
level++;
}
+ else if (event == XMLStreamReader.CHARACTERS) {
+ writer.writeCharacters(parser.getText());
+ }
+ else if (event == XMLStreamReader.COMMENT) {
+ writer.writeComment(parser.getText());
+ }
+ else if (event == XMLStreamReader.CDATA) {
+ writer.writeCData(parser.getText());
+ }
else if (event == XMLStreamReader.END_ELEMENT) {
level--;
if (level == 0) {
break;
}
+ writer.writeEndElement();
+ }
+ else {
+ break;
}
}
+ writer.writeEndDocument();
+
next(parser);
- return null;
+ return new ByteArrayInputStream(out.toByteArray());
+ }
+
+ /**
+ * Copies a XML start element.
+ */
+ private void copyStartElement(XMLStreamReader parser, XMLStreamWriter writer) throws Exception {
+ String namespaceUri = parser.getNamespaceURI();
+ String prefix = parser.getPrefix();
+ String localName = parser.getLocalName();
+
+ // write start element
+ if (namespaceUri != null) {
+ if ((prefix == null) || (prefix.length() == 0)) {
+ writer.writeStartElement(localName);
+ }
+ else {
+ writer.writeStartElement(prefix, localName, namespaceUri);
+ }
+ }
+ else {
+ writer.writeStartElement(localName);
+ }
+
+ // set namespaces
+ for (int i = 0; i < parser.getNamespaceCount(); i++) {
+ addNamespace(writer, parser.getNamespacePrefix(i), parser.getNamespaceURI(i));
+ }
+ addNamespaceIfMissing(writer, prefix, namespaceUri);
+
+ // write attributes
+ for (int i = 0; i < parser.getAttributeCount(); i++) {
+ String attrNamespaceUri = parser.getAttributeNamespace(i);
+ String attrPrefix = parser.getAttributePrefix(i);
+ String attrName = parser.getAttributeLocalName(i);
+ String attrValue = parser.getAttributeValue(i);
+
+ if ((attrNamespaceUri == null) || (attrNamespaceUri.trim().length() == 0)) {
+ writer.writeAttribute(attrName, attrValue);
+ }
+ else if ((attrPrefix == null) || (attrPrefix.trim().length() == 0)) {
+ writer.writeAttribute(attrNamespaceUri, attrName, attrValue);
+ }
+ else {
+ addNamespaceIfMissing(writer, attrPrefix, attrNamespaceUri);
+ writer.writeAttribute(attrPrefix, attrNamespaceUri, attrName, attrValue);
+ }
+ }
+ }
+
+ /**
+ * Checks if the given prefix is assigned to the given namespace.
+ */
+ @SuppressWarnings("unchecked")
+ private void addNamespaceIfMissing(XMLStreamWriter writer, String prefix, String namespaceUri)
+ throws Exception {
+ if ((namespaceUri == null) || (namespaceUri.trim().length() == 0)) {
+ return;
+ }
+
+ if (prefix == null) {
+ prefix = "";
+ }
+
+ Iterator<String> iter = (Iterator<String>) writer.getNamespaceContext().getPrefixes(
+ namespaceUri);
+ if (iter == null) {
+ return;
+ }
+
+ while (iter.hasNext()) {
+ String p = iter.next();
+ if ((p != null) && (p.equals(prefix))) {
+ return;
+ }
+ }
+
+ addNamespace(writer, prefix, namespaceUri);
+ }
+
+ /**
+ * Adds a namespace to a XML element.
+ */
+ private void addNamespace(XMLStreamWriter writer, String prefix, String namespaceUri)
+ throws Exception {
+ if ((prefix == null) || (prefix.trim().length() == 0)) {
+ writer.setDefaultNamespace(namespaceUri);
+ writer.writeDefaultNamespace(namespaceUri);
+ }
+ else {
+ writer.setPrefix(prefix, namespaceUri);
+ writer.writeNamespace(prefix, namespaceUri);
+ }
}
/**
Added: incubator/chemistry/trunk/opencmis/opencmis-server/opencmis-server-impl/src/test/java/org/apache/opencmis/server/impl/AtomEntryParserTest.java
URL: http://svn.apache.org/viewvc/incubator/chemistry/trunk/opencmis/opencmis-server/opencmis-server-impl/src/test/java/org/apache/opencmis/server/impl/AtomEntryParserTest.java?rev=915373&view=auto
==============================================================================
--- incubator/chemistry/trunk/opencmis/opencmis-server/opencmis-server-impl/src/test/java/org/apache/opencmis/server/impl/AtomEntryParserTest.java (added)
+++ incubator/chemistry/trunk/opencmis/opencmis-server/opencmis-server-impl/src/test/java/org/apache/opencmis/server/impl/AtomEntryParserTest.java Tue Feb 23 15:13:33 2010
@@ -0,0 +1,225 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.opencmis.server.impl;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.*;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+
+import org.apache.commons.codec.binary.Base64;
+import org.apache.opencmis.commons.provider.ContentStreamData;
+import org.apache.opencmis.server.impl.atompub.AtomEntryParser;
+import org.junit.Test;
+
+/**
+ * AtomEntryParser test.
+ *
+ * @author <a href="mailto:fmueller@opentext.com">Florian Müller</a>
+ *
+ */
+public class AtomEntryParserTest {
+
+ private static final String CMIS_ENTRY_CONTENT = "This is my content!";
+ private static final String CMIS_ENTRY = "<?xml version='1.0' encoding='utf-8'?>" +
+ "<atom:entry xmlns:atom=\"http://www.w3.org/2005/Atom\" xmlns:cmis=\"http://docs.oasis-open.org/ns/cmis/core/200908/\" xmlns:cmisra=\"http://docs.oasis-open.org/ns/cmis/restatom/200908/\">" +
+ "<atom:author><atom:name>test</atom:name></atom:author>" +
+ "<atom:id>http://test/id</atom:id>" +
+ "<atom:published>2009-12-31T12:53:37Z</atom:published>" +
+ "<atom:title>test.txt</atom:title>" +
+ "<atom:updated>2010-01-01T00:00:00Z</atom:updated>" +
+ "<cmisra:object xmlns:ns3=\"http://docs.oasis-open.org/ns/cmis/messaging/200908/\">" +
+ " <cmis:properties>" +
+ " <cmis:propertyId propertyDefinitionId=\"cmis:objectId\">" +
+ " <cmis:value>id</cmis:value>" +
+ " </cmis:propertyId>" +
+ " <cmis:propertyString propertyDefinitionId=\"cmis:name\">" +
+ " <cmis:value>test.txt</cmis:value>" +
+ " </cmis:propertyString>" +
+ " <cmis:propertyId propertyDefinitionId=\"cmis:objectTypeId\">" +
+ " <cmis:value>cmis:document</cmis:value>" +
+ " </cmis:propertyId>" +
+ " </cmis:properties>" +
+ "</cmisra:object>" +
+ "<cmisra:content>" +
+ " <cmisra:mediatype>text/plain</cmisra:mediatype>" +
+ " <cmisra:base64>" + Base64.encodeBase64String(CMIS_ENTRY_CONTENT.getBytes())+ "</cmisra:base64>" +
+ "</cmisra:content>" +
+ "</atom:entry>";
+
+
+ private static final String ATOM_ENTRY_TEXT_CONTENT = "This is plain text!";
+ private static final String ATOM_ENTRY_TEXT = "<?xml version='1.0' encoding='utf-8'?>" +
+ "<atom:entry xmlns:atom=\"http://www.w3.org/2005/Atom\" xmlns:cmis=\"http://docs.oasis-open.org/ns/cmis/core/200908/\" xmlns:cmisra=\"http://docs.oasis-open.org/ns/cmis/restatom/200908/\">" +
+ "<atom:author><atom:name>test</atom:name></atom:author>" +
+ "<atom:id>http://test/id</atom:id>" +
+ "<atom:published>2009-12-31T12:53:37Z</atom:published>" +
+ "<atom:title>test.txt</atom:title>" +
+ "<atom:updated>2010-01-01T00:00:00Z</atom:updated>" +
+ "<cmisra:object xmlns:ns3=\"http://docs.oasis-open.org/ns/cmis/messaging/200908/\">" +
+ " <cmis:properties>" +
+ " <cmis:propertyId propertyDefinitionId=\"cmis:objectId\">" +
+ " <cmis:value>id</cmis:value>" +
+ " </cmis:propertyId>" +
+ " <cmis:propertyString propertyDefinitionId=\"cmis:name\">" +
+ " <cmis:value>test.txt</cmis:value>" +
+ " </cmis:propertyString>" +
+ " <cmis:propertyId propertyDefinitionId=\"cmis:objectTypeId\">" +
+ " <cmis:value>cmis:document</cmis:value>" +
+ " </cmis:propertyId>" +
+ " </cmis:properties>" +
+ "</cmisra:object>" +
+ "<atom:content type=\"text\">" +
+ ATOM_ENTRY_TEXT_CONTENT +
+ "</atom:content>" +
+ "</atom:entry>";
+
+ private static final String ATOM_ENTRY_XML_CONTENT = "<first xmlns=\"http://test/1\"><second myattr=\"Cool, a value!\">hey, this is text</second><myns:third xmlns:myns=\"http://test/2\">guess what's here ... more text</myns:third></first>";
+ private static final String ATOM_ENTRY_XML = "<?xml version='1.0' encoding='utf-8'?>" +
+ "<atom:entry xmlns:atom=\"http://www.w3.org/2005/Atom\" xmlns:cmis=\"http://docs.oasis-open.org/ns/cmis/core/200908/\" xmlns:cmisra=\"http://docs.oasis-open.org/ns/cmis/restatom/200908/\">" +
+ "<atom:author><atom:name>test</atom:name></atom:author>" +
+ "<atom:id>http://test/id</atom:id>" +
+ "<atom:published>2009-12-31T12:53:37Z</atom:published>" +
+ "<atom:title>test.txt</atom:title>" +
+ "<atom:updated>2010-01-01T00:00:00Z</atom:updated>" +
+ "<cmisra:object xmlns:ns3=\"http://docs.oasis-open.org/ns/cmis/messaging/200908/\">" +
+ " <cmis:properties>" +
+ " <cmis:propertyId propertyDefinitionId=\"cmis:objectId\">" +
+ " <cmis:value>id</cmis:value>" +
+ " </cmis:propertyId>" +
+ " <cmis:propertyString propertyDefinitionId=\"cmis:name\">" +
+ " <cmis:value>test.txt</cmis:value>" +
+ " </cmis:propertyString>" +
+ " <cmis:propertyId propertyDefinitionId=\"cmis:objectTypeId\">" +
+ " <cmis:value>cmis:document</cmis:value>" +
+ " </cmis:propertyId>" +
+ " </cmis:properties>" +
+ "</cmisra:object>" +
+ "<atom:content type=\"text/xml\">" +
+ ATOM_ENTRY_XML_CONTENT +
+ "</atom:content>" +
+ "</atom:entry>";
+
+ private static final String ATOM_ENTRY_XHTML_CONTENT = "<div xmlns=\"http://www.w3.org/1999/xhtml\">This is <b>XHTML</b> content.</div>";
+ private static final String ATOM_ENTRY_XHTML = "<?xml version='1.0' encoding='utf-8'?>" +
+ "<atom:entry xmlns:atom=\"http://www.w3.org/2005/Atom\" xmlns:cmis=\"http://docs.oasis-open.org/ns/cmis/core/200908/\" xmlns:cmisra=\"http://docs.oasis-open.org/ns/cmis/restatom/200908/\">" +
+ "<atom:author><atom:name>test</atom:name></atom:author>" +
+ "<atom:id>http://test/id</atom:id>" +
+ "<atom:published>2009-12-31T12:53:37Z</atom:published>" +
+ "<atom:title>test.txt</atom:title>" +
+ "<atom:updated>2010-01-01T00:00:00Z</atom:updated>" +
+ "<cmisra:object xmlns:ns3=\"http://docs.oasis-open.org/ns/cmis/messaging/200908/\">" +
+ " <cmis:properties>" +
+ " <cmis:propertyId propertyDefinitionId=\"cmis:objectId\">" +
+ " <cmis:value>id</cmis:value>" +
+ " </cmis:propertyId>" +
+ " <cmis:propertyString propertyDefinitionId=\"cmis:name\">" +
+ " <cmis:value>test.txt</cmis:value>" +
+ " </cmis:propertyString>" +
+ " <cmis:propertyId propertyDefinitionId=\"cmis:objectTypeId\">" +
+ " <cmis:value>cmis:document</cmis:value>" +
+ " </cmis:propertyId>" +
+ " </cmis:properties>" +
+ "</cmisra:object>" +
+ "<atom:content type=\"xhtml\">" +
+ ATOM_ENTRY_XHTML_CONTENT +
+ "</atom:content>" +
+ "</atom:entry>";
+
+ private static final String ATOM_ENTRY_BASE64_CONTENT = "This is another content!";
+ private static final String ATOM_ENTRY_BASE64 = "<?xml version='1.0' encoding='utf-8'?>" +
+ "<atom:entry xmlns:atom=\"http://www.w3.org/2005/Atom\" xmlns:cmis=\"http://docs.oasis-open.org/ns/cmis/core/200908/\" xmlns:cmisra=\"http://docs.oasis-open.org/ns/cmis/restatom/200908/\">" +
+ "<atom:author><atom:name>test</atom:name></atom:author>" +
+ "<atom:id>http://test/id</atom:id>" +
+ "<atom:published>2009-12-31T12:53:37Z</atom:published>" +
+ "<atom:title>test.txt</atom:title>" +
+ "<atom:updated>2010-01-01T00:00:00Z</atom:updated>" +
+ "<cmisra:object xmlns:ns3=\"http://docs.oasis-open.org/ns/cmis/messaging/200908/\">" +
+ " <cmis:properties>" +
+ " <cmis:propertyId propertyDefinitionId=\"cmis:objectId\">" +
+ " <cmis:value>id</cmis:value>" +
+ " </cmis:propertyId>" +
+ " <cmis:propertyString propertyDefinitionId=\"cmis:name\">" +
+ " <cmis:value>test.txt</cmis:value>" +
+ " </cmis:propertyString>" +
+ " <cmis:propertyId propertyDefinitionId=\"cmis:objectTypeId\">" +
+ " <cmis:value>cmis:document</cmis:value>" +
+ " </cmis:propertyId>" +
+ " </cmis:properties>" +
+ "</cmisra:object>" +
+ "<atom:content type=\"application/something\">" +
+ Base64.encodeBase64String(ATOM_ENTRY_BASE64_CONTENT.getBytes()) +
+ "</atom:content>" +
+ "</atom:entry>";
+
+
+ @Test
+ public void testCmisContent() throws Exception {
+ byte[] content = parse(CMIS_ENTRY.getBytes());
+ assertEquals(CMIS_ENTRY_CONTENT, new String(content));
+ }
+
+ @Test
+ public void testAtomContentText() throws Exception {
+ byte[] content = parse(ATOM_ENTRY_TEXT.getBytes());
+ assertEquals(ATOM_ENTRY_TEXT_CONTENT, new String(content));
+ }
+
+ @Test
+ public void testAtomContentXml() throws Exception {
+ byte[] content = parse(ATOM_ENTRY_XML.getBytes());
+ String xmlContent = new String(content);
+ assertTrue(xmlContent.indexOf('>') > -1);
+ assertEquals(ATOM_ENTRY_XML_CONTENT, xmlContent.substring(xmlContent.indexOf('>')+1));
+ }
+
+ @Test
+ public void testAtomContentXHtml() throws Exception {
+ byte[] content = parse(ATOM_ENTRY_XHTML.getBytes());
+ String xmlContent = new String(content);
+ assertTrue(xmlContent.indexOf('>') > -1);
+ assertEquals(ATOM_ENTRY_XHTML_CONTENT, xmlContent.substring(xmlContent.indexOf('>')+1));
+ }
+
+ @Test
+ public void testAtomContentBase64() throws Exception {
+ byte[] content = parse(ATOM_ENTRY_BASE64.getBytes());
+ assertEquals(ATOM_ENTRY_BASE64_CONTENT, new String(content));
+ }
+
+ private byte[] parse(byte[] entry) throws Exception {
+ AtomEntryParser aep = new AtomEntryParser(new ByteArrayInputStream(entry));
+ ContentStreamData contentStream = aep.getContentStream();
+
+ assertNotNull(contentStream);
+ assertNotNull(contentStream.getStream());
+
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+
+ byte[] buffer = new byte[4096];
+ int b;
+
+ while ((b = contentStream.getStream().read(buffer)) > -1) {
+ baos.write(buffer, 0, b);
+ }
+
+ return baos.toByteArray();
+ }
+}
Propchange: incubator/chemistry/trunk/opencmis/opencmis-server/opencmis-server-impl/src/test/java/org/apache/opencmis/server/impl/AtomEntryParserTest.java
------------------------------------------------------------------------------
svn:eol-style = native