private boolean isTempPathWritable() { File tempPath = new File(serverConfig.getTempPath()); if (!tempPath.exists()) { boolean successCreatedMissingDirectory = tempPath.mkdirs(); if (!successCreatedMissingDirectory) { LOGGER.log(Level.SEVERE, "TempPath does not exist and can not be created! PATH {0}", new Object[]{ serverConfig.getTempPath() }); return false; } } if (!tempPath.canWrite()) { LOGGER.log(Level.SEVERE, "TempPath is not writable! PATH {0}", new Object[]{ serverConfig.getTempPath() }); return false; } return true; }
private List<ServletContextImpl> getServletContexts(final ServerConfig serverConfig) { DeploymentDescriptorBuilder deploymentDescriptorBuilder = getDeploymentDescriptorBuilder(new FileSessionStorage(serverConfig.getTempPath()), serverConfig); List<ServletContextImpl> servletContexts = deploymentDescriptorBuilder.build(); for (ServletContextImpl servletContextImpl : servletContexts) { for (Map.Entry<String, Object> entry : getAdditionalServletContextAttributes().entrySet()) { servletContextImpl.setAttribute(entry.getKey(), entry.getValue()); } } return servletContexts; }
private ServerConfig getDefaultServerConfig() throws IOException { ServerConfig serverConfig = mock(ServerConfig.class); when(serverConfig.getDocumentRootPath()).thenReturn("/tmp/SomePathThatDoesNotExist"); when(serverConfig.getTempPath()).thenReturn(FileUtils.createTempDirectory()); when(serverConfig.getMaxServerThreads()).thenReturn(99); return serverConfig; } }
@Before public void setUp() throws IOException { serverConfig = mock(ServerConfig.class); serverConfigFactory = mock(ServerConfigFactory.class); serverSocketFactory = mock(ServerSocketFactory.class); ServerSocket serverSocket = mock(ServerSocket.class); when(serverSocket.accept()).thenCallRealMethod(); // Avoid NPE when(serverConfigFactory.getServerConfig()).thenReturn(serverConfig); when(serverConfig.getMaxServerThreads()).thenReturn(1); when(serverConfig.getDocumentRootPath()).thenReturn("/somepath"); when(serverConfig.getTempPath()).thenReturn(FileUtils.createTempDirectory()); when(serverSocketFactory.createServerSocket()).thenReturn(serverSocket); serverGui = mock(ServerGui.class); }
/** * Starts the web server. */ public boolean startServer() { listen = true; if (!(new File(serverConfig. getDocumentRootPath()) .isDirectory())) { LOGGER.log(Level.WARNING, "DocumentRoot does not exist! PATH {0}", new Object[]{ serverConfig.getDocumentRootPath() }); } if (!isNumberOfThreadsSufficient() || !isTempPathWritable() || !bindSocket()) { listen = false; return false; } LOGGER.log(Level.INFO, "Server has been started. Listening on port {0}", new Object[]{ serverConfig.getListenPort() }); FileUtilities.clearDirectory(serverConfig.getTempPath()); start(); return true; }
@Test public void shouldNotStartServerOnTempPathNonWritable() throws IOException { ServerSocket serverSocket = mock(ServerSocket.class); File file = new File("/proc/"); assertThat(file.exists(), is(true)); assertThat(file.canWrite(), is(false)); ServerConfig serverConfig = getDefaultServerConfig(); when(serverConfig.getTempPath()).thenReturn(file.getAbsolutePath()); WebServer webServer = new WebServer(serverSocket, serverConfig); assertThat(webServer.startServer(), is(false)); assertThat(webServer.isRunning(), is(false)); }
@Test public void shouldNotStartServerOnTempPathDoesNotExists() throws IOException { ServerSocket serverSocket = mock(ServerSocket.class); File file = new File("/proc/someprotectedresource"); assertThat(file.exists(), is(false)); ServerConfig serverConfig = getDefaultServerConfig(); when(serverConfig.getTempPath()).thenReturn(file.getAbsolutePath()); WebServer webServer = new WebServer(serverSocket, serverConfig); assertThat(webServer.startServer(), is(false)); assertThat(webServer.isRunning(), is(false)); }
@Test public void shouldCreateFromPath() throws IOException { writeFiles(DEFAULT_CONFIG_DATA + "DefaultMimeType mime/text\n"); ServerConfig serverConfig = ServerConfigImpl.createFromPath(workingDirectory, tempDirectory); assertThat(serverConfig.getTempPath(), is(tempDirectory)); assertThat(serverConfig.getBasePath(), is(workingDirectory)); assertThat(serverConfig.getDocumentRootPath(), is(workingDirectory + "wwwx")); assertThat(serverConfig.getDirectoryIndex(), hasItem("index.php")); assertThat(serverConfig.getDirectoryIndex(), hasItem("index.html")); assertThat(serverConfig.getDirectoryIndex().size(), is(2)); assertThat(serverConfig.getErrorDocument403Path(), is(workingDirectory + "error403.html")); assertThat(serverConfig.getErrorDocument404Path(), is(workingDirectory + "error404.html")); assertThat(serverConfig.getListenPort(), is(8090)); assertThat(serverConfig.getMaxServerThreads(), is(3)); assertThat(serverConfig.isKeepAlive(), is(true)); assertThat(serverConfig.getMimeTypeMapping().getMimeTypeByExtension("ANY"), is("mime/text")); assertThat(serverConfig.getAttribute(ADDITIONAL_ATTRIBUTE_NAME), is(ORIGINAL_VALUE)); }
public ServiceContainer(final ServerConfig serverConfig) { HeadersParser headersParser = new HeadersParser(); requestWrapperFactory = new HttpServletRequestImplFactory(headersParser, new QueryStringParser(), new RequestStatusParser(), new CookieParser(), new MultipartHeadersPartParser(headersParser), serverConfig.getTempPath() ); responseFactory = new HttpServletResponseImplFactory( new HeadersSerializer(), new CookieHeaderSerializer(new DateProvider()), new StreamHelper( new RangeHelper(), new RangePartHeaderSerializer() ) ); threadPoolExecutor = new ThreadPoolExecutor(1, serverConfig.getMaxServerThreads(), DEFAULT_TIME_UNITS, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(serverConfig.getMaxServerThreads() * MAX_THREADS_MULTIPLIER), Executors.defaultThreadFactory(), new ServiceUnavailableHandler(responseFactory) ); httpErrorHandlerResolver = new HttpErrorHandlerResolverImpl(serverConfig); pathHelper = new PathHelper(); }