use of jakarta.servlet.Servlet in project atmosphere by Atmosphere.
the class MeteorServiceInterceptor method mapAnnotatedService.
protected void mapAnnotatedService(boolean reMap, String path, AtmosphereRequest request, AtmosphereFramework.AtmosphereHandlerWrapper w) {
synchronized (config.handlers()) {
if (config.handlers().get(path) == null) {
// MeteorService
if (ReflectorServletProcessor.class.isAssignableFrom(w.atmosphereHandler.getClass())) {
ReflectorServletProcessor r = (ReflectorServletProcessor) w.atmosphereHandler;
Servlet s = r.getServlet();
if (s == null) {
logger.warn("Invalid ReflectorServletProcessor state. Servlet is null");
return;
}
MeteorService m = s.getClass().getAnnotation(MeteorService.class);
if (m != null) {
String targetPath = m.path();
if (targetPath.contains("{") && targetPath.contains("}")) {
try {
boolean singleton = s.getClass().getAnnotation(Singleton.class) != null;
if (!singleton) {
r = config.framework().newClassInstance(ReflectorServletProcessor.class, ReflectorServletProcessor.class);
r.setServlet(config.framework().newClassInstance(Servlet.class, s.getClass()));
r.init(config);
}
request.localAttributes().put(Named.class.getName(), path.substring(targetPath.indexOf("{")));
((AtmosphereResourceImpl) request.resource()).atmosphereHandler(r);
config.framework().addAtmosphereHandler(path, r, config.getBroadcasterFactory().lookup(w.broadcaster.getClass(), path, true), w.interceptors);
request.setAttribute(FrameworkConfig.NEW_MAPPING, "true");
} catch (Throwable e) {
logger.warn("Unable to create AtmosphereHandler", e);
}
}
}
}
} else if (reMap) {
request.setAttribute(FrameworkConfig.NEW_MAPPING, "true");
}
}
}
use of jakarta.servlet.Servlet in project atmosphere by Atmosphere.
the class MeteorTest method testMeteor.
@Test
public void testMeteor() throws IOException, ServletException {
final AtomicReference<Meteor> meteor = new AtomicReference<Meteor>();
final Servlet s = new HttpServlet() {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
meteor.set(Meteor.lookup(req));
}
};
framework.addAtmosphereHandler("/a", new ReflectorServletProcessor(s));
AtmosphereRequest request = new AtmosphereRequestImpl.Builder().pathInfo("/a").build();
framework.interceptor(new AtmosphereInterceptorAdapter() {
@Override
public Action inspect(AtmosphereResource r) {
Meteor m = Meteor.build(r.getRequest());
return Action.CONTINUE;
}
});
framework.doCometSupport(request, AtmosphereResponseImpl.newInstance());
assertNotNull(meteor.get());
}
use of jakarta.servlet.Servlet in project atmosphere by Atmosphere.
the class MeteorTest method testMeteorNull.
@Test
public void testMeteorNull() throws IOException, ServletException {
final AtomicReference<Meteor> meteor = new AtomicReference<Meteor>();
final Servlet s = new HttpServlet() {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
meteor.set(Meteor.lookup(req));
}
};
framework.addAtmosphereHandler("/a", new ReflectorServletProcessor(s));
AtmosphereRequest request = new AtmosphereRequestImpl.Builder().pathInfo("/a").build();
framework.interceptor(new AtmosphereInterceptorAdapter() {
@Override
public Action inspect(AtmosphereResource r) {
return Action.CONTINUE;
}
});
framework.doCometSupport(request, AtmosphereResponseImpl.newInstance());
assertNull(meteor.get());
}
use of jakarta.servlet.Servlet in project spring-framework by spring-projects.
the class HttpRequestHandlerTests method testHttpRequestHandlerServletPassThrough.
@Test
public void testHttpRequestHandlerServletPassThrough() throws Exception {
MockServletContext servletContext = new MockServletContext();
MockHttpServletRequest request = new MockHttpServletRequest();
MockHttpServletResponse response = new MockHttpServletResponse();
StaticWebApplicationContext wac = new StaticWebApplicationContext();
wac.getBeanFactory().registerSingleton("myHandler", (HttpRequestHandler) (req, res) -> {
assertThat(req).isSameAs(request);
assertThat(res).isSameAs(response);
String exception = request.getParameter("exception");
if ("ServletException".equals(exception)) {
throw new ServletException("test");
}
if ("IOException".equals(exception)) {
throw new IOException("test");
}
res.getWriter().write("myResponse");
});
wac.setServletContext(servletContext);
wac.refresh();
servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
Servlet servlet = new HttpRequestHandlerServlet();
servlet.init(new MockServletConfig(servletContext, "myHandler"));
servlet.service(request, response);
assertThat(response.getContentAsString()).isEqualTo("myResponse");
request.setParameter("exception", "ServletException");
assertThatExceptionOfType(ServletException.class).isThrownBy(() -> servlet.service(request, response)).withMessage("test");
request.setParameter("exception", "IOException");
assertThatIOException().isThrownBy(() -> servlet.service(request, response)).withMessage("test");
}
use of jakarta.servlet.Servlet in project spring-boot by spring-projects.
the class ServletWebServerApplicationContextTests method multipleServletBeansWithMainDispatcher.
@Test
void multipleServletBeansWithMainDispatcher() {
addWebServerFactoryBean();
Servlet servlet1 = mock(Servlet.class, withSettings().extraInterfaces(Ordered.class));
given(((Ordered) servlet1).getOrder()).willReturn(1);
Servlet servlet2 = mock(Servlet.class, withSettings().extraInterfaces(Ordered.class));
given(((Ordered) servlet2).getOrder()).willReturn(2);
this.context.registerBeanDefinition("servletBean2", beanDefinition(servlet2));
this.context.registerBeanDefinition("dispatcherServlet", beanDefinition(servlet1));
this.context.refresh();
MockServletWebServerFactory factory = getWebServerFactory();
ServletContext servletContext = factory.getServletContext();
InOrder ordered = inOrder(servletContext);
then(servletContext).should(ordered).addServlet("dispatcherServlet", servlet1);
then(servletContext).should(ordered).addServlet("servletBean2", servlet2);
then(factory.getRegisteredServlet(0).getRegistration()).should().addMapping("/");
then(factory.getRegisteredServlet(1).getRegistration()).should().addMapping("/servletBean2/");
}
Aggregations