Search in sources :

Example 1 with URITree

use of com.tvd12.ezyhttp.core.net.URITree in project ezyhttp by youngmonkeys.

the class RequestHandlerManager method addHandler.

public void addHandler(RequestURI uri, RequestHandler handler) {
    RequestHandler old = this.handlers.put(uri, handler);
    if (old != null && !allowOverrideURI) {
        throw new DuplicateURIMappingHandlerException(uri, old, handler);
    }
    this.handledURIs.add(uri.getUri());
    this.logger.info("add mapping uri: {}", uri);
    this.handlerListByURI.computeIfAbsent(uri, k -> new ArrayList<>()).add(handler);
    this.requestURIManager.addHandledURI(uri.getMethod(), uri.getUri());
    if (uri.isApi()) {
        this.requestURIManager.addApiURI(uri.getMethod(), uri.getUri());
        this.requestURIManager.addApiURI(uri.getMethod(), uri.getSameURI());
    }
    if (uri.isAuthenticated()) {
        this.requestURIManager.addAuthenticatedURI(uri.getMethod(), uri.getUri());
        this.requestURIManager.addAuthenticatedURI(uri.getMethod(), uri.getSameURI());
    }
    if (uri.isManagement()) {
        this.requestURIManager.addManagementURI(uri.getMethod(), uri.getUri());
        this.requestURIManager.addManagementURI(uri.getMethod(), uri.getSameURI());
    }
    if (uri.isPayment()) {
        this.requestURIManager.addPaymentURI(uri.getMethod(), uri.getUri());
        this.requestURIManager.addPaymentURI(uri.getMethod(), uri.getSameURI());
    }
    if (EzyStrings.isNotBlank(uri.getFeature())) {
        this.featureURIManager.addFeatureURI(uri.getFeature(), uri.getMethod(), uri.getUri());
        this.featureURIManager.addFeatureURI(uri.getFeature(), uri.getMethod(), uri.getSameURI());
    }
    URITree uriTree = uriTreeByMethod.get(uri.getMethod());
    uriTree.addURI(uri.getUri());
}
Also used : Setter(lombok.Setter) Getter(lombok.Getter) DuplicateURIMappingHandlerException(com.tvd12.ezyhttp.server.core.exception.DuplicateURIMappingHandlerException) EzyLoggable(com.tvd12.ezyfox.util.EzyLoggable) Set(java.util.Set) HashMap(java.util.HashMap) EzyStrings(com.tvd12.ezyfox.io.EzyStrings) EzyDestroyable(com.tvd12.ezyfox.util.EzyDestroyable) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) List(java.util.List) Map(java.util.Map) HttpMethod(com.tvd12.ezyhttp.core.constant.HttpMethod) Collections(java.util.Collections) URITree(com.tvd12.ezyhttp.core.net.URITree) RequestHandler(com.tvd12.ezyhttp.server.core.handler.RequestHandler) RequestURI(com.tvd12.ezyhttp.server.core.request.RequestURI) URITree(com.tvd12.ezyhttp.core.net.URITree) DuplicateURIMappingHandlerException(com.tvd12.ezyhttp.server.core.exception.DuplicateURIMappingHandlerException) RequestHandler(com.tvd12.ezyhttp.server.core.handler.RequestHandler) ArrayList(java.util.ArrayList)

Example 2 with URITree

use of com.tvd12.ezyhttp.core.net.URITree in project ezyhttp by youngmonkeys.

the class URITreeTest method getMatchedURIChildNull.

@Test
public void getMatchedURIChildNull() {
    // given
    URITree tree = new URITree();
    tree.addURI("/api/v1/hello/{world}/{foo}/{bar}");
    // when
    // then
    System.out.println(tree);
    Asserts.assertNull(tree.getMatchedURI("/api/v1"));
    Asserts.assertNull(tree.getMatchedURI("/api/v1/hello"));
    Asserts.assertNull(tree.getMatchedURI("/api/v1/hello/{world}"));
    Asserts.assertNull(tree.getMatchedURI("/api/v1/hello/{world}/{foo}"));
    Asserts.assertNull(tree.getMatchedURI("/api/v1/hello/{world}/{foo}/{bar}/unknown"));
}
Also used : URITree(com.tvd12.ezyhttp.core.net.URITree) Test(org.testng.annotations.Test)

Example 3 with URITree

use of com.tvd12.ezyhttp.core.net.URITree in project ezyhttp by youngmonkeys.

the class RequestHandlerManager method getMatchedURI.

public String getMatchedURI(HttpMethod method, String requestURI) {
    String matchedURI = null;
    if (handledURIs.contains(requestURI)) {
        matchedURI = requestURI;
    }
    if (matchedURI == null && requestURI != null) {
        URITree uriTree = uriTreeByMethod.get(method);
        matchedURI = uriTree.getMatchedURI(requestURI);
    }
    return matchedURI;
}
Also used : URITree(com.tvd12.ezyhttp.core.net.URITree)

Example 4 with URITree

use of com.tvd12.ezyhttp.core.net.URITree in project ezyhttp by youngmonkeys.

the class URITreeTest method test.

@Test
public void test() {
    // given
    String uri11 = "/api/v1/customer/{name}/create";
    String uri12 = "/api/v1/customer/{name}/delete";
    String uri2 = "/api/v1/user";
    // when
    URITree tree = new URITree();
    tree.addURI(uri11);
    tree.addURI(uri12);
    tree.addURI(uri2);
    // then
    System.out.println("tree: " + tree);
    String check11 = "/api/v1/customer/dung/create";
    String check12 = "/api/v1/customer/dung/create/1";
    Assert.assertEquals("/api/v1/customer/{name}/create", tree.getMatchedURI(check11));
    System.out.println("check11: " + tree.getMatchedURI(check11));
    Assert.assertNull(tree.getMatchedURI(check12));
    System.out.println("check12: " + tree.getMatchedURI(check12));
}
Also used : URITree(com.tvd12.ezyhttp.core.net.URITree) Test(org.testng.annotations.Test)

Example 5 with URITree

use of com.tvd12.ezyhttp.core.net.URITree in project ezyhttp by youngmonkeys.

the class URITreeTest method getMatchedURIChildNull2.

@Test
public void getMatchedURIChildNull2() {
    // given
    URITree tree = new URITree();
    tree.addURI("/api/v1/hello/{world}/{foo}/bar");
    // when
    // then
    System.out.println(tree);
    Asserts.assertNull(tree.getMatchedURI("/api/v1"));
    Asserts.assertNull(tree.getMatchedURI("/api/v1/hello"));
    Asserts.assertNull(tree.getMatchedURI("/api/v1/hello/{world}"));
    Asserts.assertNull(tree.getMatchedURI("/api/v1/hello/{world}/{foo}"));
    Asserts.assertNull(tree.getMatchedURI("/api/v1/hello/{world}/{foo}/{bar}/unknown"));
}
Also used : URITree(com.tvd12.ezyhttp.core.net.URITree) Test(org.testng.annotations.Test)

Aggregations

URITree (com.tvd12.ezyhttp.core.net.URITree)5 Test (org.testng.annotations.Test)3 EzyStrings (com.tvd12.ezyfox.io.EzyStrings)1 EzyDestroyable (com.tvd12.ezyfox.util.EzyDestroyable)1 EzyLoggable (com.tvd12.ezyfox.util.EzyLoggable)1 HttpMethod (com.tvd12.ezyhttp.core.constant.HttpMethod)1 DuplicateURIMappingHandlerException (com.tvd12.ezyhttp.server.core.exception.DuplicateURIMappingHandlerException)1 RequestHandler (com.tvd12.ezyhttp.server.core.handler.RequestHandler)1 RequestURI (com.tvd12.ezyhttp.server.core.request.RequestURI)1 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 Getter (lombok.Getter)1 Setter (lombok.Setter)1