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();
}
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();
}
}
}
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);
}
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
}
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);
}
Aggregations