本文整理汇总了Java中org.eclipse.jetty.server.handler.ContextHandler类的典型用法代码示例。如果您正苦于以下问题:Java ContextHandler类的具体用法?Java ContextHandler怎么用?Java ContextHandler使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
ContextHandler类属于org.eclipse.jetty.server.handler包,在下文中一共展示了ContextHandler类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: startJetty
import org.eclipse.jetty.server.handler.ContextHandler; //导入依赖的package包/类
private void startJetty() throws Exception {
QueuedThreadPool jettyThreadPool = new QueuedThreadPool(jettyServerThreads);
Server server = new Server(jettyThreadPool);
ServerConnector http = new ServerConnector(server, new HttpConnectionFactory());
http.setPort(jettyListenPort);
server.addConnector(http);
ContextHandler contextHandler = new ContextHandler();
contextHandler.setHandler(botSocketHandler);
server.setHandler(contextHandler);
server.start();
server.join();
}
示例2: getAllServices
import org.eclipse.jetty.server.handler.ContextHandler; //导入依赖的package包/类
private HandlerList getAllServices() throws Exception{
// File server & Context Handler for root, also setting the index.html
// to be the "welcome file", i.e, autolink on root addresses.
ContextHandler rootContext = new ContextHandler();
rootContext.setContextPath("/*");
rootContext.setHandler(getResourceHandlers());
// Possible servlet lists, for all servlets or custom services you want to access later.
// Warning, it might become a little bit nested if you add to many classes.
ServletHandler questionHandler = new ServletHandler();
questionHandler.addServletWithMapping(QuestionHandler.class, "/question");
// Add the ResourceHandler to the server.
HandlerList handlers = new HandlerList();
handlers.setHandlers(new Handler[] {
rootContext ,
questionHandler,
});
return handlers;
}
示例3: testGetConfigWithLocalFileAndWithRemoteConfig
import org.eclipse.jetty.server.handler.ContextHandler; //导入依赖的package包/类
@Test
public void testGetConfigWithLocalFileAndWithRemoteConfig() throws Exception {
String someKey = "someKey";
String someValue = "someValue";
String anotherValue = "anotherValue";
Properties properties = new Properties();
properties.put(someKey, someValue);
createLocalCachePropertyFile(properties);
ApolloConfig apolloConfig = assembleApolloConfig(ImmutableMap.of(someKey, anotherValue));
ContextHandler handler = mockConfigServerHandler(HttpServletResponse.SC_OK, apolloConfig);
startServerWithHandlers(handler);
Config config = ConfigService.getAppConfig();
assertEquals(anotherValue, config.getProperty(someKey, null));
}
示例4: mockConfigServerHandler
import org.eclipse.jetty.server.handler.ContextHandler; //导入依赖的package包/类
private ContextHandler mockConfigServerHandler(final int statusCode, final ApolloConfig result,
final boolean failedAtFirstTime) {
ContextHandler context = new ContextHandler("/configs/*");
context.setHandler(new AbstractHandler() {
AtomicInteger counter = new AtomicInteger(0);
@Override
public void handle(String target, Request baseRequest, HttpServletRequest request,
HttpServletResponse response) throws IOException, ServletException {
if (failedAtFirstTime && counter.incrementAndGet() == 1) {
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
baseRequest.setHandled(true);
return;
}
response.setContentType("application/json;charset=UTF-8");
response.setStatus(statusCode);
response.getWriter().println(gson.toJson(result));
baseRequest.setHandled(true);
}
});
return context;
}
示例5: main
import org.eclipse.jetty.server.handler.ContextHandler; //导入依赖的package包/类
public static void main(String[] args) throws Exception {
Server server = new Server(8080);
// connector
// server.getConnectors()[0].getConnectionFactory(HttpConnectionFactory.class)
// .setHttpCompliance(HttpCompliance.LEGACY);
// server.setHandler(new HelloHandler("Hi JettyEmbeded "," light测试"));
// Add a single handler on context "/hello"
ContextHandler context = new ContextHandler();
context.setContextPath( "/hello" );
context.setHandler( new HelloHandler("Hi JettyEmbeded "," light测试") );
// Can be accessed using http://localhost:8080/hello
server.setHandler( context );
server.start();
server.join();
}
示例6: main
import org.eclipse.jetty.server.handler.ContextHandler; //导入依赖的package包/类
public static void main(String[] args) throws Exception {
final ApiServer api = new ApiServer(new InetSocketAddress(9998));
PrometheusConfig cfg = createPrometheusConfig(args);
final Optional<File> _cfg = cfg.getConfiguration();
if (_cfg.isPresent())
registry_ = new PipelineBuilder(_cfg.get()).build();
else
registry_ = new PipelineBuilder(Configuration.DEFAULT).build();
api.start();
Runtime.getRuntime().addShutdownHook(new Thread(api::close));
Server server = new Server(cfg.getPort());
ContextHandler context = new ContextHandler();
context.setClassLoader(Thread.currentThread().getContextClassLoader());
context.setContextPath(cfg.getPath());
context.setHandler(new DisplayMetrics(registry_));
server.setHandler(context);
server.start();
server.join();
}
示例7: lifeCycleStarted
import org.eclipse.jetty.server.handler.ContextHandler; //导入依赖的package包/类
@Override
public void lifeCycleStarted(LifeCycle bean) {
if (bean instanceof Server) {
Server server = (Server)bean;
Connector[] connectors = server.getConnectors();
if (connectors == null || connectors.length == 0) {
server.dumpStdErr();
throw new IllegalStateException("No Connector");
} else if (!Arrays.stream(connectors).allMatch(Connector::isStarted)) {
server.dumpStdErr();
throw new IllegalStateException("Connector not started");
}
ContextHandler context = server.getChildHandlerByClass(ContextHandler.class);
if (context == null || !context.isAvailable()) {
server.dumpStdErr();
throw new IllegalStateException("No Available Context");
}
}
}
示例8: invalidateAll
import org.eclipse.jetty.server.handler.ContextHandler; //导入依赖的package包/类
public void invalidateAll(String sessionId) {
synchronized (sessionsIds) {
sessionsIds.remove(sessionId);
//tell all contexts that may have a session object with this id to
//get rid of them
Handler[] contexts = server.getChildHandlersByClass(ContextHandler.class);
for (int i = 0; contexts != null && i < contexts.length; i++) {
SessionHandler sessionHandler = ((ContextHandler) contexts[i]).getChildHandlerByClass(SessionHandler.class);
if (sessionHandler != null) {
SessionManager manager = sessionHandler.getSessionManager();
if (manager != null && manager instanceof HazelcastSessionManager) {
((HazelcastSessionManager) manager).invalidateSession(sessionId);
}
}
}
}
}
示例9: renewSessionId
import org.eclipse.jetty.server.handler.ContextHandler; //导入依赖的package包/类
@Override
public void renewSessionId(String oldClusterId, String oldNodeId, HttpServletRequest request) {
//generate a new id
String newClusterId = newSessionId(request.hashCode());
synchronized (sessionsIds) {
//remove the old one from the list
sessionsIds.remove(oldClusterId);
//add in the new session id to the list
sessionsIds.add(newClusterId);
//tell all contexts to update the id
Handler[] contexts = server.getChildHandlersByClass(ContextHandler.class);
for (int i = 0; contexts != null && i < contexts.length; i++) {
SessionHandler sessionHandler = ((ContextHandler) contexts[i]).getChildHandlerByClass(SessionHandler.class);
if (sessionHandler != null) {
SessionManager manager = sessionHandler.getSessionManager();
if (manager != null && manager instanceof HazelcastSessionManager) {
((HazelcastSessionManager) manager).
renewSessionId(oldClusterId, oldNodeId, newClusterId, getNodeId(newClusterId, request));
}
}
}
}
}
示例10: invalidateAll
import org.eclipse.jetty.server.handler.ContextHandler; //导入依赖的package包/类
@Override
public void invalidateAll(String sessionId) {
synchronized (sessionsIds) {
sessionsIds.remove(sessionId);
//tell all contexts that may have a session object with this id to
//get rid of them
Handler[] contexts = server.getChildHandlersByClass(ContextHandler.class);
for (int i = 0; contexts != null && i < contexts.length; i++) {
SessionHandler sessionHandler = ((ContextHandler) contexts[i]).getChildHandlerByClass(SessionHandler.class);
if (sessionHandler != null) {
SessionManager manager = sessionHandler.getSessionManager();
if (manager != null && manager instanceof HazelcastSessionManager) {
((HazelcastSessionManager) manager).invalidateSession(sessionId);
}
}
}
}
}
示例11: logStartupBanner
import org.eclipse.jetty.server.handler.ContextHandler; //导入依赖的package包/类
private static void logStartupBanner(Server server) {
Object banner = null;
ContextHandler contextHandler = server.getChildHandlerByClass(ContextHandler.class);
if (contextHandler != null) {
Context context = contextHandler.getServletContext();
if (context != null) {
banner = context.getAttribute("nexus-banner");
}
}
StringBuilder buf = new StringBuilder();
buf.append("\n-------------------------------------------------\n\n");
buf.append("Started ").append(banner instanceof String ? banner : "Nexus Repository Manager");
buf.append("\n\n-------------------------------------------------");
log.info(buf.toString());
}
示例12: jsp
import org.eclipse.jetty.server.handler.ContextHandler; //导入依赖的package包/类
ContextHandler jsp(String ctx, File templateDir, String descriptor) {
File tempDir = new File(System.getProperty("java.io.tmpdir"));
File scratchDir = new File(tempDir.toString(), "jtr");
if (!scratchDir.exists()) {
if (!scratchDir.mkdirs()) {
throw new RuntimeException("Unable to create scratch directory: " + scratchDir);
}
}
final WebAppContext context = new WebAppContext(templateDir.getAbsolutePath(), ctx);
final URL url = getClass().getProtectionDomain().getCodeSource().getLocation();
if (url != null) {
// tld could be scanned under the META-INF/ after add shade jar
context.getMetaData().addWebInfJar(Resource.newResource(url));
descriptor = descriptor == null ? "jar:" + url + "!/empty-web.xml" : descriptor;
context.setDescriptor(descriptor);
}
context.setAttribute(INCLUDE_JAR_PATTERN, JSP_PATTERN);
context.setAttribute("javax.servlet.context.tempdir", scratchDir);
context.setAttribute("org.eclipse.jetty.containerInitializers", jspInitializers());
context.addBean(new ServletContainerInitializersStarter(context), true);
context.setClassLoader(new URLClassLoader(new URL[0], getClass().getClassLoader()));
return context;
}
示例13: main
import org.eclipse.jetty.server.handler.ContextHandler; //导入依赖的package包/类
public static void main(String[] args) throws Exception {
URI baseUri = UriBuilder.fromUri("http://localhost").port(SERVER_PORT)
.build();
ResourceConfig config = new ResourceConfig(Calculator.class);
Server server = JettyHttpContainerFactory.createServer(baseUri, config,
false);
ContextHandler contextHandler = new ContextHandler("/rest");
contextHandler.setHandler(server.getHandler());
ProtectionDomain protectionDomain = EmbeddedServer.class
.getProtectionDomain();
URL location = protectionDomain.getCodeSource().getLocation();
ResourceHandler resourceHandler = new ResourceHandler();
resourceHandler.setWelcomeFiles(new String[] { "index.html" });
resourceHandler.setResourceBase(location.toExternalForm());
System.out.println(location.toExternalForm());
HandlerCollection handlerCollection = new HandlerCollection();
handlerCollection.setHandlers(new Handler[] { resourceHandler,
contextHandler, new DefaultHandler() });
server.setHandler(handlerCollection);
server.start();
server.join();
}
示例14: createContextHandler
import org.eclipse.jetty.server.handler.ContextHandler; //导入依赖的package包/类
@Override
public ContextHandler createContextHandler(Environment environment) {
String appName = environment.getApplicationName();
String docsRootPath = environment.getValue(appName + ".docs.root");
if (docsRootPath != null && containsDocumentation(Paths.get(docsRootPath))) {
return createDocumentationContext(Paths.get(docsRootPath));
}
Path root = environment.getApplicationRoot();
if (root != null && containsDocumentation(root.resolve(DEFAULT_DOCS_FOLDER))) {
return createDocumentationContext(root.resolve(DEFAULT_DOCS_FOLDER));
}
String appBasePath = environment.getValue(APPLICATION_BASE);
if (appBasePath != null && Files.isDirectory(Paths.get(appBasePath))
&& containsDocumentation(Paths.get(appBasePath).resolve(DEFAULT_DOCS_FOLDER))) {
return createDocumentationContext(Paths.get(appBasePath).resolve(DEFAULT_DOCS_FOLDER));
}
return null;
}
示例15: createContextHandler
import org.eclipse.jetty.server.handler.ContextHandler; //导入依赖的package包/类
@Override
public ContextHandler createContextHandler(App app) throws IOException {
URL appResource = ClassLoader.getSystemResource(app.getOriginId());
if (appResource == null) {
throw new FileNotFoundException("Web application not found");
}
WebAppContext contextHandler = new WebAppContext();
// Excluding some from the server classes for Weld to work.
contextHandler.prependServerClass(
"-org.eclipse.jetty.server.handler.ContextHandler");
contextHandler.prependServerClass(
"-org.eclipse.jetty.servlet.ServletContextHandler");
contextHandler.prependServerClass(
"-org.eclipse.jetty.servlet.ServletHandler");
contextHandler.setWar(appResource.toString());
return contextHandler;
}