Example usage for javax.servlet.http HttpServlet HttpServlet

List of usage examples for javax.servlet.http HttpServlet HttpServlet

Introduction

In this page you can find the example usage for javax.servlet.http HttpServlet HttpServlet.

Prototype


public HttpServlet() 

Source Link

Document

Does nothing, because this is an abstract class.

Usage

From source file:org.eclipse.equinox.http.servlet.tests.ServletTest.java

public void testHttpContextSetUser() throws ServletException, NamespaceException, IOException {
    ExtendedHttpService extendedHttpService = (ExtendedHttpService) getHttpService();

    HttpContext testContext = new HttpContext() {

        @Override//ww  w. j a  v a 2 s .  c  o m
        public boolean handleSecurity(HttpServletRequest request, HttpServletResponse response)
                throws IOException {
            request.setAttribute(HttpContext.REMOTE_USER, "TEST");
            request.setAttribute(HttpContext.AUTHENTICATION_TYPE, "Basic");
            return true;
        }

        @Override
        public URL getResource(String name) {
            return null;
        }

        @Override
        public String getMimeType(String name) {
            return null;
        }
    };
    HttpServlet testServlet = new HttpServlet() {
        private static final long serialVersionUID = 1L;

        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp)
                throws ServletException, IOException {
            resp.setContentType("text/html");
            PrintWriter out = resp.getWriter();
            out.print("USER: " + req.getRemoteUser() + " AUTH_TYPE: " + req.getAuthType());
        }

    };
    extendedHttpService.registerServlet("/" + getName(), testServlet, null, testContext);

    String expected = "USER: TEST AUTH_TYPE: Basic";
    String actual = requestAdvisor.request(getName());
    Assert.assertEquals(expected, actual);
}

From source file:org.eclipse.gyrex.admin.ui.internal.AdminUiActivator.java

private void configureContextWithServletsAndResources(final ServletContextHandler contextHandler)
        throws MalformedURLException, IOException {
    // configure context base directory (required for RAP/RWT resources)
    final IPath contextBase = Platform.getStateLocation(getBundle()).append("context");
    contextHandler.setBaseResource(Resource.newResource(contextBase.toFile()));

    // configure defaults for resources served by Jetty's DefaultServlet
    if (Platform.inDevelopmentMode()) {
        contextHandler.setInitParameter("org.eclipse.jetty.servlet.Default.dirAllowed", "true");
        contextHandler.setInitParameter("org.eclipse.jetty.servlet.Default.useFileMappedBuffer", "false");
        contextHandler.setInitParameter("org.eclipse.jetty.servlet.Default.maxCachedFiles", "0");
    } else {/*from  w w w  .ja  v a2s.c  om*/
        contextHandler.setInitParameter("org.eclipse.jetty.servlet.Default.dirAllowed", "false");
        contextHandler.setInitParameter("org.eclipse.jetty.servlet.Default.maxCacheSize", "2000000");
        contextHandler.setInitParameter("org.eclipse.jetty.servlet.Default.maxCachedFileSize", "254000");
        contextHandler.setInitParameter("org.eclipse.jetty.servlet.Default.maxCachedFiles", "1000");
        contextHandler.setInitParameter("org.eclipse.jetty.servlet.Default.useFileMappedBuffer", "true");
    }

    // initialize and start RWT application
    adminApplicationRunner = new ApplicationRunner(new AdminApplicationConfiguration(),
            contextHandler.getServletContext());
    adminApplicationRunner.start();

    // serve admin application directly
    contextHandler.addServlet(new AdminServletHolder(new RWTServlet()), "/admin");

    // register additional static resources references in body html
    final ServletHolder staticResources = new AdminServletHolder(new DefaultServlet());
    staticResources.setInitParameter("resourceBase",
            FileLocator.resolve(FileLocator.find(getBundle(), new Path("html"), null)).toExternalForm());
    contextHandler.addServlet(staticResources, "/static/*");

    // redirect to admin
    contextHandler.addServlet(new AdminServletHolder(new HttpServlet() {
        private static final long serialVersionUID = 1L;

        @Override
        protected void doGet(final HttpServletRequest req, final HttpServletResponse resp)
                throws ServletException, IOException {
            resp.sendRedirect("/admin");
        }
    }), "");

    // serve context resources (required for RAP/RWT resources)
    contextHandler.addServlet(new AdminServletHolder(new DefaultServlet()), "/*");

    // register Logback status servlet
    try {
        // note, we don't reference the class directly because the package import is optional
        final Class<?> servletClass = AdminUiActivator.getInstance().getBundle()
                .loadClass("ch.qos.logback.classic.ViewStatusMessagesServlet");
        contextHandler.addServlet(new AdminServletHolder((Servlet) servletClass.newInstance()),
                "/logbackstatus");
    } catch (final ClassNotFoundException | LinkageError e) {
        LOG.warn("Logback status servlet not available. {}", e.getMessage(), e);
    } catch (final Exception e) {
        LOG.error("An error occurred while registering the Logback status servlet. {}", e.getMessage(), e);
    }

    // allow extension using custom servlets
    final AdminServletTracker adminServletTracker = new AdminServletTracker(getBundle().getBundleContext(),
            contextHandler);
    contextHandler.addBean(new AbstractLifeCycle() {
        @Override
        protected void doStart() throws Exception {
            adminServletTracker.open();
        }

        @Override
        protected void doStop() throws Exception {
            adminServletTracker.close();
        }
    });
}

From source file:org.geoserver.test.GeoServerAbstractTestSupport.java

private void dispatch(HttpServletRequest request, HttpServletResponse response) throws Exception {
    final DispatcherServlet dispatcher = getDispatcher();

    // build a filter chain so that we can test with filters as well
    MockFilterChain chain = new MockFilterChain();
    List<Filter> filters = getFilters();
    if (filters != null) {
        for (Filter filter : filters) {
            chain.addFilter(filter);//from w  w w  .  j  a  va  2 s. com
        }
    }
    chain.setServlet(new HttpServlet() {
        @Override
        protected void service(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            try {
                //excute the pre handler step
                Collection interceptors = GeoServerExtensions.extensions(HandlerInterceptor.class,
                        applicationContext);
                for (Iterator i = interceptors.iterator(); i.hasNext();) {
                    HandlerInterceptor interceptor = (HandlerInterceptor) i.next();
                    interceptor.preHandle(request, response, dispatcher);
                }

                //execute 
                //dispatcher.handleRequest( request, response );
                dispatcher.service(request, response);

                //execute the post handler step
                for (Iterator i = interceptors.iterator(); i.hasNext();) {
                    HandlerInterceptor interceptor = (HandlerInterceptor) i.next();
                    interceptor.postHandle(request, response, dispatcher, null);
                }
            } catch (RuntimeException e) {
                throw e;
            } catch (IOException e) {
                throw e;
            } catch (ServletException e) {
                throw e;
            } catch (Exception e) {
                throw (IOException) new IOException("Failed to handle the request").initCause(e);
            }
        }
    });

    chain.doFilter(request, response);

}

From source file:org.openadaptor.auxil.connector.http.ReadConnectorServlet.java

/**
 * registers servlet//from   ww w.  j a  v a  2 s  .  com
 */
public void connect() {
    log.debug("Creating HttpServlet");
    Servlet servlet = new HttpServlet() {
        private static final long serialVersionUID = 1L;

        protected void doGet(HttpServletRequest request, HttpServletResponse response) {
            if (acceptGet) {
                process(request, response);
            } else {
                log.debug("httpGet ignored");
            }
        }

        protected void doPost(HttpServletRequest request, HttpServletResponse response) {
            process(request, response);
        }
    };
    setServlet(servlet);
    super.connect();
    log.info(getId() + " added servlet " + getServletUrl());
}

From source file:org.wso2.carbon.apimgt.authenticator.oidc.ui.internal.OIDCAuthenticatorUIDSComponent.java

protected void activate(ComponentContext ctxt) {

    if (Util.isAuthenticatorEnabled()) {
        // initialize the OIDC Config params during the start-up
        boolean initSuccess = Util.initOIDCConfigParams();

        if (initSuccess) {
            HttpServlet loginServlet = new HttpServlet() {
                @Override/* w  w w. ja  v  a2s  . c om*/
                protected void doPost(HttpServletRequest req, HttpServletResponse resp)
                        throws ServletException, IOException {

                }
            };

            Filter loginPageFilter = new LoginPageFilter();
            Dictionary loginPageFilterProps = new Hashtable(2);
            Dictionary redirectionParams = new Hashtable(3);

            redirectionParams.put("url-pattern", Util.getLoginPage());

            redirectionParams.put("associated-filter", loginPageFilter);
            redirectionParams.put("servlet-attributes", loginPageFilterProps);
            ctxt.getBundleContext().registerService(Servlet.class.getName(), loginServlet, redirectionParams);

            // register the UI authenticator
            OIDCUIAuthenticator authenticator = new OIDCUIAuthenticator();
            Hashtable<String, String> props = new Hashtable<String, String>();
            props.put(CarbonConstants.AUTHENTICATOR_TYPE, authenticator.getAuthenticatorName());
            ctxt.getBundleContext().registerService(CarbonUIAuthenticator.class.getName(), authenticator,
                    props);
            if (log.isDebugEnabled()) {
                log.debug("OIDC Authenticator FE Bundle activated successfully.");
            }
        } else {
            log.warn("Initialization failed for OIDC Authenticator. Starting with the default authenticator");
        }
    } else {
        if (log.isDebugEnabled()) {
            log.debug("OIDC Authenticator is disabled");
        }
    }
}

From source file:org.wso2.carbon.identity.authenticator.saml2.sso.ui.internal.SAML2SSOAuthenticatorUIDSComponent.java

protected void activate(ComponentContext ctxt) {
    try {/*ww w  .j  a  v  a  2s. co m*/
        if (Util.isAuthenticatorEnabled()) {
            // initialize the SSO Config params during the start-up
            boolean initSuccess = Util.initSSOConfigParams();

            if (initSuccess) {
                HttpServlet loginServlet = new HttpServlet() {
                    @Override
                    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
                            throws ServletException, IOException {
                        throw new UnsupportedOperationException();

                    }
                };

                Filter loginPageFilter = new LoginPageFilter();
                Dictionary loginPageFilterProps = new Hashtable(2);
                Dictionary redirectorParams = new Hashtable(3);

                redirectorParams.put("url-pattern", Util.getLoginPage());

                redirectorParams.put("associated-filter", loginPageFilter);
                redirectorParams.put("servlet-attributes", loginPageFilterProps);
                ctxt.getBundleContext().registerService(Servlet.class.getName(), loginServlet,
                        redirectorParams);
                // register the UI authenticator
                SAML2SSOUIAuthenticator authenticator = new SAML2SSOUIAuthenticator();
                Hashtable<String, String> props = new Hashtable<String, String>();
                props.put(CarbonConstants.AUTHENTICATOR_TYPE, authenticator.getAuthenticatorName());
                ctxt.getBundleContext().registerService(CarbonUIAuthenticator.class.getName(), authenticator,
                        props);
                if (log.isDebugEnabled()) {
                    log.debug("SAML2 SSO Authenticator BE Bundle activated successfully.");
                }
            } else {
                log.warn(
                        "Initialization failed for SSO Authenticator. Starting with the default authenticator");
            }
        } else {
            if (log.isDebugEnabled()) {
                log.debug("SAML2 SSO Authenticator is disabled");
            }
        }
    } catch (Throwable e) {
        log.error("Saml Authentication Failed");
    }
}

From source file:org.wso2.carbon.identity.authenticator.webseal.ui.internal.Activator.java

public void start(BundleContext bc) {

    WebSealUIAuthenticator authenticator = new WebSealUIAuthenticator();
    Hashtable<String, String> props = new Hashtable<String, String>();
    props.put(CarbonConstants.AUTHENTICATOR_TYPE, authenticator.getAuthenticatorName());
    bc.registerService(CarbonUIAuthenticator.class.getName(), authenticator, props);

    //register log-out filter
    Utils.initConfig();/*  www .jav a2  s .co  m*/
    HttpServlet logOutServlet = new HttpServlet() {
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp)
                throws ServletException, IOException {

        }
    };

    Filter logOutPageFilter = new LogOutPageFilter();
    Dictionary logOutPageFilterProps = new Hashtable(2);
    Dictionary redirectorParams = new Hashtable(3);

    redirectorParams.put("url-pattern", Utils.getLogOutPage());

    redirectorParams.put("associated-filter", logOutPageFilter);
    redirectorParams.put("servlet-attributes", logOutPageFilterProps);
    bc.registerService(Servlet.class.getName(), logOutServlet, redirectorParams);

    //Register the SSO Assertion Consumer Service Servlet
    HttpServlet acsServlet = new WebSealConsumerService();
    Dictionary acsParams = new Hashtable(2);
    acsParams.put("url-pattern", "/webseal");
    acsParams.put("display-name", "WebSeal Consumer Service");
    bc.registerService(Servlet.class.getName(), acsServlet, acsParams);

    log.info("WebSeal Authenticator FE Bundle activated successfully.");

}

From source file:org.wso2.carbon.sso.redirector.custom.ui.SSOCustomRedirectorServiceComponent.java

protected void activate(ComponentContext ctxt) {
    log.debug("Stratos SSO Custom Redirector bundle is activating..");
    // register a servlet filter for SSO redirector page
    HttpServlet redirectJSPRedirectorServlet = new HttpServlet() {
        protected void doGet(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
        }//from w  w  w.  j  a  v  a  2  s . c o  m
    };

    Filter redirectPageFilter = new CustomRedirectorJSPFilter();
    Dictionary redirectorPageFilterAttrs = new Hashtable(2);
    Dictionary redirectorPageFilterParams = new Hashtable(2);
    redirectorPageFilterParams.put("url-pattern", "/carbon/stratos-auth/redirect_ajaxprocessor.jsp");
    //        redirectorPageFilterParams.put("url-pattern", "/carbon/sso-acs/redirect_ajaxprocessor.jsp");
    redirectorPageFilterParams.put("associated-filter", redirectPageFilter);
    redirectorPageFilterParams.put("servlet-attributes", redirectorPageFilterAttrs);
    ctxt.getBundleContext().registerService(Servlet.class.getName(), redirectJSPRedirectorServlet,
            redirectorPageFilterParams);
    log.debug("Stratos SSO Custom Redirector bundle is activated..");
}

From source file:org.wso2.stratos.identity.saml2.sso.mgt.ui.internal.StratosSSOMgtUIServiceComponent.java

protected void activate(ComponentContext ctxt) {
    // register a servlet filter for SSO login page
    HttpServlet loginPageRedirectorServlet = new HttpServlet() {
        protected void doGet(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
        }//from w  w w . ja va2s  .co m
    };

    Filter ssoLoginPageFilter = new SSOLoginPageFilter();
    Dictionary loginPageFilterAttrs = new Hashtable(2);
    Dictionary loginPageFilterParams = new Hashtable(2);
    loginPageFilterParams.put("url-pattern", "/carbon/sso-saml/login.jsp");
    loginPageFilterParams.put("associated-filter", ssoLoginPageFilter);
    loginPageFilterParams.put("servlet-attributes", loginPageFilterAttrs);
    ctxt.getBundleContext().registerService(Servlet.class.getName(), loginPageRedirectorServlet,
            loginPageFilterParams);

    HttpServlet redirectJSPRedirectorServlet = new HttpServlet() {
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp)
                throws ServletException, IOException {
        }
    };

    // register a servlet filter for SSO redirect page
    Filter redirectPageFilter = new SSORedirectPageFilter();
    Dictionary redirectorPageFilterAttrs = new Hashtable(2);
    Dictionary redirectorPageFilterParams = new Hashtable(2);
    redirectorPageFilterParams.put("url-pattern", "/carbon/sso-saml/redirect_ajaxprocessor.jsp");
    redirectorPageFilterParams.put("associated-filter", redirectPageFilter);
    redirectorPageFilterParams.put("servlet-attributes", redirectorPageFilterAttrs);
    ctxt.getBundleContext().registerService(Servlet.class.getName(), redirectJSPRedirectorServlet,
            redirectorPageFilterParams);

    // Read the config parameters from sso-idp-config.xml
    Util.populateLoginPageConfigParams();
}