[CXF-6892] Remove XmlBeans provider
Project: http://git-wip-us.apache.org/repos/asf/cxf/repo Commit: http://git-wip-us.apache.org/repos/asf/cxf/commit/30a0754e Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/30a0754e Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/30a0754e Branch: refs/heads/master-jaxrs-2.1 Commit: 30a0754ebe160d75867bc2558612d555b88c8595 Parents: 3ed1543 Author: Sergey Beryozkin <[email protected]> Authored: Wed May 4 10:49:24 2016 +0100 Committer: Sergey Beryozkin <[email protected]> Committed: Wed May 4 10:49:24 2016 +0100 ---------------------------------------------------------------------- rt/rs/extensions/providers/pom.xml | 6 - .../xmlbeans/XMLBeanStreamSerializer.java | 130 ---------- .../xmlbeans/XMLBeansElementProvider.java | 238 ------------------- .../provider/xmlbeans/XMLBeansJSONProvider.java | 108 --------- 4 files changed, 482 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cxf/blob/30a0754e/rt/rs/extensions/providers/pom.xml ---------------------------------------------------------------------- diff --git a/rt/rs/extensions/providers/pom.xml b/rt/rs/extensions/providers/pom.xml index 3237dc6..b379e7c 100644 --- a/rt/rs/extensions/providers/pom.xml +++ b/rt/rs/extensions/providers/pom.xml @@ -138,12 +138,6 @@ <scope>test</scope> </dependency> <dependency> - <groupId>org.apache.xmlbeans</groupId> - <artifactId>xmlbeans</artifactId> - <scope>provided</scope> - <optional>true</optional> - </dependency> - <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-databinding-xmlbeans</artifactId> <version>${project.version}</version> http://git-wip-us.apache.org/repos/asf/cxf/blob/30a0754e/rt/rs/extensions/providers/src/main/java/org/apache/cxf/jaxrs/provider/xmlbeans/XMLBeanStreamSerializer.java ---------------------------------------------------------------------- diff --git a/rt/rs/extensions/providers/src/main/java/org/apache/cxf/jaxrs/provider/xmlbeans/XMLBeanStreamSerializer.java b/rt/rs/extensions/providers/src/main/java/org/apache/cxf/jaxrs/provider/xmlbeans/XMLBeanStreamSerializer.java deleted file mode 100644 index 9d49ea5..0000000 --- a/rt/rs/extensions/providers/src/main/java/org/apache/cxf/jaxrs/provider/xmlbeans/XMLBeanStreamSerializer.java +++ /dev/null @@ -1,130 +0,0 @@ -/** - * 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.cxf.jaxrs.provider.xmlbeans; - -import java.io.File; -import java.io.FileInputStream; -import java.io.IOException; -import java.io.InputStream; - -import javax.xml.stream.XMLStreamConstants; -import javax.xml.stream.XMLStreamException; -import javax.xml.stream.XMLStreamReader; -import javax.xml.stream.XMLStreamWriter; - -import org.apache.cxf.staxutils.StaxUtils; -import org.apache.xmlbeans.XmlObject; - -/** - * Serializes an XMLBean data object to an XML stream Note: uses an intermediate file created by - * File.createTempFile(String, String) as I couldn't work out how to fit a normal stream into an event driven - * XML stream. - */ -public class XMLBeanStreamSerializer { - - /** - * Serialize the given XML data object. Writes the data object to a temporary file then reads it back in - * with an <code>XMLStreamReader<code>. - * This allows the events from the reader to drive the output to the <code>XMLStreamWriter</code>. - * Probably not the best way to do this. - * - * @param obj - * @param writer - */ - public void serialize(XmlObject xObj, XMLStreamWriter writer) throws IOException, XMLStreamException { - - File tmpFile = null; - XMLStreamReader rdr = null; - try { - - // create tmp file - tmpFile = File.createTempFile(Integer.toString(xObj.hashCode()), ".xml"); - // TODO may need to set some XMLOptions here - // write to tmp file - xObj.save(tmpFile); - - InputStream tmpIn = new FileInputStream(tmpFile); - rdr = StaxUtils.createXMLStreamReader(tmpIn); - - while (rdr.hasNext()) { - - int event = rdr.next(); - - switch (event) { - - case XMLStreamConstants.START_DOCUMENT: - writer.writeStartDocument(); - break; - - case XMLStreamConstants.END_DOCUMENT: - writer.writeEndDocument(); - break; - - case XMLStreamConstants.START_ELEMENT: - String name = rdr.getLocalName(); - writer.writeStartElement(name); - - // handle attributes - int attrCount = rdr.getAttributeCount(); - for (int i = 0; i < attrCount; i++) { - String attrName = rdr.getAttributeLocalName(i); - String attrNS = rdr.getAttributeNamespace(i); - String attrVal = rdr.getAttributeValue(i); - if (attrNS == null) { - - writer.writeAttribute(attrName, attrVal); - - } else { - - writer.writeAttribute(attrNS, attrName, attrVal); - } - - } - break; - - case XMLStreamConstants.END_ELEMENT: - writer.writeEndElement(); - break; - - case XMLStreamConstants.ATTRIBUTE: - // do nothing - break; - - case XMLStreamConstants.CHARACTERS: - String txt = rdr.getText(); - writer.writeCharacters(txt); - break; - - default: - // ignore - break; - } - } - - } finally { - - if (tmpFile != null && tmpFile.exists() && tmpFile.canWrite()) { - - tmpFile.delete(); - } - StaxUtils.close(rdr); - } - } -} http://git-wip-us.apache.org/repos/asf/cxf/blob/30a0754e/rt/rs/extensions/providers/src/main/java/org/apache/cxf/jaxrs/provider/xmlbeans/XMLBeansElementProvider.java ---------------------------------------------------------------------- diff --git a/rt/rs/extensions/providers/src/main/java/org/apache/cxf/jaxrs/provider/xmlbeans/XMLBeansElementProvider.java b/rt/rs/extensions/providers/src/main/java/org/apache/cxf/jaxrs/provider/xmlbeans/XMLBeansElementProvider.java deleted file mode 100644 index d49b6e1..0000000 --- a/rt/rs/extensions/providers/src/main/java/org/apache/cxf/jaxrs/provider/xmlbeans/XMLBeansElementProvider.java +++ /dev/null @@ -1,238 +0,0 @@ -/** - * 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.cxf.jaxrs.provider.xmlbeans; - -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.io.OutputStream; -import java.io.Reader; -import java.lang.annotation.Annotation; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; -import java.lang.reflect.Type; - -import javax.ws.rs.Consumes; -import javax.ws.rs.Produces; -import javax.ws.rs.core.MediaType; -import javax.ws.rs.core.MultivaluedMap; -import javax.ws.rs.ext.MessageBodyReader; -import javax.ws.rs.ext.MessageBodyWriter; -import javax.ws.rs.ext.Provider; -import javax.xml.stream.XMLStreamReader; - -import org.apache.cxf.jaxrs.provider.AbstractConfigurableProvider; -import org.apache.cxf.jaxrs.utils.ExceptionUtils; -import org.apache.xmlbeans.XmlObject; - -/** - * Provider for XMLBeans data objects. - */ -@Produces({"application/xml", "application/*+xml", "text/xml" }) -@Consumes({"application/xml", "application/*+xml", "text/xml" }) -@Provider -public class XMLBeansElementProvider extends AbstractConfigurableProvider - implements MessageBodyReader<XmlObject>, MessageBodyWriter<XmlObject> { - - /** {@inheritDoc} */ - public XmlObject readFrom(Class<XmlObject> type, Type genericType, - Annotation[] annotations, MediaType m, - MultivaluedMap<String, String> headers, InputStream is) - throws IOException { - - return parseXmlBean(type, is); - } - - /** {@inheritDoc} */ - public void writeTo(XmlObject t, Class<?> cls, Type genericType, Annotation[] annotations, - MediaType m, MultivaluedMap<String, Object> headers, OutputStream entityStream) - throws IOException { - - // May need to set some XMLOptions here - t.save(entityStream); - } - - /** {@inheritDoc} */ - public long getSize(XmlObject t, Class<?> type, Type genericType, Annotation[] annotations, - MediaType mt) { - // return length not known - return -1; - } - - /** {@inheritDoc} */ - public boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mt) { - return isXmlBean(type); - } - - /** {@inheritDoc} */ - public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mt) { - return isXmlBean(type); - } - - /** - * Create an XMLBean object from an XML stream. - * - * @param type declared type of the target object - * @param reader stream reader for the XML stream - * @return an XMLBean data object, or none if unable to process - */ - protected XmlObject parseXmlBean(Class<?> type, XMLStreamReader reader) { - - XmlObject result = null; - - // get XMLBeans inner class Factory - Class<?> factory = getFactory(type); - - try { - - // find and invoke method parse(InputStream) - Method m = factory.getMethod("parse", reader.getClass()); - Object[] args = { - reader - }; - Object obj = m.invoke(type, args); - - if (obj instanceof XmlObject) { - result = (XmlObject)obj; - } - - } catch (NoSuchMethodException nsme) { - throw ExceptionUtils.toInternalServerErrorException(nsme, null); - } catch (InvocationTargetException ite) { - throw ExceptionUtils.toInternalServerErrorException(ite, null); - } catch (IllegalAccessException iae) { - throw ExceptionUtils.toInternalServerErrorException(iae, null); - } - - return result; - } - - /** - * Create an XMLBean data object from an <code>InputStream</code> - * - * @param type declared type of the required object - * @param inStream - * @return an XMLBean object if successful, otherwise null - */ - protected XmlObject parseXmlBean(Class<?> type, InputStream inStream) { - XmlObject result = null; - - Reader r = new InputStreamReader(inStream); - - // delegate to parseXmlBean(Class type, Reader reader) - result = parseXmlBean(type, r); - - return result; - } - - /** - * Create an XMLBean data object using a stream <code>Reader</code> - * - * @param type declared type of the desired XMLBean data object - * @param reader - * @return an instance of the required object, otherwise null - */ - protected XmlObject parseXmlBean(Class<?> type, Reader reader) { - XmlObject result = null; - - Class<?> factory = getFactory(type); - - try { - - // get factory method parse(InputStream) - Method m = factory.getMethod("parse", Reader.class); - Object[] args = {reader}; - Object obj = m.invoke(type, args); - - if (obj instanceof XmlObject) { - result = (XmlObject)obj; - } - - } catch (NoSuchMethodException nsme) { - // do nothing, just return null - } catch (InvocationTargetException ite) { - // do nothing, just return null - } catch (IllegalAccessException iae) { - // do nothing, just return null - } - - return result; - } - - /** - * Locate the XMLBean <code>Factory</code> inner class. - * - * @param type - * @return the Factory class if present, otherwise null. - */ - private Class<?> getFactory(Class<?> type) { - Class<?> result = null; - - Class<?>[] interfaces = type.getInterfaces(); - - // look for XMLBeans inner class Factory - for (Class<?> inter : interfaces) { - - Class<?>[] declared = inter.getDeclaredClasses(); - - for (Class<?> c : declared) { - - if (c.getSimpleName().equals("Factory")) { - result = c; - } - } - } - - return result; - } - - /** - * Check if a <code>Class</code> is a valid XMLBeans data object. The check procedure involves looking - * for the Interface <code>XmlObject</code> in the target type's declaration. Assumed to be sufficient - * to identify the type as an XMLBean. From the javadoc (2.3.0) for XmlObject: "Corresponds to the XML - * Schema xs:anyType, the base type for all XML Beans." - * - * @param type - * @return true if found to be an XMLBean object, otherwise false - */ - protected boolean isXmlBean(Class<?> type) { - boolean result = false; - - Class<?>[] interfaces = {type}; - - if (!type.isInterface()) { - - interfaces = type.getInterfaces(); - } - - for (Class<?> i : interfaces) { - - Class<?>[] superInterfaces = i.getInterfaces(); - - for (Class<?> superI : superInterfaces) { - - if (superI.getName().equals("org.apache.xmlbeans.XmlObject")) { - result = true; - } - } - } - - return result; - } -} http://git-wip-us.apache.org/repos/asf/cxf/blob/30a0754e/rt/rs/extensions/providers/src/main/java/org/apache/cxf/jaxrs/provider/xmlbeans/XMLBeansJSONProvider.java ---------------------------------------------------------------------- diff --git a/rt/rs/extensions/providers/src/main/java/org/apache/cxf/jaxrs/provider/xmlbeans/XMLBeansJSONProvider.java b/rt/rs/extensions/providers/src/main/java/org/apache/cxf/jaxrs/provider/xmlbeans/XMLBeansJSONProvider.java deleted file mode 100644 index 55384c0..0000000 --- a/rt/rs/extensions/providers/src/main/java/org/apache/cxf/jaxrs/provider/xmlbeans/XMLBeansJSONProvider.java +++ /dev/null @@ -1,108 +0,0 @@ -/** - * 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.cxf.jaxrs.provider.xmlbeans; - -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.io.Reader; -import java.lang.annotation.Annotation; -import java.lang.reflect.Type; -import java.util.HashMap; -import java.util.Map; - -import javax.ws.rs.Consumes; -import javax.ws.rs.Produces; -import javax.ws.rs.core.MediaType; -import javax.ws.rs.core.MultivaluedMap; -import javax.ws.rs.ext.Provider; -import javax.xml.stream.XMLOutputFactory; -import javax.xml.stream.XMLStreamException; -import javax.xml.stream.XMLStreamReader; -import javax.xml.stream.XMLStreamWriter; - -import org.apache.cxf.jaxrs.utils.ExceptionUtils; -import org.apache.xmlbeans.XmlObject; -import org.codehaus.jettison.mapped.MappedXMLInputFactory; -import org.codehaus.jettison.mapped.MappedXMLOutputFactory; - -/** - * JSON provider for XMLBeans data objects. - */ -@Produces("application/json") -@Consumes("application/json") -@Provider -public class XMLBeansJSONProvider extends XMLBeansElementProvider { - - /** {@inheritDoc} */ - @Override - public XmlObject readFrom(Class<XmlObject> type, Type genericType, - Annotation[] annotations, MediaType m, - MultivaluedMap<String, String> headers, InputStream is) - throws IOException { - XmlObject result = null; - - try { - - Map<String, String> nstojns = new HashMap<String, String>(); - - MappedXMLInputFactory factory = new MappedXMLInputFactory(nstojns); - XMLStreamReader xsr = factory.createXMLStreamReader(is); - Reader r = (Reader)xsr; - result = parseXmlBean(type, r); - - xsr.close(); - - } catch (XMLStreamException e) { - throw ExceptionUtils.toBadRequestException(e, null); - } - - return result; - } - - /** {@inheritDoc} */ - @Override - public void writeTo(XmlObject obj, Class<?> cls, Type genericType, Annotation[] annotations, - MediaType m, MultivaluedMap<String, Object> headers, OutputStream os) { - - try { - - // Set up the JSON StAX implementation - Map<String, String> nstojns = new HashMap<String, String>(); - XMLOutputFactory factory = new MappedXMLOutputFactory(nstojns); - XMLStreamWriter xsw = factory.createXMLStreamWriter(os); - xsw.writeStartDocument(); - if (obj != null) { - - XmlObject xObj = obj; - XMLBeanStreamSerializer ser = new XMLBeanStreamSerializer(); - ser.serialize(xObj, xsw); - } - - xsw.flush(); - xsw.close(); - - } catch (XMLStreamException e) { - throw ExceptionUtils.toInternalServerErrorException(e, null); - } catch (IOException ioe) { - throw ExceptionUtils.toInternalServerErrorException(ioe, null); - } - } -}
