Return-Path: Delivered-To: apmail-incubator-chemistry-commits-archive@minotaur.apache.org Received: (qmail 40799 invoked from network); 22 Apr 2010 16:28:49 -0000 Received: from unknown (HELO mail.apache.org) (140.211.11.3) by 140.211.11.9 with SMTP; 22 Apr 2010 16:28:49 -0000 Received: (qmail 70517 invoked by uid 500); 22 Apr 2010 16:28:49 -0000 Delivered-To: apmail-incubator-chemistry-commits-archive@incubator.apache.org Received: (qmail 70480 invoked by uid 500); 22 Apr 2010 16:28:49 -0000 Mailing-List: contact chemistry-commits-help@incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: chemistry-dev@incubator.apache.org Delivered-To: mailing list chemistry-commits@incubator.apache.org Received: (qmail 70471 invoked by uid 99); 22 Apr 2010 16:28:49 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 22 Apr 2010 16:28:49 +0000 X-ASF-Spam-Status: No, hits=-1872.0 required=10.0 tests=ALL_TRUSTED,AWL X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 22 Apr 2010 16:28:45 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id 7BC5323889B3; Thu, 22 Apr 2010 16:28:03 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r936938 [2/29] - in /incubator/chemistry/opencmis/trunk/chemistry-opencmis-server: chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/impl/ chemistry-opencmis-server-bindings/src/main/java/org/apache/chemi... Date: Thu, 22 Apr 2010 16:28:00 -0000 To: chemistry-commits@incubator.apache.org From: dcaruana@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20100422162803.7BC5323889B3@eris.apache.org> Modified: incubator/chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/impl/atompub/AtomEntryParser.java URL: http://svn.apache.org/viewvc/incubator/chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/impl/atompub/AtomEntryParser.java?rev=936938&r1=936937&r2=936938&view=diff ============================================================================== --- incubator/chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/impl/atompub/AtomEntryParser.java (original) +++ incubator/chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/impl/atompub/AtomEntryParser.java Thu Apr 22 16:27:57 2010 @@ -60,443 +60,443 @@ import org.apache.commons.codec.binary.B */ public class AtomEntryParser { - private final static String TAG_ENTRY = "entry"; - private final static String TAG_TITLE = "title"; - private final static String TAG_OBJECT = "object"; - private final static String TAG_CONTENT = "content"; - private final static String TAG_BASE64 = "base64"; - private final static String TAG_MEDIATYPE = "mediatype"; - - private final static String ATTR_SRC = "src"; - private final static String ATTR_TYPE = "type"; - - private ObjectData fObject; - private ContentStreamImpl fAtomContentStream; - private ContentStreamImpl fCmisContentStream; - - /** - * Constructor. - */ - public AtomEntryParser() { - } - - /** - * Constructor that immediately parses the given stream. - */ - public AtomEntryParser(InputStream stream) throws Exception { - parse(stream); - } - - /** - * Returns the object. - */ - public ObjectData getObject() { - return fObject; - } - - /** - * Returns the properties of the object. - */ - public Properties getProperties() { - return (fObject == null ? null : fObject.getProperties()); - } - - /** - * Returns the Id of the object. - */ - public String getId() { - Properties properties = getProperties(); - if (properties == null) { - return null; - } - - Map> propertiesMap = properties.getProperties(); - if (propertiesMap == null) { - return null; - } - - PropertyData property = propertiesMap.get(PropertyIds.OBJECT_ID); - if (property instanceof PropertyId) { - return ((PropertyId) property).getFirstValue(); - } - - return null; - } - - /** - * Returns the ACL of the object. - */ - public Acl getAcl() { - return (fObject == null ? null : fObject.getAcl()); - } - - /** - * Returns the policy id list of the object. - */ - public List getPolicyIds() { - if ((fObject == null) || (fObject.getPolicyIds() == null)) { - return null; - } - - return fObject.getPolicyIds().getPolicyIds(); - } - - /** - * Returns the content stream. - */ - public ContentStream getContentStream() { - return (fCmisContentStream == null ? fAtomContentStream : fCmisContentStream); - } - - /** - * Parses the stream. - */ - public void parse(InputStream stream) throws Exception { - fObject = null; - fAtomContentStream = null; - fCmisContentStream = null; - - if (stream == null) { - return; - } - - XMLInputFactory factory = XMLInputFactory.newInstance(); - XMLStreamReader parser = factory.createXMLStreamReader(stream); - - while (true) { - int event = parser.getEventType(); - if (event == XMLStreamReader.START_ELEMENT) { - QName name = parser.getName(); - - if (Constants.NAMESPACE_ATOM.equals(name.getNamespaceURI()) && (TAG_ENTRY.equals(name.getLocalPart()))) { - parseEntry(parser); - break; - } else { - throw new CmisInvalidArgumentException("XML is not an Atom entry!"); - } - } - - if (!next(parser)) { - break; - } - } - - parser.close(); - } - - /** - * Parses an Atom entry. - */ - private void parseEntry(XMLStreamReader parser) throws Exception { - String atomTitle = null; - - next(parser); - - // walk through all tags in entry - while (true) { - int event = parser.getEventType(); - if (event == XMLStreamReader.START_ELEMENT) { - QName name = parser.getName(); - - if (Constants.NAMESPACE_RESTATOM.equals(name.getNamespaceURI())) { - if (TAG_OBJECT.equals(name.getLocalPart())) { - parseObject(parser); - } else if (TAG_CONTENT.equals(name.getLocalPart())) { - parseCmisContent(parser); - } else { - skip(parser); - } - } else if (Constants.NAMESPACE_ATOM.equals(name.getNamespaceURI())) { - if (TAG_CONTENT.equals(name.getLocalPart())) { - parseAtomContent(parser); - } else if (TAG_TITLE.equals(name.getLocalPart())) { - atomTitle = readText(parser); - } else { - skip(parser); - } - } else { - skip(parser); - } - } else if (event == XMLStreamReader.END_ELEMENT) { - break; - } else { - if (!next(parser)) { - break; - } - } - } - - // overwrite cmis:name with Atom title - if ((fObject != null) && (fObject.getProperties() != null) && (atomTitle != null)) { - PropertyString nameProperty = new PropertyStringImpl(PropertyIds.NAME, atomTitle); - ((PropertiesImpl) fObject.getProperties()).addProperty(nameProperty); - } - } - - /** - * Parses a CMIS object. - */ - private void parseObject(XMLStreamReader parser) throws Exception { - Unmarshaller u = JaxBHelper.createUnmarshaller(); - JAXBElement object = u.unmarshal(parser, CmisObjectType.class); - - if (object != null) { - fObject = Converter.convert(object.getValue()); - } - } - - /** - * Extract the content stream. - */ - private void parseAtomContent(XMLStreamReader parser) throws Exception { - fAtomContentStream = new ContentStreamImpl(); - - // read attributes - String type = "text"; - for (int i = 0; i < parser.getAttributeCount(); i++) { - QName attrName = parser.getAttributeName(i); - if (ATTR_TYPE.equals(attrName.getLocalPart())) { - fAtomContentStream.setMimeType(parser.getAttributeValue(i)); - if (parser.getAttributeValue(i) != null) { - type = parser.getAttributeValue(i).trim().toLowerCase(); - } - } else if (ATTR_SRC.equals(attrName.getLocalPart())) { - throw new CmisNotSupportedException("External content not supported!"); - } - } - - if (type.equals("text") || type.equals("html")) { - fAtomContentStream.setStream(new ByteArrayInputStream(readText(parser).getBytes("UTF-8"))); - } else if (type.equals("xhtml")) { - fAtomContentStream.setStream(copy(parser)); - } else if (type.endsWith("/xml") || type.endsWith("+xml")) { - fAtomContentStream.setStream(copy(parser)); - } else if (type.startsWith("text/")) { - fAtomContentStream.setStream(new ByteArrayInputStream(readText(parser).getBytes("UTF-8"))); - } else { - fAtomContentStream.setStream(new ByteArrayInputStream(Base64.decodeBase64(readText(parser)))); - } - } - - /** - * Extract the content stream. - */ - private void parseCmisContent(XMLStreamReader parser) throws Exception { - fCmisContentStream = new ContentStreamImpl(); - - next(parser); - - // walk through all tags in content - while (true) { - int event = parser.getEventType(); - if (event == XMLStreamReader.START_ELEMENT) { - QName name = parser.getName(); - - if (Constants.NAMESPACE_RESTATOM.equals(name.getNamespaceURI())) { - if (TAG_MEDIATYPE.equals(name.getLocalPart())) { - fCmisContentStream.setMimeType(readText(parser)); - } else if (TAG_BASE64.equals(name.getLocalPart())) { - fCmisContentStream.setStream(new ByteArrayInputStream(Base64.decodeBase64(readText(parser)))); - } else { - skip(parser); - } - } else { - skip(parser); - } - } else if (event == XMLStreamReader.END_ELEMENT) { - break; - } else { - if (!next(parser)) { - break; - } - } - } - - next(parser); - } - - /** - * Parses a tag that contains text. - */ - private String readText(XMLStreamReader parser) throws Exception { - StringBuilder sb = new StringBuilder(); - - next(parser); - - while (true) { - int event = parser.getEventType(); - if (event == XMLStreamReader.END_ELEMENT) { - break; - } else if (event == XMLStreamReader.CHARACTERS) { - String s = parser.getText(); - if (s != null) { - sb.append(s); - } - } else if (event == XMLStreamReader.START_ELEMENT) { - throw new RuntimeException("Unexpected tag: " + parser.getName()); - } - - if (!next(parser)) { - break; - } - } - - next(parser); - - return sb.toString(); - } - - /** - * Copies a subtree into a stream. - */ - private InputStream copy(XMLStreamReader parser) throws Exception { - // 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 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 iter = (Iterator) 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); - } - } - - /** - * Skips a tag or subtree. - */ - private void skip(XMLStreamReader parser) throws Exception { - int level = 1; - while (next(parser)) { - int event = parser.getEventType(); - if (event == XMLStreamReader.START_ELEMENT) { - level++; - } else if (event == XMLStreamReader.END_ELEMENT) { - level--; - if (level == 0) { - break; - } - } - } - - next(parser); - } - - private boolean next(XMLStreamReader parser) throws Exception { - if (parser.hasNext()) { - parser.next(); - return true; - } + private final static String TAG_ENTRY = "entry"; + private final static String TAG_TITLE = "title"; + private final static String TAG_OBJECT = "object"; + private final static String TAG_CONTENT = "content"; + private final static String TAG_BASE64 = "base64"; + private final static String TAG_MEDIATYPE = "mediatype"; + + private final static String ATTR_SRC = "src"; + private final static String ATTR_TYPE = "type"; + + private ObjectData fObject; + private ContentStreamImpl fAtomContentStream; + private ContentStreamImpl fCmisContentStream; + + /** + * Constructor. + */ + public AtomEntryParser() { + } + + /** + * Constructor that immediately parses the given stream. + */ + public AtomEntryParser(InputStream stream) throws Exception { + parse(stream); + } + + /** + * Returns the object. + */ + public ObjectData getObject() { + return fObject; + } + + /** + * Returns the properties of the object. + */ + public Properties getProperties() { + return (fObject == null ? null : fObject.getProperties()); + } + + /** + * Returns the Id of the object. + */ + public String getId() { + Properties properties = getProperties(); + if (properties == null) { + return null; + } + + Map> propertiesMap = properties.getProperties(); + if (propertiesMap == null) { + return null; + } + + PropertyData property = propertiesMap.get(PropertyIds.OBJECT_ID); + if (property instanceof PropertyId) { + return ((PropertyId) property).getFirstValue(); + } + + return null; + } + + /** + * Returns the ACL of the object. + */ + public Acl getAcl() { + return (fObject == null ? null : fObject.getAcl()); + } + + /** + * Returns the policy id list of the object. + */ + public List getPolicyIds() { + if ((fObject == null) || (fObject.getPolicyIds() == null)) { + return null; + } + + return fObject.getPolicyIds().getPolicyIds(); + } + + /** + * Returns the content stream. + */ + public ContentStream getContentStream() { + return (fCmisContentStream == null ? fAtomContentStream : fCmisContentStream); + } + + /** + * Parses the stream. + */ + public void parse(InputStream stream) throws Exception { + fObject = null; + fAtomContentStream = null; + fCmisContentStream = null; + + if (stream == null) { + return; + } + + XMLInputFactory factory = XMLInputFactory.newInstance(); + XMLStreamReader parser = factory.createXMLStreamReader(stream); + + while (true) { + int event = parser.getEventType(); + if (event == XMLStreamReader.START_ELEMENT) { + QName name = parser.getName(); + + if (Constants.NAMESPACE_ATOM.equals(name.getNamespaceURI()) && (TAG_ENTRY.equals(name.getLocalPart()))) { + parseEntry(parser); + break; + } else { + throw new CmisInvalidArgumentException("XML is not an Atom entry!"); + } + } + + if (!next(parser)) { + break; + } + } + + parser.close(); + } + + /** + * Parses an Atom entry. + */ + private void parseEntry(XMLStreamReader parser) throws Exception { + String atomTitle = null; + + next(parser); + + // walk through all tags in entry + while (true) { + int event = parser.getEventType(); + if (event == XMLStreamReader.START_ELEMENT) { + QName name = parser.getName(); + + if (Constants.NAMESPACE_RESTATOM.equals(name.getNamespaceURI())) { + if (TAG_OBJECT.equals(name.getLocalPart())) { + parseObject(parser); + } else if (TAG_CONTENT.equals(name.getLocalPart())) { + parseCmisContent(parser); + } else { + skip(parser); + } + } else if (Constants.NAMESPACE_ATOM.equals(name.getNamespaceURI())) { + if (TAG_CONTENT.equals(name.getLocalPart())) { + parseAtomContent(parser); + } else if (TAG_TITLE.equals(name.getLocalPart())) { + atomTitle = readText(parser); + } else { + skip(parser); + } + } else { + skip(parser); + } + } else if (event == XMLStreamReader.END_ELEMENT) { + break; + } else { + if (!next(parser)) { + break; + } + } + } + + // overwrite cmis:name with Atom title + if ((fObject != null) && (fObject.getProperties() != null) && (atomTitle != null)) { + PropertyString nameProperty = new PropertyStringImpl(PropertyIds.NAME, atomTitle); + ((PropertiesImpl) fObject.getProperties()).addProperty(nameProperty); + } + } + + /** + * Parses a CMIS object. + */ + private void parseObject(XMLStreamReader parser) throws Exception { + Unmarshaller u = JaxBHelper.createUnmarshaller(); + JAXBElement object = u.unmarshal(parser, CmisObjectType.class); + + if (object != null) { + fObject = Converter.convert(object.getValue()); + } + } + + /** + * Extract the content stream. + */ + private void parseAtomContent(XMLStreamReader parser) throws Exception { + fAtomContentStream = new ContentStreamImpl(); + + // read attributes + String type = "text"; + for (int i = 0; i < parser.getAttributeCount(); i++) { + QName attrName = parser.getAttributeName(i); + if (ATTR_TYPE.equals(attrName.getLocalPart())) { + fAtomContentStream.setMimeType(parser.getAttributeValue(i)); + if (parser.getAttributeValue(i) != null) { + type = parser.getAttributeValue(i).trim().toLowerCase(); + } + } else if (ATTR_SRC.equals(attrName.getLocalPart())) { + throw new CmisNotSupportedException("External content not supported!"); + } + } + + if (type.equals("text") || type.equals("html")) { + fAtomContentStream.setStream(new ByteArrayInputStream(readText(parser).getBytes("UTF-8"))); + } else if (type.equals("xhtml")) { + fAtomContentStream.setStream(copy(parser)); + } else if (type.endsWith("/xml") || type.endsWith("+xml")) { + fAtomContentStream.setStream(copy(parser)); + } else if (type.startsWith("text/")) { + fAtomContentStream.setStream(new ByteArrayInputStream(readText(parser).getBytes("UTF-8"))); + } else { + fAtomContentStream.setStream(new ByteArrayInputStream(Base64.decodeBase64(readText(parser)))); + } + } + + /** + * Extract the content stream. + */ + private void parseCmisContent(XMLStreamReader parser) throws Exception { + fCmisContentStream = new ContentStreamImpl(); + + next(parser); + + // walk through all tags in content + while (true) { + int event = parser.getEventType(); + if (event == XMLStreamReader.START_ELEMENT) { + QName name = parser.getName(); + + if (Constants.NAMESPACE_RESTATOM.equals(name.getNamespaceURI())) { + if (TAG_MEDIATYPE.equals(name.getLocalPart())) { + fCmisContentStream.setMimeType(readText(parser)); + } else if (TAG_BASE64.equals(name.getLocalPart())) { + fCmisContentStream.setStream(new ByteArrayInputStream(Base64.decodeBase64(readText(parser)))); + } else { + skip(parser); + } + } else { + skip(parser); + } + } else if (event == XMLStreamReader.END_ELEMENT) { + break; + } else { + if (!next(parser)) { + break; + } + } + } + + next(parser); + } + + /** + * Parses a tag that contains text. + */ + private String readText(XMLStreamReader parser) throws Exception { + StringBuilder sb = new StringBuilder(); + + next(parser); + + while (true) { + int event = parser.getEventType(); + if (event == XMLStreamReader.END_ELEMENT) { + break; + } else if (event == XMLStreamReader.CHARACTERS) { + String s = parser.getText(); + if (s != null) { + sb.append(s); + } + } else if (event == XMLStreamReader.START_ELEMENT) { + throw new RuntimeException("Unexpected tag: " + parser.getName()); + } + + if (!next(parser)) { + break; + } + } + + next(parser); + + return sb.toString(); + } + + /** + * Copies a subtree into a stream. + */ + private InputStream copy(XMLStreamReader parser) throws Exception { + // 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 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 iter = (Iterator) 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); + } + } + + /** + * Skips a tag or subtree. + */ + private void skip(XMLStreamReader parser) throws Exception { + int level = 1; + while (next(parser)) { + int event = parser.getEventType(); + if (event == XMLStreamReader.START_ELEMENT) { + level++; + } else if (event == XMLStreamReader.END_ELEMENT) { + level--; + if (level == 0) { + break; + } + } + } + + next(parser); + } + + private boolean next(XMLStreamReader parser) throws Exception { + if (parser.hasNext()) { + parser.next(); + return true; + } - return false; - } + return false; + } } \ No newline at end of file Modified: incubator/chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/impl/atompub/AtomFeed.java URL: http://svn.apache.org/viewvc/incubator/chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/impl/atompub/AtomFeed.java?rev=936938&r1=936937&r2=936938&view=diff ============================================================================== --- incubator/chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/impl/atompub/AtomFeed.java (original) +++ incubator/chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/impl/atompub/AtomFeed.java Thu Apr 22 16:27:57 2010 @@ -35,133 +35,133 @@ import org.apache.chemistry.opencmis.com */ public class AtomFeed extends AtomDocumentBase { - public static final BigInteger DEFAULT_PAGE_SIZE = BigInteger.valueOf(100); + public static final BigInteger DEFAULT_PAGE_SIZE = BigInteger.valueOf(100); - /** - * Creates an Atom feed document. - */ - public AtomFeed() { - } - - /** - * Creates an Atom feed that is embedded somewhere. - */ - public AtomFeed(XMLStreamWriter writer) { - setWriter(writer); - } - - /** - * Opens the feed tag. - */ - public void startFeed(boolean isRoot) throws XMLStreamException { - getWriter().writeStartElement(Constants.NAMESPACE_ATOM, "feed"); - - if (isRoot) { - writeNamespace(Constants.NAMESPACE_ATOM); - writeNamespace(Constants.NAMESPACE_CMIS); - writeNamespace(Constants.NAMESPACE_RESTATOM); - writeNamespace(Constants.NAMESPACE_APP); - } - } - - /** - * Opens the children tag. - */ - public void startChildren() throws XMLStreamException { - XMLStreamWriter writer = getWriter(); - writer.writeStartElement(Constants.NAMESPACE_RESTATOM, "children"); - } - - /** - * Closes the feed tag. - */ - public void endChildren() throws XMLStreamException { - getWriter().writeEndElement(); - } - - /** - * Closes the feed tag. - */ - public void endFeed() throws XMLStreamException { - getWriter().writeEndElement(); - } - - /** - * Writes the feed elements that are required by Atom. - */ - public void writeFeedElements(String id, String author, String title, GregorianCalendar updated, - String pathSegment, BigInteger numItems) throws XMLStreamException { - writeAuthor(author); - writeId(generateAtomId(id)); - writeTitle(title); - writeUpdated(updated); - writePathSegment(pathSegment); - writeNumItems(numItems); - } - - /** - * Writes a CMIS numItems tag. - */ - public void writeNumItems(BigInteger numItems) throws XMLStreamException { - if (numItems == null) { - return; - } - - writeSimpleTag(Constants.NAMESPACE_RESTATOM, "numItems", numItems.toString()); - } - - /** - * Writes paging links. - */ - public void writePagingLinks(UrlBuilder pagingUrl, BigInteger maxItems, BigInteger skipCount, BigInteger numItems, - Boolean hasMoreItems, BigInteger pageSize) throws XMLStreamException { - - if ((skipCount == null) || (skipCount.compareTo(BigInteger.ZERO) == -1)) { - skipCount = BigInteger.ZERO; - } - - if ((maxItems == null) || (maxItems.compareTo(BigInteger.ZERO) == -1)) { - if ((pageSize == null) || (pageSize.compareTo(BigInteger.ZERO) == -1)) { - maxItems = DEFAULT_PAGE_SIZE; - } else { - maxItems = pageSize; - } - } - - // if not first page -> add "first" and "previous" link - if (skipCount.compareTo(BigInteger.ZERO) == 1) { - // first link - UrlBuilder firstLink = new UrlBuilder(pagingUrl); - firstLink.addParameter(Constants.PARAM_SKIP_COUNT, "0"); - firstLink.addParameter(Constants.PARAM_MAX_ITEMS, maxItems); - writeFirstLink(firstLink.toString()); - - // previous link - UrlBuilder previousLink = new UrlBuilder(pagingUrl); - previousLink.addParameter(Constants.PARAM_SKIP_COUNT, skipCount.subtract(maxItems).max(BigInteger.ZERO)); - previousLink.addParameter(Constants.PARAM_MAX_ITEMS, maxItems); - writePreviousLink(previousLink.toString()); - } - - // if has more -> add "next" link - if ((hasMoreItems != null) && hasMoreItems.booleanValue()) { - // next link - UrlBuilder nextLink = new UrlBuilder(pagingUrl); - nextLink.addParameter(Constants.PARAM_SKIP_COUNT, skipCount.add(maxItems)); - nextLink.addParameter(Constants.PARAM_MAX_ITEMS, maxItems); - writeNextLink(nextLink.toString()); - } - - // if not last page -> add "last" link - if ((numItems != null) && (numItems.compareTo(BigInteger.ZERO) == 1)) { - BigInteger lastSkip = numItems.subtract(maxItems).max(BigInteger.ZERO); - if (lastSkip.compareTo(BigInteger.ZERO) == 1) { - // last link - UrlBuilder lastLink = new UrlBuilder(pagingUrl); - lastLink.addParameter(Constants.PARAM_SKIP_COUNT, lastSkip); - lastLink.addParameter(Constants.PARAM_MAX_ITEMS, maxItems); - writeLastLink(lastLink.toString()); - } - } - } + /** + * Creates an Atom feed document. + */ + public AtomFeed() { + } + + /** + * Creates an Atom feed that is embedded somewhere. + */ + public AtomFeed(XMLStreamWriter writer) { + setWriter(writer); + } + + /** + * Opens the feed tag. + */ + public void startFeed(boolean isRoot) throws XMLStreamException { + getWriter().writeStartElement(Constants.NAMESPACE_ATOM, "feed"); + + if (isRoot) { + writeNamespace(Constants.NAMESPACE_ATOM); + writeNamespace(Constants.NAMESPACE_CMIS); + writeNamespace(Constants.NAMESPACE_RESTATOM); + writeNamespace(Constants.NAMESPACE_APP); + } + } + + /** + * Opens the children tag. + */ + public void startChildren() throws XMLStreamException { + XMLStreamWriter writer = getWriter(); + writer.writeStartElement(Constants.NAMESPACE_RESTATOM, "children"); + } + + /** + * Closes the feed tag. + */ + public void endChildren() throws XMLStreamException { + getWriter().writeEndElement(); + } + + /** + * Closes the feed tag. + */ + public void endFeed() throws XMLStreamException { + getWriter().writeEndElement(); + } + + /** + * Writes the feed elements that are required by Atom. + */ + public void writeFeedElements(String id, String author, String title, GregorianCalendar updated, + String pathSegment, BigInteger numItems) throws XMLStreamException { + writeAuthor(author); + writeId(generateAtomId(id)); + writeTitle(title); + writeUpdated(updated); + writePathSegment(pathSegment); + writeNumItems(numItems); + } + + /** + * Writes a CMIS numItems tag. + */ + public void writeNumItems(BigInteger numItems) throws XMLStreamException { + if (numItems == null) { + return; + } + + writeSimpleTag(Constants.NAMESPACE_RESTATOM, "numItems", numItems.toString()); + } + + /** + * Writes paging links. + */ + public void writePagingLinks(UrlBuilder pagingUrl, BigInteger maxItems, BigInteger skipCount, BigInteger numItems, + Boolean hasMoreItems, BigInteger pageSize) throws XMLStreamException { + + if ((skipCount == null) || (skipCount.compareTo(BigInteger.ZERO) == -1)) { + skipCount = BigInteger.ZERO; + } + + if ((maxItems == null) || (maxItems.compareTo(BigInteger.ZERO) == -1)) { + if ((pageSize == null) || (pageSize.compareTo(BigInteger.ZERO) == -1)) { + maxItems = DEFAULT_PAGE_SIZE; + } else { + maxItems = pageSize; + } + } + + // if not first page -> add "first" and "previous" link + if (skipCount.compareTo(BigInteger.ZERO) == 1) { + // first link + UrlBuilder firstLink = new UrlBuilder(pagingUrl); + firstLink.addParameter(Constants.PARAM_SKIP_COUNT, "0"); + firstLink.addParameter(Constants.PARAM_MAX_ITEMS, maxItems); + writeFirstLink(firstLink.toString()); + + // previous link + UrlBuilder previousLink = new UrlBuilder(pagingUrl); + previousLink.addParameter(Constants.PARAM_SKIP_COUNT, skipCount.subtract(maxItems).max(BigInteger.ZERO)); + previousLink.addParameter(Constants.PARAM_MAX_ITEMS, maxItems); + writePreviousLink(previousLink.toString()); + } + + // if has more -> add "next" link + if ((hasMoreItems != null) && hasMoreItems.booleanValue()) { + // next link + UrlBuilder nextLink = new UrlBuilder(pagingUrl); + nextLink.addParameter(Constants.PARAM_SKIP_COUNT, skipCount.add(maxItems)); + nextLink.addParameter(Constants.PARAM_MAX_ITEMS, maxItems); + writeNextLink(nextLink.toString()); + } + + // if not last page -> add "last" link + if ((numItems != null) && (numItems.compareTo(BigInteger.ZERO) == 1)) { + BigInteger lastSkip = numItems.subtract(maxItems).max(BigInteger.ZERO); + if (lastSkip.compareTo(BigInteger.ZERO) == 1) { + // last link + UrlBuilder lastLink = new UrlBuilder(pagingUrl); + lastLink.addParameter(Constants.PARAM_SKIP_COUNT, lastSkip); + lastLink.addParameter(Constants.PARAM_MAX_ITEMS, maxItems); + writeLastLink(lastLink.toString()); + } + } + } } Modified: incubator/chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/impl/atompub/AtomPubUtils.java URL: http://svn.apache.org/viewvc/incubator/chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/impl/atompub/AtomPubUtils.java?rev=936938&r1=936937&r2=936938&view=diff ============================================================================== --- incubator/chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/impl/atompub/AtomPubUtils.java (original) +++ incubator/chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/impl/atompub/AtomPubUtils.java Thu Apr 22 16:27:57 2010 @@ -52,446 +52,446 @@ import org.apache.chemistry.opencmis.ser */ public final class AtomPubUtils { - public static final String RESOURCE_CHILDREN = "children"; - public static final String RESOURCE_DESCENDANTS = "descendants"; - public static final String RESOURCE_FOLDERTREE = "foldertree"; - public static final String RESOURCE_TYPE = "type"; - public static final String RESOURCE_TYPES = "types"; - public static final String RESOURCE_TYPESDESC = "typedesc"; - public static final String RESOURCE_ENTRY = "entry"; - public static final String RESOURCE_PARENTS = "parents"; - public static final String RESOURCE_VERSIONS = "versions"; - public static final String RESOURCE_ALLOWABLEACIONS = "allowableactions"; - public static final String RESOURCE_ACL = "acl"; - public static final String RESOURCE_POLICIES = "policies"; - public static final String RESOURCE_RELATIONSHIPS = "relationships"; - public static final String RESOURCE_OBJECTBYID = "id"; - public static final String RESOURCE_OBJECTBYPATH = "path"; - public static final String RESOURCE_QUERY = "query"; - public static final String RESOURCE_CHECKEDOUT = "checkedout"; - public static final String RESOURCE_UNFILED = "unfiled"; - public static final String RESOURCE_CHANGES = "changes"; - public static final String RESOURCE_CONTENT = "content"; - - public static final BigInteger PAGE_SIZE = BigInteger.valueOf(100); - - public static final String TYPE_AUTHOR = "unknown"; - - /** - * Private constructor. - */ - private AtomPubUtils() { - } - - /** - * Compiles the base URL for links, collections and templates. - */ - public static UrlBuilder compileBaseUrl(HttpServletRequest request, String repositoryId) { - UrlBuilder url = new UrlBuilder(request.getScheme(), request.getServerName(), request.getServerPort(), null); - - url.addPath(request.getContextPath()); - url.addPath(request.getServletPath()); - - if (repositoryId != null) { - url.addPath(repositoryId); - } - - return url; - } - - /** - * Compiles a URL for links, collections and templates. - */ - public static String compileUrl(UrlBuilder baseUrl, String resource, String id) { - return compileUrlBuilder(baseUrl, resource, id).toString(); - } - - /** - * Compiles a URL for links, collections and templates. - */ - public static UrlBuilder compileUrlBuilder(UrlBuilder baseUrl, String resource, String id) { - UrlBuilder url = new UrlBuilder(baseUrl); - url.addPath(resource); - - if (id != null) { - url.addParameter("id", id); - } - - return url; - } - - // ------------------------------------------------------------------------- - // --- parameters --- - // ------------------------------------------------------------------------- - - /** - * Extracts a string parameter. - */ - @SuppressWarnings("unchecked") - public static String getStringParameter(HttpServletRequest request, String name) { - if (name == null) { - return null; - } - - Map parameters = (Map) request.getParameterMap(); - for (Map.Entry parameter : parameters.entrySet()) { - if (name.equalsIgnoreCase(parameter.getKey())) { - if (parameter.getValue() == null) { - return null; - } - return parameter.getValue()[0]; - } - } - - return null; - } - - /** - * Extracts a boolean parameter (with default). - */ - public static boolean getBooleanParameter(HttpServletRequest request, String name, boolean def) { - String value = getStringParameter(request, name); - if (value == null) { - return def; - } - - return Boolean.valueOf(value); - } - - /** - * Extracts a boolean parameter. - */ - public static Boolean getBooleanParameter(HttpServletRequest request, String name) { - String value = getStringParameter(request, name); - if (value == null) { - return null; - } - - return Boolean.valueOf(value); - } - - /** - * Extracts an integer parameter (with default). - */ - public static BigInteger getBigIntegerParameter(HttpServletRequest request, String name, long def) { - BigInteger result = getBigIntegerParameter(request, name); - if (result == null) { - result = BigInteger.valueOf(def); - } - - return result; - } - - /** - * Extracts an integer parameter. - */ - public static BigInteger getBigIntegerParameter(HttpServletRequest request, String name) { - String value = getStringParameter(request, name); - if (value == null) { - return null; - } - - try { - return new BigInteger(value); - } catch (Exception e) { - throw new CmisInvalidArgumentException("Invalid parameter '" + name + "'!"); - } - } - - /** - * Extracts an enum parameter. - */ - @SuppressWarnings("unchecked") - public static T getEnumParameter(HttpServletRequest request, String name, Class clazz) { - String value = getStringParameter(request, name); - if (value == null) { - return null; - } - - try { - Method m = clazz.getMethod("fromValue", new Class[] { String.class }); - return (T) m.invoke(null, new Object[] { value }); - } catch (Exception e) { - if (e instanceof IllegalArgumentException) { - throw new CmisInvalidArgumentException("Invalid parameter '" + name + "'!"); - } - - throw new CmisRuntimeException(e.getMessage(), e); - } - } - - /** - * Extracts a property from an object. - */ - @SuppressWarnings("unchecked") - public static T getProperty(ObjectData object, String name, Class clazz) { - if (object == null) { - return null; - } - - Properties propData = object.getProperties(); - if (propData == null) { - return null; - } - - Map> properties = propData.getProperties(); - if (properties == null) { - return null; - } - - PropertyData property = properties.get(name); - if (property == null) { - return null; - } - - Object value = property.getFirstValue(); - if (!clazz.isInstance(value)) { - return null; - } - - return (T) value; - } - - // ------------------------------------------------------------------------- - // --- entry builder --- - // ------------------------------------------------------------------------- - - /** - * Writes the a object entry. - */ - public static void writeObjectEntry(AtomEntry entry, ObjectData object, ObjectInfoHolder infoHolder, - List children, String repositoryId, String pathSegment, - String relativePathSegment, UrlBuilder baseUrl, boolean isRoot) throws XMLStreamException, JAXBException { - if ((object == null) || (infoHolder == null)) { - throw new CmisRuntimeException("Object or Object Info not set!"); - } - - ObjectInfo info = infoHolder.getObjectInfo(object.getId()); - if (info == null) { - throw new CmisRuntimeException("Object Info not found!"); - } - - // start - entry.startEntry(isRoot); - - // write object - String contentSrc = null; - - if (info.hasContent()) { - UrlBuilder contentSrcBuilder = compileUrlBuilder(baseUrl, RESOURCE_CONTENT, info.getId()); - if (info.getFileName() != null) { - contentSrcBuilder.addPath(info.getFileName()); - } - - contentSrc = contentSrcBuilder.toString(); - } - - entry.writeObject(object, info, contentSrc, info.getContentType(), pathSegment, relativePathSegment); - - // write links - entry.writeServiceLink(baseUrl.toString(), repositoryId); - - entry.writeSelfLink(compileUrl(baseUrl, RESOURCE_ENTRY, info.getId()), info.getId()); - entry.writeEnclosureLink(compileUrl(baseUrl, RESOURCE_ENTRY, info.getId())); - entry.writeEditLink(compileUrl(baseUrl, RESOURCE_ENTRY, info.getId())); - entry.writeDescribedByLink(compileUrl(baseUrl, RESOURCE_TYPE, info.getTypeId())); - entry.writeAllowableActionsLink(compileUrl(baseUrl, RESOURCE_ALLOWABLEACIONS, info.getId())); - - if (info.hasParent()) { - entry.writeUpLink(compileUrl(baseUrl, RESOURCE_PARENTS, info.getId()), Constants.MEDIATYPE_FEED); - } - - if (info.getBaseType() == BaseTypeId.CMIS_FOLDER) { - entry.writeDownLink(compileUrl(baseUrl, RESOURCE_CHILDREN, info.getId()), Constants.MEDIATYPE_FEED); - - if (info.supportsDescendants()) { - entry.writeDownLink(compileUrl(baseUrl, RESOURCE_DESCENDANTS, info.getId()), - Constants.MEDIATYPE_DESCENDANTS); - } - - if (info.supportsFolderTree()) { - entry.writeFolderTreeLink(compileUrl(baseUrl, RESOURCE_FOLDERTREE, info.getId())); - } - } - - if (info.getVersionSeriesId() != null) { - entry.writeVersionHistoryLink(compileUrl(baseUrl, RESOURCE_VERSIONS, info.getVersionSeriesId())); - } - - if (!info.isCurrentVersion()) { - UrlBuilder cvUrl = compileUrlBuilder(baseUrl, RESOURCE_ENTRY, info.getId()); - cvUrl.addParameter(Constants.PARAM_RETURN_VERSION, ReturnVersion.LATEST); - entry.writeEditLink(cvUrl.toString()); - } - - if (contentSrc != null) { - entry.writeEditMediaLink(contentSrc, info.getContentType()); - } - - if (info.getWorkingCopyId() != null) { - entry.writeWorkingCopyLink(compileUrl(baseUrl, RESOURCE_ENTRY, info.getWorkingCopyId())); - } - - if (info.getWorkingCopyOriginalId() != null) { - entry.writeViaLink(compileUrl(baseUrl, RESOURCE_ENTRY, info.getWorkingCopyOriginalId())); - } - - if (info.getRenditionInfos() != null) { - for (RenditionInfo ri : info.getRenditionInfos()) { - entry.writeAlternateLink(compileUrl(baseUrl, RESOURCE_CONTENT, ri.getId()), ri.getContenType(), ri - .getKind(), ri.getTitle(), ri.getLength()); - } - } - - if (info.hasAcl()) { - entry.writeAclLink(compileUrl(baseUrl, RESOURCE_ACL, info.getId())); - } - - if (info.supportsPolicies()) { - entry.writeAclLink(compileUrl(baseUrl, RESOURCE_POLICIES, info.getId())); - } - - if (info.supportsRelationships()) { - entry.writeRelationshipsLink(compileUrl(baseUrl, RESOURCE_RELATIONSHIPS, info.getId())); - } - - if (info.getRelationshipSourceIds() != null) { - for (String id : info.getRelationshipSourceIds()) { - entry.writeRelationshipSourceLink(compileUrl(baseUrl, RESOURCE_ENTRY, id)); - } - } - - if (info.getRelationshipTargetIds() != null) { - for (String id : info.getRelationshipTargetIds()) { - entry.writeRelationshipTargetLink(compileUrl(baseUrl, RESOURCE_ENTRY, id)); - } - } - - // write children - if ((children != null) && (children.size() > 0)) { - writeObjectChildren(entry, info, children, infoHolder, repositoryId, baseUrl); - } - - // we are done - entry.endEntry(); - } - - /** - * Writes an objects entry children feed. - */ - public static void writeObjectChildren(AtomEntry entry, ObjectInfo folderInfo, - List children, ObjectInfoHolder infoHolder, String repositoryId, UrlBuilder baseUrl) - throws XMLStreamException, JAXBException { - - // start - AtomFeed feed = new AtomFeed(entry.getWriter()); - feed.startChildren(); - feed.startFeed(false); - - // write basic Atom feed elements - feed.writeFeedElements(folderInfo.getId(), folderInfo.getCreatedBy(), folderInfo.getName(), folderInfo - .getLastModificationDate(), null, null); - - // write links - feed.writeServiceLink(baseUrl.toString(), repositoryId); - - feed.writeSelfLink(compileUrl(baseUrl, RESOURCE_DESCENDANTS, folderInfo.getId()), null); - - feed.writeViaLink(compileUrl(baseUrl, RESOURCE_ENTRY, folderInfo.getId())); - - feed.writeDownLink(compileUrl(baseUrl, RESOURCE_CHILDREN, folderInfo.getId()), Constants.MEDIATYPE_FEED); - - feed.writeDownLink(compileUrl(baseUrl, RESOURCE_FOLDERTREE, folderInfo.getId()), - Constants.MEDIATYPE_DESCENDANTS); - - feed.writeUpLink(compileUrl(baseUrl, RESOURCE_PARENTS, folderInfo.getId()), Constants.MEDIATYPE_FEED); - - for (ObjectInFolderContainer container : children) { - if ((container != null) && (container.getObject() != null)) { - writeObjectEntry(entry, container.getObject().getObject(), infoHolder, container.getChildren(), - repositoryId, container.getObject().getPathSegment(), null, baseUrl, false); - } - } - - // we are done - feed.endFeed(); - feed.endChildren(); - } - - /** - * Writes the a type entry. - */ - public static void writeTypeEntry(AtomEntry entry, TypeDefinition type, List children, - String repositoryId, UrlBuilder baseUrl, boolean isRoot) throws XMLStreamException, JAXBException { - - // start - entry.startEntry(isRoot); - - // write type - entry.writeType(type); - - // write links - entry.writeServiceLink(baseUrl.toString(), repositoryId); - - entry.writeSelfLink(compileUrl(baseUrl, RESOURCE_TYPE, type.getId()), type.getId()); - entry.writeEnclosureLink(compileUrl(baseUrl, RESOURCE_TYPE, type.getId())); - if (type.getParentTypeId() != null) { - entry.writeUpLink(compileUrl(baseUrl, RESOURCE_TYPE, type.getParentTypeId()), Constants.MEDIATYPE_ENTRY); - } - UrlBuilder downLink = compileUrlBuilder(baseUrl, RESOURCE_TYPES, null); - downLink.addParameter(Constants.PARAM_TYPE_ID, type.getId()); - entry.writeDownLink(downLink.toString(), Constants.MEDIATYPE_CHILDREN); - entry.writeDescribedByLink(compileUrl(baseUrl, RESOURCE_TYPE, type.getBaseTypeId().value())); - - // write children - if ((children != null) && (children.size() > 0)) { - writeTypeChildren(entry, type, children, repositoryId, baseUrl); - } - - // we are done - entry.endEntry(); - } - - /** - * Writes the a type entry children feed. - */ - private static void writeTypeChildren(AtomEntry entry, TypeDefinition type, List children, - String repositoryId, UrlBuilder baseUrl) throws XMLStreamException, JAXBException { - - // start - AtomFeed feed = new AtomFeed(entry.getWriter()); - feed.startChildren(); - feed.startFeed(false); - - // write basic Atom feed elements - feed.writeFeedElements(type.getId(), TYPE_AUTHOR, type.getDisplayName(), new GregorianCalendar(), null, null); - - feed.writeServiceLink(baseUrl.toString(), repositoryId); - - UrlBuilder selfLink = compileUrlBuilder(baseUrl, RESOURCE_TYPESDESC, null); - selfLink.addParameter(Constants.PARAM_TYPE_ID, type.getId()); - feed.writeSelfLink(selfLink.toString(), type.getId()); - - feed.writeViaLink(compileUrl(baseUrl, RESOURCE_TYPE, type.getId())); - - UrlBuilder downLink = compileUrlBuilder(baseUrl, RESOURCE_TYPES, null); - downLink.addParameter(Constants.PARAM_TYPE_ID, type.getId()); - feed.writeDownLink(downLink.toString(), Constants.MEDIATYPE_FEED); - - if (type.getParentTypeId() != null) { - feed.writeUpLink(compileUrl(baseUrl, RESOURCE_TYPE, type.getParentTypeId()), Constants.MEDIATYPE_ENTRY); - } - - // write tree - for (TypeDefinitionContainer container : children) { - if ((container != null) && (container.getTypeDefinition() != null)) { - writeTypeEntry(entry, container.getTypeDefinition(), container.getChildren(), repositoryId, baseUrl, - false); - } - } - - // we are done - feed.endFeed(); - feed.endChildren(); - } + public static final String RESOURCE_CHILDREN = "children"; + public static final String RESOURCE_DESCENDANTS = "descendants"; + public static final String RESOURCE_FOLDERTREE = "foldertree"; + public static final String RESOURCE_TYPE = "type"; + public static final String RESOURCE_TYPES = "types"; + public static final String RESOURCE_TYPESDESC = "typedesc"; + public static final String RESOURCE_ENTRY = "entry"; + public static final String RESOURCE_PARENTS = "parents"; + public static final String RESOURCE_VERSIONS = "versions"; + public static final String RESOURCE_ALLOWABLEACIONS = "allowableactions"; + public static final String RESOURCE_ACL = "acl"; + public static final String RESOURCE_POLICIES = "policies"; + public static final String RESOURCE_RELATIONSHIPS = "relationships"; + public static final String RESOURCE_OBJECTBYID = "id"; + public static final String RESOURCE_OBJECTBYPATH = "path"; + public static final String RESOURCE_QUERY = "query"; + public static final String RESOURCE_CHECKEDOUT = "checkedout"; + public static final String RESOURCE_UNFILED = "unfiled"; + public static final String RESOURCE_CHANGES = "changes"; + public static final String RESOURCE_CONTENT = "content"; + + public static final BigInteger PAGE_SIZE = BigInteger.valueOf(100); + + public static final String TYPE_AUTHOR = "unknown"; + + /** + * Private constructor. + */ + private AtomPubUtils() { + } + + /** + * Compiles the base URL for links, collections and templates. + */ + public static UrlBuilder compileBaseUrl(HttpServletRequest request, String repositoryId) { + UrlBuilder url = new UrlBuilder(request.getScheme(), request.getServerName(), request.getServerPort(), null); + + url.addPath(request.getContextPath()); + url.addPath(request.getServletPath()); + + if (repositoryId != null) { + url.addPath(repositoryId); + } + + return url; + } + + /** + * Compiles a URL for links, collections and templates. + */ + public static String compileUrl(UrlBuilder baseUrl, String resource, String id) { + return compileUrlBuilder(baseUrl, resource, id).toString(); + } + + /** + * Compiles a URL for links, collections and templates. + */ + public static UrlBuilder compileUrlBuilder(UrlBuilder baseUrl, String resource, String id) { + UrlBuilder url = new UrlBuilder(baseUrl); + url.addPath(resource); + + if (id != null) { + url.addParameter("id", id); + } + + return url; + } + + // ------------------------------------------------------------------------- + // --- parameters --- + // ------------------------------------------------------------------------- + + /** + * Extracts a string parameter. + */ + @SuppressWarnings("unchecked") + public static String getStringParameter(HttpServletRequest request, String name) { + if (name == null) { + return null; + } + + Map parameters = (Map) request.getParameterMap(); + for (Map.Entry parameter : parameters.entrySet()) { + if (name.equalsIgnoreCase(parameter.getKey())) { + if (parameter.getValue() == null) { + return null; + } + return parameter.getValue()[0]; + } + } + + return null; + } + + /** + * Extracts a boolean parameter (with default). + */ + public static boolean getBooleanParameter(HttpServletRequest request, String name, boolean def) { + String value = getStringParameter(request, name); + if (value == null) { + return def; + } + + return Boolean.valueOf(value); + } + + /** + * Extracts a boolean parameter. + */ + public static Boolean getBooleanParameter(HttpServletRequest request, String name) { + String value = getStringParameter(request, name); + if (value == null) { + return null; + } + + return Boolean.valueOf(value); + } + + /** + * Extracts an integer parameter (with default). + */ + public static BigInteger getBigIntegerParameter(HttpServletRequest request, String name, long def) { + BigInteger result = getBigIntegerParameter(request, name); + if (result == null) { + result = BigInteger.valueOf(def); + } + + return result; + } + + /** + * Extracts an integer parameter. + */ + public static BigInteger getBigIntegerParameter(HttpServletRequest request, String name) { + String value = getStringParameter(request, name); + if (value == null) { + return null; + } + + try { + return new BigInteger(value); + } catch (Exception e) { + throw new CmisInvalidArgumentException("Invalid parameter '" + name + "'!"); + } + } + + /** + * Extracts an enum parameter. + */ + @SuppressWarnings("unchecked") + public static T getEnumParameter(HttpServletRequest request, String name, Class clazz) { + String value = getStringParameter(request, name); + if (value == null) { + return null; + } + + try { + Method m = clazz.getMethod("fromValue", new Class[] { String.class }); + return (T) m.invoke(null, new Object[] { value }); + } catch (Exception e) { + if (e instanceof IllegalArgumentException) { + throw new CmisInvalidArgumentException("Invalid parameter '" + name + "'!"); + } + + throw new CmisRuntimeException(e.getMessage(), e); + } + } + + /** + * Extracts a property from an object. + */ + @SuppressWarnings("unchecked") + public static T getProperty(ObjectData object, String name, Class clazz) { + if (object == null) { + return null; + } + + Properties propData = object.getProperties(); + if (propData == null) { + return null; + } + + Map> properties = propData.getProperties(); + if (properties == null) { + return null; + } + + PropertyData property = properties.get(name); + if (property == null) { + return null; + } + + Object value = property.getFirstValue(); + if (!clazz.isInstance(value)) { + return null; + } + + return (T) value; + } + + // ------------------------------------------------------------------------- + // --- entry builder --- + // ------------------------------------------------------------------------- + + /** + * Writes the a object entry. + */ + public static void writeObjectEntry(AtomEntry entry, ObjectData object, ObjectInfoHolder infoHolder, + List children, String repositoryId, String pathSegment, + String relativePathSegment, UrlBuilder baseUrl, boolean isRoot) throws XMLStreamException, JAXBException { + if ((object == null) || (infoHolder == null)) { + throw new CmisRuntimeException("Object or Object Info not set!"); + } + + ObjectInfo info = infoHolder.getObjectInfo(object.getId()); + if (info == null) { + throw new CmisRuntimeException("Object Info not found!"); + } + + // start + entry.startEntry(isRoot); + + // write object + String contentSrc = null; + + if (info.hasContent()) { + UrlBuilder contentSrcBuilder = compileUrlBuilder(baseUrl, RESOURCE_CONTENT, info.getId()); + if (info.getFileName() != null) { + contentSrcBuilder.addPath(info.getFileName()); + } + + contentSrc = contentSrcBuilder.toString(); + } + + entry.writeObject(object, info, contentSrc, info.getContentType(), pathSegment, relativePathSegment); + + // write links + entry.writeServiceLink(baseUrl.toString(), repositoryId); + + entry.writeSelfLink(compileUrl(baseUrl, RESOURCE_ENTRY, info.getId()), info.getId()); + entry.writeEnclosureLink(compileUrl(baseUrl, RESOURCE_ENTRY, info.getId())); + entry.writeEditLink(compileUrl(baseUrl, RESOURCE_ENTRY, info.getId())); + entry.writeDescribedByLink(compileUrl(baseUrl, RESOURCE_TYPE, info.getTypeId())); + entry.writeAllowableActionsLink(compileUrl(baseUrl, RESOURCE_ALLOWABLEACIONS, info.getId())); + + if (info.hasParent()) { + entry.writeUpLink(compileUrl(baseUrl, RESOURCE_PARENTS, info.getId()), Constants.MEDIATYPE_FEED); + } + + if (info.getBaseType() == BaseTypeId.CMIS_FOLDER) { + entry.writeDownLink(compileUrl(baseUrl, RESOURCE_CHILDREN, info.getId()), Constants.MEDIATYPE_FEED); + + if (info.supportsDescendants()) { + entry.writeDownLink(compileUrl(baseUrl, RESOURCE_DESCENDANTS, info.getId()), + Constants.MEDIATYPE_DESCENDANTS); + } + + if (info.supportsFolderTree()) { + entry.writeFolderTreeLink(compileUrl(baseUrl, RESOURCE_FOLDERTREE, info.getId())); + } + } + + if (info.getVersionSeriesId() != null) { + entry.writeVersionHistoryLink(compileUrl(baseUrl, RESOURCE_VERSIONS, info.getVersionSeriesId())); + } + + if (!info.isCurrentVersion()) { + UrlBuilder cvUrl = compileUrlBuilder(baseUrl, RESOURCE_ENTRY, info.getId()); + cvUrl.addParameter(Constants.PARAM_RETURN_VERSION, ReturnVersion.LATEST); + entry.writeEditLink(cvUrl.toString()); + } + + if (contentSrc != null) { + entry.writeEditMediaLink(contentSrc, info.getContentType()); + } + + if (info.getWorkingCopyId() != null) { + entry.writeWorkingCopyLink(compileUrl(baseUrl, RESOURCE_ENTRY, info.getWorkingCopyId())); + } + + if (info.getWorkingCopyOriginalId() != null) { + entry.writeViaLink(compileUrl(baseUrl, RESOURCE_ENTRY, info.getWorkingCopyOriginalId())); + } + + if (info.getRenditionInfos() != null) { + for (RenditionInfo ri : info.getRenditionInfos()) { + entry.writeAlternateLink(compileUrl(baseUrl, RESOURCE_CONTENT, ri.getId()), ri.getContenType(), ri + .getKind(), ri.getTitle(), ri.getLength()); + } + } + + if (info.hasAcl()) { + entry.writeAclLink(compileUrl(baseUrl, RESOURCE_ACL, info.getId())); + } + + if (info.supportsPolicies()) { + entry.writeAclLink(compileUrl(baseUrl, RESOURCE_POLICIES, info.getId())); + } + + if (info.supportsRelationships()) { + entry.writeRelationshipsLink(compileUrl(baseUrl, RESOURCE_RELATIONSHIPS, info.getId())); + } + + if (info.getRelationshipSourceIds() != null) { + for (String id : info.getRelationshipSourceIds()) { + entry.writeRelationshipSourceLink(compileUrl(baseUrl, RESOURCE_ENTRY, id)); + } + } + + if (info.getRelationshipTargetIds() != null) { + for (String id : info.getRelationshipTargetIds()) { + entry.writeRelationshipTargetLink(compileUrl(baseUrl, RESOURCE_ENTRY, id)); + } + } + + // write children + if ((children != null) && (children.size() > 0)) { + writeObjectChildren(entry, info, children, infoHolder, repositoryId, baseUrl); + } + + // we are done + entry.endEntry(); + } + + /** + * Writes an objects entry children feed. + */ + public static void writeObjectChildren(AtomEntry entry, ObjectInfo folderInfo, + List children, ObjectInfoHolder infoHolder, String repositoryId, UrlBuilder baseUrl) + throws XMLStreamException, JAXBException { + + // start + AtomFeed feed = new AtomFeed(entry.getWriter()); + feed.startChildren(); + feed.startFeed(false); + + // write basic Atom feed elements + feed.writeFeedElements(folderInfo.getId(), folderInfo.getCreatedBy(), folderInfo.getName(), folderInfo + .getLastModificationDate(), null, null); + + // write links + feed.writeServiceLink(baseUrl.toString(), repositoryId); + + feed.writeSelfLink(compileUrl(baseUrl, RESOURCE_DESCENDANTS, folderInfo.getId()), null); + + feed.writeViaLink(compileUrl(baseUrl, RESOURCE_ENTRY, folderInfo.getId())); + + feed.writeDownLink(compileUrl(baseUrl, RESOURCE_CHILDREN, folderInfo.getId()), Constants.MEDIATYPE_FEED); + + feed.writeDownLink(compileUrl(baseUrl, RESOURCE_FOLDERTREE, folderInfo.getId()), + Constants.MEDIATYPE_DESCENDANTS); + + feed.writeUpLink(compileUrl(baseUrl, RESOURCE_PARENTS, folderInfo.getId()), Constants.MEDIATYPE_FEED); + + for (ObjectInFolderContainer container : children) { + if ((container != null) && (container.getObject() != null)) { + writeObjectEntry(entry, container.getObject().getObject(), infoHolder, container.getChildren(), + repositoryId, container.getObject().getPathSegment(), null, baseUrl, false); + } + } + + // we are done + feed.endFeed(); + feed.endChildren(); + } + + /** + * Writes the a type entry. + */ + public static void writeTypeEntry(AtomEntry entry, TypeDefinition type, List children, + String repositoryId, UrlBuilder baseUrl, boolean isRoot) throws XMLStreamException, JAXBException { + + // start + entry.startEntry(isRoot); + + // write type + entry.writeType(type); + + // write links + entry.writeServiceLink(baseUrl.toString(), repositoryId); + + entry.writeSelfLink(compileUrl(baseUrl, RESOURCE_TYPE, type.getId()), type.getId()); + entry.writeEnclosureLink(compileUrl(baseUrl, RESOURCE_TYPE, type.getId())); + if (type.getParentTypeId() != null) { + entry.writeUpLink(compileUrl(baseUrl, RESOURCE_TYPE, type.getParentTypeId()), Constants.MEDIATYPE_ENTRY); + } + UrlBuilder downLink = compileUrlBuilder(baseUrl, RESOURCE_TYPES, null); + downLink.addParameter(Constants.PARAM_TYPE_ID, type.getId()); + entry.writeDownLink(downLink.toString(), Constants.MEDIATYPE_CHILDREN); + entry.writeDescribedByLink(compileUrl(baseUrl, RESOURCE_TYPE, type.getBaseTypeId().value())); + + // write children + if ((children != null) && (children.size() > 0)) { + writeTypeChildren(entry, type, children, repositoryId, baseUrl); + } + + // we are done + entry.endEntry(); + } + + /** + * Writes the a type entry children feed. + */ + private static void writeTypeChildren(AtomEntry entry, TypeDefinition type, List children, + String repositoryId, UrlBuilder baseUrl) throws XMLStreamException, JAXBException { + + // start + AtomFeed feed = new AtomFeed(entry.getWriter()); + feed.startChildren(); + feed.startFeed(false); + + // write basic Atom feed elements + feed.writeFeedElements(type.getId(), TYPE_AUTHOR, type.getDisplayName(), new GregorianCalendar(), null, null); + + feed.writeServiceLink(baseUrl.toString(), repositoryId); + + UrlBuilder selfLink = compileUrlBuilder(baseUrl, RESOURCE_TYPESDESC, null); + selfLink.addParameter(Constants.PARAM_TYPE_ID, type.getId()); + feed.writeSelfLink(selfLink.toString(), type.getId()); + + feed.writeViaLink(compileUrl(baseUrl, RESOURCE_TYPE, type.getId())); + + UrlBuilder downLink = compileUrlBuilder(baseUrl, RESOURCE_TYPES, null); + downLink.addParameter(Constants.PARAM_TYPE_ID, type.getId()); + feed.writeDownLink(downLink.toString(), Constants.MEDIATYPE_FEED); + + if (type.getParentTypeId() != null) { + feed.writeUpLink(compileUrl(baseUrl, RESOURCE_TYPE, type.getParentTypeId()), Constants.MEDIATYPE_ENTRY); + } + + // write tree + for (TypeDefinitionContainer container : children) { + if ((container != null) && (container.getTypeDefinition() != null)) { + writeTypeEntry(entry, container.getTypeDefinition(), container.getChildren(), repositoryId, baseUrl, + false); + } + } + + // we are done + feed.endFeed(); + feed.endChildren(); + } } Modified: incubator/chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/impl/atompub/BasicAuthCallContextHandler.java URL: http://svn.apache.org/viewvc/incubator/chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/impl/atompub/BasicAuthCallContextHandler.java?rev=936938&r1=936937&r2=936938&view=diff ============================================================================== --- incubator/chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/impl/atompub/BasicAuthCallContextHandler.java (original) +++ incubator/chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/impl/atompub/BasicAuthCallContextHandler.java Thu Apr 22 16:27:57 2010 @@ -34,47 +34,47 @@ import org.apache.commons.codec.binary.B */ public class BasicAuthCallContextHandler implements CallContextHandler { - /** - * Constructor. - */ - public BasicAuthCallContextHandler() { - } - - /* - * (non-Javadoc) - * - * @seeorg.apache.opencmis.server.impl.atompub.CallContextHandler# - * getCallContextMap(javax.servlet.http. HttpServletRequest) - */ - public Map getCallContextMap(HttpServletRequest request) { - Map result = null; - - String authHeader = request.getHeader("Authorization"); - if ((authHeader != null) && (authHeader.trim().toLowerCase().startsWith("basic "))) { - int x = authHeader.lastIndexOf(' '); - if (x == -1) { - return result; - } - - String credentials = null; - try { - credentials = new String(Base64.decodeBase64(authHeader.substring(x + 1).getBytes("ISO-8859-1")), - "ISO-8859-1"); - } catch (Exception e) { - return result; - } - - x = credentials.indexOf(':'); - if (x == -1) { - return result; - } - - // extract user and password and add them to map - result = new HashMap(); - result.put(CallContext.USERNAME, credentials.substring(0, x)); - result.put(CallContext.PASSWORD, credentials.substring(x + 1)); - } + /** + * Constructor. + */ + public BasicAuthCallContextHandler() { + } + + /* + * (non-Javadoc) + * + * @seeorg.apache.opencmis.server.impl.atompub.CallContextHandler# + * getCallContextMap(javax.servlet.http. HttpServletRequest) + */ + public Map getCallContextMap(HttpServletRequest request) { + Map result = null; + + String authHeader = request.getHeader("Authorization"); + if ((authHeader != null) && (authHeader.trim().toLowerCase().startsWith("basic "))) { + int x = authHeader.lastIndexOf(' '); + if (x == -1) { + return result; + } + + String credentials = null; + try { + credentials = new String(Base64.decodeBase64(authHeader.substring(x + 1).getBytes("ISO-8859-1")), + "ISO-8859-1"); + } catch (Exception e) { + return result; + } + + x = credentials.indexOf(':'); + if (x == -1) { + return result; + } + + // extract user and password and add them to map + result = new HashMap(); + result.put(CallContext.USERNAME, credentials.substring(0, x)); + result.put(CallContext.PASSWORD, credentials.substring(x + 1)); + } - return result; - } + return result; + } } Modified: incubator/chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/impl/atompub/CallContextHandler.java URL: http://svn.apache.org/viewvc/incubator/chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/impl/atompub/CallContextHandler.java?rev=936938&r1=936937&r2=936938&view=diff ============================================================================== --- incubator/chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/impl/atompub/CallContextHandler.java (original) +++ incubator/chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/impl/atompub/CallContextHandler.java Thu Apr 22 16:27:57 2010 @@ -29,8 +29,8 @@ import org.apache.chemistry.opencmis.com */ public interface CallContextHandler { - /** - * Returns key-value pairs that will be added to the {@link CallContext}. - */ - Map getCallContextMap(HttpServletRequest request); + /** + * Returns key-value pairs that will be added to the {@link CallContext}. + */ + Map getCallContextMap(HttpServletRequest request); }