You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ni...@apache.org on 2010/01/05 12:31:19 UTC
svn commit: r895993 - in /camel/trunk/components/camel-jaxb/src:
main/java/org/apache/camel/converter/jaxb/ test/java/org/apache/camel/jaxb/
Author: ningjiang
Date: Tue Jan 5 11:31:17 2010
New Revision: 895993
URL: http://svn.apache.org/viewvc?rev=895993&view=rev
Log:
CAMEL-2330 filtered the invalide charater for camel-jaxb unmarshal
Added:
camel/trunk/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/JaxbFilterReader.java (with props)
Modified:
camel/trunk/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/FallbackTypeConverter.java
camel/trunk/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/JaxbConverter.java
camel/trunk/components/camel-jaxb/src/test/java/org/apache/camel/jaxb/CamelJaxbTest.java
Modified: camel/trunk/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/FallbackTypeConverter.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/FallbackTypeConverter.java?rev=895993&r1=895992&r2=895993&view=diff
==============================================================================
--- camel/trunk/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/FallbackTypeConverter.java (original)
+++ camel/trunk/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/FallbackTypeConverter.java Tue Jan 5 11:31:17 2010
@@ -116,16 +116,17 @@
Unmarshaller unmarshaller = context.createUnmarshaller();
if (parentTypeConverter != null) {
- InputStream inputStream = parentTypeConverter.convertTo(InputStream.class, value);
- if (inputStream != null) {
- Object unmarshalled = unmarshal(unmarshaller, inputStream);
- return type.cast(unmarshalled);
- }
+ // Prefer to use the Reader which can skip the control characters
Reader reader = parentTypeConverter.convertTo(Reader.class, value);
if (reader != null) {
Object unmarshalled = unmarshal(unmarshaller, reader);
return type.cast(unmarshalled);
}
+ InputStream inputStream = parentTypeConverter.convertTo(InputStream.class, value);
+ if (inputStream != null) {
+ Object unmarshalled = unmarshal(unmarshaller, inputStream);
+ return type.cast(unmarshalled);
+ }
Source source = parentTypeConverter.convertTo(Source.class, value);
if (source != null) {
Object unmarshalled = unmarshal(unmarshaller, source);
@@ -180,7 +181,13 @@
if (value instanceof InputStream) {
return unmarshaller.unmarshal((InputStream) value);
} else if (value instanceof Reader) {
- return unmarshaller.unmarshal((Reader) value);
+ JaxbFilterReader filterReader;
+ if (value instanceof JaxbFilterReader) {
+ filterReader = (JaxbFilterReader) value;
+ } else {
+ filterReader = new JaxbFilterReader((Reader)value);
+ }
+ return unmarshaller.unmarshal(filterReader);
} else if (value instanceof Source) {
return unmarshaller.unmarshal((Source) value);
}
Modified: camel/trunk/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/JaxbConverter.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/JaxbConverter.java?rev=895993&r1=895992&r2=895993&view=diff
==============================================================================
--- camel/trunk/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/JaxbConverter.java (original)
+++ camel/trunk/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/JaxbConverter.java Tue Jan 5 11:31:17 2010
@@ -41,7 +41,7 @@
private XmlConverter xmlConverter = new XmlConverter();
private Map<Class<?>, JAXBContext> contexts = new HashMap<Class<?>, JAXBContext>();
- @Converter
+ //@Converter
public JAXBSource toSource(Object value) throws JAXBException {
if (value == null) {
throw new IllegalArgumentException("Cannot convert from null value to JAXBSource");
@@ -55,7 +55,7 @@
}
}
- @Converter
+ //@Converter
public Document toDocument(Object value) throws JAXBException, ParserConfigurationException {
if (value == null) {
throw new IllegalArgumentException("Cannot convert from null value to JAXBSource");
Added: camel/trunk/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/JaxbFilterReader.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/JaxbFilterReader.java?rev=895993&view=auto
==============================================================================
--- camel/trunk/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/JaxbFilterReader.java (added)
+++ camel/trunk/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/JaxbFilterReader.java Tue Jan 5 11:31:17 2010
@@ -0,0 +1,81 @@
+/**
+ * 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.camel.converter.jaxb;
+
+import java.io.FilterReader;
+import java.io.IOException;
+import java.io.Reader;
+
+/**
+ * This FilterReader will skip the ISO control character and others
+ *
+ */
+public class JaxbFilterReader extends FilterReader {
+
+ protected JaxbFilterReader(Reader in) {
+ super(in);
+ }
+
+ /**
+ * Reads a single character.
+ *
+ * @exception IOException If an I/O error occurs
+ */
+ public int read() throws IOException {
+ char ch = (char) in.read();
+ while (isFiltered(ch)) {
+ // Skip the character that need to be filtered.
+ ch = (char) in.read();
+ }
+ return ch;
+ }
+
+ /**
+ * Reads characters into a portion of an array.
+ *
+ * @exception IOException If an I/O error occurs
+ */
+ public int read(char cbuf[], int off, int len) throws IOException {
+ char buffer[] = new char[len];
+ int readed = in.read(buffer, 0, len - off);
+ if (readed >= 0) {
+ int copyed = 0;
+ for (int i = 0; i < readed; i++) {
+ if (!isFiltered(buffer[i])) {
+ cbuf[off + copyed] = buffer[i];
+ copyed++;
+ } else {
+ // Skip the character that need to be filtered.
+ }
+ }
+ return copyed;
+ } else {
+ return readed;
+ }
+ }
+
+ // According to http://www.w3.org/TR/2004/REC-xml-20040204/#NT-Char,
+ // we filter these Chars
+ protected boolean isFiltered(char ch) {
+ return Character.isISOControl(ch) || ((int)ch >= 0xFDD0 && (int)ch <= 0xFDDF);
+ }
+
+
+
+
+
+}
Propchange: camel/trunk/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/JaxbFilterReader.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: camel/trunk/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/JaxbFilterReader.java
------------------------------------------------------------------------------
svn:keywords = Rev Date
Modified: camel/trunk/components/camel-jaxb/src/test/java/org/apache/camel/jaxb/CamelJaxbTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-jaxb/src/test/java/org/apache/camel/jaxb/CamelJaxbTest.java?rev=895993&r1=895992&r2=895993&view=diff
==============================================================================
--- camel/trunk/components/camel-jaxb/src/test/java/org/apache/camel/jaxb/CamelJaxbTest.java (original)
+++ camel/trunk/components/camel-jaxb/src/test/java/org/apache/camel/jaxb/CamelJaxbTest.java Tue Jan 5 11:31:17 2010
@@ -16,14 +16,19 @@
*/
package org.apache.camel.jaxb;
+import java.io.ByteArrayInputStream;
+import java.io.InputStream;
+
import javax.xml.bind.JAXBElement;
import org.apache.camel.ContextTestSupport;
+import org.apache.camel.Exchange;
import org.apache.camel.TypeConverter;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.converter.jaxb.JaxbDataFormat;
import org.apache.camel.foo.bar.PersonType;
+import org.apache.camel.impl.DefaultExchange;
public class CamelJaxbTest extends ContextTestSupport {
@@ -36,6 +41,18 @@
assertEquals("Get the wrong first name ", person.getFirstName(), "FOO");
assertEquals("Get the wrong second name ", person.getLastName(), "BAR");
}
+
+ public void testFilteringUnmarshal() throws Exception {
+ final byte[] buffers = "<Person><firstName>FOO</firstName><lastName>BAR\u0008</lastName></Person>".getBytes("UTF-8");
+ InputStream is = new ByteArrayInputStream(buffers);
+ Exchange exchange = new DefaultExchange(context);
+ exchange.setProperty(Exchange.CHARSET_NAME, "UTF-8");
+ TypeConverter converter = context.getTypeConverter();
+ PersonType person = converter.convertTo(PersonType.class, exchange, is);
+ assertNotNull("Person should not be null ", person);
+ assertEquals("Get the wrong first name ", person.getFirstName(), "FOO");
+ assertEquals("Get the wrong second name ", person.getLastName(), "BAR");
+ }
public void testUnmarshal() throws Exception {
final String xml = "<Person><firstName>FOO</firstName><lastName>BAR</lastName></Person>";