use of io.helidon.webserver.Routing in project helidon by oracle.
the class OutboundOverrideExample method startClientService.
static CompletionStage<Void> startClientService(int port) {
Config config = createConfig("client-service");
Routing routing = Routing.builder().register(WebSecurity.create(config.get("security"))).get("/override", OutboundOverrideExample::override).get("/propagate", OutboundOverrideExample::propagate).build();
return startServer(routing, port, server -> clientPort = server.port());
}
use of io.helidon.webserver.Routing in project helidon by oracle.
the class OutboundOverrideJwtExample method startServingService.
static CompletionStage<Void> startServingService(int port) {
Config config = createConfig("serving-service-jwt");
Routing routing = Routing.builder().register(WebSecurity.create(config.get("security"))).get("/hello", (req, res) -> {
// This is the token. It should be bearer <signed JWT base64 encoded>
req.headers().first("Authorization").ifPresent(System.out::println);
res.send(req.context().get(SecurityContext.class).flatMap(SecurityContext::user).map(Subject::principal).map(Principal::getName).orElse("Anonymous"));
}).build();
return startServer(routing, port, server -> servingPort = server.port());
}
use of io.helidon.webserver.Routing in project helidon by oracle.
the class DigestExampleConfigMain method main.
/**
* Starts this example. Loads configuration from src/main/resources/application.conf. See standard output for instructions.
*
* @param args ignored
*/
public static void main(String[] args) {
// load logging configuration
LogConfig.configureRuntime();
// load configuration
Config config = Config.create();
// build routing (security is loaded from config)
Routing routing = Routing.builder().register(WebSecurity.create(config.get("security"))).get("/{*}", (req, res) -> {
Optional<SecurityContext> securityContext = req.context().get(SecurityContext.class);
res.headers().contentType(MediaType.TEXT_PLAIN.withCharset("UTF-8"));
res.send("Hello, you are: \n" + securityContext.map(ctx -> ctx.user().orElse(SecurityContext.ANONYMOUS).toString()).orElse("Security context is null"));
}).build();
server = DigestExampleUtil.startServer(routing);
}
use of io.helidon.webserver.Routing in project helidon by oracle.
the class MicrometerCdiExtension method registerService.
/**
* Registers the service-related endpoint, after security and as CDI initializes the app scope, returning the default routing
* for optional use by the caller.
*
* @param adv app-scoped initialization event
* @param bm BeanManager
* @return default routing
*/
@Override
public Routing.Builder registerService(@Observes @Priority(LIBRARY_BEFORE + 10) @Initialized(ApplicationScoped.class) Object adv, BeanManager bm, ServerCdiExtension serverCdiExtension) {
Routing.Builder result = super.registerService(adv, bm, serverCdiExtension);
MeterRegistry meterRegistry = serviceSupport().registry();
annotationsForMeters.forEach(deferredAnnotation -> {
Annotation annotation = deferredAnnotation.annotation;
AnnotatedCallable<?> annotatedCallable = deferredAnnotation.annotatedCallable;
Meter newMeter;
boolean isOnlyOnException = false;
if (annotation instanceof Counted) {
Counter counter = MeterProducer.produceCounter(meterRegistry, (Counted) annotation);
LOGGER.log(Level.FINE, () -> "Registered counter " + counter.getId().toString());
newMeter = counter;
isOnlyOnException = ((Counted) annotation).recordFailuresOnly();
} else {
Timed timed = (Timed) annotation;
if (timed.longTask()) {
LongTaskTimer longTaskTimer = MeterProducer.produceLongTaskTimer(meterRegistry, timed);
LOGGER.log(Level.FINE, () -> "Registered long task timer " + longTaskTimer.getId().toString());
newMeter = longTaskTimer;
} else {
Timer timer = MeterProducer.produceTimer(meterRegistry, timed);
LOGGER.log(Level.FINE, () -> "Registered timer " + timer.getId().toString());
newMeter = timer;
}
}
workItemsManager.put(Executable.class.cast(annotatedCallable.getJavaMember()), annotation.annotationType(), MeterWorkItem.create(newMeter, isOnlyOnException));
});
return result;
}
use of io.helidon.webserver.Routing in project helidon by oracle.
the class JsonSupportTest method acceptHeaders.
@Test
public void acceptHeaders() throws Exception {
Routing routing = Routing.builder().post("/foo", Handler.create(JsonObject.class, (req, res, json) -> res.send(json))).build();
JsonObject json = createJson();
// Has accept
TestResponse response = TestClient.create(routing, JsonpSupport.create()).path("/foo").header("Accept", "text/plain; q=.8, application/json; q=.1").post(MediaPublisher.create(MediaType.APPLICATION_JSON.withCharset("UTF-8"), json.toString()));
assertThat(response.status(), is(equalTo(Http.Status.OK_200)));
assertThat(response.headers().first(Http.Header.CONTENT_TYPE).orElse(null), is(equalTo(MediaType.APPLICATION_JSON.toString())));
// Has accept with +json
response = TestClient.create(routing, JsonpSupport.create()).path("/foo").header("Accept", "text/plain; q=.8, application/specific+json; q=.1").post(MediaPublisher.create(MediaType.APPLICATION_JSON.withCharset("UTF-8"), json.toString()));
assertThat(response.status(), is(equalTo(Http.Status.OK_200)));
assertThat(response.headers().first(Http.Header.CONTENT_TYPE).orElse(null), is(equalTo(MediaType.parse("application/specific+json").toString())));
// With start
response = TestClient.create(routing, JsonpSupport.create()).path("/foo").header("Accept", "text/plain; q=.8, application/*; q=.1").post(MediaPublisher.create(MediaType.APPLICATION_JSON.withCharset("UTF-8"), json.toString()));
assertThat(response.status(), is(equalTo(Http.Status.OK_200)));
assertThat(response.headers().first(Http.Header.CONTENT_TYPE).orElse(null), is(equalTo(MediaType.APPLICATION_JSON.toString())));
// With JSONP standard application/javascript
response = TestClient.create(routing, JsonpSupport.create()).path("/foo").header("Accept", "application/javascript").post(MediaPublisher.create(MediaType.APPLICATION_JSON.withCharset("UTF-8"), json.toString()));
assertThat(response.status(), is(equalTo(Http.Status.INTERNAL_SERVER_ERROR_500)));
// Without start
response = TestClient.create(routing, JsonpSupport.create()).path("/foo").header("Accept", "text/plain; q=.8, application/specific; q=.1").post(MediaPublisher.create(MediaType.APPLICATION_JSON.withCharset("UTF-8"), json.toString()));
assertThat(response.status(), is(equalTo(Http.Status.INTERNAL_SERVER_ERROR_500)));
}
Aggregations