PropertyPlaceholderConfigurer inside routeContext. Can't set uri from property file - java

I have property file and I want to set uri in my route from this file:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">
<import resource="TransactionsParserConfig.xml"/>
<bean id="transactionsValidator" class="com.class.TransactionsValidator"/>
<bean id="transactionsValidator123" name="${ftp.host}"/> <!--here I can use property-->
<routeContext id="transactionsRoutes" xmlns="http://camel.apache.org/schema/spring">
<route id="routeFTP">
<from uri="direct:start" />
<!--here I can NOT use property-->
<from uri="ftps://${ftp.host}/?securityProtocol=SSL"/>
etc...
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<array>
<value>classpath:/ftp.properties</value>
</array>
</property>
</bean>
</beans>
But looks like it's not allowed to set uri from property file. Because when I use ${ftp.host} outside the routexContext tag everething is fine. Also even Idea can't redirect me when I hit ctrl+mouseclick inside routeContext.
Why?

Camel cannot use PropertyPlaceholderConfigurer inside it's Spring DSL due to Spring restrictions. There are many ways to achieve what you want. Some of them are described by link.

You can use bean configuration mentioned below
<bean id="myProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations">
<list>
<value>file:/properties1.properties</value>
<value>file:/properties2.properties</value>
</list>
</property>
</bean>
Properties can be access in java classes like
#Value("${categories.insert.sql}")
private String insertQuery;
Properties can be accessed in camel context like
<setHeader headerName="signature">
<constant>{{api.secretkey}}</constant>
</setHeader>
Hope this works

Related

Spring remove printout Could not load properties from URL

I'm building a gradle plugin in which I want to give users the ability to override some properties from an external file.
I'm using the Spring propertyPlaceholderConfigurer in order to set a few destinations where users can place their files, This works as expected.
My problem is that when running my plugin Spring prints out to the console: "Could not load properties from URL [...]"
Does anyone have any idea how I would go about squelching this message?
I've tried overriding logback and sl4j to no avail.
My Spring application-context xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:security="http://www.springframework.org/schema/security"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-2.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd"
default-lazy-init="true">
<!-- Activates scanning of #Autowired -->
<context:annotation-config/>
<!-- Activates scanning of #Repository and #Service -->
<context:component-scan base-package="com.example.test"/>
<bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:/conf/plugin.properties</value>
<value>file:${user.home}/conf/plugin.properties</value>
<value>file:${user.home}/conf/plugin-override.properties</value>
</list>
</property>
<property name="placeholderPrefix" value="${"/>
<property name="ignoreResourceNotFound" value="true"/>
<property name="systemPropertiesMode" value="2"/>
</bean>
If you want to implement custom logic to set locations you can extend PropertyPlaceholderConfigurer like
class CustomPropertyPlaceholderConfigurer extends org.springframework.beans.factory.config.PropertyPlaceholderConfigurer {
public void setFileName(String fileName) throws FileNotFoundException {
File file = findPropertyFile(fileName); // implement property file loading
super.setLocation(new FileSystemResource(file));
}
and use it like
<bean id="propertyPlaceholderConfigurer" class="package.CustomPropertyPlaceholderConfigurer ">
<property name="placeholderPrefix" value="${"/>
<property name="ignoreResourceNotFound" value="true"/>
<property name="systemPropertiesMode" value="2"/>
</bean>
As far as I understand there are no such files.
Try to add configs below to the propertyPlaceholderConfigurer
<property name="ignoreUnresolvablePlaceholders" value="true" />
<property name="ignoreResourceNotFound" value="true" />
I found that the message was originating in PropertiesLoaderSupport
at the loadProperties method:
if (this.ignoreResourceNotFound) {
if (logger.isWarnEnabled()) {
logger.warn("Could not load properties from " + location + ": " + ex.getMessage());
}
}
So what I ended up doing is set the logger level in my gradle task to Error as follows:
task.doFirst {
logger.context.level = LogLevel.ERROR
logging.captureStandardOutput LogLevel.ERROR
}

Socks proxy failure with camel-box component

I've got the camel-box component working well via the corporate web proxy but it is very slow. I tried using the socks proxy instead, which works well for me for sftp transfers but it fails with almost no useful error message. Just an exception which I'll dig out and add to the question but didn't tell me anything. Can anyone spot anything wrong with my proxy configuration? This is equivalent to my applicationContext.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:amq="http://activemq.apache.org/schema/core" xmlns:camel="http://camel.apache.org/schema/spring" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core.xsd http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd ">
<camelContext xmlns="http://camel.apache.org/schema/spring" id="boxtest">
<route id="getids">
<description>Fetch the names and Box unique ids for the root folder</description>
<from uri="jetty:http://0.0.0.0:/info"/>
<to uri="box://folders/getFolderItems?folderId=0&pagingRequest=#pr"/>
<to uri="bean:dump"/>
</route>
</camelContext>
<bean class="org.apache.camel.component.box.BoxComponent" id="box">
<property name="configuration" ref="cfg"/>
</bean>
<bean class="org.apache.camel.component.box.BoxConfiguration" id="cfg">
<property name="httpParams">
<map>
<entry key="http.route.socks-proxy" value="true"/>
<entry key="http.route.default-proxy">
<bean class="org.apache.http.HttpHost" id="proxy">
<constructor-arg index="0" value="my-socks-proxy"/>
<constructor-arg index="1" value="1085"/>
</bean>
</entry>
</map>
</property>
<property name="userName" value="j#b.com"/>
<property name="clientId" value="a83445cd422dbfc62ba9"/>
<property name="clientSecret" value="1701df702c00126783fc1701df702c00126783fc"/>
<property name="authSecureStorage" ref="auth"/>
</bean>
<bean id="auth" class="mypackage.Auth">
<property name="filename" value="box_credentials.properties"/>
</bean>
<bean id="pr" class="com.box.boxjavalibv2.requests.requestobjects.BoxPagingRequestObject"/>
</beans>
here is the exception: http://pastebin.com/GpBeHJiD
So to formally answer my question, the error in the config is that the socks parameter needs to be boolean as follows:
<entry key="http.route.socks-proxy">
<value type="java.lang.Boolean">true</value>
</entry>
But the real problem is that camel-box in Camel 2.14.1 only has socks support for the login process not for the restful client.
I've submitted a patch for this.
https://issues.apache.org/jira/browse/CAMEL-8272

Cannot resolve property `paramName` in Spring MVC

I need to set Spring MVC interseptors to catch url parameter language and accordingly get data from .properties file. Getting error Cannot resolve propertyparamName when configuring context in servlet-config.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<mvc:annotation-driven/>
<mvc:resources mapping="/pdfs" location="pdfs"/>
<context:component-scan base-package="com.ttu.cs.controller"/>
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource" p:basename="messages" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" />
<bean id="LocaleResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver" p:defaultLocale="en"/>
<!-- Declare the Interceptor -->
<mvc:interceptors>
<bean class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
<property name="paramName" value="language"/>
</bean>
</mvc:interceptors>
</beans>
I think you meant to use org.springframework.web.servlet.i18n.LocaleChangeInterceptor rather than SessionLocaleResolver.
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="language"/>
</bean>
This class is a HandlerInterceptor and does have a paramName property.
SessionLocaleResolver, on the other hand, is not.
Not sure what you expect but SessionLocaleResolver doesn't have such a param. Maybe you meant locale?

how can I use util:list in spring configuration file

I am trying to use spring's unmarshaller to automatically unmarshal request body from xml to an object. But I failed getting the error util prfeix unbind. Could you tell me how can I use util:list and where can I find reference for util:list.
I did spot that the official documentation leave out a closing > in </property and I correct that in my configuration.
I find configuration far more frustrating that just write the code.
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<util:list id="beanList">
<ref bean="stringHttpMessageConverter"/>
<ref bean="marshallingHttpMessageConverter"/>
</util:list>
</property
</bean>
<bean id="stringHttpMessageConverter"
class="org.springframework.http.converter.StringHttpMessageConverter"/>
<bean id="marshallingHttpMessageConverter"
class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
<property name="marshaller" ref="castorMarshaller" />
<property name="unmarshaller" ref="castorMarshaller" />
</bean>
<bean id="castorMarshaller" class="org.springframework.oxm.castor.CastorMarshaller"/>
You need to declare to util namespace; e.g.:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">

Getting the present working directory using spring

This is the code to get the present working directory of a java application at runtime.
String currentWorkingDirectory = System.getProperty("user.dir")+System.getProperty("file.separator");
Is there any way by which this can be configured using the spring-context xml.
For ex:
<bean id="csvReportGenerator" class="some.path.CSVReportGenerator">
<constructor-arg name="outputFileName" value="${currentWorkingDirectory}/${reportOutputFileGeneric}"/>
</bean>
Yes, you can do it using Spring expressions. See section 6.4.1 of this article
<property name="userDir" value="#{ systemProperties['user.dir'] }"/>
<property name="fileSep" value="#{ systemProperties['file.separator'] }"/>
You can simply use classpath: or can use ./ if you are deploying in an unix environment(which usually is). Say, classpath:sample.properties or ./sample.properties
In spring-context.xml You can use
1) classpath:filename.properties or
2) ./filename.properties
3) file:./
For current dir of context-xml, ./ should work, but for working dir, file:./ works fine.
eg.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<context:annotation-config />
<bean id="properties"
class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="singleton" value="true" />
<property name="ignoreResourceNotFound" value="true" />
<property name="locations">
<list>
<value>classpath:/shaharma.properties</value>
<value>./shaharma-custom.properties</value>
</list>
</property>
</bean>
</beans>

Categories