Search in sources :

Example 46 with ServletConfig

use of javax.servlet.ServletConfig in project opennms by OpenNMS.

the class PollerConfigServlet method init.

/**
 * <p>init</p>
 *
 * @throws javax.servlet.ServletException if any.
 */
@Override
public void init() throws ServletException {
    ServletConfig config = this.getServletConfig();
    try {
        PollerConfigFactory.init();
        pollerFactory = PollerConfigFactory.getInstance();
        pollerConfig = pollerFactory.getConfiguration();
        if (pollerConfig == null) {
            throw new ServletException("Poller Configuration file is empty");
        }
    } catch (Throwable e) {
        throw new ServletException(e.getMessage());
    }
    initPollerServices();
    this.redirectSuccess = config.getInitParameter("redirect.success");
    if (this.redirectSuccess == null) {
        throw new ServletException("Missing required init parameter: redirect.success");
    }
}
Also used : ServletException(javax.servlet.ServletException) ServletConfig(javax.servlet.ServletConfig)

Example 47 with ServletConfig

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

the class AccessDeniedHandlerImpl method handle.

public void handle(ServletRequest request, ServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException {
    HttpServletRequest req = (HttpServletRequest) request;
    HttpServletResponse rsp = (HttpServletResponse) response;
    rsp.setStatus(HttpServletResponse.SC_FORBIDDEN);
    req.setAttribute("exception", accessDeniedException);
    Stapler stapler = new Stapler();
    stapler.init(new ServletConfig() {

        public String getServletName() {
            return "Stapler";
        }

        public ServletContext getServletContext() {
            return Hudson.getInstance().servletContext;
        }

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

        public Enumeration getInitParameterNames() {
            return new Vector().elements();
        }
    });
    stapler.invoke(req, rsp, Hudson.getInstance(), "/accessDenied");
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) Enumeration(java.util.Enumeration) ServletConfig(javax.servlet.ServletConfig) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletContext(javax.servlet.ServletContext) Stapler(org.kohsuke.stapler.Stapler) Vector(java.util.Vector)

Example 48 with ServletConfig

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

the class FastCGIProxyServlet method newHttpClient.

@Override
protected HttpClient newHttpClient() {
    ServletConfig config = getServletConfig();
    String scriptRoot = config.getInitParameter(SCRIPT_ROOT_INIT_PARAM);
    if (scriptRoot == null)
        throw new IllegalArgumentException("Mandatory parameter '" + SCRIPT_ROOT_INIT_PARAM + "' not configured");
    return new HttpClient(new ProxyHttpClientTransportOverFCGI(scriptRoot), null);
}
Also used : HttpClient(org.eclipse.jetty.client.HttpClient) ServletConfig(javax.servlet.ServletConfig)

Example 49 with ServletConfig

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

the class AbstractProxyServlet method createHttpClient.

/**
     * <p>Creates a {@link HttpClient} instance, configured with init parameters of this servlet.</p>
     * <p>The init parameters used to configure the {@link HttpClient} instance are:</p>
     * <table>
     * <caption>Init Parameters</caption>
     * <thead>
     * <tr>
     * <th>init-param</th>
     * <th>default</th>
     * <th>description</th>
     * </tr>
     * </thead>
     * <tbody>
     * <tr>
     * <td>maxThreads</td>
     * <td>256</td>
     * <td>The max number of threads of HttpClient's Executor.  If not set, or set to the value of "-", then the
     * Jetty server thread pool will be used.</td>
     * </tr>
     * <tr>
     * <td>maxConnections</td>
     * <td>32768</td>
     * <td>The max number of connections per destination, see {@link HttpClient#setMaxConnectionsPerDestination(int)}</td>
     * </tr>
     * <tr>
     * <td>idleTimeout</td>
     * <td>30000</td>
     * <td>The idle timeout in milliseconds, see {@link HttpClient#setIdleTimeout(long)}</td>
     * </tr>
     * <tr>
     * <td>timeout</td>
     * <td>60000</td>
     * <td>The total timeout in milliseconds, see {@link Request#timeout(long, java.util.concurrent.TimeUnit)}</td>
     * </tr>
     * <tr>
     * <td>requestBufferSize</td>
     * <td>HttpClient's default</td>
     * <td>The request buffer size, see {@link HttpClient#setRequestBufferSize(int)}</td>
     * </tr>
     * <tr>
     * <td>responseBufferSize</td>
     * <td>HttpClient's default</td>
     * <td>The response buffer size, see {@link HttpClient#setResponseBufferSize(int)}</td>
     * </tr>
     * </tbody>
     * </table>
     *
     * @return a {@link HttpClient} configured from the {@link #getServletConfig() servlet configuration}
     * @throws ServletException if the {@link HttpClient} cannot be created
     */
protected HttpClient createHttpClient() throws ServletException {
    ServletConfig config = getServletConfig();
    HttpClient client = newHttpClient();
    // Redirects must be proxied as is, not followed.
    client.setFollowRedirects(false);
    // Must not store cookies, otherwise cookies of different clients will mix.
    client.setCookieStore(new HttpCookieStore.Empty());
    Executor executor;
    String value = config.getInitParameter("maxThreads");
    if (value == null || "-".equals(value)) {
        executor = (Executor) getServletContext().getAttribute("org.eclipse.jetty.server.Executor");
        if (executor == null)
            throw new IllegalStateException("No server executor for proxy");
    } else {
        QueuedThreadPool qtp = new QueuedThreadPool(Integer.parseInt(value));
        String servletName = config.getServletName();
        int dot = servletName.lastIndexOf('.');
        if (dot >= 0)
            servletName = servletName.substring(dot + 1);
        qtp.setName(servletName);
        executor = qtp;
    }
    client.setExecutor(executor);
    value = config.getInitParameter("maxConnections");
    if (value == null)
        value = "256";
    client.setMaxConnectionsPerDestination(Integer.parseInt(value));
    value = config.getInitParameter("idleTimeout");
    if (value == null)
        value = "30000";
    client.setIdleTimeout(Long.parseLong(value));
    value = config.getInitParameter("timeout");
    if (value == null)
        value = "60000";
    _timeout = Long.parseLong(value);
    value = config.getInitParameter("requestBufferSize");
    if (value != null)
        client.setRequestBufferSize(Integer.parseInt(value));
    value = config.getInitParameter("responseBufferSize");
    if (value != null)
        client.setResponseBufferSize(Integer.parseInt(value));
    try {
        client.start();
        // Content must not be decoded, otherwise the client gets confused.
        client.getContentDecoderFactories().clear();
        // Pass traffic to the client, only intercept what's necessary.
        ProtocolHandlers protocolHandlers = client.getProtocolHandlers();
        protocolHandlers.clear();
        protocolHandlers.put(new ProxyContinueProtocolHandler());
        return client;
    } catch (Exception x) {
        throw new ServletException(x);
    }
}
Also used : ServletConfig(javax.servlet.ServletConfig) HttpCookieStore(org.eclipse.jetty.util.HttpCookieStore) ServletException(javax.servlet.ServletException) TimeoutException(java.util.concurrent.TimeoutException) UnknownHostException(java.net.UnknownHostException) UnavailableException(javax.servlet.UnavailableException) ServletException(javax.servlet.ServletException) Executor(java.util.concurrent.Executor) QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) HttpClient(org.eclipse.jetty.client.HttpClient) ProtocolHandlers(org.eclipse.jetty.client.ProtocolHandlers)

Example 50 with ServletConfig

use of javax.servlet.ServletConfig in project guice by google.

the class ServletDefinition method init.

public void init(final ServletContext servletContext, Injector injector, Set<HttpServlet> initializedSoFar) throws ServletException {
    // This absolutely must be a singleton, and so is only initialized once.
    if (!Scopes.isSingleton(injector.getBinding(servletKey))) {
        throw new ServletException("Servlets must be bound as singletons. " + servletKey + " was not bound in singleton scope.");
    }
    HttpServlet httpServlet = injector.getInstance(servletKey);
    this.httpServlet.set(httpServlet);
    // Only fire init() if we have not appeared before in the filter chain.
    if (initializedSoFar.contains(httpServlet)) {
        return;
    }
    //initialize our servlet with the configured context params and servlet context
    httpServlet.init(new ServletConfig() {

        @Override
        public String getServletName() {
            return servletKey.toString();
        }

        @Override
        public ServletContext getServletContext() {
            return servletContext;
        }

        @Override
        public String getInitParameter(String s) {
            return initParams.get(s);
        }

        @Override
        public Enumeration getInitParameterNames() {
            return Iterators.asEnumeration(initParams.keySet().iterator());
        }
    });
    // Mark as initialized.
    initializedSoFar.add(httpServlet);
}
Also used : ServletException(javax.servlet.ServletException) Enumeration(java.util.Enumeration) HttpServlet(javax.servlet.http.HttpServlet) ServletConfig(javax.servlet.ServletConfig) 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