-
Notifications
You must be signed in to change notification settings - Fork 329
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
+135
−0
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
73 changes: 73 additions & 0 deletions
73
vraptor-core/src/main/java/br/com/caelum/vraptor/converter/PrimitiveByteArrayConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
@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()); | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
...r-core/src/test/java/br/com/caelum/vraptor/converter/PrimitiveByteArrayConverterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
orLong
, but can be used as we haveByteConverter
orCharacterConverter
. I have two applications thar uses this converter, and in the next week another app. So I think can be useful for our users.There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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: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?