Replacing Illegal Characters in a String - java

I'm using Javamail to get the FROM address of a message.
The problem is I always get the following exception.
javax.mail.internet.AddressException: Local address contains control or whitespace in string ``ÇáÞÑíÉ ÇáÅáßÊÑæäíÉ''
another exception is like
javax.mail.internet.AddressException: Nested group in string ``.:DoWnLoAdiZ:.FiLMs <900000009#google.com>'' at position 12
How can I format the string before showing it and replace all those illegal white spaces,..etc ?

These are in messages you've received, right? They're almost certainly spam,
just throw them away. Spammers are notoriously bad about following internet
standards.
If you really want to try to read these messages, set the System property
"mail.mime.address.strict" to "false", as described in the javadocs for the
javax.mail.internet package.

Related

How to use String ressource in Java file in Android without a Layout

I have a Java class that mainly contains strings. It does not have a layout as it is neither a Fragment nor an Activity. It is more used as an auxilliary class. I would like to assign the String values in the class by using the Resource strings as I would like to automatically translate the Strings. Howvever, I can't access the string resources from Java. I use the following code:
static String more = getString(R.string.more);
And in the XML file I have the ressource:
<string name="more">More</string>
I get the error message
Cannot resolve method 'getString'
I also tried static String more = getContext().getString(R.string.more);
but I got the error message:
Cannot resolve method 'getContext'
Would anyone mind helping me on that? I'd appreciate every comment.
Update: I tried to use the answer from "MikaelM"
Resources.getSystem().getString(R.string.more)
However, I get an "exception in initializer error" and when I use the initial String again, I do not get this error. So I still can't get the String from the ressource. DO you have an idea what I can do? I'd appreciate every further comment.
So the error is caused by "Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x7f120038"
The strange thing is that the resource in fact exists (I checked this several times).
getString(R.string...
is not the same as
Resources.getSystem().getString(android.R.string...
Only the second variant you can use in the static context. Alas, you can get this way only system resources.
If you need to get your own resources, no one-line solution exists. Use https://stackoverflow.com/a/4391811/715269 solution of #Cristian. And notice: it is a very good solution, even more, it is the solution meant to be used by the creators of Android.
You should be able to get your string with:
Resources.getSystem().getString(R.string.more)
See more here, getString Outside of a Context or Activity

QuickfixJ required field missing, even if it's there

I'm getting a "Conditionally Required Field Missing" error message, even though I'm sure that field is there.
58=Conditionally Required Field Missing, field=55
Versions:
QuickFixJ 2.1.0
FIX 4.4
Here is the FIX message that I'm sending (with mocked values and a few fields removed for clarity)
8=FIX.4.4
9=709
35=R
34=4
49=TARGET
56=ME
11=myClOrdID
131=myQuoteReqID
146=myNoRelatedSym
55=mySymbol // field missing
167=mySecurityType // field missing
Here is the calling code:
String symbol = quoteRequest.getField(new StringField(55)).getValue();
I also tried:
String symbol = quoteRequest.getString(55);
Here is my Data Dictionary:
<field number="55" name="Symbol" type="STRING"/>
I realize that the symbol field is no longer a part of the QuoteRequest FIX specification for 4.4 (though it was in earlier versions, such as 4.0), however surely there are ways to retrieve custom fields? I have no control over the QuoteRequest message that I receive.
I can always parse the message myself using toString() but that kinda defeats the purpose of using quickfixj in the first place.
Any ideas?
Tag 55 is inside the 146 repeating group. See the docs for reading repeating groups.
The symbol field is still in FIX44. You should spend some time familiarizing yourself with the FIX44.xml data dictionary file that you're using.
(You may find that you need to customize that file based on your counterparty's messaging; in practice, nobody uses the basic FIX44 message definitions without changing them at least a little.)
// create group
QuoteRequest.NoRelatedSym group = new QuoteRequest.NoRelatedSym();
// set group, confusing method name I find
message.getGroup(1, group);
// you now have all the getters of fields in that group
Symbol symbol = group.getSymbol();

How to customize / replace exception messages in Freemarker to make them more meaningful?

I would like to "improve" some exception messages thrown by Freemarker template messages to make the exceptions more meaningful for the users. Although Freemarker has become a lot better in terms of meaningful error messages, there are still cases, where I would like to be more specific.
Example
Freemarker is throwing this exception for a template like this:
<#if (""?number > 1)>foo</#if>
(just an example... imagine the empty string could also be a variable containing an empty string)
value of templateException.getMessage():
(java.lang.String) Can't convert this string to number: ""
The blamed expression:
==> ""?number [in nameless template at line 1, column 7]
----
FTL stack trace ("~" means nesting-related):
- Failed at: #if (""?number > 1) [in nameless template at line 1, column 1]
----
I would like to rephrase this specific case to:
You tried to convert an EMPTY string variable to a number.
I could try my own Exception handler, to contains checks, replace the message and rethrow an Exception like this:
configuration.setTemplateExceptionHandler(new TemplateExceptionHandler() {
public void handleTemplateException(TemplateException te, Environment env, java.io.Writer out)
throws TemplateException {
String message = te.getMessage();
if(StringUtils.contains(message, "Can't convert this string to number: \"\"")){
message = StringUtils.replace(message, "Can't convert this string to number: \"\"", "You tried to convert an EMPTY string variable to a number. Solution: Try checking if the variable is empty to avoid this error.");
}
throw new TemplateException(message, env);
}
});
But this feels very hacky.
My questions:
Is there a way how I can customize the Exception messages Freemarker is throwing? I have the feeling in my TemplateExceptionHandler it is too late, as the message gets constructed much earlier inside Freemarker.
What are common ways to improve / rewrite exception messages from 3rd party libs?
Search and replace may won't work after version updates, as there's no backward compatibility promise regarding the message content.
If the changes you want are generally useful (not only for your project), then you could improve the existing error messages by contributing to FreeMarker (sign Apache CLA, fork on GitHub, make pull request).
The only really correct and flexible way I see is adding l10n support to the error message mechanism, where the message strings aren't hard-wired in to the code (except their defaults), but are retrieved based on message keys from external source. It can be a big work of course, especially as FreeMarker messages are assembled from many smaller pieces.

WSDL address containing underscores throws an error.Error:- toLowerCase() in java.net.HostPortrange fails on special characters

In NetBeans 8 running on JDK 8u25 When i was adding a Web Service Client. I inserted WSDL address, but the environment constantly returned "Problem with downloading wsdl(Web Service Description Language) or schema file", although it worked in other applications.
The problem is that the address contained underscores and the error is invoked by the IllegalArgumentException("Invalid characters in hostname") that was thrown by toLowerCase() method in java.net.HostPortrange.
toLowerCase() checks the characters of an address and if they are not within [a-z0-9A-Z.-] an exception is thrown even though also other characters are valid in URL.
Please provide me any idea for the resolution of this problem.
It's a bug in JDK 8 b177. It is fixed in b122 Please refer below link:
https://bugs.openjdk.java.net/browse/JDK-8029354

IOException: unexpected response from remote api

I'm having exactly the same problem as this person:
https://groups.google.com/forum/#!topic/google-appengine-java/4y90M9NlWsA
but there are no responses and I can't see anything else on the internet that helps. Does anyone know why this would happen?
RemoteApiOptions options = new RemoteApiOptions()
.server("localhost", 8888)
.credentials("blah#blah.blah", "");
RemoteApiInstaller installer = new RemoteApiInstaller();
installer.install(options);
The stacktrace is:
Exception in thread "main" java.io.IOException: unexpected response from remote api: {rtok: null, app_id: MyCorrectAppId}
at com.google.appengine.tools.remoteapi.RemoteApiInstaller.getAppIdFromServer(RemoteApiInstaller.java:326)
at com.google.appengine.tools.remoteapi.RemoteApiInstaller.loginImpl(RemoteApiInstaller.java:278)
at com.google.appengine.tools.remoteapi.RemoteApiInstaller.login(RemoteApiInstaller.java:239)
at com.google.appengine.tools.remoteapi.RemoteApiInstaller.install(RemoteApiInstaller.java:106)
at uk.blah.blah.MyClass.main(MyClass.java:114)
I've noticed you've set the <application> element inside appengine-web.xml to MyCorrectAppId. That's the problem. As Eugene rightly pointed out, the ID must be all lowercase, like mycorrectappid. Quoting Dan Sanderson's book Programming Google App Engine:
Because the application ID is used in the domain name, an ID can contain only lowercase letters, numbers, or hyphens, and must be shorter than 32 characters. Additionally, Google reserves every Gmail username as an application ID that only the corresponding Gmail user can register.
If you are working in Eclipse, you need to remember that when setting up a new web application project (there is an option towards the bottom of the "New Web Application Project" dialogue that allows you to specify the ID instead of leaving it blank).
For a standalone environment, I think you're probably fine if you leave the ID blank (it will default to "no_app_id").
Hope that helps!
According to the code of the RemoteApiInstaller.getAppIdFromServer method the exception unexpected response from remote api can be thrown when no app_id property found in the response body. And it seems that in your case it is probable that MyCorrectAppId does not match the PAIR_REGEXP pattern, i.e. it contains any characters other than digits, letters in lower case, tilde, underscore or minus sign.

Categories