Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding a byte array converter #1004

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there an use case which you need to get a byte array in the controller?
I can't see this as a global need, so I'm not sure if it should be added to core
what do you think? isn't it too specific?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the pull request description there are an example. Of course threre is not too common used like Integer or Long, but can be used as we have ByteConverter or CharacterConverter. I have two applications thar uses this converter, and in the next week another app. So I think can be useful for our users.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd vote to wait a little more users asking for it (maybe here, after seeing this pr)
that's because I've never seen someone asking for it on vraptor's mail list or something
you can easily package this converter in a jar that can be shared on your apps

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really your are ask me to share a jar with my apps for a simple converter that can be useful for some users, that won't disturb anyone? There is a lot of very useful and valuable features never asked in the mailing list, but I contribute (and suggested) to vraptor, and now users love it. So its too hard to me to understand your arg. I can understand when I suggest a dangerous feature or behavior, but not a simple component like this.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I have a lot of valuable contributions for vraptor, over 6 years, 2 thousand commits, so I'm a good user too suggest new features like this. I have a lot of applications using vraptor, and all my contributions and suggestions are based in these applications.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for this, garcia. I'm just providing my option/vote, it doesn't mean your contribution aren't valuable or something like that. This kind of reaction isn't healthy for the project. If anyone else upvote this pr, I'm ok merging it. I'm just suggesting to wait a little more before merging, since IMHO it's 135 lines of dead code to (guessing) 99% of our users now.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Relax, guys!

by the way, I can't imagine a case where I could use this feature.

Otavio, could you give us some use cases for this feature?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Turini Maybe you don't see, but you are too hard to me in your comments, like saying my contribution is 135 lines of dead code. And this action makes me think to giving up contribute to vraptor, to avoid this kind of feeling. It is very important to you look beyond the horizon and see that even if you have not used in your projects, others can use and have these needs. As I have many applications using vraptor, I just try some needs before someone have the same needed.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rponte There are an example in the pull request description. I can't provide more information about the application due a NDA.

But I have some applications using a hard cryptography. Since Java Strings are immutable and shared by a pool, we need to use password as String so less time when possible. Using byte array or char array as good pratice when we are using a hight security, because we can override the values when we want, and Strings not.

Without this converter, in any method I need to use byte array I need to inject EncodingHandler to do something like this:

public void foo(String passwd) {
   Charset charset = Charset.fromName(encondingHandler.getCharset());
   byte[] passwdArray = passwd.getBytes(charset);
}

So this converter can save too much work.

At this time we have ByteConverter, CharacterConverter converters, and also for primitive types: PrimitiveByteConverter, PrimitiveCharacterConverter. Why these converters are useful and byte array isn't?

@ApplicationScoped
@Alternative
@Priority(Interceptor.Priority.LIBRARY_BEFORE)
public class PrimitiveByteArrayConverter implements Converter<byte[]> {

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<? extends byte[]> type) {
if (isNullOrEmpty(value)) {
return new byte[0];
}

return value.getBytes(getCurrentCharset());
}

protected Charset getCurrentCharset() {
return Charset.forName(encodingHandler.getEncoding());
}
}
Original file line number Diff line number Diff line change
@@ -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)));
}
}