WebSocket not working on Jetty 9.4 - servlets

My websocket servlet does not work on Jetty 9.4.6.v20170531 although it works perfectly with version 9.3.2.v20150730.
My code looks like this:
#SuppressWarnings("serial")
#WebServlet(name = "TcpProxy", urlPatterns = { "/sockets/tcpProxy" })
public class TcpProxySocketServlet extends WebSocketServlet {
#Override
public void configure(WebSocketServletFactory factory) {
factory.register(TcpProxySocket.class);
}
}
and
#WebSocket
public class TcpProxySocket {
/* ... */
public TcpProxySocket() {
LOGGER.info("Instantiating a TCP proxy");
}
/**
* Open a new socket
*
* #param session the session
*/
#OnWebSocketConnect
public void onConnect(Session session) throws RestException {
this.session = session;
CachedSession toriiSession = null;
...
When trying to access my socket, I get a 404 error.
On server side, the configure is never called.
I tried to force the loading of the servlet by adding it to web.xml
<servlet>
<servlet-name>TcpProxySocket</servlet-name>
<servlet-class>com.fujitsu.fse.torii.servlets.tcpProxy.TcpProxySocketServlet</servlet-class>
</servlet>
<servlet-mapping> <servlet-name>TcpProxySocket</servlet-name>
<url-pattern>/sockets/tcpProxy</url-pattern>
</servlet-mapping>
Then the servet is loaded, configure function is called.
When trying to open the socket, I don't get any error but the onConnect error is never called.
So far I have reverted to using Jetty 9.3.2, but it's not satisfying.
Any Idea ?

This was fixed by using a correct web-app markup in web.xml to use webapp version 3.1
## -1,6 +1,8 ##
<?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">
+<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
+ version="3.1">
The other problem was that the onConnect method was never called. It disappeared when I changed the servlet mapping using a path with a trailing slash ("/sockets/scripts/" instead of "/sockets/scripts").
We could not reproduce the trailing-slash problem on a simpler example. So I'm not sure if there was an actual problem or if it was just a misinterpretation of mine.
the full story is on https://github.com/eclipse/jetty.project/issues/1800
I thank Joakim and the Jetty project for their reactivity.

I had the same issue with Java Spark web framework when they updated Jetty to 9.4.
The trailing slash issue mentioned by Michael Dussere did the trick for me, I changed the path in my client from "http://example.org/chat/" to "http://example.org//chat" (in the server it is ".../chat" as well).

Related

Jboss EAP 7.2 #RolesAllowed is not working inside implementation class

I have 2 modules, one describes api with just interfaces of controllers and second with implementations.
Example:
Resource interface:
#Path("/resource")
public interface Resource {
#POST
#Path("/getAll")
#Produces("application/json")
Response getAll();
Resource implementation:
#RequestScoped
#Path("/user")
public class ResourceImpl implements Resource {
#RolesAllowed({"admin"})
public Response getAll() {
When I login with user that has role "user", I'm not getting 401 or 403 error.
If I add #RolesAllowed({"admin"}) to interface, then it will work as expected. But I believe there should be other solution for it
I tried solution from this topic https://access.redhat.com/solutions/766483 but with no luck
My ejb.xml looks like so:
<ejb-jar
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/ejb-jar_3_1.xsd"
version="3.1">
<enterprise-beans>
<message-driven>
*Queues describtion*
</message-driven>
</enterprise-beans>
<assembly-descriptor>
<method-permission>
<role-name>admin</role-name>
<method>
<ejb-name>ResourceImpl</ejb-name>
<method-name>*</method-name>
</method>
</method-permission>
</assembly-descriptor>
</ejb-jar>
I'm using Jboss EAP 7.2.9
Thanks in advance

#SessionScoped CDI bean is a different Instance when injected

My config is a bean that I inject in my code wherever I need it. However, when injected, I get a new instance of the bean instead of the one from the session.
My bean:
#Named
#SessionScoped
public class TestModel implements Serializable {
private static final long serialVersionUID = 4873651498076344849L;
private String version;
public String getVersion() {
return version;
}
public void setVersion(String version) {
this.version = version;
}
public void changeVersion() {
this.version = "Version 2";
System.out.println("New version : " + version + ", Object : " + this);
}
}
When injected in different classes, all occurences are different instances.
When annotating the bean with #ApplicationScoped, it is the same instance.
I do need the bean to be #SessionScoped since every user should have his own config.
The WebApp is running on TomEE 1.7.4
UPDATE: I created a new project to test it, and the SessionScope works. I now need to find out what is wrong with my current project in order to fix it.
Facets:
CDI 1.0
Dynamic Web Module 3.0
Java 1.8
JSF 2.2 (MyFaces impl from TomEE)
JPA 2.1
Web.xml
<?xml version="1.0" encoding="UTF-8"?>
<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>Project</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<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>
<context-param>
<param-name>primefaces.THEME</param-name>
<param-value>omega</param-value>
</context-param>
<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>
<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>*.xhtml</url-pattern>
</servlet-mapping>
</web-app>
faces-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<faces-config
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd"
version="2.2">
</faces-config>
beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
version="1.1" bean-discovery-mode="annotated">
</beans>
Any ideas ?
Looks like your test doesn't work:
testModel object = model.TestModel#689a6064
New version : Version 2, Object : model.TestModel#61606aa6
So you update an instance which is not the same as the one linked to the session (another request not reusing the same session I'd say)
You are doing it right. That is, from CDI perspective, you made no mistake and what you want is perfectly legit and should work (assuming you solved the problem of multiple sessions, which you did).
I just tried this with my own piece of code and it works as expected. You can check it on GitHub. The sample is more or less identical to yours.
However, I am running Wildfly 10 and therefore Weld 2.3 which comes with it (Weld being a reference impl of CDI). While you are running TomEE which contains OpenWebBeans (another CDI implementation).
To me it seems like you either missed some TomEE/OWB specific configuration (unrealistic scenario) or, more likely, you found a bug. In any case, if I were you, I would try asking on their forums or creating an issue in their tracking system because, once again, there is imho nothing wrong with your bean/servlet setup.
We have #SessionScope annotation in both JSF & CDI. Please review whether the annotation you are using in your old project is from JSF or from CDI.
Find more on the difference between the annotation from JSF & CDI

Issue trying to solve 404 status error code on servlet GET request

Once again thanks for the help on previous help. Anyway I try to move on to my EFTscreen.java code to get my Search buttons to work. In my JSP page I have the following search button with the following function call (hope I get parathesis pasting this:
<button id="searchEFT" class="btn smBtn">Search</button>
xmlhttp = new XMLHttpRequest();
if (Cmd_Sched_Number != "")
{
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
{
document.getElementById("EFTSumList").innerHTML = http.responseText;
}
};
xmlhttp.open("GET","/servlet/EFTscreen?method=searchSched&Schedule_Number="
+ Cmd_Sched_Number + "&Contract_Year=" + Cmd_Contract_Year,true);
xmlhttp.send();
}
Now in looking around I was told that I need to create the servlet name and classs in the web.xml file located the WEB-INF directory of project
<?xml version = '1.0' encoding = 'windows-1252'?>
<web-app 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"
version="3.0">
<servlet>
<display-name>EFTscreen</display-name>
<servlet-name>EFTscreen</servlet-name>
<servlet-class>eftproject.EFTscreen</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>EFTscreen</servlet-name>
<url-pattern>/servlet/EFTscreen</url-pattern>
</servlet-mapping>
here is my declaration in the EFTscreen.java file to supposedly pull the Httpservlet (removed some of the imports because it is already too big a question)
package eftproject;
import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
#WebServlet(name = "EFTscreen", urlPatterns = { "/servlet/EFTscreen"})
public class EFTscreen extends HttpServlet { }
When I try running this going through debugging, I am getting the 404 error in the xmlhttp.status field not being able to find the URL. The readystate fields are working but not sure if that is relevant.
Can you review to see what I am missing here or incorrectly? I am thinking it is something with my url-pattern in web.xml and how I am accessing it but maybe it is something even worse.
Thanks for the help

Spring MVC or Wicket?

I have a long (and happy so far) experience with Spring MVC, but lately I'm getting interested in Wicket.
My question is also on how to handle (with Wicket) DI, Transaction Mgmt, JDBC connections and all that stuff? Is it okay to mix certain parts of the Springsource suite with Wicket? Wicket & Weld? Wicket & Guice?
Wicket is a presentation-layer framework. It will not handle DI, transactions or connections.
But it can be easily integrated with a number of frameworks, including Spring, Guice (official Wicket modules, wicket-spring and wicket-guice) and CDI/Weld (wicket-cdi, a side project from Igor Vaynberg, one of the Wicket committers).
Wicket Wiki: Integration guides
Below, a simple Wicket application with Spring. The transaction bits are plain old Spring configuration, so I didn't bother including them.
HelloWorldService.java
public class HelloWorldService {
private String message;
public void setMessage(String message) {
this.message = message;
}
public String getMessage() {
return message;
}
}
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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
<bean id="helloWorldService" class="minimal.wicketspring.HelloWorldService">
<property name="message" value="Hello, World!" />
</bean>
</beans>
WicketApplication.java
public class WicketApplication extends WebApplication {
#Override
public void init() {
super.init();
getComponentInstantiationListeners().add(new SpringComponentInjector(this));
}
#Override
public Class<HomePage> getHomePage() {
return HomePage.class;
}
}
HomePage.java
public class HomePage extends WebPage {
#SpringBean
private HelloWorldService helloWorldService;
public HomePage() {
add(new FeedbackPanel("feedback"));
add(new Link<Void>("link") {
#Override
public void onClick() {
info(helloWorldService.getMessage());
}
});
}
}
HomePage.html
<!DOCTYPE html>
<html xmlns:wicket="http://wicket.apache.org">
<body>
<div wicket:id="feedback"></div>
<a wicket:id="link">Show Message</a>
</body>
</html>
web.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app 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" version="2.5">
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<filter>
<filter-name>wicket.wicket-spring</filter-name>
<filter-class>org.apache.wicket.protocol.http.WicketFilter</filter-class>
<init-param>
<param-name>applicationClassName</param-name>
<param-value>minimal.wicketspring.WicketApplication</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>wicket.wicket-spring</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
Wicket effectively replaces Spring MVC, but not the Spring container itself. Wicket integrates easily with Spring via #SpringBean annotation which allows you to inject Spring beans (services, DAOs, etc.) directly to pages. You cannot perform DI the other way around - for a good reason.
It is a smart choice to use Spring and Wicket together. However as far as I remember Wicket pages and components aren't managed by Spring container so you cannot use #Transactional annotation on them (which is a bad idea anyway - transactions belong to deeper levels).
Everything else works exactly the same - AOP, JDBC, etc.
I would recommend to leave Spring entirely and try the Java EE 6 + Wicket 6.x. I was using Spring MVC, then Spring + Wicket in the days of Java EE 5 but Java EE 6 as the middleware layer simply beats Spring solutions.
Update 2017:
With the new goodies in Java 7 and 8 (lambdas, method refereces, default interface method implementations, ...), the Java EE 7 + Wicket 8 combo is even more appealing. Although Spring MVC is quite popular and might have better learning curve, once you try Wicket you'll miss it when you get to the "next cool thing" (Angular2 in my case).
Note: I am paid by Red Hat, but the above is my honest opinion. In 2011, I'd tell you to go with Spring.
Just forget on wickets. Simple spring MVC, Twitter bootstrap layout and whole spring portfolio let you create scalable and top high performance applications, with top security. Wickets is pain as soon as you go behind your first impress and start real development.
http://www.slideshare.net/mraible/comparing-jvm-web-frameworks-february-2014 hope thi help make decision for me this presentation was realy helpfull.

Jython and implementing HttpServlet.contextInitialized

I'd like my Jython servlet to implement the HttpServlet.contextInitialized method but I'm not sure how to express this in the web.xml. What I currently have is:
from javax.servlet import ServletContextListener;
from javax.servlet.http import HttpServlet
class JythonServlet1 ( HttpServlet, ServletContextListener ):
def contextInitialized( self, event ):
print "contextInitialized"
context = event.getServletContext()
def contextDestroyed( self, event ):
print "contextDestroyed"
context = event.getServletContext()
def doGet( self, request, response ):
print "doGet"
def doPost( self, request, response ):
print "doPost"
And my web.xml looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<web-app
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>JythonTest</display-name>
<servlet>
<servlet-name>PyServlet</servlet-name>
<servlet-class>org.python.util.PyServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>PyServlet</servlet-name>
<url-pattern>*.py</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>JythonServlet1</display-name>
<servlet-name>JythonServlet1</servlet-name>
<servlet-class>JythonServlet1</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
</web-app>
As you can see, in the last <servlet> entry I'd like to initialize the servlet with the context (where I can start a scheduler) but it doesn't seem to work the same as with a Java servlet.
I don't do Jython, but there's no means of contextInitialized or contextDestroyed methods in the HttpServlet API. You're probably looking for ServletContextListener interface which is normally to be implemented as the following Java-based example:
package com.example;
import javax.servlet.ServletContextListener;
public class MyServletContextListener implements ServletContextListener {
public void contextInitialized(ServletContextEvent event) {
// ...
}
public void contextDestroyed(ServletContextEvent event) {
// ...
}
}
...which is to be definied as <listener> in web.xml as follows:
<listener>
<listener-class>com.example.MyServletContextListener</listener-class>
</listener>
This must give you an idea how to pickup it using Jython.
You can optionally also let your servlet both extend HttpServlet and implement ServletContextListener like follows:
public class MyServlet extends HttpServlet implements ServletContextListener {
// ...
}
so that you can end up with the code you've posted (don't forget to import the particular interface and define your class as both servlet and listener in web.xml). But this is not always considered a good practice.
That said, you should be placing classes in a package to avoid portability problems. It may work in some environments, but not in other. Sun also discourages using packageless classes in non-prototyping environments. They can normally namely not be imported by other classes which are itself in a package.
You really need to write some java bootstrapper like PyServlet that dispatches init() to a pre-defined python script.
Or.. if you want to use the ServletContextListener interface then something like Pyservlet that also implements ServletContextListner and again, dispatches to some python script.
I'm looking for a similar solution and was very disappointed to see that PyServlet doesn't offer anything like this itself.

Resources