Apache CXF SOAP JAXB issue on WebLogic 12c - spring-boot

We are using Java 8, Apache CXF as a SOAP client on top of Spring Boot to send SOAP messages to WS.
If the app is deployed as a WAR on Tomcat 8, the app works well and the SOAP client is sending the right XML messages with the right namespaces.
If the same app WAR is deployed on Weblogic 12c the SOAP message that is produced by the CXF SOAP client has missing namespaces.
We know that the WebLogic maybe uses some old JAXB jars that are responsible for creating the XML message from Java objects and they are different then the Tomcat server and this maybe the reason why we are seeing this issue.
We also know that we can specify in the weblogic.xml in the war file what jars the Weblogic needs to load from the war and what dependencies to load from directly from the Weblogic libraries, but every combination that we tried in the weblogic.xml does not work.
Any good advice will be fully appreciated
Sample XML output from Tomcat server with Apache CXF
<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
<env:Header/>
<env:Body>
<event xmlns="http://www.test.com" xmlns:ns5="http://www.test2.com" xmlns:ns3="urn:test1:1423.15465:123123:namespace">
<ns5:created-date-time>2020-08-12T08:02:35Z</ns5:created-date-time>
<ns5:payload>
<Test2>
<ns3:ID>f14bb</ns3:ID>
<ns3:createdDateTime>2020-08-12T08:02:35Z</ns3:createdDateTime>
</Test2>
</ns5:payload>
</event>
</env:Body>
</env:Envelope>
Sample code from Weblogic 12c
<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
<env:Header/>
<env:Body>
<event xmlns="http://www.test.com" xmlns:ns5="http://www.test2.com">
<ns5:created-date-time>2020-08-12T08:02:35Z</ns5:created-date-time>
<ns5:payload>
<Test2>
<ID>f14bb</ID>
<createdDateTime>2020-08-12T08:02:35Z</createdDateTime>
</Test2>
</ns5:payload>
</event>
</env:Body>
</env:Envelope>
The "urn:test1:1423.15465:123123:namespace" is completely ignored in the weblogic server making this XML message not valid by the consumer
weblogic.xml
we are trying to tell weblogic to load our classes from the war file instead of the JaxB classes from the web logic but without success
<wls:container-descriptor>
<wls:prefer-application-packages>
<wls:package-name>java.xml.bind.*</wls:package-name>
<wls:package-name>org.apache.cxf.*</wls:package-name>
<wls:package-name>javax.xml.ws.*</wls:package-name>
<wls:package-name>javax.wsdl.*</wls:package-name>
</wls:prefer-application-resources>
</wls:container-descriptor>
Except this issue everything else is working fine, the Apache CXF is sending correct in multiple scenarios, just in one is it not adding the namespace we need

It looks like you are describing a class loading problem here. Thus, kindly use the below tag in your weblogic.xml descriptor.
<prefer-web-inf-classes>false</prefer-web-inf-classes>
Some years ago I was struggling with class loading issues because I was missing it. Below you have an example about this extracted from this blog.
If after applying this you still are facing issues with the class loader, you should install Classloader Analysis Tool (CAT) to get the class loader, which is loading the conflicting classes. In this blog you will have some instructions about how to use CAT.
Importantly, in this document Oracle states about this
Note that in order to use prefer-application-packages or prefer-application-resources, prefer-web-inf-classes must be set to false.

This issue was solved by updating the package-info.java
#javax.xml.bind.annotation.XmlSchema(namespace = "urn:test1",
xmlns = {#XmlNs(prefix = "",
namespaceURI = "http://www.test.com")},
elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
This is the part of the package-info that was not been there before
xmlns = {#XmlNs(prefix = "",
namespaceURI = "http://www.test.com")}
adding the namespace trick JavaXB to add the original namespace

Please check the mentioned page, which has a different Filtering classloading:
<wls:prefer-application-packages>
<wls:package-name>com.ctc.wstx.*</wls:package-name>
<wls:package-name>javax.wsdl.*</wls:package-name>
<wls:package-name>org.apache.cxf.*</wls:package-name>
<!-- <wls:package-name>javax.jws.*</wls:package-name> -->
</wls:prefer-application-packages>

Related

LegacyCookieProcessor in standalone Tomcat and Spring Boot [duplicate]

My code is working on tomcat 8 version 8.0.33 but on 8.5.4 i get :
An invalid domain [.mydomain] was specified for this cookie.
I have found that Rfc6265CookieProcessor is introduced in tomcat 8 latest versions.
It says on official doc that this can be reverted to LegacyCookieProcessor in context.xml but i don't know how.
Please let me know how to do this.
Thanks
You can try in context.xml
<CookieProcessor className="org.apache.tomcat.util.http.LegacyCookieProcessor" />
reference:
https://tomcat.apache.org/tomcat-8.0-doc/config/cookie-processor.html
Case 1: You are using Standalone Tomcat & have access to change files in tomcat server
Please follow answer by #linzkl
Case 2: You are using Standalone Tomcat but you don't have access to change files in tomcat server
Create a new file called context.xml under src/main/webapp/META-INF folder in your application & paste the content given below
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<WatchedResource>WEB-INF/tomcat-web.xml</WatchedResource>
<WatchedResource>${catalina.base}/conf/web.xml</WatchedResource>
<CookieProcessor className="org.apache.tomcat.util.http.LegacyCookieProcessor" />
</Context>
When you deploy your application in Standalone Tomcat, the context.xml file you placed under META-INF folder will override the context.xml file given in tomcat/conf/context.xml
Note: If you are following this solution, you have to do it for every single application because META-INF/context.xml is application specific
Case 3: You are using Embedded Tomcat
Create a new bean for WebServerFactoryCustomizer
#Bean
WebServerFactoryCustomizer<TomcatServletWebServerFactory> cookieProcessorCustomizer() {
return new WebServerFactoryCustomizer<TomcatServletWebServerFactory>() {
#Override
void customize(TomcatServletWebServerFactory tomcatServletWebServerFactory) {
tomcatServletWebServerFactory.addContextCustomizers(new TomcatContextCustomizer() {
#Override
public void customize(Context context) {
context.setCookieProcessor(new LegacyCookieProcessor());
}
});
}
};
}
Enabling the LegacyCookieProcessor which is used in previous versions of Tomcat has solved the problem in my application. As linzkl mentioned this is explained in Apache's website https://tomcat.apache.org/tomcat-8.0-doc/config/cookie-processor.html.
The reason is that the new version of Tomcat does not understand the . (dot) in front of the domain name of the Cookie being used.
Also, make sure to check this post when you are using Internet Explorer. Apparently, it's very likely to break.
You can find context.xml in the following path.
tomcat8/conf/context.xml
<?xml version="1.0" encoding="UTF-8”?>
<!-- The contents of this file will be loaded for each web application —>
<Context>
<!-- Default set of monitored resources. If one of these changes, the -->
<!-- web application will be reloaded. -->
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<WatchedResource>${catalina.base}/conf/web.xml</WatchedResource>
<!-- Uncomment this to disable session persistence across Tomcat restarts -->
<!-- <Manager pathname="" /> -->
<CookieProcessor className="org.apache.tomcat.util.http.LegacyCookieProcessor"/>
</Context>
The problem is still with Tomcat9. Same process need to follow for Tomcat 9 to set the class.
Add the class in context.xml file.
If you are using eclipse to run the application, need to set in the context.xml file in the server folder. Refer the below screenshot for more reference.
Hope this helps someone.
SameSite issue in tomcat version < 8.5.47 has resolved
In Tomcat 8.5.47 and bellow (Tomcat 8 versions), setting CookieProcessor tag to enable same site (as given bellow) in context.xml does not work due to a bug in Tomcat.
<CookieProcessor className="org.apache.tomcat.util.http.LegacyCookieProcessor" sameSiteCookies="none" />
If you find in this situation where it is not a easy thing to upgrade tomcat immediately (which I faced recently), or if you find any other case where you just need custom processing in cookies; You can write your own CookieProcessor class to get around.
Please find a custom CookieProcessor implementation and details of it's deployment steps here.
In my case I wrote a custom CookieProcessor based on LegacyCookieProcessor source code that allows tomcat 8.5.47 to enable SameSite attribute in cookies.
As mentioned by #atul, this issue persists in Tomcat 9. It will most likely persist moving forward with all future versions of Tomcat, since this is the new standard.
Using the legacy cookie processor (by adding the line above to the context.xml file) is working well for us. However, the true 'fix' is to adjust how your cookie is formed in the first place. This will need to be done in your application, not in Tomcat.
The new cookie processor does not allow the domain to start with a . (dot). Adjusting your cookie (if possible) to start with a value other than that will fix this problem without reverting to the old, legacy cookie processor.
Also, it should be obvious, but I didn't see it mentioned above: after updating the context.xml file, you need to restart the Tomcat service for the change to take effect.
Cheers!

How can I configure custom Serializers / Copiers in XML for JSR-107 (JCache) with Ehcache?

I have a question concerning JCache / Ehcache XML configuration.
I'm using the JSR 107 Cache Annotations CDI Reference Implementation together with Ehcache.
I want to annotate specific methods with #CacheResult, so that the result of these methods will be cached in a cache generated for that specific method.
In our case, these methods have Optional as their return value, and as you probably know, Optional instances are not Serializable and thus cannot be cached by default.
The default used SerializingCopier and PlainJavaSerializer fail on this.
To solve this, I have implemented our own custom OptionalSerializer and OptionalCopier to be able to handle Optional values.
But I am not able to make JCache / Ehcache clear to use my custom Serializer and Copier when it comes around Optional return values.
I tried putting ehcache.xml on the classpath of JBoss EAP 6.4.0 (using JBoss modules) and I tried putting an ehcache.xml in the resources folder of the Maven module, but they are just plainly ignored.
Even if I make those XML's invalid, I don't get any error.
I want to avoid creating a CacheManager, referring to our own ehcache.xml configuration file programmatically, since I would have to create the caches then at that point. I don't want this.
I want the caches to be created by the Cache Annotations CDI CacheResultInterceptor, and I don't seem to have an option to override a method to create a CacheManager of my own...
Does anyone know what I should do to solve this issue?
I just want to use the provided interceptors, and have an XML configuration that specifies that custom serializers and copiers will need to be used for specific return types.
beans.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/beans_1_0.xsd">
<interceptors>
<class>org.jsr107.ri.annotations.cdi.CacheResultInterceptor</class>
<class>org.jsr107.ri.annotations.cdi.CachePutInterceptor</class>
<class>org.jsr107.ri.annotations.cdi.CacheRemoveEntryInterceptor</class>
<class>org.jsr107.ri.annotations.cdi.CacheRemoveAllInterceptor</class>
</interceptors>
</beans>
Example method:
#CacheResult
public Optional<User> findUser(String username) {
return userDao.findUser(user);
}
What version of Ehcache you are currently using;
Ehcache 3.4.0
Paste the configuration for the Cache/CacheManager you have an issue with;
<ehcache:config xmlns:ehcache="http://www.ehcache.org/v3"
xmlns:jcache="http://www.ehcache.org/v3/jsr107">
<ehcache:service>
<jcache:defaults default-template="defaultTemplate"/>
</ehcache:service>
<ehcache:cache-template name="defaultTemplate">
<ehcache:value-type
serializer="be.post.min.client.cache.OptionalSerializer"
copier="be.post.min.client.cache.OptionalCopier">java.util.Optional</ehcache:value-type>
</ehcache:cache-template>
<ehcache:default-serializers>
<ehcache:serializer type="java.util.Optional">be.post.min.client.cache.OptionalSerializer</ehcache:serializer>
</ehcache:default-serializers>
<ehcache:default-copiers>
<ehcache:copier type="java.util.Optional">be.post.min.client.cache.OptionalCopier</ehcache:copier>
</ehcache:default-copiers>
</ehcache:config>
Add any name and version of other library or framework you use Ehcache with (e.g. Hibernate);
Java EE 6
EJB 3.x
CDI 1.x
JCache 1.0.0 (JSR 107)
JSR 107 Cache Annotations CDI RI 1.0.0
Providing JDK and OS versions maybe useful as well.
Java SDK 1.8 update 65
JBoss EAP 6.4.0
Windows 10
From my vague understanding of CDI, putting it in src/main/resources should work. Are you sure it ends up in your jar/war?

.ClassCastException: org.apache.xerces.jaxp.DocumentBuilderFactoryImpl cannot be cast to javax.xml.parsers.DocumentBuilderFactory

I am upgrading Weblogic server from 9 to 10.3.6. when I am trying to deploy my ear application and got below exception.
Caused By: java.lang.ClassCastException: org.apache.xerces.jaxp.DocumentBuilderFactoryImpl cannot be cast to javax.xml.parsers.DocumentBuilderFactory
at javax.xml.parsers.DocumentBuilderFactory.newInstance(DocumentBuilderFactory.java:123)
at org.springframework.beans.factory.xml.DefaultDocumentLoader.createDocumentBuilderFactory(DefaultDocumentLoader.java:89)
at org.springframework.beans.factory.xml.DefaultDocumentLoader.loadDocument(DefaultDocumentLoader.java:70)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:396)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:342)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:310)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:143)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:178)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:149)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:212)
at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:113)
at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:80)
at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:123)
at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:423)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:353)
at org.springframework.context.access.ContextSingletonBeanFactoryLocator.initializeDefinition(ContextSingletonBeanFactoryLocator.java:141)
at org.springframework.beans.factory.access.SingletonBeanFactoryLocator.useBeanFactory(SingletonBeanFactoryLocator.java:384)
at org.springframework.web.context.ContextLoader.loadParentContext(ContextLoader.java:341)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:195)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:45)
at weblogic.servlet.internal.EventsManager$FireContextListenerAction.run(EventsManager.java:481)
at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321)
at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:120)
at weblogic.servlet.internal.EventsManager.notifyContextCreatedEvent(EventsManager.java:181)
at weblogic.servlet.internal.WebAppServletContext.preloadResources(WebAppServletContext.java:1868)
I tried all sorts for things including adding a weblogic-application.xml but it still does not work.
<?xml version="1.0" encoding="UTF-8"?>
<weblogic-application>
<xml>
<parser-factory>
<saxparser-factory>
org.apache.xerces.jaxp.SAXParserFactoryImpl
</saxparser-factory>
<document-builder-factory>
org.apache.xerces.jaxp.DocumentBuilderFactoryImpl
</document-builder-factory>
<transformer-factory>
org.apache.xalan.processor.TransformerFactoryImpl
</transformer-factory>
</parser-factory>
</xml>
<prefer-application-packages>
<package-name>org.apache.xerces.parsers.*</package-name>
</prefer-application-packages>
</weblogic-application>
my weblogic.xml has
<prefer-web-inf-classes>true</prefer-web-inf-classes>
This is part of my pom.xml:
<dependency>
<groupId>xerces</groupId>
<artifactId>xercesImpl</artifactId>
<version>2.8.1</version>
<scope>runtime</scope>
</dependency>
Please help. Thanks!
I answer my own question:
Below link inspired me of fixing this issue:
Dealing with "Xerces hell" in Java/Maven?
Basiclly I have removed all the dependencies of xml-api and xmlParserAPIs in pom.xml. The problem is fixed. The root cause is my classpath should not include any javax libraries which cause the library conflict with Weblogic app server. Hope it helps.
I had the similar problem. I was using an application installed on wildly server. The problem was that I have to place xerces jars in both framework's and third party lib folder of the application. After much research, I found the solution in the documentation of xerces.
Why do I get a ClassCastException when I use Xerces and WebSphere Application Server?
Xerces uses the ObjectFactory class to load some classes dynamically, e.g. the parser configuration. The ObjectFactory finds the specified implementation class by querying the system property, reading META-INF/services/factoryId file or using a fallback classname. After the implementation is found, the ObjectFactory tries to load the file using the context classloader and if it is null, the ObjectFactory uses the system classloader.
If you run Xerces in an environment, such as WebSphere® Application Server, that has multiple classloaders you may get ClassCastExceptions thrown from Xerces because different classloaders might get involved in loading Xerces classes. For example, ClassCastExceptions may occur when utility EAR classes that use Xerces load Xerces classes from WAR modules.
We suggest you read the "Avoiding ClassCastExceptions..." article which explains a workaround for this problem. Also you might want to read the "J2EE Class Loading Demystified" article that explains how multiple classloaders work in WebSphere Application Server.
"
Basically, use two util class to create domparser object. For the service call from your application, change the classloader and create the object. After doing the processing, revert back the classloader.
ClassLoader savedClassLoader = Thread.currentThread().getContextClassLoader();
ClassLoader currentClassLoader = this.getClass().getClassLoader() ;
Thread.currentThread().setContextClassLoader(currentClassLoader);
//do the processing, after that revert back
https://xerces.apache.org/xerces2-j/faq-general.html

OC4J 10.1.3.5 / Spring3 Issue

I'm using OC4J 10.1.3.5.0, and have an issue with the XML namespaces in the Spring XML files supplied in the WAR/EAR.
According to the Oracle documentation, there is a known issue in parsing the Spring 3 XSD files within OC4J, as this embeds the Oracle XMLParserV2 jar and uses this for all XML parsing (which has issues with some XSD tricks used in Spring 3 apparently).
I've folowed the Oracle work-around, defining my own XML parser shared libraries on the OC4J instance, and (in the orion-application.xml), defining the shared library to use. I created a shared library,'apache.xml', with xercesImpl (v 2.9.1), xml-apis (v 1.3.04), and xml-resolver (v 1.2). I tried defining the EAR to use the new library
<imported-shared-libraries>
<imported-shared-library name="apache.xml"/>
</imported-shared-libraries>
I receive the following error
14:50:31 ERROR (UserId:) [DispatcherServlet] Context initialization failed
org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException:
Line 10 in XML document from ServletContext resource [/WEB-INF/spring/webflow-config.xml] is invalid;
nested exception is org.xml.sax.SAXParseException: cvc-complex-type.2.4.c:
The matching wildcard is strict, but no declaration can be found for element 'webflow:flow-executor'.
The webflow-config.xml file is defined as normal:
<?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:webflow="http://www.springframework.org/schema/webflow-config"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/webflow-config
http://www.springframework.org/schema/webflow-config/spring-webflow-config-2.0.xsd">
<!-- Executes web flows -->
<webflow:flow-executor id="flowExecutor" flow-registry="flowRegistry" />
<!-- Rest of the file ... -->
</beans>
Does anyone have any ideas?
[Edit]
The snippet:
<imported-shared-libraries>
<imported-shared-library name="apache.xml"/>
</imported-shared-libraries>
should, of course read:
<imported-shared-libraries>
<import-shared-library name="apache.xml"/>
</imported-shared-libraries>
Sorry!
I found the answer to this. Turns out this was specifically because I was running OC4J on a Java 6 JVM. If anyone else gets this problem, here's what we did to resolve:
The Oracle XML parser does not handle the Spring 3 XSD files well, this is a know issue. You need to remove the Oracle XSD libraries for your application. In your orion-application.xml file, you need
<imported-shared-libraries>
<remove-inherited name="oracle.xml"/>
</imported-shared-libraries>
The Oracle documentation then tells you to import a new shared library. However, if you're running OC4J on a Java 6 JVM (which we are!), you can't do this. It looks like there is an XML parser in the Java 6 core now, and importing Xerces libraries will conflict with these classes, causing weird errors.
Anyway, on Java 6, remove the Oracle libraries, but don't import any other ones!

Unable to locate Spring NamespaceHandler for element 'flow'

I am developing a spring webflow (2.0.7) project using SpringSource Tool Suite. I am trying to setup a basic flow.
My someflow.xml looks like this:
<flow xmlns="http://www.springframework.org/schema/webflow"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/webflow
http://www.springframework.org/schema/webflow/spring-webflow-2.0.xsd">
<!- view-state declarations -->
</flow>
On STS tool(Spring IDE on eclipse), I see a warning message near the flow schemaLocation:
Unable to locate Spring NamespaceHandler for element 'flow' of schema namespace 'http://
www.springframework.org/schema/webflow'
Then when tomcat starts up, I get the error
org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Unable to locate Spring NamespaceHandler for XML schema namespace [http://www.springframework.org/schema/webflow]
Offending resource: ServletContext resource [/WEB-INF/flows/someflow.xml]
I googled a while and some posts suggested that the problem is spring-webflow jars not being in class path. In my case, springsource tool created the template and all jars are in place. I manually checked them as well. So that can't be the issue
One of the suggestions in this post http://forum.springsource.org/archive/index.php/t-49098.html was to splice the jar! That can't be a solution, but I tried to see if it fixes it. But no.
Stuck now.. Did anyone else face this issue?
I've had similar issues before and it usually boiled down to the jar missing from the built war. Can you open up the war you are using and check that the webflow jar is in the /WEB-INF/lib directory?
If you are using Maven to do your builds, check your dependency settings for webflow as well.
if you are using eclipse,please provide the appresource path name,right click on your test case select Run as --> Run Configurations --> click on the Classpath tab and copy the below line and give the full path name of your property file location.
appResourcePath = ../../environment-dev.properties).

Resources