Setting a character limit for a string using Java annotations - java

I'm trying to create a string with a character length restriction of 12 for my JBOSS Seam project. The string must either be 12 characters or blank. My length annotation is correct which is the following:
#Length(min = 12,max = 12)
However when I try to put a null value in there I get an InvalidStateException: validation fail error. Any ideas how to allow this?

Null value for String and empty String are not the same thing. You are passing a null value (not a String of length 0). Check this out:
Difference between null and empty ("") Java String
Also, you should try out #Size(min=,max=).

Well I decided to not rely on the #Length annotation and instead created my own custom validator class to do the job and that worked out well. Thanks anyway!

Related

How i can send null values in protobuf 3 using java?

I know we can't send null values using protobuf 3.
but i can see people are using oneof to achieve this.
message GetHomePageDataRequest{
string client_id = 1;
string user_id = 2;
oneof one_of_importance {
google.protobuf.NullValue null_importance = 3;
string importance = 4;
}
}
but i am not sure about server/client look like for above request. how we can write code so we get to know that importance is null or not?
i am using gRpc with protobuf 3 using java8
In Java, the oneof API exposes an enum, per oneof, that tells you which option is selected; see docs here. Interestingly, you don't actually need a specific value for the null option - it is sufficient simply not to assign a value at all, and test for the *_NOT_SET enum.
Note that there is also now experimental support for presence tracking in proto3, which works the same as optional did in proto2; essentially, if you use --experimental_allow_proto3_optional with protoc, you can then use:
message GetHomePageDataRequest{
string client_id = 1;
string user_id = 2;
optional string importance = 4; // or 3, if you can still change it
}
and all of the usual APIs to test for a value and clear a value etc will be generated, meaning: you will be able to distinguish explicit empty values from missing values. The proto3 presence APIs are discussed more here.

How to set an empty space as constant in Apache Camel?

I tried to set a String with an empty space as a constant in Camel.
from("timer:test?fixedRate=true&period=5000")
.setBody().constant(" ")
.log("'${body}'")
;
It seems not to work, because the above code prints '' as log output.
I'm using Camel Version 2.23.1 and figured out that the method constant in the Class ExpressionClauseSupport needs to set the Attribute trim to true when creating the ConstantExpression. See the creation of the Object and the trimming of the constant String.
In my opinion a constant should not be trimmed or am I wrong?
May be this ?
ConstantExpression exp = new ConstantExpression(" ");
exp.setTrim(false);
from("timer:test?fixedRate=true&period=5000")
.setBody(exp)
.log("'${body}'");

Filter Special Characters in Spring / Java

I'm using jsoup to get all text from websites.
Document doc = Jsoup.connect("URL").get();
String allText doc.text().toLowerCase();
Then I'm using Hibernate to persist the object that holds all text to a MySQL DB:
...
#Column(name="all_text")
#Lob
private String allText = null;
...
Everything is good so far. Only that sometimes I get a MySQL error when I try to save the object with allText:
java.sql.SQLException: Incorrect string value: '\xF0\x9F\x98\x8A s...' for column 'all_text' at row 1
Already looked this up and it's an encoding error. Probably have some special characters on their websites. I found a way to fix this by changing the encoding in the DB.
But my actual question is: what's the best way to filter and remove the special characters from the allText string and not persist them at all?
EDIT: To clarify, by special characters I mean Emoticons and all that stuff. Definitely anything that doesn't fit into UTF-8 encoding. I'm not concerned about ~ ^ etc...
Thanks in advance!
Just use regex:
allText.replaceAll("\\p{C}", "");
Don't forget to import java.util.regexPattern

How to convert string to MarketDataIncrementalRefresh in QUICKFIX in Java?

I am working on the Stock and Exchange Markets. I have a situation like : I need to take a string from the log and convert it to "Message" type Object. As per this link I have tried using all the three methods of the "MessageUtils" class in JAVA. But my String is being stripped to a Message class type object with unique tags. But as my string is "MarketDataIncrementalRefresh" type I want each every tag to be present in the Message.
For example : I am providing the following string to "MessageUtils.parse()" method.
8=FIX.4.2|9=00795|35=W|49=TT_PRICE|56=SAP0094X|34=2392|52=20170623-04:41:33.375|55=CL|48=00A0HR00CLZ|10455=CLQ7|167=FUT|207=CME|15=USD|262=MDRQ-751|200=201708|18210=1|387=12292|268=24|269=0|290=1|270=4290|271=33|269=0|290=2|270=4289|271=34|269=0|290=3|270=4288|271=40|269=0|290=4|270=4287|271=38|269=0|290=5|270=4286|271=46|269=0|290=6|270=4285|271=53|269=0|290=7|270=4284|271=46|269=0|290=8|270=4283|271=66|269=0|290=9|270=4282|271=48|269=0|290=10|270=4281|271=64|269=1|290=1|270=4291|271=21|269=1|290=2|270=4292|271=40|269=1|290=3|270=4293|271=48|269=1|290=4|270=4294|271=83|269=1|290=5|270=4295|271=62|269=1|290=6|270=4296|271=46|269=1|290=7|270=4297|271=34|269=1|290=8|270=4298|271=55|269=1|290=9|270=4299|271=31|269=1|290=10|270=4300|271=128|269=2|270=4291|271=1|269=4|270=4280|269=7|270=4292|269=8|270=4277|10=044|
But what I am getting is this:
8=FIX.4.2|9=192|35=W|34=2|49=TT_PRICE|52=20170622-14:16:23.685|56=SAP0094X|15=USD|48=00A0HR00GCZ|55=GC|167=FUT|200=201708|207=CME|262=MDRQ-21|268=25|269=0|270=12510|271=24|290=1|387=121890|10455=GCQ7|18210=1|10=036|
As you can observe only unique tags are present in the String. But I want each and every tag , no matter how many times it exists in the provided string.
Please can anyone help me doing this in JAVA. It will be really appreciable.
Below is the code I am using for converting :
MessageUtils mu = new MessageUtils();
Session session = Session.lookupSession(sessionID);
Message msg = MessageUtils.parse(new DefaultMessageFactory(), null, str);
// Message msg = new Message(str, false); //This can also be used for converting
System.out.println(msg.toString());
The other thread says:
MessageUtils.parse(MessageFactory messageFactory, DataDictionary dataDictionary, java.lang.String messageString)
And your code says:
Message msg = MessageUtils.parse(new DefaultMessageFactory(), null, str);
So you need to fix your data dictionary and pass it to the parse method instead of passing 'null'
I think the problem is as follows. There's a repeating group that starts with tag 286 (NoMDEntries). The order of fields in a repeating group should be strict, i.e. the same order as the definition of the repeating group. See Market Data - Snapshot/Full Refresh or the data dictionnary supplied by QuickFIX/J (FIX42.xml).
The 268 tag should be followed by 269 and then 270. I am seeing in your message string: |268=24|269=0|290=1|270=4290| which is the incorrect order of tags. That is probably the reason why the message is truncated by MessageUtils.parse.
As a test you could try to manually correct the order in the string and try parsing that to see if that gives the correct message.

Spring Boot - custom validation message with annotation parameters

I want to create my custom validation messages with Spring Boot/MVC Validation.
I found following setup working:
public class RegisterCredentials {
#NotEmpty
#NotNull
#Size(min=3, max=15)
private String username;
...
}
messages.properties:
NotEmpty.registerCredentials.username = Username field cannot be empty.
NotNull.registerCredentials.username = Username field cannot be empty.
Size.registerCredentials.username = Username length must be between 3 and 15 characters.
After that I wanted to replace fixed values min=3 and max=15 with attributes. I tried like that, but it didn't worked (it works in #Size(message="..") annotation):
Size.registerCredentials.username = Username length must be between {min} and {max} characters.
Then I found following code working..:
Size.registerCredentials.username = Username length must be between {1} and {2} characters.
Well.. almost, because it produces following message:
Username length must be between 15 and 3 characters.
Replacing order of these {1} and {2} solves the problem, however it produces confusing code in futher analyze.
Is there a solution to solve this problem with clean code?

Categories