org.rapidoid.setup.App Java Examples

The following examples show how to use org.rapidoid.setup.App. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: HttpBeanParamsTest.java    From rapidoid with Apache License 2.0 6 votes vote down vote up
@Test
@ExpectErrors
public void testBeanParams() {
	App.beans(new Object() {

		@GET
		@SuppressWarnings("unchecked")
		public List<Object> pers(String name, Person person, int id, Integer xx) {
			return U.list(id, name, person, xx);
		}

	});

	onlyGet("/pers?name=Einstein&id=1000");
	onlyGet("/pers?name=Mozart");
	onlyGet("/pers?id=200");
}
 
Example #2
Source File: OverviewHandler.java    From rapidoid with Apache License 2.0 6 votes vote down vote up
@Override
public Object call() {
	List<Object> info = U.list();

	info.add(h3(center("Basic overview:")));

	Map<String, Object> appInfo = U.map();

	if (!Msc.isPlatform()) {
		appInfo.put("Application JAR", ClasspathUtil.appJar());
		appInfo.put("Application path (root packages)", App.path());
	}

	appInfo.put("Active profiles", Env.profiles());
	appInfo.put("Command line arguments", Env.args());

	info.add(grid(appInfo));

	info.add(h3(center("System metrics:")));
	info.add(GraphsHandler.graphs(3));

	return multi(info);
}
 
Example #3
Source File: HttpPojoControllerTest.java    From rapidoid with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Test
public void testPojoHandlersWithIoC() {
	notFound("/b");

	List<String> ctrls = Scan.annotated(MyTestController.class).in(U.list("pkg1", "pkg2")).getAll();
	isTrue(ctrls.isEmpty());

	List<String> ctrls2 = Scan.annotated(MyTestController.class, Generated.class).in(App.path()).getAll();
	eq(ctrls2, U.list(Ff.class.getName()));

	Scan.annotated(MyTestController.class, MyTestController.class).in(App.path()).forEach(cls -> App.beans(IoC.singleton(cls)));

	onlyGet("/b");
	onlyGet("/x");
	notFound("/x");
}
 
Example #4
Source File: HttpTestCommons.java    From rapidoid with Apache License 2.0 6 votes vote down vote up
@BeforeEach
public void openContext() {
	Msc.reset();

	ClasspathUtil.setRootPackage("some.nonexisting.app");

	JPAUtil.reset();
	Conf.ROOT.setPath(getTestNamespace());
	IoC.reset();

	App.resetGlobalState();
	On.changes().ignore();

	On.setup().activate();
	On.setup().reload();

	verifyNoRoutes();
}
 
Example #5
Source File: JPAInjectionTest.java    From rapidoid with Apache License 2.0 6 votes vote down vote up
@Test
public void testJPAInjection() {
	JPA.bootstrap(path());
	App.path(path());
	App.scan();

	postData("/books?title=a", U.map("title", "My Book 1"));
	postData("/books?title=b", U.map("title", "My Book 2"));
	postData("/books?title=c", U.map("title", "My Book 3"));

	onlyGet("/allBooks");

	onlyGet("/del?id=1");
	getAndPost("/del2?id=2");
	onlyPost("/del3?id=3");
	onlyPost("/del4?id=3");

	onlyGet("/allBooks?finally");
}
 
Example #6
Source File: Main.java    From rapidoid with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) {

		// first thing to do - initializing Rapidoid, without bootstrapping anything at the moment
		App.run(args); // instead of App.bootstrap(args), which might start the server

		// customizing the server address and port - before the server is bootstrapped
		On.address("0.0.0.0").port(9998);
		Admin.address("127.0.0.1").port(9999);

		// fine-tuning the HTTP server
		Conf.HTTP.set("maxPipeline", 32);
		Conf.NET.set("bufSizeKB", 16);

		// now bootstrap some components, e.g. classpath scanning (beans)
		App.scan();

		Boot.jmx()
			.adminCenter();

		// continue with normal setup
		On.get("/x").json("x");
	}
 
Example #7
Source File: Main.java    From rapidoid with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) {
	App.run(args);

	On.get("/hello").plain((req, resp) -> {

		req.async(); // mark asynchronous request processing

		// send part 1
		resp.chunk("part 1".getBytes());

		// after some time, send part 2 and finish
		Jobs.after(100).milliseconds(() -> {
			resp.chunk(" & part 2".getBytes());
			resp.done();
		});

		return resp;
	});
}
 
Example #8
Source File: RestServer.java    From apollo with Apache License 2.0 6 votes vote down vote up
@PostConstruct
public void start() {
    if (configuration.getApi().isDisableApiServer()) {
        logger.info("Not starting the API...");
        return;
    }

    registerLoginProvider();
    registerRolesProvider();

    ApiConfiguration apiConfiguration = configuration.getApi();
    String[] args = new String[] {
            "secret=" + apiConfiguration.getSecret(),
            "on.address=" + apiConfiguration.getListen(),
            "on.port=" + apiConfiguration.getPort()
    };

    // Initialize the REST API server
    On.changes().ignore();
    GuiceBeans beans = Integrate.guice(injector);
    App.run(args).auth();
    App.register(beans);
}
 
Example #9
Source File: GuiceIntegrationExample.java    From rapidoid with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) {
	Log.info("Starting application");

	// disable Rapidoid's hot class reloading, it doesn't play well with Guice
	On.changes().ignore();

	GuiceBeans beans = Integrate.guice(new WebModule());
	App.register(beans);

	// test the RESTful service
	Self.get("/add?x=6&y=4").print();
	Self.get("/add?x=1&y=22").expect().entry("sum", 23);

	// usually you wouldn't shutdown the application
	App.shutdown();
}
 
Example #10
Source File: HttpReregistrationTest.java    From rapidoid with Apache License 2.0 5 votes vote down vote up
@Test
public void testControllerDeregistration() {
	Object ctrl1 = ctrl1("next");
	Object ctrl2 = ctrl2();

	notFound("/inc");
	notFound("/dec");

	App.beans(ctrl1);
	verifyRoutes("ctrl1");

	onlyGet("/inc?x=5");
	notFound("/dec");

	App.setup().deregister(ctrl1);
	verifyNoRoutes();

	App.beans(ctrl2);
	verifyRoutes("ctrl2");

	onlyPost("/dec?x=12");
	notFound("/inc");

	App.setup().deregister(ctrl2.getClass());
	verifyNoRoutes();

	notFound("/inc");
	notFound("/dec");
}
 
Example #11
Source File: HttpPojoApiTest.java    From rapidoid with Apache License 2.0 5 votes vote down vote up
@Test
public void test8() {
	App.beans(new Object() {
		@PUT
		public Object test8(Req req, Resp resp) {
			return U.set("b", 0, false);
		}
	});

	notFound("/");
	onlyPut("/test8");
}
 
Example #12
Source File: HttpReregistrationTest.java    From rapidoid with Apache License 2.0 5 votes vote down vote up
@Test
public void testControllerReregistration() {
	notFound("/inc");
	notFound("/dec");

	App.beans(ctrl1("nextA"));
	verifyRoutes("ctrl1");

	onlyGet("/inc?x=100");

	App.beans(ctrl1("nextB"));
	verifyRoutes("ctrl1");

	onlyGet("/inc?x=200");

	App.beans(ctrl1("nextC"));
	verifyRoutes("ctrl1");

	onlyGet("/inc?x=300");

	// can deregister with other instance, only the class matters for deregistration, not the instance
	App.setup().deregister(ctrl1("invisible"));
	verifyNoRoutes();

	notFound("/inc");
	notFound("/dec");
}
 
Example #13
Source File: HttpReregistrationTest.java    From rapidoid with Apache License 2.0 5 votes vote down vote up
@Test
public void testLambdaDeregistration() {
	notFound("/foo");

	On.page("/foo").html((Req req, Integer x) -> req.data() + ":" + x);
	verifyRoutes("foo");

	getAndPost("/foo?a=12&x=3");

	App.setup().deregister("GET,POST", "/foo");
	verifyNoRoutes();

	notFound("/foo");
}
 
Example #14
Source File: HttpWrapTest.java    From rapidoid with Apache License 2.0 5 votes vote down vote up
@Test
public void testWrap() {
	App.defaults().wrappers((req, invocation) -> invocation.invokeAndTransformResult(x -> x + "!"));
	App.defaults().contentType(MediaType.BINARY);

	On.get("/").html(() -> "a");

	onlyGet("/");
}
 
Example #15
Source File: HttpErrorHandlerTest.java    From rapidoid with Apache License 2.0 5 votes vote down vote up
@Test
public void testErrorHandler1() {
	App.custom().errorHandler((req, resp, e) -> {
		if (e instanceof NotFound)
			return Defaults.errorHandler().handleError(req, resp, e); // default error processing
		return req + ":err:" + e;
	});

	On.get("/err").html((ReqHandler) req -> {
		String s = null;
		return s.toString(); // NPE
	});

	onlyGet("/err?x=1");
}
 
Example #16
Source File: HttpRolesTest.java    From rapidoid with Apache License 2.0 5 votes vote down vote up
@Test
public void testRoles() {
	App.scan(path());

	App.defaults().roles("aa", "bb");

	On.get("/a").json(() -> "ok");
	On.get("/ok").roles().json(() -> "ok");

	onlyGet("/a");
	onlyGet("/ok");

	verifyRoutes();
}
 
Example #17
Source File: HttpPojoApiTest.java    From rapidoid with Apache License 2.0 5 votes vote down vote up
@Test
public void test5() {
	App.beans(new Object() {
		@DELETE
		public boolean test5() {
			return true;
		}
	});

	notFound("/");
	onlyDelete("/test5");
}
 
Example #18
Source File: HttpPojoApiTest.java    From rapidoid with Apache License 2.0 5 votes vote down vote up
@Test
public void test7() {
	App.beans(new Object() {
		@POST
		public Object test7(Req req, Resp resp) {
			return U.list("a", 123, true);
		}
	});

	notFound("/");
	onlyPost("/test7");
}
 
Example #19
Source File: HttpPojoApiTest.java    From rapidoid with Apache License 2.0 5 votes vote down vote up
@Test
public void test6() {
	App.beans(new Object() {
		@GET
		public Object test6(Req req, Resp resp) {
			return U.map("a", 1, "b", 2);
		}
	});

	notFound("/");
	onlyGet("/test6");
}
 
Example #20
Source File: Main.java    From FrameworkBenchmarks with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static void main(String[] args) {
	App.run(args);

	Conf.HTTP.set("maxPipeline", 128);
	Conf.HTTP.set("timeout", 0);
	Conf.HTTP.sub("mandatoryHeaders").set("connection", false);

	On.port(8080);

	if (Env.hasAnyProfile("mysql", "postgres")) {
		setupDbHandlers();
	} else {
		setupSimpleHandlers();
	}
}
 
Example #21
Source File: HttpBeanValidationTest.java    From rapidoid with Apache License 2.0 5 votes vote down vote up
@Test
public void testValidation() {
	App.path("org.rapidoid.validation");
	App.bootstrap(new String[0]);
	JPA.bootstrap(App.path());

	onlyGet("/echo?num=123");
	onlyGet("/echo");

	onlyGet("/validating?num=123");
	onlyGet("/validating");

	onlyPost("/save?num=123");
	onlyPost("/save");
}
 
Example #22
Source File: WebShutdownTest.java    From rapidoid with Apache License 2.0 5 votes vote down vote up
@Test
public void step2() {
	App.run(new String[0]);

	On.get("/x").plain("X");

	onlyGet("/x");
}
 
Example #23
Source File: GuiceIntegrationTest.java    From rapidoid with Apache License 2.0 5 votes vote down vote up
@Test
public void testGuiceIntegration() {
	Beans beans = Integrate.guice(new MathModule());
	App.register(beans);

	Self.get("/add?x=6&y=4").expect().entry("sum", 10);
	Self.get("/add?x=1&y=22").expect().entry("sum", 23);
}
 
Example #24
Source File: HttpMainEntryTest.java    From rapidoid with Apache License 2.0 5 votes vote down vote up
@Test
public void testSequentialControllerRegistration() {
	App.path(path());
	App.scan();
	On.get("/a").plain("A");

	onlyGet("/a");
	onlyGet("/b");
}
 
Example #25
Source File: HttpRecursiveMainEntryTest.java    From rapidoid with Apache License 2.0 5 votes vote down vote up
@Test
public void testSequentialControllerRegistration() {
	App.path(path());

	App.scan();
	On.get("/a").plain("A");

	onlyGet("/a");
	onlyGet("/b");
	onlyGet("/c");
}
 
Example #26
Source File: HttpTemplatesPathTest.java    From rapidoid with Apache License 2.0 5 votes vote down vote up
@Test
public void testTemplatesPath1() {
	App.custom().templatesPath("test-templates");

	Templates.setPath("something-different");
	eq(Templates.getPath(), U.array("something-different"));

	setupAndTest();
}
 
Example #27
Source File: HttpModule.java    From rapidoid with Apache License 2.0 5 votes vote down vote up
@Override
public void cleanUp() {
	My.reset();
	App.resetGlobalState();
	On.changes().ignore();

	Setups.clear();

	Env.reset();
}
 
Example #28
Source File: HttpPojoControllerTest.java    From rapidoid with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Test
public void testPojoHandlers() {
	App.beans(new Object() {

		@GET(uri = "/a")
		public Object theFoo() {
			return "foo";
		}

		@POST(uri = "/x")
		public Object x(Req req, Resp resp) {
			return "x";
		}
	});

	onlyGet("/a");
	onlyPost("/x");
	notFound("/b");

	List<String> ctrls = Scan.annotated(MyTestController.class).in("pkg1", "pkg2").getAll();
	isTrue(ctrls.isEmpty());

	List<String> ctrls2 = Scan.annotated(MyTestController.class).in("non-existing-pkg", "").getAll();
	eq(ctrls2, U.list(Ff.class.getName()));

	Scan.annotated(MyTestController.class, MyTestController.class).in(App.path()).forEach(App::beans);

	onlyGet("/a");
	onlyGet("/b");
	onlyPost("/x");
}
 
Example #29
Source File: HttpStaticFilesTest.java    From rapidoid with Apache License 2.0 5 votes vote down vote up
@Test
public void serveStaticFiles() {
	App.custom().staticFilesPath("static1", "non-existing-location", "static2");

	On.get("/c").managed(false).contentType(MediaType.JSON).serve("override");

	onlyGet("/"); // home page
	onlyGet("/index"); // home page
	onlyGet("/index.html"); // home page

	onlyGet("/a");
	onlyGet("/a.html");

	onlyGet("/b");
	onlyGet("/c");

	onlyGet("/dir1/sub1.txt");

	// no private files (starting with '.')
	notFound("/dir1/.sub2.txt");
	notFound("/.priv.txt");

	// no folders
	Res dir1 = Res.from("dir1", "static2");
	isFalse(dir1.exists());
	notFound("/dir1");

	notFound("/xx");
	notFound("/page1");
	notFound("/page2");
}
 
Example #30
Source File: HttpPojoApiTest.java    From rapidoid with Apache License 2.0 5 votes vote down vote up
@Test
public void test12() {
	App.beans(new Ctrl2());

	notFound("/");

	onlyGet("/x");
	onlyPost("/y");
	getAndPost("/p");

	notFound("/z");
	notFound("/w");
}