pekko/akka-docs/rst/java/http/routing-dsl/directives/host-directives/host.rst
2016-01-14 00:31:03 +01:00

44 lines
1.8 KiB
ReStructuredText

.. _-host-java-:
host
====
Filter requests matching conditions against the hostname part of the Host header value
in the request.
There are a few variants:
* reject all requests with a hostname different from the given ones
* reject all requests for which the hostname does not satisfy the given predicate
* reject all requests for which the hostname does not satisfy the given regular expression
The regular expression matching works a little bit different: it rejects all requests with a hostname
that doesn't have a prefix matching the given regular expression and also extracts a ``String`` to its
inner route following this rules:
* For all matching requests the prefix string matching the regex is extracted and passed to the inner route.
* If the regex contains a capturing group only the string matched by this group is extracted.
* If the regex contains more than one capturing group an ``IllegalArgumentException`` is thrown.
Example
-------
Matching a list of hosts:
.. includecode:: ../../../../code/docs/http/javadsl/server/directives/HostDirectivesExamplesTest.java#host1
Making sure the host satisfies the given predicate
.. includecode:: ../../../../code/docs/http/javadsl/server/directives/HostDirectivesExamplesTest.java#host2
Using a regular expressions:
.. includecode:: ../../../../code/docs/http/javadsl/server/directives/HostDirectivesExamplesTest.java#matchAndExtractHost
Beware that in the case of introducing multiple capturing groups in the regex such as in the case bellow, the
directive will fail at runtime, at the moment the route tree is evaluated for the first time. This might cause
your http handler actor to enter in a fail/restart loop depending on your supervision strategy.
.. includecode:: ../../../../code/docs/http/javadsl/server/directives/HostDirectivesExamplesTest.java#failing-matchAndExtractHost