Search in sources :

Example 1 with UriPattern

use of com.yahoo.jdisc.application.UriPattern in project vespa by vespa-engine.

the class Utils method getBindingMatch.

/**
 * If request is an HTTP request and a jdisc request, return the {@link com.yahoo.jdisc.application.BindingMatch}
 * of the request. Otherwise return a dummy match useful only for testing based on the <code>uriPattern</code>
 * supplied.
 *
 * @param req        an {@link com.yahoo.container.jdisc.HttpRequest}
 * @param uriPattern a pattern to create a BindingMatch for in tests
 * @return match
 */
public static BindingMatch<?> getBindingMatch(HttpRequest req, String uriPattern) {
    com.yahoo.jdisc.http.HttpRequest jDiscRequest = req.getJDiscRequest();
    BindingMatch<?> bm = jDiscRequest.getBindingMatch();
    if (bm == null) {
        UriPattern pattern = new UriPattern(uriPattern);
        bm = new BindingMatch<>(pattern.match(URI.create(jDiscRequest.getUri().toString())), new Object(), pattern);
    }
    return bm;
}
Also used : UriPattern(com.yahoo.jdisc.application.UriPattern)

Example 2 with UriPattern

use of com.yahoo.jdisc.application.UriPattern in project vespa by vespa-engine.

the class ThreadedRequestHandler method contextFor.

private Metric.Context contextFor(BindingMatch match) {
    if (match == null)
        return null;
    UriPattern matched = match.matched();
    if (matched == null)
        return null;
    String name = matched.toString();
    Metric.Context context = handlerContexts.get(name);
    if (context == null) {
        Map<String, String> dimensions = singletonMap("handler", name);
        context = this.metric.createContext(dimensions);
        handlerContexts.put(name, context);
    }
    return context;
}
Also used : Metric(com.yahoo.jdisc.Metric) UriPattern(com.yahoo.jdisc.application.UriPattern)

Example 3 with UriPattern

use of com.yahoo.jdisc.application.UriPattern in project vespa by vespa-engine.

the class UriMatchingTestCase method benchmarkMatch.

private static long benchmarkMatch(String pattern, List<String> inputs) {
    UriPattern compiled = new UriPattern(pattern);
    List<URI> uriList = new ArrayList<>(inputs.size());
    for (String input : inputs) {
        uriList.add(URI.create(input));
    }
    long now = System.nanoTime();
    for (int i = 0; i < NUM_MATCHES; ++i) {
        for (URI uri : uriList) {
            UriPattern.Match match = compiled.match(uri);
            preventOptimization += match != null ? match.groupCount() : 1;
        }
    }
    return TimeUnit.NANOSECONDS.toMicros(System.nanoTime() - now);
}
Also used : ArrayList(java.util.ArrayList) UriPattern(com.yahoo.jdisc.application.UriPattern) URI(java.net.URI)

Example 4 with UriPattern

use of com.yahoo.jdisc.application.UriPattern in project vespa by vespa-engine.

the class ActiveContainerTestCase method requireThatServerBindingAccessorWorks.

@Test
public void requireThatServerBindingAccessorWorks() {
    TestDriver driver = TestDriver.newSimpleApplicationInstanceWithoutOsgi();
    ContainerBuilder builder = driver.newContainerBuilder();
    RequestHandler foo = new NonWorkingRequestHandler();
    RequestHandler bar = new NonWorkingRequestHandler();
    builder.serverBindings().bind("http://host/foo", foo);
    builder.serverBindings("bar").bind("http://host/bar", bar);
    ActiveContainer container = new ActiveContainer(builder);
    Map<String, BindingSet<RequestHandler>> bindings = container.serverBindings();
    assertNotNull(bindings);
    assertEquals(2, bindings.size());
    BindingSet<RequestHandler> set = bindings.get(BindingSet.DEFAULT);
    assertNotNull(set);
    Iterator<Map.Entry<UriPattern, RequestHandler>> it = set.iterator();
    assertNotNull(it);
    assertTrue(it.hasNext());
    Map.Entry<UriPattern, RequestHandler> entry = it.next();
    assertNotNull(entry);
    assertEquals(new UriPattern("http://host/foo"), entry.getKey());
    assertSame(foo, entry.getValue());
    assertFalse(it.hasNext());
    assertNotNull(set = bindings.get("bar"));
    assertNotNull(it = set.iterator());
    assertTrue(it.hasNext());
    assertNotNull(entry = it.next());
    assertEquals(new UriPattern("http://host/bar"), entry.getKey());
    assertSame(bar, entry.getValue());
    assertFalse(it.hasNext());
    assertNotNull(bindings = container.clientBindings());
    assertEquals(1, bindings.size());
    assertNotNull(set = bindings.get(BindingSet.DEFAULT));
    assertNotNull(it = set.iterator());
    assertFalse(it.hasNext());
    driver.close();
}
Also used : BindingSet(com.yahoo.jdisc.application.BindingSet) UriPattern(com.yahoo.jdisc.application.UriPattern) TestDriver(com.yahoo.jdisc.test.TestDriver) ContainerBuilder(com.yahoo.jdisc.application.ContainerBuilder) RequestHandler(com.yahoo.jdisc.handler.RequestHandler) NonWorkingRequestHandler(com.yahoo.jdisc.test.NonWorkingRequestHandler) NonWorkingRequestHandler(com.yahoo.jdisc.test.NonWorkingRequestHandler) Map(java.util.Map) Test(org.junit.Test)

Example 5 with UriPattern

use of com.yahoo.jdisc.application.UriPattern in project vespa by vespa-engine.

the class HttpConfigRequests method getBindingMatch.

/**
 * Produces the binding match for the request. If it's not available on the jDisc request, create one for
 * testing using the long and short app id URL patterns given.
 * @param req an {@link com.yahoo.container.jdisc.HttpRequest}
 * @param patterns A list of patterns that should be matched if no match on binding.
 * @return match
 */
public static BindingMatch<?> getBindingMatch(HttpRequest req, String... patterns) {
    com.yahoo.jdisc.http.HttpRequest jDiscRequest = req.getJDiscRequest();
    if (jDiscRequest == null)
        throw new IllegalArgumentException("No JDisc request for: " + req.getUri());
    BindingMatch<?> jdBm = jDiscRequest.getBindingMatch();
    if (jdBm != null)
        return jdBm;
    // If not, use provided patterns
    for (String pattern : patterns) {
        UriPattern fullAppIdPattern = new UriPattern(pattern);
        URI uri = req.getUri();
        Match match = fullAppIdPattern.match(uri);
        if (match != null)
            return new BindingMatch<>(match, new Object(), fullAppIdPattern);
    }
    throw new IllegalArgumentException("Illegal url for config request: " + req.getUri());
}
Also used : UriPattern(com.yahoo.jdisc.application.UriPattern) URI(java.net.URI) BindingMatch(com.yahoo.jdisc.application.BindingMatch) Match(com.yahoo.jdisc.application.UriPattern.Match)

Aggregations

UriPattern (com.yahoo.jdisc.application.UriPattern)6 BindingSet (com.yahoo.jdisc.application.BindingSet)2 ContainerBuilder (com.yahoo.jdisc.application.ContainerBuilder)2 RequestHandler (com.yahoo.jdisc.handler.RequestHandler)2 NonWorkingRequestHandler (com.yahoo.jdisc.test.NonWorkingRequestHandler)2 TestDriver (com.yahoo.jdisc.test.TestDriver)2 URI (java.net.URI)2 Map (java.util.Map)2 Test (org.junit.Test)2 Metric (com.yahoo.jdisc.Metric)1 BindingMatch (com.yahoo.jdisc.application.BindingMatch)1 Match (com.yahoo.jdisc.application.UriPattern.Match)1 ArrayList (java.util.ArrayList)1