diff --git a/vraptor-core/src/main/java/br/com/caelum/vraptor/converter/PrimitiveByteArrayConverter.java b/vraptor-core/src/main/java/br/com/caelum/vraptor/converter/PrimitiveByteArrayConverter.java new file mode 100644 index 000000000..16537e2ca --- /dev/null +++ b/vraptor-core/src/main/java/br/com/caelum/vraptor/converter/PrimitiveByteArrayConverter.java @@ -0,0 +1,73 @@ +/*** + * Copyright (c) 2009 Caelum - www.caelum.com.br/opensource + * All rights reserved. + * + * 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 br.com.caelum.vraptor.converter; + +import static com.google.common.base.Strings.isNullOrEmpty; + +import java.nio.charset.Charset; + +import javax.annotation.Priority; +import javax.enterprise.context.ApplicationScoped; +import javax.enterprise.inject.Alternative; +import javax.inject.Inject; +import javax.interceptor.Interceptor; + +import br.com.caelum.vraptor.Convert; +import br.com.caelum.vraptor.http.EncodingHandler; + +/** + * A Byte array converter. Null or empty values are returned as an empty byte array. + * + * @author Otávio Scherer Garcia + * @since 4.2.0 + */ +@Convert(byte[].class) +@ApplicationScoped +@Alternative +@Priority(Interceptor.Priority.LIBRARY_BEFORE) +public class PrimitiveByteArrayConverter implements Converter { + + public static final String INVALID_MESSAGE_KEY = "is_not_a_valid_number"; + + private final EncodingHandler encodingHandler; + + public PrimitiveByteArrayConverter() { + this(null); + } + + /** + * @deprecated CDI eyes only + */ + @Inject + public PrimitiveByteArrayConverter(EncodingHandler encodingHandler) { + this.encodingHandler = encodingHandler; + } + + @Override + public byte[] convert(String value, Class type) { + if (isNullOrEmpty(value)) { + return new byte[0]; + } + + return value.getBytes(getCurrentCharset()); + } + + protected Charset getCurrentCharset() { + return Charset.forName(encodingHandler.getEncoding()); + } +} \ No newline at end of file diff --git a/vraptor-core/src/test/java/br/com/caelum/vraptor/converter/PrimitiveByteArrayConverterTest.java b/vraptor-core/src/test/java/br/com/caelum/vraptor/converter/PrimitiveByteArrayConverterTest.java new file mode 100644 index 000000000..76e44ba31 --- /dev/null +++ b/vraptor-core/src/test/java/br/com/caelum/vraptor/converter/PrimitiveByteArrayConverterTest.java @@ -0,0 +1,62 @@ +/*** + * Copyright (c) 2009 Caelum - www.caelum.com.br/opensource + * All rights reserved. + * + * 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 br.com.caelum.vraptor.converter; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.is; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import java.nio.charset.StandardCharsets; + +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + +import br.com.caelum.vraptor.http.EncodingHandler; + +public class PrimitiveByteArrayConverterTest { + + @Rule + public ExpectedException exception = ExpectedException.none(); + + private PrimitiveByteArrayConverter converter; + + + @Before + public void setup() { + final EncodingHandler encodingHandler = mock(EncodingHandler.class); + when(encodingHandler.getEncoding()).thenReturn(StandardCharsets.UTF_8.toString()); + + this.converter = new PrimitiveByteArrayConverter(encodingHandler); + } + + @Test + public void shouldReturnEmptyByteArrayWhenNull() { + assertThat(converter.convert(null, byte[].class), is(equalTo(new byte[0]))); + } + + @Test + public void shouldBeAbleToConvertUsingByteArray() { + String input = "abc"; + byte[] expected = { (byte) 0x61, (byte) 0x62, (byte) 0x63 }; + assertThat(converter.convert(input, byte[].class), is(equalTo(expected))); + } +} \ No newline at end of file