We have a #WebServlet that's annotated with a custom interceptor annotation like this:
#WebServlet("/path")
#CustomInterceptor
public class InitialHtmlServlet extends HttpServlet
{
#Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
{
}
}
We have the CustomInterceptor in the beans.xml in /WEB-INF and the interceptor works in other CDI-components. In this servlet, however, we cannot get it working.
We're running the latest JBoss EAP, which should be somewhat similar to JBoss 7.1.1. Is there something we should do different to get the interceptor to catch invocations on the servlet or is this not possible at all?
After some digging around I also found it somewhat confusing, that while being a good candidate for calling it a 'bean', servlet are exempt from interceptor mechanism.
It looks like various parts of JEE6 may or may not support interceptors at will :). Found some discussion here.
Related
I have an application that is using JAX-RS (and swagger) annotations heavily and all is shown nicely in the swagger definition.
However one servlet implements a customer restful interface (oData, Apache oLingo) and this is not using any of the JAX-RS annotations. Yet I would like to document it.
To make it as simple as possible, let's say I have a class
#WebServlet("/rest1")
public class Login extends HttpServlet {
#Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) {
response.setHeader("Content-Type", "application/json");
PrintWriter out = resp.getWriter();
out.print("{ \"text\": \"Hello World\" }");
}
}
I would like that servlet to show up in the swagger output as well without losing the JAX-RS entities.
According to the docs I would think I need to extend the reader to output this servlet as well and a custom ModelConverter?? to add hardcoded properties. But even if that is correct, I can't find a good starting point. Not to mention that I hope there is a simpler, more direct, way. Samples of swagger have been of no help either.
I'm building an application where I'd like to intercept HTTP requests and decide whether or not to pass them to a JAX-RS implementation for processing.
I basically have a single filter-and-front-controller-servlet combination and would like the servlet to delegate routing either to Jersey or to my "standard" router.
I can see lots of examples of using Jersey as a servlet, or of starting up an HTTP server, but there doesn't seem to be a handy way to take an HttpServletRequest/HttpServletResponse pair and say "here you go Jersey, route this for me".
Am I missing something obvious?
In this case, I think a RequestDispatcher might helps
A RequestDispatcher object can be used to forward a request to another resource, so you can try something like the following:
public class FrontServlet extends HttpServlet {
#Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext sc = this.getServletContext();
if (someCondition) {
sc.getRequestDispatcher("/jersey/servlet").forward(req, resp);
}else{
sc.getRequestDispatcher("/standard/router").forward(req, resp);
}
}
}
how can I add a robots.txt file to a Vaadin application?
I found nearly nothing related, but what I found states that there is no support for such a file.
I'm using Vaadin 7.1.1 with JBoss 7.1.1 and Vaadin-CDI-Integration.
My workaround approach is: By adding RobotsUI to the project, the URL http://localhost:8080/App/robots.txt becomes accessible.
#CDIUI(value="robots.txt")
public class RobotsUI extends UI {
#Override
protected void init(VaadinRequest request) {
// Send a response with mimetype
// `text/plain` with self defined content.
}
}
My problem is: How can I deliver a self-edited, text/plain response?
Thanks for any help :-)
I successfully published text/plain by adding a common HttpServlet to the project:
#WebServlet("/robots.txt")
public class RobotsServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.getWriter().write("Text...\n");
}
}
Do it outside of Vaadin, register a filter before vaadin servlet and in case of robots.txt uri return your robots file. Or add some static resource serving servlet registered lets say to /static/* and bind your /robots.txt redirect with UrlRewrite.
Is there any way to use pure Java servlets not spring mvc request mapping to map a URL to a method?
something like:
#GET(/path/of/{id})
It's also possible with "plain vanilla" servlets (heck, Spring MVC and JAX-RS are also built on top of servlet API), it only requires a little bit more boilerplate.
#WebServlet("/path/of/*")
public class PathOfServlet extends HttpServlet {
#Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String id = request.getPathInfo().substring(1);
// ...
}
}
That's all. Thanks to the new Servlet 3.0 #WebServlet annotation, you don't need any web.xml entry.
See also:
Our Servlets wiki page
I have a Filter implementation that has worked previously in a single instance Glassfish 3.1.1.
It has been annotated with #WebFilter and is as follows:
#WebFilter(urlPatterns = { "/*" })
public class SomeFilter implements Filter {
#Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,
ServletException {
HttpServletRequest req = (HttpServletRequest) request;
...
}
#Override
public void init(FilterConfig filterConfig) throws ServletException {
// do nothing
}
#Override
public void destroy() {
// do nothing
}
It should be invoked with every request.
Now I have a Glassfish setup that has two separate instances for FRONT and SERVICE. I deploy the WAR containing the filter to FRONT and the Filter does not seem to work. I tried logging and debugging, but it seems the filter, or at least the doFilter-method is never invoked. I also tried to put some logging to init method but it also isn't showing up on the logs at all.
Anyone know what might cause such behaviour. Is there a way to log the Glassfish filterchain somehow?
Did you check the Glassfish server.log for errors regarding initialization of the filter? I've encountered similar problems and got a hunch that filter initialization is the cause of your problem.