Search in sources :

Example 71 with ServletContext

use of jakarta.servlet.ServletContext in project atmosphere by Atmosphere.

the class EncoderDecoderTest method create.

@BeforeMethod
public void create() throws Throwable {
    framework = new AtmosphereFramework();
    framework.setDefaultBroadcasterClassName(SimpleBroadcaster.class.getName());
    framework.addAnnotationPackage(ManagedMessage.class);
    framework.setAsyncSupport(new AsynchronousProcessor(framework.getAtmosphereConfig()) {

        @Override
        public Action service(AtmosphereRequest req, AtmosphereResponse res) throws IOException, ServletException {
            return suspended(req, res);
        }

        public void action(AtmosphereResourceImpl r) {
            try {
                resumed(r.getRequest(), r.getResponse());
            } catch (IOException e) {
                e.printStackTrace();
            } catch (ServletException e) {
                e.printStackTrace();
            }
        }
    }).init(new ServletConfig() {

        @Override
        public String getServletName() {
            return "void";
        }

        @Override
        public ServletContext getServletContext() {
            return mock(ServletContext.class);
        }

        @Override
        public String getInitParameter(String name) {
            return null;
        }

        @Override
        public Enumeration<String> getInitParameterNames() {
            return null;
        }
    });
    latch.set(new CountDownLatch(1));
}
Also used : AtmosphereResponse(org.atmosphere.cpr.AtmosphereResponse) Enumeration(java.util.Enumeration) ServletConfig(jakarta.servlet.ServletConfig) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) SimpleBroadcaster(org.atmosphere.util.SimpleBroadcaster) ServletException(jakarta.servlet.ServletException) AtmosphereRequest(org.atmosphere.cpr.AtmosphereRequest) AsynchronousProcessor(org.atmosphere.cpr.AsynchronousProcessor) AtmosphereFramework(org.atmosphere.cpr.AtmosphereFramework) ServletContext(jakarta.servlet.ServletContext) AtmosphereResourceImpl(org.atmosphere.cpr.AtmosphereResourceImpl) BeforeMethod(org.testng.annotations.BeforeMethod)

Example 72 with ServletContext

use of jakarta.servlet.ServletContext in project atmosphere by Atmosphere.

the class AtmosphereFilter method init.

/**
 * Initialize the {@link Filter}.
 *
 * @param filterConfig The {@link jakarta.servlet.FilterConfig}
 */
public void init(final FilterConfig filterConfig) throws ServletException {
    logger.info("AtmosphereServlet running as a Filter");
    as.init(new ServletConfig() {

        @Override
        public String getServletName() {
            return filterConfig.getFilterName();
        }

        @Override
        public ServletContext getServletContext() {
            return filterConfig.getServletContext();
        }

        @Override
        public String getInitParameter(String name) {
            return filterConfig.getInitParameter(name);
        }

        @Override
        public Enumeration<String> getInitParameterNames() {
            return filterConfig.getInitParameterNames();
        }
    });
    String s = filterConfig.getInitParameter(ApplicationConfig.ATMOSPHERE_EXCLUDED_FILE);
    if (s != null) {
        excluded = s;
    }
}
Also used : Enumeration(java.util.Enumeration) ServletConfig(jakarta.servlet.ServletConfig) ServletContext(jakarta.servlet.ServletContext)

Example 73 with ServletContext

use of jakarta.servlet.ServletContext in project atmosphere by Atmosphere.

the class DefaultAnnotationProcessor method configure.

@Override
public void configure(final AtmosphereConfig config) {
    ServletContext sc = config.framework().getServletContext();
    Map<Class<? extends Annotation>, Set<Class<?>>> annotations = (Map<Class<? extends Annotation>, Set<Class<?>>>) sc.getAttribute(ANNOTATION_ATTRIBUTE);
    sc.removeAttribute(ANNOTATION_ATTRIBUTE);
    boolean useByteCodeProcessor = config.getInitParameter(ApplicationConfig.BYTECODE_PROCESSOR, false);
    boolean scanForAtmosphereAnnotation = false;
    if (useByteCodeProcessor || annotations == null || annotations.isEmpty()) {
        delegate = new BytecodeBasedAnnotationProcessor(handler);
        scanForAtmosphereAnnotation = true;
    } else {
        Map<Class<? extends Annotation>, Set<Class<?>>> clone = new HashMap<Class<? extends Annotation>, Set<Class<?>>>();
        clone.putAll(annotations);
        delegate = new ServletContainerInitializerAnnotationProcessor(handler, clone, config.framework());
    }
    logger.info("AnnotationProcessor {} being used", delegate.getClass());
    if (scanForAtmosphereAnnotation) {
        scanForAnnotation(config.framework());
    }
    delegate.configure(config.framework().getAtmosphereConfig());
}
Also used : Set(java.util.Set) HashMap(java.util.HashMap) ServletContext(jakarta.servlet.ServletContext) IOUtils.loadClass(org.atmosphere.util.IOUtils.loadClass) HashMap(java.util.HashMap) Map(java.util.Map) AtmosphereAnnotation(org.atmosphere.config.AtmosphereAnnotation) Annotation(java.lang.annotation.Annotation)

Example 74 with ServletContext

use of jakarta.servlet.ServletContext in project spring-security by spring-projects.

the class SessionManagementConfigTests method requestWhenCreateSessionIsSetToIfRequiredThenDoesNotCreateSessionOnPublicInvocation.

@Test
public void requestWhenCreateSessionIsSetToIfRequiredThenDoesNotCreateSessionOnPublicInvocation() throws Exception {
    this.spring.configLocations(xml("CreateSessionIfRequired")).autowire();
    ServletContext servletContext = this.mvc.getDispatcherServlet().getServletContext();
    MockHttpServletRequest request = get("/").buildRequest(servletContext);
    MockHttpServletResponse response = request(request, this.spring.getContext());
    assertThat(response.getStatus()).isEqualTo(HttpStatus.SC_OK);
    assertThat(request.getSession(false)).isNull();
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ServletContext(jakarta.servlet.ServletContext) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Example 75 with ServletContext

use of jakarta.servlet.ServletContext in project tomcat by apache.

the class StandardHostValve method custom.

/**
 * Handle an HTTP status code or Java exception by forwarding control
 * to the location included in the specified errorPage object.  It is
 * assumed that the caller has already recorded any request attributes
 * that are to be forwarded to this page.  Return <code>true</code> if
 * we successfully utilized the specified error page location, or
 * <code>false</code> if the default error report should be rendered.
 *
 * @param request The request being processed
 * @param response The response being generated
 * @param errorPage The errorPage directive we are obeying
 */
private boolean custom(Request request, Response response, ErrorPage errorPage) {
    if (container.getLogger().isDebugEnabled()) {
        container.getLogger().debug("Processing " + errorPage);
    }
    try {
        // Forward control to the specified location
        ServletContext servletContext = request.getContext().getServletContext();
        RequestDispatcher rd = servletContext.getRequestDispatcher(errorPage.getLocation());
        if (rd == null) {
            container.getLogger().error(sm.getString("standardHostValue.customStatusFailed", errorPage.getLocation()));
            return false;
        }
        if (response.isCommitted()) {
            // Response is committed - including the error page is the
            // best we can do
            rd.include(request.getRequest(), response.getResponse());
            // written to the client
            try {
                response.flushBuffer();
            } catch (Throwable t) {
                ExceptionUtils.handleThrowable(t);
            }
            // Now close immediately as an additional signal to the client
            // that something went wrong
            response.getCoyoteResponse().action(ActionCode.CLOSE_NOW, request.getAttribute(RequestDispatcher.ERROR_EXCEPTION));
        } else {
            // Reset the response (keeping the real error code and message)
            response.resetBuffer(true);
            response.setContentLength(-1);
            rd.forward(request.getRequest(), response.getResponse());
            // If we forward, the response is suspended again
            response.setSuspended(false);
        }
        // Indicate that we have successfully processed this custom page
        return true;
    } catch (Throwable t) {
        ExceptionUtils.handleThrowable(t);
        // Report our failure to process this custom page
        container.getLogger().error("Exception Processing " + errorPage, t);
        return false;
    }
}
Also used : ServletContext(jakarta.servlet.ServletContext) RequestDispatcher(jakarta.servlet.RequestDispatcher)

Aggregations

ServletContext (jakarta.servlet.ServletContext)116 Test (org.junit.jupiter.api.Test)45 ServletConfig (jakarta.servlet.ServletConfig)34 Enumeration (java.util.Enumeration)29 MockServletContext (org.springframework.web.testfixture.servlet.MockServletContext)24 BeforeMethod (org.testng.annotations.BeforeMethod)22 IOException (java.io.IOException)17 FilterRegistration (jakarta.servlet.FilterRegistration)15 DelegatingFilterProxy (org.springframework.web.filter.DelegatingFilterProxy)15 ServletException (jakarta.servlet.ServletException)12 ServletContextAwareProcessor (org.springframework.web.context.support.ServletContextAwareProcessor)12 MockHttpServletRequest (org.springframework.web.testfixture.servlet.MockHttpServletRequest)12 MockHttpServletResponse (org.springframework.web.testfixture.servlet.MockHttpServletResponse)12 StaticWebApplicationContext (org.springframework.web.context.support.StaticWebApplicationContext)11 BlockingIOCometSupport (org.atmosphere.container.BlockingIOCometSupport)9 Filter (jakarta.servlet.Filter)8 WebApplicationContext (org.springframework.web.context.WebApplicationContext)8 Context (org.apache.catalina.Context)7 AtmosphereFramework (org.atmosphere.cpr.AtmosphereFramework)6 Test (org.junit.Test)6