Search in sources :

Example 96 with ServletContext

use of javax.servlet.ServletContext in project uavstack by uavorg.

the class JettyPlusIT method onAppStarting.

/**
 * onAppStarting
 *
 * @param args
 */
public void onAppStarting(Object... args) {
    WebAppContext sc = (WebAppContext) args[0];
    InterceptSupport iSupport = InterceptSupport.instance();
    InterceptContext context = iSupport.createInterceptContext(Event.WEBCONTAINER_INIT);
    context.put(InterceptConstants.WEBAPPLOADER, sc.getClassLoader());
    context.put(InterceptConstants.WEBWORKDIR, sc.getServletContext().getRealPath(""));
    context.put(InterceptConstants.CONTEXTPATH, sc.getContextPath());
    context.put(InterceptConstants.APPNAME, sc.getDisplayName());
    ServletContext sContext = sc.getServletContext();
    context.put(InterceptConstants.SERVLET_CONTEXT, sContext);
    getBasePath(context, sContext);
    iSupport.doIntercept(context);
}
Also used : WebAppContext(org.eclipse.jetty.webapp.WebAppContext) InterceptContext(com.creditease.monitor.interceptframework.spi.InterceptContext) InterceptSupport(com.creditease.monitor.interceptframework.InterceptSupport) ServletContext(javax.servlet.ServletContext)

Example 97 with ServletContext

use of javax.servlet.ServletContext in project BIMserver by opensourceBIM.

the class Jsr356Impl method onOpen.

@OnOpen
public void onOpen(Session websocketSession, EndpointConfig config) {
    LOGGER.debug("WebSocket open");
    try {
        this.websocketSession = websocketSession;
        ServletContext servletContext = servletContexts.get(websocketSession.getContainer());
        if (servletContext == null) {
            servletContext = defaultServletContext;
        }
        BimServer bimServer = (BimServer) servletContext.getAttribute("bimserver");
        streamer = new Streamer(this, bimServer);
        streamer.onOpen();
    } catch (Throwable t) {
        LOGGER.error("", t);
    }
}
Also used : Streamer(org.bimserver.servlets.Streamer) BimServer(org.bimserver.BimServer) ServletContext(javax.servlet.ServletContext) OnOpen(javax.websocket.OnOpen)

Example 98 with ServletContext

use of javax.servlet.ServletContext in project BIMserver by opensourceBIM.

the class WarServerInitializer method contextInitialized.

public void contextInitialized(ServletContextEvent servletContextEvent) {
    ServletContext servletContext = servletContextEvent.getServletContext();
    Path homeDir = null;
    if (servletContext.getAttribute("homedir") != null) {
        homeDir = Paths.get((String) servletContext.getAttribute("homedir"));
    }
    if (homeDir == null && servletContext.getInitParameter("homedir") != null) {
        homeDir = Paths.get(servletContext.getInitParameter("homedir"));
    }
    boolean autoMigrate = false;
    if (servletContext.getAttribute("autoMigrate") != null) {
        autoMigrate = (Boolean) servletContext.getAttribute("autoMigrate");
    }
    if (autoMigrate == false && servletContext.getInitParameter("autoMigrate") != null) {
        autoMigrate = Boolean.valueOf(servletContext.getInitParameter("autoMigrate"));
    }
    String realPath = servletContext.getRealPath("/");
    if (!realPath.endsWith("/")) {
        realPath = realPath + "/";
    }
    Path baseDir = Paths.get(realPath + "WEB-INF");
    if (homeDir == null) {
        homeDir = baseDir;
    }
    ResourceFetcher resourceFetcher = new WarResourceFetcher(servletContext, homeDir);
    BimServerConfig config = new BimServerConfig();
    config.setAutoMigrate(autoMigrate);
    config.setEnvironment(Environment.WAR);
    config.setHomeDir(homeDir);
    config.setResourceFetcher(resourceFetcher);
    if (homeDir != null) {
        // Basically doing this twice (also in BimServer.init), but this makes sure the logback.xml file is copied to the homedir
        try {
            BimServer.initHomeDir(config);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    setupLogging(homeDir);
    try {
        fixLogging(config);
        config.setClassPath(makeClassPath(resourceFetcher.getFile("lib")));
    } catch (IOException e1) {
        e1.printStackTrace();
    }
    config.setStartEmbeddedWebServer(false);
    bimServer = new BimServer(config);
    Jsr356Impl.setDefaultServletContext(servletContextEvent.getServletContext());
    Logger LOGGER = LoggerFactory.getLogger(WarServerInitializer.class);
    LOGGER.info("Servlet Context Name: " + servletContext.getServletContextName());
    try {
        bimServer.start();
    } catch (ServerException e) {
        LOGGER.error("", e);
    } catch (DatabaseInitException e) {
        LOGGER.error("", e);
    } catch (BimserverDatabaseException e) {
        LOGGER.error("", e);
    } catch (PluginException e) {
        LOGGER.error("", e);
    } catch (DatabaseRestartRequiredException e) {
        LOGGER.error("", e);
    }
    servletContext.setAttribute("bimserver", bimServer);
}
Also used : Path(java.nio.file.Path) ServerException(org.bimserver.shared.exceptions.ServerException) WarResourceFetcher(org.bimserver.resources.WarResourceFetcher) BimServer(org.bimserver.BimServer) PluginException(org.bimserver.shared.exceptions.PluginException) WarResourceFetcher(org.bimserver.resources.WarResourceFetcher) ResourceFetcher(org.bimserver.plugins.ResourceFetcher) IOException(java.io.IOException) BimServerConfig(org.bimserver.BimServerConfig) Logger(org.slf4j.Logger) DatabaseInitException(org.bimserver.database.berkeley.DatabaseInitException) BimserverDatabaseException(org.bimserver.BimserverDatabaseException) ServletContext(javax.servlet.ServletContext) DatabaseRestartRequiredException(org.bimserver.database.DatabaseRestartRequiredException)

Example 99 with ServletContext

use of javax.servlet.ServletContext in project cuba by cuba-platform.

the class PortalAppContextLoader method createApplicationContext.

@Override
protected ApplicationContext createApplicationContext(String[] locations) {
    CubaXmlWebApplicationContext webContext = new CubaXmlWebApplicationContext();
    String[] classPathLocations = new String[locations.length];
    for (int i = 0; i < locations.length; i++) {
        classPathLocations[i] = "classpath:" + locations[i];
    }
    webContext.setConfigLocations(classPathLocations);
    webContext.setServletContext(ServletContextHolder.getServletContext());
    webContext.refresh();
    ServletContext servletContext = ServletContextHolder.getServletContext();
    if (servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE) != null) {
        throw new IllegalStateException("Cannot initialize context because there is already a root application context present - " + "check whether you have multiple ContextLoader* definitions in your web.xml!");
    }
    servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, webContext);
    return webContext;
}
Also used : CubaXmlWebApplicationContext(com.haulmont.cuba.core.sys.CubaXmlWebApplicationContext) ServletContext(javax.servlet.ServletContext)

Example 100 with ServletContext

use of javax.servlet.ServletContext in project thymeleaf-tests by thymeleaf.

the class TestMockServletUtil method createServletContext.

public static ServletContext createServletContext(final Map<String, Object> attributes) {
    final String contextName = "/testing";
    final ServletContext servletContext = Mockito.mock(ServletContext.class);
    Mockito.when(servletContext.getAttributeNames()).thenAnswer(new GetVariableNamesAnswer(attributes));
    Mockito.when(servletContext.getAttribute(Matchers.anyString())).thenAnswer(new GetAttributeAnswer(attributes));
    Mockito.doAnswer(new SetAttributeAnswer(attributes)).when(servletContext).setAttribute(Matchers.anyString(), Matchers.anyObject());
    Mockito.doAnswer(new RemoveAttributeAnswer(attributes)).when(servletContext).removeAttribute(Matchers.anyString());
    Mockito.when(servletContext.getContextPath()).thenReturn(contextName);
    Mockito.when(servletContext.getInitParameterNames()).thenReturn(new ObjectEnumeration<String>(null));
    Mockito.when(servletContext.getInitParameter(Matchers.anyString())).thenReturn(null);
    return servletContext;
}
Also used : ServletContext(javax.servlet.ServletContext)

Aggregations

ServletContext (javax.servlet.ServletContext)1059 Test (org.junit.Test)231 HttpServletRequest (javax.servlet.http.HttpServletRequest)171 IOException (java.io.IOException)138 HttpServletResponse (javax.servlet.http.HttpServletResponse)127 ServletException (javax.servlet.ServletException)95 File (java.io.File)75 ServletConfig (javax.servlet.ServletConfig)68 FilterConfig (javax.servlet.FilterConfig)65 HashMap (java.util.HashMap)63 Enumeration (java.util.Enumeration)52 InputStream (java.io.InputStream)51 ArrayList (java.util.ArrayList)49 URL (java.net.URL)47 HttpSession (javax.servlet.http.HttpSession)43 Map (java.util.Map)38 PrintWriter (java.io.PrintWriter)32 List (java.util.List)32 RequestDispatcher (javax.servlet.RequestDispatcher)30 WebApplicationContext (org.springframework.web.context.WebApplicationContext)28