Handling multiple Rest web services using Spring and Castor - web-services

Would anyone be able to advise me on the best way of handling access to multiple Rest web services using Springs RestTemplate?
I know that the RestTemplate object has a message converter reference (MarshallingHttpMessageConverter) which in turn has a reference to an unmarshaller. In my case I am using the Spring Frameworks CastorMarshaller object with associated mapping file.
Normally I could have just added all my mappings to one Castor mapping file. However in my case all the web services are of this format (block below) with the < rows ... /> holding different entities depending on the service called.
<data>
<output>
<dataset>
<row id="" .... />
<row id="" .... />
<row id="" .... />
<row id="" .... />
<row id="" .... />
</dataset>
</output>
<nextUpdate><nextUpdate/>
</data>
The CastorMarshaller is injected into the MessageConverter which itself is injected into the RestTemplate in the application context configuration file.
<bean id="restTemplate" class="org.springframework.web.client.RestTemplate">
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
<property name="marshaller" ref="castorMarshaller"/>
<property name="unmarshaller" ref="castorMarshaller"/>
<property name="supportedMediaTypes">
<list>
<bean class="org.springframework.http.MediaType">
<constructor-arg index="0" value="application"/>
<constructor-arg index="1" value="xml"/>
</bean>
<bean class="org.springframework.http.MediaType">
<constructor-arg index="0" value="text"/>
<constructor-arg index="1" value="xml"/>
</bean>
</list>
</property>
</bean>
</list>
</property>
</bean>
<bean id="castorMarshaller" class="org.springframework.oxm.castor.CastorMarshaller">
<property name="mappingLocation" value="classpath:oxm-mapping-worldweather.xml"/>
</bean>
Possible options that I have been thinking about:
1 Create multiple RestTemplates for each Rest service.
2 Create multiple MessageConverters for the different services and change the message converters on the template when accessing a different service.
3 Create multiple CasterMarshaller objects for the different services and update the message converter with the new unmarshaller
What is the best way to approach handling multiple services like this with the same root and sub elements?
Thanks in advance
Sman UK

If specifying multiple mapping files is the issue then below is the solution.
Use mappingLocations property instead of mappingLocation as given below,
<bean id="castorMarshaller" class="org.springframework.oxm.castor.CastorMarshaller">
<property name="mappingLocations">
<list>
<value>classpath:oxm-mapping-worldweather.xml</value>
<value>classpath:sample-mapping.xml</value>
</list>
</property>
</bean>

Related

Is it possible to set expiry time using Apache Ignite in C++?

I am using the C++ thin client API and I want to have the data deleted from the cache after 5 minutes. I am connecting to ignite through docker and using the persistence storage. In the documentation for the C++ libraries, I cannot find anything relating to "expiry" and I tried adding the expiry option into the config xml file that my docker container reads in, but that didn't seem to work either. I put data into the cache and checked for the data after 5 minutes (I also checked 10, 20, 30 minutes later) and the data was still there.
Here is my config xml file:
<?xml version="1.0" encoding="UTF-8"?>
<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 id="ignite.cfg" class="org.apache.ignite.configuration.IgniteConfiguration">
<!-- Enabling Apache Ignite Persistent Store. -->
<property name="dataStorageConfiguration">
<bean class="org.apache.ignite.configuration.DataStorageConfiguration">
<property name="defaultDataRegionConfiguration">
<bean class="org.apache.ignite.configuration.DataRegionConfiguration">
<property name="persistenceEnabled" value="true"/>
<property name="name" value="Default_Region" />
</bean>
</property>
</bean>
</property>
<property name="discoverySpi">
<bean class="org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi">
<property name="ipFinder">
<!-- Uncomment static IP finder to enable static-based discovery of initial nodes. -->
<!--<bean class="org.apache.ignite.spi.discovery.tcp.ipfinder.vm.TcpDiscoveryVmIpFinder">-->
<bean class="org.apache.ignite.spi.discovery.tcp.ipfinder.multicast.TcpDiscoveryMulticastIpFinder">
<property name="addresses">
<list>
<!-- In distributed environment, replace with actual host IP address. -->
<value>127.0.0.1:47500..47502</value>
</list>
</property>
</bean>
</property>
</bean>
</property>
</bean>
<bean class="org.apache.ignite.configuration.CacheConfiguration">
<property name="expiryPolicyFactory">
<bean class="javax.cache.expiry.CreatedExpiryPolicy" factory-method="factoryOf">
<constructor-arg>
<bean class="javax.cache.expiry.Duration">
<constructor-arg value="MINUTES"/>
<constructor-arg value="5"/>
</bean>
</constructor-arg>
</bean>
</property>
</bean>
</beans>
Yes, the c++ thin client doesn't support this feature at the moment.
I think you could either define a cache with expiration completely on the server-side or to define only a cache template https://apacheignite.readme.io/docs/cache-template with expiry policy and use it from a client.

It's possible to custom the mapper for ClassData and ClassWsDTO - Hybris - YCommerceWebServices

I have an object in my data class and I want only a specific attribute of this object in the WsDTO class.
Declaration of custom B2BUnitData
<bean class="de.hybris.platform.b2bcommercefacades.company.data.B2BUnitData">
<property name="PointOfServiceData"
type="de.hybris.platform.commercefacades.storelocator.data.PointOfServiceData"/>
</bean>
Declaration of B2bUnitWsDTO
<bean class="de.hybris.platform.b2boccaddon.dto.b2bunit.B2bUnitWsDTO">
<property name="PointOfServiceData" type="PointOfServiceWsDTO" />
</bean>
file : dto-level-mappings-v2-spring.xml
<bean parent="fieldSetLevelMapping" id="b2bunitWsDTOFieldSetLevelMapping">
<property name="dtoClass"
value="de.hybris.platform.b2boccaddon.dto.pricerow.B2bUnitWsDTO"/>
<property name="levelMapping">
<map>
<entry key="FULL" value="PointOfServiceData" />
</map>
</property>
</bean>
this implementation give me all the object pointOfService but I only want the UID attribute in the B2bUnitWsDTO
The only solution I know, will be to create a PointOfServiceUID attribute in the data and map it directly in the b2bunitWsDTOFieldSetLevelMapping bean.
I would know if it's possible to map in the dto-level-mappings-v2-spring.xml only one attribute of my object :
Exemple :
Or if it exist some solution to do that
As you already mentioned, you could change the dto-level-mappings-v2-spring.xml so that for all levels (BASIC, DEFAULT, FULL) only the uid is returned.
<bean parent="fieldSetLevelMapping" id="b2bunitWsDTOFieldSetLevelMapping">
<property name="dtoClass"
value="de.hybris.platform.b2boccaddon.dto.pricerow.B2bUnitWsDTO"/>
<property name="levelMapping">
<map>
<entry key="BASIC" value="PointOfServiceData(uid)" />
<entry key="DEFAULT" value="PointOfServiceData(uid)" />
<entry key="FULL" value="PointOfServiceData(uid)" />
</map>
</property>
</bean>
Beware, the fieldSetLevelMapping beans only define how your response looks like!
If you want to change how a B2BUnitData is mapped to a B2bUnitWsDTO, you have to define a custom field mapper (you can find examples in dto-mappings-v2-spring.xml)
Assuming your B2bUnitWsDTO now only has pointOfServiceUID as property, this may look like this (disclaimer: you need to test this):
<bean id="b2bUnitFieldMapper" parent="fieldMapper">
<property name="sourceClass"
value="de.hybris.platform.b2bcommercefacades.company.data.B2BUnitData"/>
<property name="destClass"
value="com.customer.some.package.B2bUnitWsDTO"/>
<property name="fieldMapping">
<map>
<entry key="PointOfServiceData.uid" value="pointOfServiceUID"/>
</map>
</property>
</bean>
Here is a good documentation entry point regarding Field Mappings and Field Level Definitions:
https://help.hybris.com/1808/hcd/8c404c5886691014a48c88f4a49f9bf3.html

CXF Transform feature

I am new to CXF,I have a requirement to drop few tags from the input XML .so I am using CXF Transform feature ,which should drop the version tag from my input XML ,I am able change but not drop. Kindly let me know how can I achieve it
<bean id="transformFeature" class="org.`enter code here`apache`enter code here`.cxf.feature.StaxTransformFeature">
<property name="inTransformElem`enter code here`ents">
<map>
<entry key="version" value=""/>
</map>
</property>
</bean>
You need to specify the namespace of the element. For example, if the version element has a namespace of http://www.example.org/test, you would need to configure the CXF transformation feature as follows:
<bean id="transformFeature" class="org.apache.cxf.feature.StaxTransformFeature">
<property name="inTransformElements">
<map>
<entry key="{http://www.example.org/test}version value=""/>
</map>
</property>
</bean>
You also need to add the feature to your jaxws:endpoint configuration, if you have not already done so.
<jaxws:endpoint ...>
<jaxws:features>
<ref bean="transformFeature" />
</jaxws:features>
</jaxws:endpoint>

Stateless Spring Security usage

I need your help with stateless Spring Security. I wrote service that authorize user, my security.xml:
<http use-expressions="true" create-session="stateless" entry-point-ref="restAuthenticationEntryPoint">
<intercept-url pattern="/auth/**" access="permitAll" />
<intercept-url pattern="/**" access="isAuthenticated()" />
<custom-filter ref="myFilter" position="FORM_LOGIN_FILTER"/>
</http>
<beans:bean id="myFilter" class="org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter">
<beans:property name="authenticationManager" ref="authenticationManager"/>
</beans:bean>
<authentication-manager alias="authenticationManager">
<authentication-provider user-service-ref="userDetailsService" />
</authentication-manager>
It hasn't state, thats why after my authentication, when I want get anything via another URL, it takes me 401 Unauthorized. I heard about token but I don't know how achieve this.
Maximus,
This is what I did in similar scenario:
Used OAuth - http://oauth.net/
There are multiple libraries that implement OAuth specifications
http://tools.ietf.org/html/rfc6749
Spring has an implementation that is easy to configure. There are two sample applications (server and client) from Spring for this. Tutorials are available at:
https://github.com/SpringSource/spring-security-oauth/wiki/tutorial
Sample working config:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:oauth="http://www.springframework.org/schema/security/oauth2"
xmlns:ss="http://www.springframework.org/schema/security" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:tx="http://www.springframework.org/schema/tx"
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.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.1.xsd
http://www.springframework.org/schema/security/oauth2
http://www.springframework.org/schema/security/spring-security-oauth2-1.0.xsd
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd
">
<ss:http pattern="/test/customer/**" create-session="stateless"
access-decision-manager-ref="accessDecisionManager"
entry-point-ref="oauthAuthenticationEntryPoint"
xmlns="http://www.springframework.org/schema/security">
<ss:anonymous enabled="false" />
<ss:intercept-url pattern="/test/customer/welcome*"
access="ROLE_USER" />
<ss:custom-filter ref="resourceServerFilter" before="PRE_AUTH_FILTER" />
<ss:access-denied-handler ref="oauth2AccessDeniedHandler" />
</ss:http>
<ss:http pattern="/oauth/token" create-session="stateless"
entry-point-ref="oauthAuthenticationEntryPoint"
authentication-manager-ref="authenticationManager">
<ss:intercept-url pattern="/oauth/token" access="IS_AUTHENTICATED_FULLY" />
<ss:anonymous enabled="false" />
<ss:custom-filter ref="clientCredentialsTokenEndpointFilter"
before="BASIC_AUTH_FILTER" />
<ss:access-denied-handler ref="oauth2AccessDeniedHandler" />
</ss:http>
<ss:authentication-manager alias="authenticationManager">
<ss:authentication-provider ref="myAuthenticationProvider" />
</ss:authentication-manager>
<oauth:resource-server id="resourceServerFilter" token-services-ref="tokenServices" />
<bean id="myAuthenticationProvider" class="com.sachin.test.ws.user.MyUserAuthenticationProvider" />
<bean id="oauthAuthenticationEntryPoint"
class="org.springframework.security.oauth2.provider.error.OAuth2AuthenticationEntryPoint">
<property name="realmName" value="myCustomerAppRealm" />
</bean>
<bean id="clientDetailsUserService"
class="org.springframework.security.oauth2.provider.client.ClientDetailsUserDetailsService">
<constructor-arg ref="clientDetailsService" />
</bean>
<oauth:authorization-server
client-details-service-ref="clientDetailsService" token-services-ref="tokenServices">
<oauth:authorization-code />
<oauth:implicit />
<oauth:refresh-token />
<oauth:client-credentials />
<oauth:password />
</oauth:authorization-server>
<oauth:client-details-service id="clientDetailsService">
<oauth:client client-id="admin"
authorized-grant-types="password,authorization_code,refresh_token,implicit,client_credentials"
authorities="ROLE_USER, ROLE_TRUSTED_CLIENT" scope="read,write,trust"
access-token-validity="60" />
</oauth:client-details-service>
<bean id="oauth2AccessDeniedHandler"
class="org.springframework.security.web.access.AccessDeniedHandlerImpl" />
<bean id="clientCredentialsTokenEndpointFilter"
class="org.springframework.security.oauth2.provider.client.ClientCredentialsTokenEndpointFilter">
<property name="authenticationManager" ref="authenticationManager" />
</bean>
<bean id="accessDecisionManager" class="org.springframework.security.access.vote.UnanimousBased"
xmlns="http://www.springframework.org/schema/beans">
<constructor-arg>
<list>
<bean class="org.springframework.security.oauth2.provider.vote.ScopeVoter" />
<bean class="org.springframework.security.access.vote.RoleVoter" />
<bean class="org.springframework.security.access.vote.AuthenticatedVoter" />
</list>
</constructor-arg>
</bean>
<bean id="tokenStore"
class="org.springframework.security.oauth2.provider.token.InMemoryTokenStore" />
<bean id="tokenServices"
class="org.springframework.security.oauth2.provider.token.DefaultTokenServices">
<property name="tokenStore" ref="tokenStore" />
<property name="supportRefreshToken" value="true" />
</bean>
</beans>
Add this to web.xml:
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
You'll need to read the specs for OAuth and Spring security to understand what I did. You may extend this code to use your DB for authentication and token sharing across multiple servers.
Hope this helps.

Apache CXF + resource handler with embedded jetty in osgi with spring dm

I'm trying to run an apache cxf endpoint in an equinox osgi environment with jetty 7. I need the endpoint to be on address http://x.x.x.x:8080/ws/endpoint1 and have static resources on the root path http://x.x.x.x:8080/*.
I have a dedicated bundle for this purpose containing the cxf libraries. Spring dynamic modules are part of my target platform.
After some research I tried to start the jetty webserver in my spring application context.
<bean id="Server" class="org.eclipse.jetty.server.Server"
init-method="start" destroy-method="stop">
<property name="connectors">
<list>
<bean id="Connector" class="org.eclipse.jetty.server.nio.SelectChannelConnector">
<property name="port" value="8080" />
</bean>
</list>
</property>
<property name="handler">
<bean id="handlers" class="org.eclipse.jetty.server.handler.HandlerList">
<property name="handlers">
<list>
<bean class="org.eclipse.jetty.server.handler.ResourceHandler">
<property name="directoriesListed" value="true" />
<property name="welcomeFiles">
<list>
<value>index.html</value>
</list>
</property>
<property name="resourceBase" value="./someDir" />
</bean>
<ref bean="web-service-cxf" />
<bean class="org.eclipse.jetty.server.handler.DefaultHandler" />
</list>
</property>
</bean>
</property>
</bean>
<bean name="web-service-cxf" class="org.eclipse.jetty.servlet.ServletContextHandler">
<property name="contextPath" value="/ws" />
<property name="handler">
<bean class="org.eclipse.jetty.servlet.ServletHandler">
<property name="servlets">
<list>
<bean class="org.eclipse.jetty.servlet.ServletHolder">
<property name="name" value="cxf-servlet-holder" />
<property name="servlet">
<bean class="org.apache.cxf.transport.servlet.CXFServlet">
</bean>
</property>
</bean>
</list>
</property>
<property name="servletMappings">
<list>
<bean class="org.eclipse.jetty.servlet.ServletMapping">
<property name="servletName" value="cxf-servlet-holder" />
<property name="pathSpec" value="/*" />
</bean>
</list>
</property>
</bean>
</property>
</bean>
My WebService Endpoint is declared with:
<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
<bean id="someService" class="abc.xyz.SomeClass" />
<jaxws:endpoint id="endpointId" implementor="#someBean"
address="/endpoint1">
</jaxws:endpoint>
Unfortunatly this is not working. I can reach the static resources, but not the webservice.
The log says, the WebService is published under address /endpoint1. No warnings, no exceptions.
When I change the address of the webservice to its full url
<bean id="someService" class="abc.xyz.SomeClass" />
<jaxws:endpoint id="endpointId" implementor="#someBean"
address="http://x.x.x.x:8080/ws/endpoint1">
</jaxws:endpoint>
the webservice works fine, but the static ressources are not available any more.
Is it possible with a configuration like this to publish an endpoint to a running jetty with relative address? Or am I totally wrong? Most examples I found on the web use a web.xml, but I'm not working with an application server like tomcat and need the application to be a standalone eclipse app.
Spend the whole last two nights on this, any help is highly appreciated.
Kind regards,
Onno
There are sooo many samples here. U shud be able to find what u r looking for
http://svn.apache.org/repos/asf/cxf/branches/2.4.x-fixes/distribution/src/main/release/samples