Apache CXF with Spring - spring

I am using Apache CXF with Spring , please tell me how the CXFServlet reads the myapp-ws-context.xml
<web-app>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:myapp-ws-context.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<servlet>
<display-name>CXF Servlet</display-name>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>
org.apache.cxf.transport.servlet.CXFServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>

Have you seen sources of org.apache.cxf.transport.servlet.CXFServlet (open source)?
Everything is more than explicit:
#Override
protected void loadBus(ServletConfig sc) {
ApplicationContext wac = WebApplicationContextUtils.
getWebApplicationContext(sc.getServletContext());
String configLocation = sc.getInitParameter("config-location");
if (configLocation == null) {
try {
InputStream is = sc.getServletContext().getResourceAsStream("/WEB-INF/cxf-servlet.xml");
if (is != null && is.available() > 0) {
is.close();
configLocation = "/WEB-INF/cxf-servlet.xml";
}
} catch (Exception ex) {
//ignore
}
}
if (configLocation != null) {
wac = createSpringContext(wac, sc, configLocation);
}
if (wac != null) {
setBus(wac.getBean("cxf", Bus.class));
} else {
setBus(BusFactory.newInstance().createBus());
}
}
Note that WebApplicationContextUtils is a Spring class that tries to find an application context in servlet context attribute named: org.springframework.web.context.WebApplicationContext.ROOT.

Actually your classpath:myapp-ws-context.xml is read by Spring, not CXF.
By adding below configuration in your web.xml, it would be read by Spring and the context would be loaded:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:myapp-ws-context.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
But you could configure your Servlet/WebApp scope of Spring objects, like multipartResolver etc., to make the objects' scopes clearly, by enhancing your CXFServlet configuration like below:
<servlet>
<display-name>CXF Servlet</display-name>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>
org.apache.cxf.transport.servlet.CXFServlet
</servlet-class>
<init-param>
<param-name>config-location</param-name>
<param-value>/WEB-INF/your-webapp-scope-spring-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
*Please take note that from your web-app context, you can access all objects in the context loaded from contextConfigLocation. *

Related

JSP linking with Java Servlet BackEnd Code

I am trying to link my Jsp page with my servlet but I'm getting this error :
javax.servlet.ServletException: Servlet.init() for servlet ImageServlet threw exception
java.lang.IllegalStateException: No WebApplicationContext found: no ContextLoaderListener registered`
Below is my Servlet code :
package servlet;
#Component("ImageServlet")
public class ImageServlet implements HttpRequestHandler {
#Autowired
imageDA imageda = new imageDA();
ResultSet rs = null;
byte[] thumb ;// get the thumb from the user entity
#Override
public void handleRequest(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
int generatedDocId = Integer.parseInt(request.getParameter("generatedDocId"));
try{
rs = imageda.getAllImage(generatedDocId);
if(rs.next()){
thumb = rs.getBytes("IMAGE");
}
}catch(SQLException ex){
ex.getMessage();
}
String name = "images";
response.setContentType("image/jpeg");
response.setContentLength(thumb.length);
response.setHeader("Content-Disposition", "inline; filename=\"" + name+ "\"");
BufferedInputStream input = null;
BufferedOutputStream output = null;
try {
input = new BufferedInputStream(new ByteArrayInputStream(thumb));
output = new BufferedOutputStream(response.getOutputStream());
byte[] buffer = new byte[8192];
int length;
while ((length = input.read(buffer)) > 0) {
output.write(buffer, 0, length);
}
} catch (IOException e) {
System.out.println("There are errors in reading/writing image stream "
+ e.getMessage());
} finally {
if (output != null) {
try {
output.close();
} catch (IOException ignore) {
}
}
if (input != null) {
try {
input.close();
} catch (IOException ignore) {
}
}
}
}
}
and this is my XML code linking to my viewData.jsp JSP page :
<servlet>
<servlet-name>ImageServlet</servlet-name>
<servlet-class>org.springframework.web.context.support.HttpRequestHandlerServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ImageServlet</servlet-name>
<url-pattern>/viewData.jsp</url-pattern>
</servlet-mapping>
</web-app>
Edited, here is my full version of XML codes. I use most of them for my Request.getPart(), tried to configure XML for spring but it doesn't work.Thanks for helping
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 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/web-app_3_0.xsd">
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<servlet>
<servlet-name>UploadFile</servlet-name>
<jsp-file>/projectAddData.jsp</jsp-file>
<multipart-config>
<max-file-size>20848820</max-file-size>
<max-request-size>418018841</max-request-size>
<file-size-threshold>1048576</file-size-threshold>
</multipart-config>
</servlet>
<servlet-mapping>
<servlet-name>UploadFile</servlet-name>
<url-pattern>/projectAddData.jsp</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>UploadFile1</servlet-name>
<jsp-file>/addClaim.jsp</jsp-file>
<multipart-config>
<max-file-size>20848820</max-file-size>
<max-request-size>418018841</max-request-size>
<file-size-threshold>1048576</file-size-threshold>
</multipart-config>
</servlet>
<servlet>
<servlet-name>UploadServlet1</servlet-name>
<servlet-class>UploadServlet1</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>UploadFile1</servlet-name>
<url-pattern>/addClaim.jsp</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>UploadFile2</servlet-name>
<jsp-file>/addInvoice.jsp</jsp-file>
<multipart-config>
<max-file-size>20848820</max-file-size>
<max-request-size>418018841</max-request-size>
<file-size-threshold>1048576</file-size-threshold>
</multipart-config>
</servlet>
<servlet>
<servlet-name>UploadServlet2</servlet-name>
<servlet-class>UploadServlet2</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>UploadFile2</servlet-name>
<url-pattern>/addInvoice.jsp</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>imageServlet</servlet-name>
<servlet-class>org.springframework.web.context.support.HttpRequestHandlerServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>imageServlet</servlet-name>
<url-pattern>/viewData.jsp</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<!-- The path to your main spring xml file, for example: /WEB-INF/spring-config.xml -->
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
</web-app>
The exception is telling you that although you intend to use spring, the ApplicationContext can't be loaded due to the absence of a required listener configuration. Spring needs to know the path to your main bean config file, and the missing listener is what it uses to find that information.
Add this to your web.xml (as a child of the root <web-app> element, and your web app will start (or at least make it past the error you're getting now).
<context-param>
<param-name>contextConfigLocation</param-name>
<!-- The path to your main spring xml file, for example: /WEB-INF/spring-config.xml -->
<param-value>/WEB-INF/spring-config.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>

spring mvc annotation many servlet dispatcher

Hi I am just starting to learn Spring mvc, I use spring mvc #annotation and I have 3 servlets dispatchers (appservlet, admin, student):
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 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/web-app_2_5.xsd">
<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>
<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Processes application requests -->
<servlet>
<servlet-name>admin</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/admin-servlet.xml</param-value>
</init-param>
</servlet>
<servlet>
<servlet-name>student</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>admin</servlet-name>
<url-pattern>*.admin</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>student</servlet-name>
<url-pattern>*.student</url-pattern>
</servlet-mapping>
</web-app>
After a test in a method I want a redirect to another servlet dispatcher, either the .admin or a .student. How can I do that please?
#controller
#Controller
public class AuthentificationController {
#RequestMapping(value = "verifier", method = RequestMethod.POST)
public ModelAndView redir(#ModelAttribute("per") Personne per,
HttpSession session) {
if (p1.equals(per)) {
session.setAttribute("login", per);
ModelAndView M = new ModelAndView(".admin");
return M;
}
if (p2.equals(per)) {
ModelAndView M = new ModelAndView(".student");
return M;
} else {
ModelAndView M = new ModelAndView("home");
return M;
}
}
}
The problem is that ModelAndView returns (.jsp) but I want a redirection for another servlet-dispatcher !!
Here you are:
#RequestMapping(value = "verifier", method = RequestMethod.POST)
public String redir(#ModelAttribute("per") Personne per, HttpSession session) {
if (p1.equals(per)) {
session.setAttribute("login", per);
return "redirect:.admin";
} else if (p2.equals(per)) {
return "redirect:.student"
} else {
return "home";
}
}

Setting up JavaServer Faces in a spring project

I want add support to JavaServer Faces to a spring project mine, but the tutorials I found on the web teach that for setting this to a project, add this line to the file web.xml:
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
but my spring project don't use XML files for configuration, only java classes. Anyone can tell me how to configure JavaServer Faces in this scenario?
Equivalent class based configuration would be:
public class MyInitializer implements WebApplicationInitializer {
#Override
public void onStartup(ServletContext servletContext) throws ServletException {
ServletRegistration.Dynamic facesServlet = servletContext.addServlet("Faces Servlet", new FacesServlet());
facesServlet.setLoadOnStartup(1);
facesServlet.addMapping("/faces/*");
}
}
More details here

java.lang.IllegalStateException: Could not find backup for factory javax.faces.context.FacesContextFactory

I'm getting the java.lang.IllegalStateException: Could not find backup for factory javax.faces.context.FacesContextFactory. when I deploy my webapp as a .war. Does anyone have an idea where to begin looking for the root cause of this problem? The application runs as expected in the Eclipse IDE, but when exported as a war for deploying on the publicly available stand-alone TomEE environment, it fails.
Many thanks.
My configuration:
Apache TomEE - WebProfile 1.6.0.2
JSF 2.2.7 (com.sun.faces)(Mojarra lib 2.2.0) <-- This may be a problem? But why does the app run in the Eclipse environment but not the stand-alone TomEE?
Maven 4.0.0
java.lang.IllegalStateException: Could not find backup for factory javax.faces.context.FacesContextFactory.
javax.faces.FactoryFinder$FactoryManager.getFactory(FactoryFinder.java:1004)
javax.faces.FactoryFinder.getFactory(FactoryFinder.java:316)
javax.faces.webapp.FacesServlet.init(FacesServlet.java:302)
org.apache.tomee.catalina.OpenEJBValve.invoke(OpenEJBValve.java:45)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040)
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:315)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
java.lang.Thread.run(Thread.java:744)
The web.xml:
<web-app
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID"
version="3.0">
<display-name>ProjName</display-name>
<welcome-file-list>
<welcome-file>login.xhtml</welcome-file>
</welcome-file-list>
<listener>
<listener-class>com.sun.faces.config.ConfigureListener</listener-class>
</listener>
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
<context-param>
<param-name>javax.faces.FACELETS_SKIP_COMMENTS</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<param-name>javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<param-name>com.sun.faces.forceLoadConfiguration</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<description>State saving method: 'client' or 'server' (=default). See JSF Specification 2.5.2</description>
<param-name>javax.faces.STATE_SAVING_METHOD</param-name>
<param-value>client</param-value>
</context-param>
<context-param>
<param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>
<param-value>resources.application</param-value>
</context-param>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.jsf</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.faces</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
The jars that are exported in the .war WEB-INF/lib directory:
WEB-INF/lib/javax.faces.jar
WEB-INF/lib/javax.faces-api-2.2.jar
WEB-INF/lib/jsf-api-2.2.7.jar
WEB-INF/lib/jsf-impl-2.2.7.jar
WEB-INF/lib/poi-3.10-FINAL.jar
WEB-INF/lib/commons-codec-1.5.jar
WEB-INF/lib/poi-ooxml-3.10-FINAL.jar
WEB-INF/lib/poi-ooxml-schemas-3.10-FINAL.jar
WEB-INF/lib/xmlbeans-2.3.0.jar
WEB-INF/lib/stax-api-1.0.1.jar
WEB-INF/lib/dom4j-1.6.1.jar
WEB-INF/lib/xml-apis-1.0.b2.jar
WEB-INF/lib/cxf-bundle-jaxrs-2.7.11.jar
WEB-INF/lib/woodstox-core-asl-4.2.1.jar
WEB-INF/lib/stax2-api-3.1.4.jar
WEB-INF/lib/xmlschema-core-2.1.0.jar
WEB-INF/lib/geronimo-javamail_1.4_spec-1.7.1.jar
WEB-INF/lib/wsdl4j-1.6.3.jar
WEB-INF/lib/jaxb-impl-2.1.13.jar
WEB-INF/lib/jetty-server-8.1.14.v20131031.jar
WEB-INF/lib/jetty-continuation-8.1.14.v20131031.jar
WEB-INF/lib/jetty-http-8.1.14.v20131031.jar
WEB-INF/lib/jetty-io-8.1.14.v20131031.jar
WEB-INF/lib/jetty-util-8.1.14.v20131031.jar
WEB-INF/lib/jetty-security-8.1.14.v20131031.jar
WEB-INF/lib/slf4j-api-1.7.6.jar
WEB-INF/lib/geronimo-servlet_3.0_spec-1.0.jar
WEB-INF/lib/javax.ws.rs-api-2.0-m10.jar
WEB-INF/lib/postgresql-9.3-1101-jdbc41.jar
WEB-INF/lib/commons-httpclient-3.1.jar
WEB-INF/lib/commons-logging-1.0.4.jar
WEB-INF/lib/hamcrest-all-1.3.jar
WEB-INF/lib/junit-4.11.jar
WEB-INF/lib/hamcrest-core-1.3.jar
WEB-INF/lib/httpclient-4.3.3.jar
WEB-INF/lib/httpcore-4.3.2.jar
WEB-INF/lib/commons-dbcp2-2.0.jar
WEB-INF/lib/commons-pool2-2.2.jar
WEB-INF/lib/mockito-core-1.9.5.jar
WEB-INF/lib/objenesis-1.0.jar
I resolved this by removing the parameter from the web.xml file.
<listener>
<listener-class>com.sun.faces.config.ConfigureListener</listener-class>
</listener>
Removing the above text, rebuilding, exporting the war from Eclipse and deploying with the Tomcat Web Application Manager UI resolved the problem. Apparently, one of the jsf jars has this code built-in since http://java.sun.com/xml/ns/javaee/web-app_3_0 . I don't know this to be a fact, but read it in could not find Factory: javax.faces.context.FacesContextFactory. The difference here being that I removed the parameter instead of adding it.
I resolved this issue by removing the web.xml file and put these two methods in the Spring configuration file.
#Bean
public ServletContextInitializer servletContextCustomizer() {
return new ServletContextInitializer() {
#Override
public void onStartup(ServletContext sc) throws ServletException {
sc.setInitParameter(Constants.ContextParams.THEME, "bootstrap");
sc.setInitParameter(Constants.ContextParams.FONT_AWESOME, "true");
sc.setInitParameter(ProjectStage.PROJECT_STAGE_PARAM_NAME,ProjectStage.Development.name());
sc.setInitParameter("com.sun.faces.forceLoadConfiguration", "true");
sc.setInitParameter("contextConfigLocation", "test");
}
};
}
#Bean
public ServletRegistrationBean servletRegistrationBean() {
FacesServlet servlet = new FacesServlet();
ServletRegistrationBean registration = new ServletRegistrationBean(servlet, "*.jsf");
registration.setName("Faces Servlet");
registration.addUrlMappings("*.jsf");
registration.setMultipartConfig(new MultipartConfigElement((String) null));
registration.setEnabled(true);
registration.setLoadOnStartup(1);
return registration;
}

what is the "default applicationContext" in Jersey?

I am using Jersey to perform acceptance tests on RESTful web services. It appears as though my applicationContext.xml is not being loaded when the client loads. I see the following log output:
INFO: Using default applicationContext
Is this "default" file soemthing that Jersey loads when it cannot find my file? Or does this indicate that my file was found?
#ContextConfiguration(locations={"/applicationContext.xml", "/applicationContextTest.xml"})
public class BaseResourceTest extends JerseyTest {
final static URI baseUri = UriBuilder.fromUri( "http://localhost" ).port( 9998 ).build();
public BaseResourceTest() throws Exception {
super(new WebAppDescriptor.Builder("xxx.yyy.zzz").contextPath(baseUri.getPath())
.contextParam(
SpringServlet.CONTEXT_CONFIG_LOCATION, "classpath:applicationContextTest.xml" )
.servletClass(SpringServlet.class )
.contextListenerClass( ContextLoaderListener.class )
.build());
}
.......
some tests
.......
}
my web.xml:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>xxx.yyy.LoggingAssuranceListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>jersey-servlet</servlet-name>
<servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>xxx.yyy.zzzz</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.config.property.WadlGeneratorConfig</param-name>
<param-value>xxx.yyy.zzz.BroadsoftWadlGeneratorConfig</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jersey-servlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>

Resources