Alfresco Share: Retrieve config data from Java - share

I'd like to access share-config-custom.xml data from a Java bean in the Share webapp.
What is the equivalent to the following javascript syntax (which access the config root object) but in a Java context:
config.scoped['RepositoryLibrary']['root-node']
Is the share-config-custom translated to a bean itself? Or is there an API to read it from Java?

First, there is no "reasonable" way to use this API "looking" at one XML config file. In gen eral, the ConfigService creates a configuration merging from various sources. But looking directly at the XML should not be needed anyways.
That being said, the Javascript object config actually is a org.springframework.extensions.webscripts.ScriptConfigModel.
To get something equivalent in Java get yourself a reference to the ConfigService. To obtain the reference, let spring inject it in your custom bean:
<property name="configService" ref="web.config" />
Calling configService.getGlobalConfig() should get you the equivalent of config.scoped.

I know this is already answered, but Andreas' answer only got me halfway there. The configuration class is actually the XMLConfigService now.
Here's a code snippet that worked with Enterprise 4.1.* Alfresco:
Java class
import org.springframework.extensions.config.xml.XMLConfigService;
public class PDFValidate extends BaseJavaDelegate implements ExecutionListener
{
protected XMLConfigService configService;
public void setConfigService( XMLConfigService scriptConfigModel )
{
this.configService = scriptConfigModel;
}
Bean registration:
<bean id="AbstractWorkflowDelegate" parent="baseJavaDelegate" abstract="true" depends-on="activitiBeanRegistry" />
<bean id="PDFValidate" parent="AbstractWorkflowDelegate" class="com.epnet.alfresco.metadata.listener.PDFValidate">
<property name="repository" ref="repositoryHelper" />
<property name="configService" ref="web.config" />
</bean>
And from there, you can use the configService in your java code to get the config values.
The XMLConfigService is located in the spring-surf-core-configservice-1.2.0-SNAPSHOT.jar for my version of Alfresco.

Related

MongoTemplate - get the connection URI

We are injecting the mongotemplate using the spring config
<mongo:mongo-client id="mongoClient" connection-string="${mongodb.connect-string}">
<mongo:client-settings read-preference="PRIMARY_PREFERRED" retry-writes="true" />
</mongo:mongo-client>
<mongo:db-factory id="mongoDbFactory" dbname="${mongodb.database}" mongo-client-ref="mongoClient" />
<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
<constructor-arg name="mongoDbFactory" ref="mongoDbFactory"/>
</bean>
#Inject
private MongoTemplate mongoTemplate;
Now we are creating a separate integration checkpoint to ensure the mongo db is available and show a green or red traffic light. Is it possible to get the connection URI from the instantiated mongotemplate or I should use ${mongodb.connect-string} to build the integration point?
In the older version of mongotemplate (springDataMongoDb : 1.8.2) we were able to use the following method to get the address
ServerAddress address = mongoTemplate.getDb().getMongo().getAddress();
but with spring 3.4.6 it is not possible to get the address using the above.
I would suggest using ${mongodb.connect-string} as you suggested since this is much more straight forward and doesn't require you to have to wire in the mongo template just to return a constant value when that is the exact propose of spring configs via the #Value annotation.
But a better solution to the green/red for mongo connectivity is using spring actuator which already has this functionality built in.
https://reflectoring.io/spring-boot-health-check/

TestContext always Null

Is there any way I can inject TestContext in my cucumber step class?
I am using citrus, spring, and cucumber together with the latest version. But when I use the below injection I always get null pointer exception of the TestContext. For TestDesigner and TestRunner I have no issue to get.
#CitrusResource private TestContext tContext;
and in the log i am seeing
Failed to get proper TestContext from Cucumber Spring application context: No qualifying bean of type 'com.consol.citrus.context.TestContext' available
You are obviously using the setting
cucumber.api.java.ObjectFactory=cucumber.runtime.java.spring.CitrusSpringObjectFactory
in the cucumber.properties.
When doing so you need to manually add the Citrus Spring configuration with #ContextConfiguration annotation on your steps class.
#ContextConfiguration(classes = CitrusSpringConfig.class)
public class MySteps {
#CitrusResource
private TestDesigner designer;
[...]
}
In case you are using the default cucumber.xml Spring XML application context you need to add the Citrus Spring config as bean to that file:
<!-- JavaConfig bean post-processor -->
<bean class="org.springframework.context.annotation.ConfigurationClassPostProcessor"/>
<!-- Citrus Java config -->
<bean id="citrusSpringConfig" class="com.consol.citrus.config.CitrusSpringConfig"/>

How to configure Bean class in root-context.xml file using STS in Spring?

I am new to Spring and going to develop a spring MVC Application.What is the best way to write root-context.xml for Bean class properties?
For database Connection I want to use Spring jdbc(JdbcTemplate).Can you please suggest me the best way to do so?
You're going to need to set up a DataSource and then create a JdbcTemplate bean that utilizes it. The Spring JDBC Reference Documentation provides examples and really good explanations on how to accomplish this.
Here's a basic bean definition for a datasource. The properties specified will depend on the database that you are using.
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
Then you can create a jdbcTemplate bean that uses the dataSource or you can instantiate the jdbcTemplate within your code.
The JdbcTemplate API mentions this:
Can be used within a service implementation via direct instantiation with a DataSource reference, or get prepared in an application context and given to services as bean reference. Note: The DataSource should always be configured as a bean in the application context, in the first case given to the service directly, in the second case to the prepared template.

Order of execution of RequestHandlers configured in Apache CXF and JAX-RS

I am in process of implementing a REST API server using Apache CXF JAX-RS v(2.30). I am using spring as container. I am thinking of making use of org.apache.cxf.jaxrs.ext.RequestHandler to implement few features like license check, authentication, authorization (All of which has custom code). My idea is to segregate this code in individual implementation classes (implementing RequestHandler) and configure it for a base REST url something like /rest/*. Being new to Apache CXF and JAX-RS, I want to understand following things.
Is this approach the right way to implement the features I want to?
If yes, then is the order in which the RequestHandlers are declared is the order of their invocation?
For example if in my definition I declare:
<beans>
<jaxrs:server id="abcRestService" address="/rest">
<jaxrs:serviceBeans>
<bean class="com.abc.api.rest.service.FooService" />
</jaxrs:serviceBeans>
<jaxrs:providers>
<ref bean="licenseFilter" />
<ref bean="authorizationFilter" />
</jaxrs:providers>
</jaxrs:server>
<bean id="licenseFilter" class="com.abc.api.rest.providers.LicenseValidator">
<!-- License check bean properties -->
</bean>
<bean id="authorizationFilter" class="com.abc.api.rest.providers.AuthorizationFilter">
<!-- authorization bean properties -->
</bean>
</beans>
then will the licenseFilter always get invoked before authorizationFilter?
I did not find a mention of invocation ordering of RequestHandlers as well as ResponseHandlers.
Thanks in advance.
Figured this out.
It gets invoked in the order of declaration of beans in <jaxrs:providers>. Thus in case mentioned in question, licenseFilter will get invoked before authorizationFilter.

Spring: problem with setter Dependence Injection

I have the following situation:
I set the beans (in applicationContext.xml) as follows:
...
<bean id="bDaoImpl" class="BDaoImpl"></bean>
<bean id="injBInA" class="ADaoImpl">
<property name="b" ref="bDaoImpl"/>
</bean>
...
Why when I use "b" in the value of the Object class ADaoImpl is null?
I have the solution. Sorry. Basically when I load the bean from application context (ApplicationContext.getBean) does not load the bean with id = "injBInA" therefore right was never carried out the injection