Search in sources :

Example 6 with ServletConfig

use of javax.servlet.ServletConfig in project spring-framework by spring-projects.

the class ServletContextAwareProcessorTests method servletConfigAwareWithNullServletContextAndNonNullServletConfig.

@Test
public void servletConfigAwareWithNullServletContextAndNonNullServletConfig() {
    ServletContext servletContext = new MockServletContext();
    ServletConfig servletConfig = new MockServletConfig(servletContext);
    ServletContextAwareProcessor processor = new ServletContextAwareProcessor(null, servletConfig);
    ServletConfigAwareBean bean = new ServletConfigAwareBean();
    assertNull(bean.getServletConfig());
    processor.postProcessBeforeInitialization(bean, "testBean");
    assertNotNull("ServletConfig should have been set", bean.getServletConfig());
    assertEquals(servletConfig, bean.getServletConfig());
}
Also used : ServletContextAwareProcessor(org.springframework.web.context.support.ServletContextAwareProcessor) ServletConfig(javax.servlet.ServletConfig) MockServletConfig(org.springframework.mock.web.test.MockServletConfig) MockServletContext(org.springframework.mock.web.test.MockServletContext) ServletContext(javax.servlet.ServletContext) MockServletConfig(org.springframework.mock.web.test.MockServletConfig) MockServletContext(org.springframework.mock.web.test.MockServletContext) Test(org.junit.Test)

Example 7 with ServletConfig

use of javax.servlet.ServletConfig in project spring-framework by spring-projects.

the class ServletAnnotationControllerHandlerMethodTests method parameterDispatchingController.

@Test
public void parameterDispatchingController() throws Exception {
    final MockServletContext servletContext = new MockServletContext();
    final MockServletConfig servletConfig = new MockServletConfig(servletContext);
    WebApplicationContext webAppContext = initServlet(new ApplicationContextInitializer<GenericWebApplicationContext>() {

        @Override
        public void initialize(GenericWebApplicationContext wac) {
            wac.setServletContext(servletContext);
            AnnotationConfigUtils.registerAnnotationConfigProcessors(wac);
            wac.getBeanFactory().registerResolvableDependency(ServletConfig.class, servletConfig);
        }
    }, MyParameterDispatchingController.class);
    MockHttpServletRequest request = new MockHttpServletRequest(servletContext, "GET", "/myPath.do");
    MockHttpServletResponse response = new MockHttpServletResponse();
    HttpSession session = request.getSession();
    getServlet().service(request, response);
    assertEquals("myView", response.getContentAsString());
    assertSame(servletContext, request.getAttribute("servletContext"));
    assertSame(servletConfig, request.getAttribute("servletConfig"));
    assertSame(session.getId(), request.getAttribute("sessionId"));
    assertSame(request.getRequestURI(), request.getAttribute("requestUri"));
    assertSame(request.getLocale(), request.getAttribute("locale"));
    request = new MockHttpServletRequest(servletContext, "GET", "/myPath.do");
    response = new MockHttpServletResponse();
    session = request.getSession();
    getServlet().service(request, response);
    assertEquals("myView", response.getContentAsString());
    assertSame(servletContext, request.getAttribute("servletContext"));
    assertSame(servletConfig, request.getAttribute("servletConfig"));
    assertSame(session.getId(), request.getAttribute("sessionId"));
    assertSame(request.getRequestURI(), request.getAttribute("requestUri"));
    request = new MockHttpServletRequest(servletContext, "GET", "/myPath.do");
    request.addParameter("view", "other");
    response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertEquals("myOtherView", response.getContentAsString());
    request = new MockHttpServletRequest(servletContext, "GET", "/myPath.do");
    request.addParameter("view", "my");
    request.addParameter("lang", "de");
    response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertEquals("myLangView", response.getContentAsString());
    request = new MockHttpServletRequest(servletContext, "GET", "/myPath.do");
    request.addParameter("surprise", "!");
    response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertEquals("mySurpriseView", response.getContentAsString());
    MyParameterDispatchingController deserialized = (MyParameterDispatchingController) SerializationTestUtils.serializeAndDeserialize(webAppContext.getBean(MyParameterDispatchingController.class.getSimpleName()));
    assertNotNull(deserialized.request);
    assertNotNull(deserialized.session);
}
Also used : MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) HttpSession(javax.servlet.http.HttpSession) MockServletConfig(org.springframework.mock.web.test.MockServletConfig) ServletConfig(javax.servlet.ServletConfig) MockServletConfig(org.springframework.mock.web.test.MockServletConfig) GenericWebApplicationContext(org.springframework.web.context.support.GenericWebApplicationContext) MockServletContext(org.springframework.mock.web.test.MockServletContext) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) GenericWebApplicationContext(org.springframework.web.context.support.GenericWebApplicationContext) WebApplicationContext(org.springframework.web.context.WebApplicationContext) Test(org.junit.Test)

Example 8 with ServletConfig

use of javax.servlet.ServletConfig in project hadoop by apache.

the class TestConfServlet method verifyGetProperty.

private void verifyGetProperty(Configuration conf, String format, String propertyName) throws Exception {
    StringWriter sw = null;
    PrintWriter pw = null;
    ConfServlet service = null;
    try {
        service = new ConfServlet();
        ServletConfig servletConf = mock(ServletConfig.class);
        ServletContext context = mock(ServletContext.class);
        service.init(servletConf);
        when(context.getAttribute(HttpServer2.CONF_CONTEXT_ATTRIBUTE)).thenReturn(conf);
        when(service.getServletContext()).thenReturn(context);
        HttpServletRequest request = mock(HttpServletRequest.class);
        when(request.getHeader(HttpHeaders.ACCEPT)).thenReturn(TEST_FORMATS.get(format));
        when(request.getParameter("name")).thenReturn(propertyName);
        HttpServletResponse response = mock(HttpServletResponse.class);
        sw = new StringWriter();
        pw = new PrintWriter(sw);
        when(response.getWriter()).thenReturn(pw);
        // response request
        service.doGet(request, response);
        String result = sw.toString().trim();
        // in the response
        if (Strings.isNullOrEmpty(propertyName)) {
            for (String key : TEST_PROPERTIES.keySet()) {
                assertTrue(result.contains(key) && result.contains(TEST_PROPERTIES.get(key)));
            }
        } else {
            if (conf.get(propertyName) != null) {
                // if property name is not empty and property is found
                assertTrue(result.contains(propertyName));
                for (String key : TEST_PROPERTIES.keySet()) {
                    if (!key.equals(propertyName)) {
                        assertFalse(result.contains(key));
                    }
                }
            } else {
                // if property name is not empty, and it's not in configuration
                // expect proper error code and error message is set to the response
                Mockito.verify(response).sendError(Mockito.eq(HttpServletResponse.SC_NOT_FOUND), Mockito.eq("Property " + propertyName + " not found"));
            }
        }
    } finally {
        if (sw != null) {
            sw.close();
        }
        if (pw != null) {
            pw.close();
        }
        if (service != null) {
            service.destroy();
        }
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) StringWriter(java.io.StringWriter) ServletConfig(javax.servlet.ServletConfig) ServletContext(javax.servlet.ServletContext) HttpServletResponse(javax.servlet.http.HttpServletResponse) PrintWriter(java.io.PrintWriter)

Example 9 with ServletConfig

use of javax.servlet.ServletConfig in project jetty.project by eclipse.

the class AbstractProxyServlet method init.

@Override
public void init() throws ServletException {
    _log = createLogger();
    ServletConfig config = getServletConfig();
    _preserveHost = Boolean.parseBoolean(config.getInitParameter("preserveHost"));
    _hostHeader = config.getInitParameter("hostHeader");
    _viaHost = config.getInitParameter("viaHost");
    if (_viaHost == null)
        _viaHost = viaHost();
    try {
        _client = createHttpClient();
        // Put the HttpClient in the context to leverage ContextHandler.MANAGED_ATTRIBUTES
        getServletContext().setAttribute(config.getServletName() + ".HttpClient", _client);
        String whiteList = config.getInitParameter("whiteList");
        if (whiteList != null)
            getWhiteListHosts().addAll(parseList(whiteList));
        String blackList = config.getInitParameter("blackList");
        if (blackList != null)
            getBlackListHosts().addAll(parseList(blackList));
    } catch (Exception e) {
        throw new ServletException(e);
    }
}
Also used : ServletException(javax.servlet.ServletException) ServletConfig(javax.servlet.ServletConfig) ServletException(javax.servlet.ServletException) TimeoutException(java.util.concurrent.TimeoutException) UnknownHostException(java.net.UnknownHostException) UnavailableException(javax.servlet.UnavailableException)

Example 10 with ServletConfig

use of javax.servlet.ServletConfig in project hudson-2.x by hudson.

the class ServletRegistrationFilterAdapter method init.

public void init(final FilterConfig config) throws ServletException {
    checkNotNull(config);
    servlet.init(new ServletConfig() {

        public String getServletName() {
            return registration.getName();
        }

        public ServletContext getServletContext() {
            return config.getServletContext();
        }

        public Enumeration getInitParameterNames() {
            final Iterator<String> iter = registration.getParameters().keySet().iterator();
            return new Enumeration() {

                public boolean hasMoreElements() {
                    return iter.hasNext();
                }

                public Object nextElement() {
                    return iter.next();
                }
            };
        }

        public String getInitParameter(final String name) {
            return registration.getParameters().get(name);
        }
    });
}
Also used : Enumeration(java.util.Enumeration) ServletConfig(javax.servlet.ServletConfig) Iterator(java.util.Iterator) ServletContext(javax.servlet.ServletContext)

Aggregations

ServletConfig (javax.servlet.ServletConfig)79 ServletContext (javax.servlet.ServletContext)54 Enumeration (java.util.Enumeration)38 ServletException (javax.servlet.ServletException)24 BeforeMethod (org.testng.annotations.BeforeMethod)21 Test (org.junit.Test)17 IOException (java.io.IOException)13 HttpServletRequest (javax.servlet.http.HttpServletRequest)10 BlockingIOCometSupport (org.atmosphere.container.BlockingIOCometSupport)9 HttpServletResponse (javax.servlet.http.HttpServletResponse)8 MockServletConfig (org.springframework.mock.web.test.MockServletConfig)8 HttpServlet (javax.servlet.http.HttpServlet)7 MockServletContext (org.springframework.mock.web.test.MockServletContext)7 AtmosphereFramework (org.atmosphere.runtime.AtmosphereFramework)6 ServletContextAwareProcessor (org.springframework.web.context.support.ServletContextAwareProcessor)6 UnavailableException (javax.servlet.UnavailableException)5 SimpleBroadcaster (org.atmosphere.util.SimpleBroadcaster)5 File (java.io.File)3 PrintWriter (java.io.PrintWriter)3 AsynchronousProcessor (org.atmosphere.runtime.AsynchronousProcessor)3