Skip to content

Commit

Permalink
#391 Switched to new programatically routing
Browse files Browse the repository at this point in the history
  • Loading branch information
svenkubiak committed Sep 26, 2018
1 parent 996df57 commit e5e46ec
Show file tree
Hide file tree
Showing 34 changed files with 871 additions and 246 deletions.
10 changes: 4 additions & 6 deletions mangooio-core/src/main/java/io/mangoo/admin/AdminController.java
Original file line number Diff line number Diff line change
Expand Up @@ -170,15 +170,13 @@ public Response metrics() {
}

public Response routes() {
// Set<Route> routes = Router.getRoutes()
// .stream()
// .filter((Route route) -> !route.getUrl().contains("@admin"))
// .collect(Collectors.toSet());
//
//FIX ME
//Set<MangooRoute> routes = Router.getRoutes();

return Response.withOk()
.andContent(SPACE, ROUTES)
.andContent(VERSION, VERSION_TAG)
//.andContent(ROUTES, routes)
.andContent(ROUTES, new ArrayList<>())
.andTemplate(Template.DEFAULT.routesPath());
}

Expand Down
15 changes: 11 additions & 4 deletions mangooio-core/src/main/java/io/mangoo/core/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -423,12 +423,15 @@ private static void createRoutes() {

pathHandler.addExactPath(webSocketRoute.getUrl(),
Handlers.websocket(getInstance(WebSocketHandler.class)
.withControllerClass(webSocketRoute.getControllerClass())));
.withControllerClass(webSocketRoute.getControllerClass())
.withAuthentication(webSocketRoute.hasAuthentication())));

} else if (mangooRoute instanceof ServerSentEventRoute) {
ServerSentEventRoute serverSentEventRoute = (ServerSentEventRoute) mangooRoute;

pathHandler.addExactPath(serverSentEventRoute.getUrl(),
Handlers.serverSentEvents(getInstance(ServerSentEventHandler.class)));
Handlers.serverSentEvents(getInstance(ServerSentEventHandler.class)
.withAuthentication(serverSentEventRoute.hasAuthentication())));
} else if (mangooRoute instanceof PathRoute) {
PathRoute pathRoute = (PathRoute) mangooRoute;

Expand All @@ -446,7 +449,8 @@ private static RoutingHandler getRoutingHandler() {

Config config = getInstance(Config.class);
if (config.isApplicationAdminEnable()) {
Bind.controller(AdminController.class).with(
Bind.controller(AdminController.class).withBasicAuthentication(config.getApplicationAdminUsername(), config.getApplicationAdminPassword())
.withRoutes(
On.get().to("/@admin").respondeWith("index"),
On.get().to("/@admin/health").respondeWith("health"),
On.get().to("/@admin/scheduler").respondeWith("scheduler"),
Expand All @@ -469,6 +473,9 @@ private static RoutingHandler getRoutingHandler() {
DispatcherHandler dispatcherHandler = Application.getInstance(DispatcherHandler.class)
.dispatch(requestRoute.getControllerClass(), requestRoute.getControllerMethod())
.isBlocking(requestRoute.isBlocking())
.withBasicAuthentication(requestRoute.getUsername(), requestRoute.getPassword())
.withAuthentication(requestRoute.hasAuthentication())
.withAuthorization(requestRoute.hasAuthorization())
.withLimit(requestRoute.getLimit());

routingHandler.add(requestRoute.getMethod().toString(), requestRoute.getUrl(), dispatcherHandler);
Expand Down Expand Up @@ -557,7 +564,7 @@ private static List<Module> getModules() {
modules.add((AbstractModule) Class.forName(Default.MODULE_CLASS.toString()).getConstructor().newInstance());
} catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException
| NoSuchMethodException | SecurityException | ClassNotFoundException e) {
LOG.error("Failed to load modules. Check that conf/Module.java exists in your application", e);
LOG.error("Failed to load modules. Check that app/Module.java exists in your application", e);
failsafe();
}

Expand Down
13 changes: 0 additions & 13 deletions mangooio-core/src/main/java/io/mangoo/enums/SecureType.java

This file was deleted.

12 changes: 10 additions & 2 deletions mangooio-core/src/main/java/io/mangoo/interfaces/MangooRoute.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
package io.mangoo.interfaces;

/**
*
* @author svenkubiak
*
*/
public interface MangooRoute {

}
/**
* @return The configured URL for this route
*/
String getUrl();
}
5 changes: 1 addition & 4 deletions mangooio-core/src/main/java/io/mangoo/models/Identity.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import java.util.Set;

import io.mangoo.enums.Required;
import io.mangoo.utils.CodecUtils;
import io.undertow.security.idm.Account;
import io.undertow.security.idm.Credential;
import io.undertow.security.idm.IdentityManager;
Expand Down Expand Up @@ -73,9 +72,7 @@ public Set<String> getRoles() {

private boolean verifyCredential(Credential credential) {
if (credential instanceof PasswordCredential) {
return CodecUtils.checkJBCrypt(
new String (((PasswordCredential) credential).getPassword()),
this.password);
return new String (((PasswordCredential) credential).getPassword()).equals(this.password);
}

return false;
Expand Down
18 changes: 15 additions & 3 deletions mangooio-core/src/main/java/io/mangoo/routing/Attachment.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public class Attachment {
private String password;
private Request request;
private boolean requestFilter;
private boolean authorization;
private Map<String, String> requestParameter;
private Response response;
private Session session;
Expand Down Expand Up @@ -159,6 +160,10 @@ public String getUsername() {
public boolean hasAuthentication() {
return StringUtils.isNotBlank(this.username) && StringUtils.isNotBlank(this.password);
}

public boolean hasAuthorization() {
return this.authorization;
}

public boolean hasLimit() {
return this.limit > 0;
Expand Down Expand Up @@ -256,8 +261,10 @@ public Attachment withMethodParameters(Map<String, Class<?>> methodParameters) {
return this;
}

public Attachment withPassword(String password) {
public Attachment withBasicAuthentication(String username, String password) {
this.username = username;
this.password = password;

return this;
}

Expand All @@ -276,8 +283,13 @@ public Attachment withTemplateEngine(TemplateEngine templateEngine) {
return this;
}

public Attachment withUsername(String username) {
this.username = username;
public Attachment withAuthorization(boolean authroization) {
this.authorization = authroization;
return this;
}

public Attachment withAuthentication(boolean authentication) {
//this.authentication = authentication;
return this;
}
}
10 changes: 9 additions & 1 deletion mangooio-core/src/main/java/io/mangoo/routing/Router.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public static void addRoute(MangooRoute route) {
if (route instanceof RequestRoute) {
RequestRoute requestRoute = (RequestRoute) route;
if (requestRoute.getControllerClass() != null && StringUtils.isNotBlank(requestRoute.getControllerMethod())) {
reverseRoutes.put((requestRoute.getControllerClass().getSimpleName() + ":" + requestRoute.getControllerMethod()).toLowerCase(Locale.ENGLISH), requestRoute);
reverseRoutes.put((requestRoute.getControllerClass().getSimpleName().toLowerCase(Locale.ENGLISH) + ":" + requestRoute.getControllerMethod()).toLowerCase(Locale.ENGLISH), requestRoute);
}
}
}
Expand All @@ -63,4 +63,12 @@ public static RequestRoute getReverseRoute(String key) {
Objects.requireNonNull(key, Required.KEY.toString());
return (RequestRoute) reverseRoutes.get(key.toLowerCase(Locale.ENGLISH));
}

/**
* Removes all routes from the router
*/
public static void reset() {
routes = ConcurrentHashMap.newKeySet();
reverseRoutes = new ConcurrentHashMap<>();
}
}
88 changes: 0 additions & 88 deletions mangooio-core/src/main/java/io/mangoo/routing/Secure.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ public class DispatcherHandler implements HttpHandler {
private int methodParametersCount;
private boolean hasRequestFilter;
private boolean blocking;
private boolean authentication;
private boolean authroization;

public DispatcherHandler dispatch(Class<?> controllerClass, String controllerMethodName) {
Objects.requireNonNull(controllerClass, Required.CONTROLLER_CLASS.toString());
Expand Down Expand Up @@ -98,13 +100,24 @@ public DispatcherHandler isBlocking(boolean blocking) {
return this;
}

public DispatcherHandler withUsername(String username) {
public DispatcherHandler withBasicAuthentication(String username, String password) {
this.username = username;
this.password = password;

return this;
}

public DispatcherHandler withAuthentication(boolean hasAuthentication) {
this.authentication = true;
return this;
}

public DispatcherHandler withPassword(String password) {
this.password = password;
public DispatcherHandler withAuthorization(boolean authorization) {
// if (authorization) {
// this.authentication = true;
// this.authroization = true;
// }
//
return this;
}

Expand Down Expand Up @@ -134,8 +147,9 @@ public void handleRequest(HttpServerExchange exchange) throws Exception {
.withRequestParameter(RequestUtils.getRequestParameters(exchange))
.withMessages(this.messages)
.withLimit(this.limit)
.withUsername(this.username)
.withPassword(this.password)
.withAuthorization(this.authroization)
.withAuthentication(this.authentication)
.withBasicAuthentication(this.username, this.password)
.withTemplateEngine(this.templateEngine);

exchange.putAttachment(RequestUtils.getAttachmentKey(), attachment);
Expand Down
Loading

0 comments on commit e5e46ec

Please sign in to comment.