INT-1280: ship commons code out to external project
This commit is contained in:
@@ -1,45 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2010 the original author or authors.
|
||||
*
|
||||
* Licensed 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.springframework.commons.serializer;
|
||||
|
||||
/**
|
||||
* @author Gary Russell
|
||||
* @since 2.0
|
||||
*
|
||||
*/
|
||||
@SuppressWarnings("serial")
|
||||
public class DeserializationFailureException extends SerializationException {
|
||||
|
||||
/**
|
||||
* Construct a <code>DeserializationException</code> with the specified detail message.
|
||||
* @param msg the detail message
|
||||
*/
|
||||
public DeserializationFailureException(String msg) {
|
||||
super(msg);
|
||||
}
|
||||
|
||||
/**
|
||||
* Construct a <code>DeserializationFailureException</code> with the specified detail message
|
||||
* and nested exception.
|
||||
* @param msg the detail message
|
||||
* @param cause the nested exception
|
||||
*/
|
||||
public DeserializationFailureException(String msg, Throwable cause) {
|
||||
super(msg, cause);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,57 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2010 the original author or authors.
|
||||
*
|
||||
* Licensed 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.springframework.commons.serializer;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
import org.springframework.core.convert.converter.Converter;
|
||||
|
||||
/**
|
||||
* A {@link Converter} that delegates to a {@link InputStreamingConverter}
|
||||
* to convert data in a byte[] to an object.
|
||||
*
|
||||
* @author Gary Russell
|
||||
* @since 2.0
|
||||
*
|
||||
*/
|
||||
public class DeserializingConverter implements Converter<byte[], Object> {
|
||||
|
||||
private InputStreamingConverter<Object> streamingConverter;
|
||||
|
||||
/**
|
||||
* @param streamingConverter the InputStreamingConverter
|
||||
*/
|
||||
public DeserializingConverter(InputStreamingConverter<Object> streamingConverter) {
|
||||
this.streamingConverter = streamingConverter;
|
||||
}
|
||||
|
||||
public Object convert(byte[] source) {
|
||||
ByteArrayInputStream byteStream = new ByteArrayInputStream(source);
|
||||
try {
|
||||
return streamingConverter.convert(byteStream);
|
||||
}
|
||||
catch (Exception e) {
|
||||
try {
|
||||
byteStream.close();
|
||||
} catch (IOException e1) { }
|
||||
throw new DeserializationFailureException(
|
||||
"Failed to deserialize payload. Is the byte array a result of Object serialization?", e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,40 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2010 the original author or authors.
|
||||
*
|
||||
* Licensed 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.springframework.commons.serializer;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
/**
|
||||
* An standard interface to convert from data in an InputStream to
|
||||
* an Object.
|
||||
*
|
||||
* @author Gary Russell
|
||||
* @since 2.0
|
||||
*
|
||||
*/
|
||||
public interface InputStreamingConverter<T> {
|
||||
|
||||
/**
|
||||
* Read (assemble an object of type T) from an InputStream.
|
||||
* @param inputStream The InputStream.
|
||||
* @return The object.
|
||||
* @throws IOException
|
||||
*/
|
||||
public T convert(InputStream inputStream) throws IOException;
|
||||
|
||||
}
|
||||
@@ -1,39 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2010 the original author or authors.
|
||||
*
|
||||
* Licensed 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.springframework.commons.serializer;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
|
||||
/**
|
||||
* A standard interface to stream an object to an OutputStream.
|
||||
*
|
||||
* @author Gary Russell
|
||||
* @since 2.0
|
||||
*
|
||||
*/
|
||||
public interface OutputStreamingConverter<T> {
|
||||
|
||||
/**
|
||||
* Write an object of type T to the outputSream.
|
||||
* @param object The object.
|
||||
* @param outputStream The outputStream.
|
||||
* @throws IOException
|
||||
*/
|
||||
public void convert(T object, OutputStream outputStream) throws IOException;
|
||||
|
||||
}
|
||||
@@ -1,47 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2010 the original author or authors.
|
||||
*
|
||||
* Licensed 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.springframework.commons.serializer;
|
||||
|
||||
import org.springframework.core.NestedRuntimeException;
|
||||
|
||||
/**
|
||||
* @author Gary Russell
|
||||
* @since 2.0
|
||||
*
|
||||
*/
|
||||
@SuppressWarnings("serial")
|
||||
public abstract class SerializationException extends NestedRuntimeException {
|
||||
|
||||
/**
|
||||
* Construct a <code>SerializationException</code> with the specified detail message.
|
||||
* @param msg the detail message
|
||||
*/
|
||||
public SerializationException(String msg) {
|
||||
super(msg);
|
||||
}
|
||||
|
||||
/**
|
||||
* Construct a <code>SerializationException</code> with the specified detail message
|
||||
* and nested exception.
|
||||
* @param msg the detail message
|
||||
* @param cause the nested exception
|
||||
*/
|
||||
public SerializationException(String msg, Throwable cause) {
|
||||
super(msg, cause);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,45 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2010 the original author or authors.
|
||||
*
|
||||
* Licensed 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.springframework.commons.serializer;
|
||||
|
||||
/**
|
||||
* @author Gary Russell
|
||||
* @since 2.0
|
||||
*
|
||||
*/
|
||||
@SuppressWarnings("serial")
|
||||
public class SerializationFailureException extends SerializationException {
|
||||
|
||||
/**
|
||||
* Construct a <code>SerializationFailureException</code> with the specified detail message.
|
||||
* @param msg the detail message
|
||||
*/
|
||||
public SerializationFailureException(String msg) {
|
||||
super(msg);
|
||||
}
|
||||
|
||||
/**
|
||||
* Construct a <code>SerializationFailureException</code> with the specified detail message
|
||||
* and nested exception.
|
||||
* @param msg the detail message
|
||||
* @param cause the nested exception
|
||||
*/
|
||||
public SerializationFailureException(String msg, Throwable cause) {
|
||||
super(msg, cause);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,52 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2010 the original author or authors.
|
||||
*
|
||||
* Licensed 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.springframework.commons.serializer;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
|
||||
import org.springframework.core.convert.converter.Converter;
|
||||
|
||||
/**
|
||||
* A {@Link Converter} that delegates to a {@link OutputStreamingConverter}
|
||||
* to convert an object to a byte[].
|
||||
*
|
||||
* @author Gary Russell
|
||||
* @since 2.0
|
||||
*
|
||||
*/
|
||||
public class SerializingConverter implements Converter<Object, byte[]> {
|
||||
|
||||
private OutputStreamingConverter<Object> streamingConverter;
|
||||
|
||||
/**
|
||||
* @param streamingConverter the OutputStreamingConverter
|
||||
*/
|
||||
public SerializingConverter(OutputStreamingConverter<Object> streamingConverter) {
|
||||
this.streamingConverter = streamingConverter;
|
||||
}
|
||||
|
||||
public byte[] convert(Object source) {
|
||||
ByteArrayOutputStream byteStream = new ByteArrayOutputStream(128);
|
||||
try {
|
||||
this.streamingConverter.convert(source, byteStream);
|
||||
return byteStream.toByteArray();
|
||||
} catch (Exception e) {
|
||||
throw new SerializationFailureException("Failed to serialize", e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,69 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2010 the original author or authors.
|
||||
*
|
||||
* Licensed 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.springframework.commons.serializer.java;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.io.Serializable;
|
||||
|
||||
import org.springframework.commons.serializer.InputStreamingConverter;
|
||||
import org.springframework.commons.serializer.OutputStreamingConverter;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
|
||||
/**
|
||||
* Converter that implements both input and output streaming using Java
|
||||
* Serialization.
|
||||
*
|
||||
* @author Gary Russell
|
||||
* @since 2.0
|
||||
*
|
||||
*/
|
||||
public class JavaStreamingConverter
|
||||
implements InputStreamingConverter<Object>,
|
||||
OutputStreamingConverter<Object> {
|
||||
|
||||
public Object convert(InputStream inputStream) throws IOException {
|
||||
ObjectInputStream objectInputStream = null;
|
||||
try {
|
||||
objectInputStream = new ObjectInputStream(inputStream);
|
||||
return objectInputStream.readObject();
|
||||
} catch (ClassNotFoundException e) {
|
||||
if (objectInputStream != null) {
|
||||
objectInputStream.close();
|
||||
}
|
||||
throw new IOException(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Source object must implement {@link Serializable}.
|
||||
*/
|
||||
public void convert(Object object, OutputStream outputStream)
|
||||
throws IOException {
|
||||
Assert.isTrue(object instanceof Serializable, this.getClass().getName()
|
||||
+ " requires a Serializable payload, but received [" +
|
||||
object.getClass().getName() + "]");
|
||||
ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);
|
||||
objectOutputStream.writeObject(object);
|
||||
objectOutputStream.flush();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,5 +0,0 @@
|
||||
/**
|
||||
* Implementation of In/Out JavaStreamingConverter.
|
||||
*/
|
||||
package org.springframework.commons.serializer.java;
|
||||
|
||||
@@ -1,9 +0,0 @@
|
||||
/**
|
||||
*
|
||||
* Root package for Spring commons' serializer interfaces and implementations.
|
||||
* Provides an abstraction over various serialization techniques.
|
||||
* Includes exceptions for serialization and deserialization failures.
|
||||
* Actual (de)serializers are in subpackages.
|
||||
*/
|
||||
package org.springframework.commons.serializer;
|
||||
|
||||
@@ -1,87 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2010 the original author or authors.
|
||||
*
|
||||
* Licensed 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.springframework.commons.serializer;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import java.io.NotSerializableException;
|
||||
import java.io.Serializable;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.commons.serializer.java.JavaStreamingConverter;
|
||||
|
||||
|
||||
/**
|
||||
* @author Gary Russell
|
||||
* @since 2.0
|
||||
*
|
||||
*/
|
||||
public class JavaSerializationTests {
|
||||
|
||||
@Test
|
||||
public void testGood() {
|
||||
JavaStreamingConverter streamingConverter = new JavaStreamingConverter();
|
||||
SerializingConverter toBytes = new SerializingConverter(streamingConverter);
|
||||
byte[] bytes = toBytes.convert("Testing");
|
||||
DeserializingConverter fromBytes = new DeserializingConverter(streamingConverter);
|
||||
assertEquals("Testing", fromBytes.convert(bytes));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBadSerializeNotSerializable() {
|
||||
SerializingConverter toBytes = new SerializingConverter(new JavaStreamingConverter());
|
||||
try {
|
||||
toBytes.convert(new Object());
|
||||
fail("Expected IllegalArgumentException");
|
||||
} catch (SerializationFailureException e) {
|
||||
assertNotNull(e.getCause());
|
||||
assertTrue(e.getCause() instanceof IllegalArgumentException);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBadSerializeNotSerializableField() {
|
||||
SerializingConverter toBytes = new SerializingConverter(new JavaStreamingConverter());
|
||||
try {
|
||||
toBytes.convert(new UnSerializable());
|
||||
fail("Expected SerializationFailureException");
|
||||
} catch (SerializationFailureException e) {
|
||||
assertNotNull(e.getCause());
|
||||
assertTrue(e.getCause() instanceof NotSerializableException);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBadDeserialize() {
|
||||
DeserializingConverter fromBytes = new DeserializingConverter(new JavaStreamingConverter());
|
||||
try {
|
||||
fromBytes.convert("Junk".getBytes());
|
||||
fail("Expected DeserializationFailureException");
|
||||
} catch (DeserializationFailureException e) { }
|
||||
}
|
||||
|
||||
class UnSerializable implements Serializable {
|
||||
private static final long serialVersionUID = 1L;
|
||||
@SuppressWarnings("unused")
|
||||
private Object object;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user