Search in sources :

Example 86 with ServletContext

use of javax.servlet.ServletContext in project hadoop by apache.

the class ConfServlet method doGet.

@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // If user is a static user and auth Type is null, that means
    // there is a non-security environment and no need authorization,
    // otherwise, do the authorization.
    final ServletContext servletContext = getServletContext();
    if (!HttpServer2.isStaticUserAndNoneAuthType(servletContext, request) && !HttpServer2.isInstrumentationAccessAllowed(servletContext, request, response)) {
        return;
    }
    String format = parseAccecptHeader(request);
    if (FORMAT_XML.equals(format)) {
        response.setContentType("text/xml; charset=utf-8");
    } else if (FORMAT_JSON.equals(format)) {
        response.setContentType("application/json; charset=utf-8");
    }
    String name = request.getParameter("name");
    Writer out = response.getWriter();
    try {
        writeResponse(getConfFromContext(), out, format, name);
    } catch (BadFormatException bfe) {
        response.sendError(HttpServletResponse.SC_BAD_REQUEST, bfe.getMessage());
    } catch (IllegalArgumentException iae) {
        response.sendError(HttpServletResponse.SC_NOT_FOUND, iae.getMessage());
    }
    out.close();
}
Also used : ServletContext(javax.servlet.ServletContext) Writer(java.io.Writer)

Example 87 with ServletContext

use of javax.servlet.ServletContext in project hadoop by apache.

the class TestConfServlet method verifyGetProperty.

private void verifyGetProperty(Configuration conf, String format, String propertyName) throws Exception {
    StringWriter sw = null;
    PrintWriter pw = null;
    ConfServlet service = null;
    try {
        service = new ConfServlet();
        ServletConfig servletConf = mock(ServletConfig.class);
        ServletContext context = mock(ServletContext.class);
        service.init(servletConf);
        when(context.getAttribute(HttpServer2.CONF_CONTEXT_ATTRIBUTE)).thenReturn(conf);
        when(service.getServletContext()).thenReturn(context);
        HttpServletRequest request = mock(HttpServletRequest.class);
        when(request.getHeader(HttpHeaders.ACCEPT)).thenReturn(TEST_FORMATS.get(format));
        when(request.getParameter("name")).thenReturn(propertyName);
        HttpServletResponse response = mock(HttpServletResponse.class);
        sw = new StringWriter();
        pw = new PrintWriter(sw);
        when(response.getWriter()).thenReturn(pw);
        // response request
        service.doGet(request, response);
        String result = sw.toString().trim();
        // in the response
        if (Strings.isNullOrEmpty(propertyName)) {
            for (String key : TEST_PROPERTIES.keySet()) {
                assertTrue(result.contains(key) && result.contains(TEST_PROPERTIES.get(key)));
            }
        } else {
            if (conf.get(propertyName) != null) {
                // if property name is not empty and property is found
                assertTrue(result.contains(propertyName));
                for (String key : TEST_PROPERTIES.keySet()) {
                    if (!key.equals(propertyName)) {
                        assertFalse(result.contains(key));
                    }
                }
            } else {
                // if property name is not empty, and it's not in configuration
                // expect proper error code and error message is set to the response
                Mockito.verify(response).sendError(Mockito.eq(HttpServletResponse.SC_NOT_FOUND), Mockito.eq("Property " + propertyName + " not found"));
            }
        }
    } finally {
        if (sw != null) {
            sw.close();
        }
        if (pw != null) {
            pw.close();
        }
        if (service != null) {
            service.destroy();
        }
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) StringWriter(java.io.StringWriter) ServletConfig(javax.servlet.ServletConfig) ServletContext(javax.servlet.ServletContext) HttpServletResponse(javax.servlet.http.HttpServletResponse) PrintWriter(java.io.PrintWriter)

Example 88 with ServletContext

use of javax.servlet.ServletContext in project ranger by apache.

the class RangerKmsAuthorizerTest method startServers.

@BeforeClass
public static void startServers() throws Exception {
    if (!UNRESTRICTED_POLICIES_INSTALLED) {
        return;
    }
    DerbyTestUtils.startDerby();
    Path configDir = Paths.get("src/test/resources/kms");
    Path logDir = Paths.get("target");
    System.setProperty(KMSConfiguration.KMS_CONFIG_DIR, configDir.toFile().getAbsolutePath());
    System.setProperty("kms.log.dir", logDir.toFile().getAbsolutePath());
    System.setProperty("hostname", "localhost");
    System.setProperty("user", "autotest");
    // Start KMSWebApp
    ServletContextEvent servletContextEvent = Mockito.mock(ServletContextEvent.class);
    ServletContext servletContext = Mockito.mock(ServletContext.class);
    Mockito.when(servletContextEvent.getServletContext()).thenReturn(servletContext);
    kmsWebapp = new KMSWebApp();
    kmsWebapp.contextInitialized(servletContextEvent);
}
Also used : Path(java.nio.file.Path) KMSWebApp(org.apache.hadoop.crypto.key.kms.server.KMSWebApp) ServletContext(javax.servlet.ServletContext) ServletContextEvent(javax.servlet.ServletContextEvent) BeforeClass(org.junit.BeforeClass)

Example 89 with ServletContext

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

the class SpringBootTomcatPlusIT method onAppStart.

/**
 * onAppStart
 *
 * @param args
 */
@Override
public void onAppStart(Object... args) {
    StandardContext sc = (StandardContext) args[0];
    String contextPath = (String) ReflectionHelper.getField(StandardContext.class, sc, "encodedPath", true);
    // springboot use threadlocalContext to store the WEBCONTAINER_STARTED Event context, just return when it's uav's inner app in case of rewriting the real app's context
    if ("/com.creditease.uav".equals(contextPath)) {
        return;
    }
    InterceptSupport iSupport = InterceptSupport.instance();
    InterceptContext context = iSupport.getThreadLocalContext(Event.WEBCONTAINER_STARTED);
    /**
     * NOTE: spring boot rewrite the tomcat webappclassloader, makes the addURL for nothing, then we can't do
     * anything on this we may use its webappclassloader's parent as the classloader
     */
    context.put(InterceptConstants.WEBAPPLOADER, sc.getLoader().getClassLoader().getParent());
    context.put(InterceptConstants.WEBWORKDIR, sc.getWorkPath());
    context.put(InterceptConstants.CONTEXTPATH, contextPath);
    if (context.get(InterceptConstants.APPNAME) == null) {
        context.put(InterceptConstants.APPNAME, ReflectionHelper.getField(StandardContext.class, sc, "displayName", true));
    }
    ServletContext sContext = (ServletContext) ReflectionHelper.getField(StandardContext.class, sc, "context", true);
    context.put(InterceptConstants.SERVLET_CONTEXT, sContext);
    String basePath = sContext.getRealPath("");
    /*
         * NOTE: springboot couldn't get the basePath through method "getRealPath", temporary process
         */
    if (basePath == null) {
        basePath = "";
    } else if (basePath.lastIndexOf("/") == (basePath.length() - 1) || basePath.lastIndexOf("\\") == (basePath.length() - 1)) {
        basePath = basePath.substring(0, basePath.length() - 1);
    }
    context.put(InterceptConstants.BASEPATH, basePath);
// we don't doIntercept here cause some pre-profile(like dubbo) not happen yet, profile will be done after
// finishRefresh
}
Also used : InterceptContext(com.creditease.monitor.interceptframework.spi.InterceptContext) InterceptSupport(com.creditease.monitor.interceptframework.InterceptSupport) StandardContext(org.apache.catalina.core.StandardContext) ServletContext(javax.servlet.ServletContext)

Example 90 with ServletContext

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

the class TomcatPlusIT method onAppStarting.

/**
 * onAppStarting
 *
 * @param args
 */
public void onAppStarting(Object... args) {
    // release the left contexts
    StandardInterceptContextHelper.releaseContext(Event.WEBCONTAINER_RESOURCE_INIT);
    StandardInterceptContextHelper.releaseContext(Event.WEBCONTAINER_RESOURCE_CREATE);
    StandardContext sc = (StandardContext) args[0];
    InterceptSupport iSupport = InterceptSupport.instance();
    InterceptContext context = iSupport.createInterceptContext(Event.WEBCONTAINER_INIT);
    context.put(InterceptConstants.WEBAPPLOADER, sc.getLoader().getClassLoader());
    context.put(InterceptConstants.WEBWORKDIR, sc.getWorkPath());
    context.put(InterceptConstants.CONTEXTPATH, ReflectionHelper.getField(StandardContext.class, sc, "encodedPath", true));
    context.put(InterceptConstants.APPNAME, ReflectionHelper.getField(StandardContext.class, sc, "displayName", true));
    ServletContext sContext = (ServletContext) ReflectionHelper.getField(StandardContext.class, sc, "context", true);
    context.put(InterceptConstants.SERVLET_CONTEXT, sContext);
    getBasePath(context, sContext);
    iSupport.doIntercept(context);
}
Also used : InterceptContext(com.creditease.monitor.interceptframework.spi.InterceptContext) InterceptSupport(com.creditease.monitor.interceptframework.InterceptSupport) StandardContext(org.apache.catalina.core.StandardContext) 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