Search in sources :

Example 86 with HttpServlet

use of javax.servlet.http.HttpServlet in project tomcat by apache.

the class TestExpiresFilter method testUseMajorTypeExpiresConfiguration.

@Test
public void testUseMajorTypeExpiresConfiguration() throws Exception {
    HttpServlet servlet = new HttpServlet() {

        private static final long serialVersionUID = 1L;

        @Override
        protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            response.setContentType("text/json; charset=iso-8859-1");
            response.getWriter().print("Hello world");
        }
    };
    validate(servlet, Integer.valueOf(7 * 60));
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServlet(javax.servlet.http.HttpServlet) HttpServletResponse(javax.servlet.http.HttpServletResponse) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Example 87 with HttpServlet

use of javax.servlet.http.HttpServlet in project hive by apache.

the class HttpServer method initializeWebServer.

void initializeWebServer(Builder b) {
    // Create the thread pool for the web server to handle HTTP requests
    QueuedThreadPool threadPool = new QueuedThreadPool();
    if (b.maxThreads > 0) {
        threadPool.setMaxThreads(b.maxThreads);
    }
    threadPool.setDaemon(true);
    threadPool.setName(b.name + "-web");
    webServer.setThreadPool(threadPool);
    // Create the channel connector for the web server
    Connector connector = createChannelConnector(threadPool.getMaxThreads(), b);
    connector.setHost(b.host);
    connector.setPort(b.port);
    webServer.addConnector(connector);
    RewriteHandler rwHandler = new RewriteHandler();
    rwHandler.setRewriteRequestURI(true);
    rwHandler.setRewritePathInfo(false);
    RewriteRegexRule rootRule = new RewriteRegexRule();
    rootRule.setRegex("^/$");
    rootRule.setReplacement(b.contextRootRewriteTarget);
    rootRule.setTerminating(true);
    rwHandler.addRule(rootRule);
    rwHandler.setHandler(webAppContext);
    // Configure web application contexts for the web server
    ContextHandlerCollection contexts = new ContextHandlerCollection();
    contexts.addHandler(rwHandler);
    webServer.setHandler(contexts);
    addServlet("jmx", "/jmx", JMXJsonServlet.class);
    addServlet("conf", "/conf", ConfServlet.class);
    addServlet("stacks", "/stacks", StackServlet.class);
    for (Pair<String, Class<? extends HttpServlet>> p : b.servlets) {
        addServlet(p.getFirst(), "/" + p.getFirst(), p.getSecond());
    }
    ServletContextHandler staticCtx = new ServletContextHandler(contexts, "/static");
    staticCtx.setResourceBase(appDir + "/static");
    staticCtx.addServlet(DefaultServlet.class, "/*");
    staticCtx.setDisplayName("static");
    String logDir = getLogDir(b.conf);
    if (logDir != null) {
        ServletContextHandler logCtx = new ServletContextHandler(contexts, "/logs");
        setContextAttributes(logCtx.getServletContext(), b.contextAttrs);
        logCtx.addServlet(AdminAuthorizedServlet.class, "/*");
        logCtx.setResourceBase(logDir);
        logCtx.setDisplayName("logs");
    }
}
Also used : SelectChannelConnector(org.eclipse.jetty.server.nio.SelectChannelConnector) SslSelectChannelConnector(org.eclipse.jetty.server.ssl.SslSelectChannelConnector) Connector(org.eclipse.jetty.server.Connector) QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) RewriteRegexRule(org.eclipse.jetty.rewrite.handler.RewriteRegexRule) HttpServlet(javax.servlet.http.HttpServlet) ContextHandlerCollection(org.eclipse.jetty.server.handler.ContextHandlerCollection) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) RewriteHandler(org.eclipse.jetty.rewrite.handler.RewriteHandler)

Example 88 with HttpServlet

use of javax.servlet.http.HttpServlet in project Openfire by igniterealtime.

the class PluginServlet method handleDevJSP.

/**
     * Handles a request for a JSP page in development mode. If development mode is
     * not enabled, this method returns false so that normal JSP handling can be performed.
     * If development mode is enabled, this method tries to locate the JSP, compile
     * it using JSPC, and then return the output.
     *
     * @param pathInfo the extra path info.
     * @param request  the request object.
     * @param response the response object.
     * @return true if this page request was handled; false if the request was not handled.
     */
private boolean handleDevJSP(String pathInfo, HttpServletRequest request, HttpServletResponse response) {
    String jspURL = pathInfo.substring(1);
    // Handle pre-existing pages and fail over to pre-compiled pages.
    int fileSeperator = jspURL.indexOf("/");
    if (fileSeperator != -1) {
        String pluginName = jspURL.substring(0, fileSeperator);
        Plugin plugin = pluginManager.getPlugin(pluginName);
        PluginDevEnvironment environment = pluginManager.getDevEnvironment(plugin);
        // If development mode not turned on for plugin, return false.
        if (environment == null) {
            return false;
        }
        File webDir = environment.getWebRoot();
        if (webDir == null || !webDir.exists()) {
            return false;
        }
        File pluginDirectory = pluginManager.getPluginDirectory(plugin);
        File compilationDir = new File(pluginDirectory, "classes");
        compilationDir.mkdirs();
        String jsp = jspURL.substring(fileSeperator + 1);
        int indexOfLastSlash = jsp.lastIndexOf("/");
        String relativeDir = "";
        if (indexOfLastSlash != -1) {
            relativeDir = jsp.substring(0, indexOfLastSlash);
            relativeDir = relativeDir.replaceAll("//", ".") + '.';
        }
        File jspFile = new File(webDir, jsp);
        String filename = jspFile.getName();
        int indexOfPeriod = filename.indexOf(".");
        if (indexOfPeriod != -1) {
            filename = "dev" + StringUtils.randomString(4);
        }
        JspC jspc = new JspC();
        if (!jspFile.exists()) {
            return false;
        }
        try {
            jspc.setJspFiles(jspFile.getCanonicalPath());
        } catch (IOException e) {
            Log.error(e.getMessage(), e);
        }
        jspc.setOutputDir(compilationDir.getAbsolutePath());
        jspc.setClassName(filename);
        jspc.setCompile(true);
        jspc.setClassPath(getClasspathForPlugin(plugin));
        jspc.execute();
        try {
            Object servletInstance = pluginManager.loadClass(plugin, "org.apache.jsp." + relativeDir + filename).newInstance();
            HttpServlet servlet = (HttpServlet) servletInstance;
            servlet.init(servletConfig);
            servlet.service(request, response);
            return true;
        } catch (Exception e) {
            Log.error(e.getMessage(), e);
        }
    }
    return false;
}
Also used : HttpServlet(javax.servlet.http.HttpServlet) IOException(java.io.IOException) File(java.io.File) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException) JasperException(org.apache.jasper.JasperException) JspC(org.apache.jasper.JspC)

Example 89 with HttpServlet

use of javax.servlet.http.HttpServlet in project druid by druid-io.

the class AsyncQueryForwardingServletTest method makeTestDeleteServer.

private static Server makeTestDeleteServer(int port, final CountDownLatch latch) {
    Server server = new Server(port);
    ServletHandler handler = new ServletHandler();
    handler.addServletWithMapping(new ServletHolder(new HttpServlet() {

        @Override
        protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            latch.countDown();
            resp.setStatus(200);
        }
    }), "/default/*");
    server.setHandler(handler);
    return server;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletHandler(org.eclipse.jetty.servlet.ServletHandler) Server(org.eclipse.jetty.server.Server) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) HttpServlet(javax.servlet.http.HttpServlet) HttpServletResponse(javax.servlet.http.HttpServletResponse)

Example 90 with HttpServlet

use of javax.servlet.http.HttpServlet in project OpenAM by OpenRock.

the class RestEndpointServletTest method shouldHandleRequestWithRestletServlet.

@Test(dataProvider = "restletPaths", enabled = false)
public void shouldHandleRequestWithRestletServlet(String path, HttpServlet servlet) throws Exception {
    //Given
    HttpServletRequest request = mock(HttpServletRequest.class);
    HttpServletResponse response = mock(HttpServletResponse.class);
    given(request.getHeaderNames()).willReturn(Collections.enumeration(Collections.<String>emptySet()));
    given(request.getAttributeNames()).willReturn(Collections.enumeration(Collections.<String>emptySet()));
    given(request.getServletPath()).willReturn(path);
    restEndpointServlet.init();
    //When
    restEndpointServlet.service(request, response);
    //Then
    verify(servlet).service(Matchers.<HttpServletRequest>anyObject(), eq(response));
    for (HttpServlet s : Arrays.asList(restletXACMLHttpServlet, restletOAuth2ServiceServlet, restletUMAServiceServlet)) {
        if (s != servlet) {
            verifyZeroInteractions(s);
        }
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServlet(javax.servlet.http.HttpServlet) HttpServletResponse(javax.servlet.http.HttpServletResponse) Test(org.testng.annotations.Test)

Aggregations

HttpServlet (javax.servlet.http.HttpServlet)173 HttpServletRequest (javax.servlet.http.HttpServletRequest)152 HttpServletResponse (javax.servlet.http.HttpServletResponse)152 IOException (java.io.IOException)130 ServletException (javax.servlet.ServletException)128 Test (org.junit.Test)117 CountDownLatch (java.util.concurrent.CountDownLatch)68 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)61 InterruptedIOException (java.io.InterruptedIOException)55 ServletOutputStream (javax.servlet.ServletOutputStream)36 AsyncContext (javax.servlet.AsyncContext)34 HttpFields (org.eclipse.jetty.http.HttpFields)32 MetaData (org.eclipse.jetty.http.MetaData)32 HeadersFrame (org.eclipse.jetty.http2.frames.HeadersFrame)32 ServletInputStream (javax.servlet.ServletInputStream)31 Session (org.eclipse.jetty.http2.api.Session)30 Stream (org.eclipse.jetty.http2.api.Stream)27 Response (org.eclipse.jetty.client.api.Response)26 HttpContentResponse (org.eclipse.jetty.client.HttpContentResponse)24 HashMap (java.util.HashMap)22