Single Curly Bracket in Properties file - java

I am trying to read this text which has only a single curly brace
Y8R30j)i{sjmPXfE
from a .properties file using MessageResources.getMessage()
and am getting this exception
java.lang.IllegalArgumentException: Unmatched braces in the pattern.
at java.text.MessageFormat.applyPattern(MessageFormat.java:508)
at java.text.MessageFormat.<init>(MessageFormat.java:363)
I tried to escape by using
Y8R30j)i'{'sjmPXfE
but am getting the same exception.

org.apache.struts.util.MessageResources uses java.text.MessageFormat which interprets things between curly braces as patterns or placeholders to be replaced with strings.
Per the exception it is clear that java is not able to find the closing brace for the opening curly brace you have in your key value, possible workaround (working with struts 1.3) is below. ( in light of unicode escaping or any other escaping not working, can refer to java.text.MessageFormat.applyPattern() method for further escaping possibles)
Specify key as below in message resources file -
key=Y8R30j)i{0}sjmPXfE
Read value of the key with the code below in your action (or any other java) class
MessageResources messages = MessageResources.getMessageResources("MessageResources");
Object[] leftCurlyBrace = { "{" };
String value = messages.getMessage(request.getLocale(), "key", leftCurlyBrace);
Am assuming you are trying to read some encrypted value from a properties file in a struts 1.x J2EE environment

Related

Escape special characters in properties file java

I have key, value in properties file like this proj.path=${HOME}/dir. I have both environment variable and directory also with ${HOME}.
In my case I would like to use it as directory path only but when I read this from file it is getting replaced with environment variable value (home/user/dir).
I tried to escape it like proj.path=\\$\\{HOME\\}/dir but in code it is coming like \$\{HOME\}/dir
Required output is ${HOME}/dir.
EDIT:
Prop file:
proj.path=${HOME}/dir,some/dir/dir2
I am accessing in spring like below.
#Value("#{'${proj.path}'.split(',')}")
private List<String> customPaths;
One way of escaping the ${HOME} value is by wrapping the $ character in an expression and changing the type from List<String> to String[].
proj.path=#{'$'}{HOME}/dir,some/dir/dir2
#Value("${proj.path}")
private String[] customPaths;

Apache Camel Java DSL add newline to body

So I have a netty4 socket route set up in Java DSL that looks like the following:
#Override
public void configure() throws Exception {
String dailyDataUri = "{{SOCKET.daily.file}}" + "&fileName=SocketData-${date:now:yyyyMMdd}.txt";
from(socketLocation).routeId("thisRoute")
.transform()
.simple("${in.body}\n")
.wireTap(dailyDataUri)
.to(destination)
;
Where both the wireTap and the destination are sending their data to two separate files. And the data collection in the destination file is separated by a \n (line break)... or at least it should be.
When viewing the files created, the \n is never added.
The equivalent idea in the Spring DSL worked before I switched to Java:
<transform>
<simple>${in.body}\n</simple>
</transform>
After using that and opening the files created during the route, the lines of data that came in through the socket would be separated by a newline.
What am I doing wrong in the Java DSL that doesn't allow the newline to be appended to the socket data as it comes in?
I feel like it's something obvious that I just don't see.
The data that is coming in is just a CSV-like line of text.
I found a solution, I'm never sure what can be translated almost word from word from Spring to Java. Apparently the transform/simple combination has some issue where it will not work for me in Java DSL.
So a possible solution (there may be more solutions) is to do this:
#Override
public void configure() throws Exception {
String dailyDataUri = "{{SOCKET.daily.file}}" + "&fileName=SocketData-${date:now:yyyyMMdd}.txt";
from(socketLocation).routeId("thisRoute")
.transform(body().append("\n"))
.wireTap(dailyDataUri)
.to(destination)
;
Where instead of using the Simple language to manipulate the body, I just call on the body and append a String of \n to it. And that solves my issue.
Update : Camel version 3.x and above File component provides features to append your desired character.
As you are writing file using file component (producer)
appendChars (producer)
Used to append characters (text) after writing files. This can for example be used to add new lines or other separators when writing and appending new files or existing files. To specify new-line (slash-n or slash-r) or tab (slash-t) characters then escape with an extra slash, eg slash-slash-n.

Store Backward slash and forward slash in java string

I am parsing below string value into OData query through java code.
objects.put("EndDate", "\/Date(1441756800)\/";
How can i parse the /Date(1441756800)/ into a string in java.
I have tried with below :
objects.put("EndDate", ""\\""//"Date(1441756800)""\\""//"";
throws error:(
I never used OData so I may not understand your question correctly, but if you are asking how to write \/Date(1441756800)\/ as String then you need to escape \ as it is String special character (used for instance when escaping or when creating other special characters like line separators \n).
So try with "\\/Date(1441756800)\\/"
Try this - objects.put("EndDate", "'Date(1441756800)'";

Freemarker encoding - question marks in the place of accented characters

I am trying to print accented characters with Freemarker, but in the place of accented characters, I get only question marks. I have verified, that following statement holds:
"UTF-8" == Environment.getCurrentEnvironment().getConfiguration().getDefaultEncoding()
I can easily see, that the accented characters are correctly held in the variable before giving it to the template.
My freemarker context can be found here: https://gist.github.com/1975239
For instance instead of:
Jedinečný živý koncert, kde nejen, že uslyšíte, ale i uvidíte splynutí metalové kapely s padesátičlenným orchestrem včetně.
I keep getting:
Jedine?ný ?ivý koncert, kde nejen, ?e usly?íte, ale i uvidíte splynutí metalové kapely s padesáti?lenným orchestrem v?etn?.
Thanks.
I was able to resolve a similar issue with non-standard symbols (like ™) by setting the content-type on the FreeMarkerViewResolver:
<bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
...
<property name="contentType" value="text/html;charset=UTF-8"/>
...
</bean>
For DROPWIZARD Users: passing through the UTF-8 Charset in the constructor worked out:
import io.dropwizard.views.View;
import java.nio.charset.Charset;
public class SomeView extends View {
public SomeView() {
super("/views/some.ftl", Charset.forName("UTF-8"));
}
}
FreeMarker always treats text as UNICODE, so it doesn't generate question marks. Since the accented letters aren't coming from the templates (if I understand it well), it must be your output encoding that's improper. See also: http://freemarker.org/docs/app_faq.html#faq_questionmark
BTW, getDefaultEncoding() has no role in this. That influences the decoding used when you load the templates, but you are saying that the accented characters aren't coming from the template file, also I don't think you can get ?-s from decoding (unless, for invalid UTF-8 byte sequences). As of the encoding of the output, FreeMarker just uses a Writer (as opposed to an OutputStream), so it can't influence that.
For the freemarker servlet there exist init parameters for encoding of template and output. You might compare it with your configuration.
In a Dropwizard project this can be fixed by adding <#ftl encoding="utf-8"> at the start of the template file, as described in FreeMarkers’s FAQ. This works because Dropwizard uses the encoding of the template for the output.

apache commons configuration loads property until "," character

I want to load configuration (apache commons configuration) from a properties file. My program is:
PropertiesConfiguration pc = new PropertiesConfiguration("my.properties");
System.out.println(pc.getString("myValue"));
In my.properties I have
myValue=value,
with comma
When I run program the output is value, not value, with comma. Looks like value is loaded until , character.
Any ideas?
That behavior is clearly documented, i.e., that PropertiesConfiguration treats a value with a comma as multiple values allowing things like:
fruit=apples,banana,oranges
to be interpreted sensibly. The fix (from the doc) is to add a backslash to escape the comma, e.g.,
myKey=value\, with an escaped comma
Check Javadoc. You have to setDelimiterParsingDisabled(true) to disable parsing list of properties.
Actually propConfig.setDelimiterParsingDisabled(true) is working, but you must load the config file after this setting, for example:
propConfig = new PropertiesConfiguration();
propConfig.setDelimiterParsingDisabled(true);
propConfig.load(propertiesFile);
Settings won't work if your code like is:
propConfig = new PropertiesConfiguration(propertiesFile);
propConfig.setDelimiterParsingDisabled(true);
PropertiesConfiguration interprets ',' as a value separator.
If you put \ before the ,, you escape it, and you can read the value
Example:
myValue=value\, with comma
You read = value, with comma without problems

Categories

Resources