本文整理汇总了Java中org.wisdom.api.Controller类的典型用法代码示例。如果您正苦于以下问题:Java Controller类的具体用法?Java Controller怎么用?Java Controller使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Controller类属于org.wisdom.api包,在下文中一共展示了Controller类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: collectRouteFromControllerAnnotations
import org.wisdom.api.Controller; //导入依赖的package包/类
/**
* Collects the @Route annotation on <em>action</em> method.
* This set will be added at the end of the list retrieved from the {@link org.wisdom.api
* .Controller#routes()}
*
* @param controller the controller
* @return the list of route, empty if none are available
*/
public static List<Route> collectRouteFromControllerAnnotations(Controller controller) {
String prefix = getPath(controller);
List<Route> routes = new ArrayList<>();
Method[] methods = controller.getClass().getMethods();
for (Method method : methods) {
org.wisdom.api.annotations.Route annotation = method.getAnnotation(org.wisdom.api.annotations.Route.class);
if (annotation != null) {
String uri = annotation.uri();
uri = getPrefixedUri(prefix, uri);
final Route route = new RouteBuilder().route(annotation.method())
.on(uri)
.to(controller, method)
.accepting(annotation.accepts())
.producing(annotation.produces());
routes.add(route);
}
}
return routes;
}
示例2: testAcceptWhenNoAcceptOrRequest
import org.wisdom.api.Controller; //导入依赖的package包/类
@Test
public void testAcceptWhenNoAcceptOrRequest() throws Exception {
Controller controller = new DefaultController() {
public Result method1() {
return null;
}
};
Route route1 = new RouteBuilder().route(HttpMethod.GET).on("/foo").to(controller, "method1").accepts
("text/plain");
assertThat(route1.isCompliantWithRequestContentType(null)).isEqualTo(2);
route1 = new RouteBuilder().route(HttpMethod.GET).on("/foo").to(controller, "method1");
Request request1 = mock(Request.class);
when(request1.contentMimeType()).thenReturn("text/plain");
assertThat(route1.isCompliantWithRequestContentType(request1)).isEqualTo(2);
}
示例3: testOk
import org.wisdom.api.Controller; //导入依赖的package包/类
@Test
public void testOk() throws InterruptedException, IOException {
Router router = prepareServer();
// Prepare the router with a controller
Controller controller = new DefaultController() {
@SuppressWarnings("unused")
public Result index() {
return ok("Alright");
}
};
Route route = new RouteBuilder().route(HttpMethod.GET)
.on("/")
.to(controller, "index");
when(router.getRouteFor(anyString(), anyString(), any(Request.class))).thenReturn(route);
server.start();
waitForStart(server);
int port = server.httpPort();
URL url = new URL("http://localhost:" + port + "/");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
assertThat(connection.getResponseCode()).isEqualTo(200);
String body = IOUtils.toString(connection.getInputStream());
assertThat(body).isEqualTo("Alright");
}
示例4: testInternalError
import org.wisdom.api.Controller; //导入依赖的package包/类
@Test
public void testInternalError() throws InterruptedException, IOException {
Router router = prepareServer();
// Prepare the router with a controller
Controller controller = new DefaultController() {
@SuppressWarnings("unused")
public Result index() throws IOException {
throw new IOException("My bad");
}
};
Route route = new RouteBuilder().route(HttpMethod.GET)
.on("/")
.to(controller, "index");
when(router.getRouteFor(anyString(), anyString(), any(Request.class))).thenReturn(route);
server.start();
waitForStart(server);
int port = server.httpPort();
URL url = new URL("http://localhost:" + port + "/");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
assertThat(connection.getResponseCode()).isEqualTo(500);
}
示例5: testJavaScript
import org.wisdom.api.Controller; //导入依赖的package包/类
@Test
public void testJavaScript() {
TemplateEngine engine = createWisdomEngine();
engine.initialize();
Context context = new Context();
context.setVariable("test", "test");
FakeRouter router = new FakeRouter();
Controller controller = new FakeController();
router.addController(controller);
Assets assets = mock(Assets.class);
context.setVariable(Routes.ROUTES_VAR, new Routes(router, assets, controller));
String processed = engine.process("templates/javascript.thl.html", context);
assertThat(processed).containsIgnoringCase("var t = 'test';");
assertThat(processed).containsIgnoringCase("var url = '/';");
assertThat(processed).containsIgnoringCase("$(document).ready(function () {");
}
示例6: testSessionScope
import org.wisdom.api.Controller; //导入依赖的package包/类
@Test
public void testSessionScope() {
final WisdomTemplateEngine engine = createWisdomEngine();
engine.initialize();
final Template template = mock(Template.class);
when(template.fullName()).thenReturn("templates/var.thl.html");
final FakeRouter router = new FakeRouter();
final Controller controller = new FakeController();
router.addController(controller);
final Assets assets = mock(Assets.class);
Action.ActionResult result = action(new Invocation() {
@Override
public Result invoke() throws Throwable {
return ok(engine.process(template, controller, router, assets, ImmutableMap.<String, Object>of("key",
"test")));
}
}).with(new FakeContext().addToSession("key2", "session")).invoke();
String content = (String) result.getResult().getRenderable().content();
assertThat(content)
.contains("<span>KEY</span> = <span>test</span>")
.contains("<span>KEY2</span> = <span>session</span>");
}
示例7: testFlashScope
import org.wisdom.api.Controller; //导入依赖的package包/类
@Test
public void testFlashScope() {
final WisdomTemplateEngine engine = createWisdomEngine();
engine.initialize();
final Template template = mock(Template.class);
when(template.fullName()).thenReturn("templates/var.thl.html");
final FakeRouter router = new FakeRouter();
final Controller controller = new FakeController();
router.addController(controller);
final Assets assets = mock(Assets.class);
Action.ActionResult result = action(new Invocation() {
@Override
public Result invoke() throws Throwable {
context().flash().put("key2", "ongoing");
return ok(engine.process(template, controller, router, assets, ImmutableMap.<String, Object>of()));
}
}).with(new FakeContext().addToFlash("key", "incoming")).invoke();
String content = (String) result.getResult().getRenderable().content();
assertThat(content)
.contains("<span>KEY</span> = <span>incoming</span>")
.contains("<span>KEY2</span> = <span>ongoing</span>");
}
示例8: testParameter
import org.wisdom.api.Controller; //导入依赖的package包/类
@Test
public void testParameter() {
final WisdomTemplateEngine engine = createWisdomEngine();
engine.initialize();
final Template template = mock(Template.class);
when(template.fullName()).thenReturn("templates/var.thl.html");
final FakeRouter router = new FakeRouter();
final Controller controller = new FakeController();
router.addController(controller);
final Assets assets = mock(Assets.class);
Action.ActionResult result = action(new Invocation() {
@Override
public Result invoke() throws Throwable {
context().session().put("key2", "ongoing");
return ok(engine.process(template, controller, router, assets, ImmutableMap.<String, Object>of()));
}
}).parameter("key", "param").invoke();
String content = (String) result.getResult().getRenderable().content();
assertThat(content)
.contains("<span>KEY</span> = <span>param</span>")
.contains("<span>KEY2</span> = <span>ongoing</span>");
}
示例9: testCustomDialect
import org.wisdom.api.Controller; //导入依赖的package包/类
@Test
public void testCustomDialect() {
MyDialect dialect = new MyDialect();
final WisdomTemplateEngine engine = createWisdomEngine(ImmutableSet.<IDialect>of(dialect));
engine.initialize();
final Template template = mock(Template.class);
when(template.fullName()).thenReturn("templates/dialect.thl.html");
final FakeRouter router = new FakeRouter();
final Controller controller = new FakeController();
router.addController(controller);
final Assets assets = mock(Assets.class);
Action.ActionResult result = action(new Invocation() {
@Override
public Result invoke() throws Throwable {
return ok(engine.process(template, controller, router, assets, ImmutableMap.<String, Object>of()));
}
}).parameter("key", "param").invoke();
String content = (String) result.getResult().getRenderable().content();
assertThat(content).contains("Hello, World!");
}
示例10: switchToHeadWhenGetRouteExist
import org.wisdom.api.Controller; //导入依赖的package包/类
@Test
public void switchToHeadWhenGetRouteExist() throws Exception {
DefaultPageErrorHandler handler = new DefaultPageErrorHandler();
handler.router = mock(Router.class);
handler.configuration = mock(ApplicationConfiguration.class);
when(handler.configuration.isDev()).thenReturn(false);
Controller controller = new MyController();
Route route = new Route(HttpMethod.GET, "/", controller, controller.getClass().getMethod("action"));
Route reqRoute = new Route(HttpMethod.HEAD, "/", null, null);
when(handler.router.getRouteFor(HttpMethod.HEAD, "/")).thenReturn(new Route(HttpMethod.HEAD, "/", null, null));
when(handler.router.getRouteFor(HttpMethod.GET, "/")).thenReturn(route);
RequestContext rc = new RequestContext(reqRoute, Collections.<Filter>emptyList(),
Collections.<Interceptor<?>, Object>emptyMap(), new Object[0], null);
Result result = handler.call(reqRoute, rc);
assertThat(result).isNotNull();
assertThat(result.getStatusCode()).isEqualTo(Status.OK);
assertThat(result.getRenderable().length()).isEqualTo(0);
assertThat(result.getContentType()).isEqualTo(MimeTypes.JSON);
}
示例11: getRoutes
import org.wisdom.api.Controller; //导入依赖的package包/类
private static ArrayNode getRoutes(Controller controller, Router router, Json json) {
ArrayNode array = json.newArray();
for (Route route : router.getRoutes()) {
if (route.getControllerClass().equals(controller.getClass())) {
array.add(RouteModel.from(route, json));
}
}
return array;
}
示例12: to
import org.wisdom.api.Controller; //导入依赖的package包/类
/**
* Sets the targeted action method of the resulting route.
*
* @param controller the controller object, must not be {@literal null}.
* @param method the method name, must not be {@literal null}.
* @return the current route builder
*/
public Route to(Controller controller, String method) {
Preconditions.checkNotNull(controller);
Preconditions.checkNotNull(method);
this.controller = controller;
try {
this.controllerMethod = verifyThatControllerAndMethodExists(controller.getClass(),
method);
} catch (Exception e) {
throw new IllegalArgumentException(ERROR_CTRL + method + ERROR_IN + controller
.getClass() + "`, or the method is invalid", e);
}
return _build();
}
示例13: Route
import org.wisdom.api.Controller; //导入依赖的package包/类
/**
* Main constructor.
*
* @param httpMethod the method
* @param uri the uri
* @param controller the controller object
* @param controllerMethod the controller method
*/
public Route(HttpMethod httpMethod,
String uri,
Controller controller,
Method controllerMethod) {
this.httpMethod = httpMethod;
this.uri = uri;
this.controller = controller;
this.controllerMethod = controllerMethod;
// Unbound route case.
if (controllerMethod != null) {
if (!controllerMethod.isAccessible()) {
controllerMethod.setAccessible(true);
}
this.arguments = RouteUtils.buildActionParameterList(this.controllerMethod);
parameterNames = ImmutableList.copyOf(RouteUtils.extractParameters(uri));
regex = Pattern.compile(RouteUtils.convertRawUriToRegex(uri));
} else {
parameterNames = Collections.emptyList();
regex = null;
arguments = Collections.emptyList();
}
if (controller == null) {
unboundStatus = Status.NOT_FOUND;
}
}
示例14: getPath
import org.wisdom.api.Controller; //导入依赖的package包/类
/**
* Gets the 'path' value of the given controller. If the controller does not use the {@link org.wisdom.api
* .annotations.Path} annotation, and empty prefix is returned.
*
* @param controller the controller
* @return the prefix coming either empty or the value of the Path annotation
*/
public static String getPath(Controller controller) {
Path path = controller.getClass().getAnnotation(Path.class);
if (path == null) {
return EMPTY_PREFIX;
} else {
return path.value();
}
}
示例15: testAcceptedAndRefusedContentType
import org.wisdom.api.Controller; //导入依赖的package包/类
@Test
public void testAcceptedAndRefusedContentType() throws Exception {
Controller controller = new DefaultController() {
public Result method1() {
return null;
}
public Result method2() {
return null;
}
};
Route route1 = new RouteBuilder().route(HttpMethod.GET).on("/foo").to(controller, "method1").accepts
("text/plain");
Route route2 = new RouteBuilder().route(HttpMethod.GET).on("/foo").to(controller, "method2").accepts
("application/json", "text/*");
assertThat(route1.matches(HttpMethod.GET, "/foo")).isTrue();
Request request1 = mock(Request.class);
when(request1.contentMimeType()).thenReturn("text/plain");
assertThat(route1.isCompliantWithRequestContentType(request1)).isEqualTo(2);
when(request1.contentMimeType()).thenReturn("text/foo");
assertThat(route1.isCompliantWithRequestContentType(request1)).isEqualTo(0);
assertThat(route2.matches(HttpMethod.GET, "/foo")).isTrue();
Request request2 = mock(Request.class);
when(request2.contentMimeType()).thenReturn("text/plain");
assertThat(route1.isCompliantWithRequestContentType(request2)).isEqualTo(2);
when(request2.contentMimeType()).thenReturn("text/foo");
assertThat(route2.isCompliantWithRequestContentType(request2)).isEqualTo(1);
when(request2.contentMimeType()).thenReturn("application/foo");
assertThat(route2.isCompliantWithRequestContentType(request2)).isEqualTo(0);
when(request2.contentMimeType()).thenReturn("application/json");
assertThat(route2.isCompliantWithRequestContentType(request2)).isEqualTo(2);
}