From adea41469549477757c88940940fde98a2e1be06 Mon Sep 17 00:00:00 2001 From: Hynek Mlnarik Date: Sat, 19 May 2018 18:39:38 +0200 Subject: [PATCH] KEYCLOAK-7279 Camel example --- examples/fuse/camel-fuse7-undertow/pom.xml | 107 ++++++++++++++++++ .../keycloak/example/CamelHelloProcessor.java | 48 ++++++++ .../OSGI-INF/blueprint/blueprint.xml | 69 +++++++++++ .../src/main/resources/WEB-INF/keycloak.json | 10 ++ examples/fuse/pom.xml | 1 + 5 files changed, 235 insertions(+) create mode 100755 examples/fuse/camel-fuse7-undertow/pom.xml create mode 100644 examples/fuse/camel-fuse7-undertow/src/main/java/org/keycloak/example/CamelHelloProcessor.java create mode 100644 examples/fuse/camel-fuse7-undertow/src/main/resources/OSGI-INF/blueprint/blueprint.xml create mode 100644 examples/fuse/camel-fuse7-undertow/src/main/resources/WEB-INF/keycloak.json diff --git a/examples/fuse/camel-fuse7-undertow/pom.xml b/examples/fuse/camel-fuse7-undertow/pom.xml new file mode 100755 index 0000000000..b2d8c32988 --- /dev/null +++ b/examples/fuse/camel-fuse7-undertow/pom.xml @@ -0,0 +1,107 @@ + + + + + + keycloak-examples-fuse-parent + org.keycloak + 4.0.0.Beta3-SNAPSHOT + + + 4.0.0 + org.keycloak.example.demo + camel-endpoint-example-fuse7-undertow + bundle + Camel endpoint example - Secured in Karaf/Fuse 7.0 on Undertow + + + + + + + + javax.servlet;version="[3,4)", + javax.servlet.http;version="[3,4)", + javax.net.ssl, + org.apache.camel.*, + io.undertow.*, + org.apache.camel;version="[2.13,3)", + org.keycloak.*;version="${project.version}", + org.osgi.service.blueprint, + org.osgi.service.blueprint.container + + + org.keycloak.example.* + + + + + + org.jboss.spec.javax.servlet + jboss-servlet-api_3.0_spec + provided + + + org.keycloak + keycloak-core + + + org.apache.camel + camel-core + ${camel.version} + + + org.apache.camel + camel-blueprint + ${camel.version} + + + org.apache.camel + camel-undertow + ${camel.version} + + + io.undertow + undertow-core + + + + + install + + + + org.apache.felix + maven-bundle-plugin + true + + + ${project.name} + ${project.groupId}.${project.artifactId} + ${keycloak.osgi.import} + ${keycloak.osgi.private} + ${keycloak.osgi.export} + + + + + + + + diff --git a/examples/fuse/camel-fuse7-undertow/src/main/java/org/keycloak/example/CamelHelloProcessor.java b/examples/fuse/camel-fuse7-undertow/src/main/java/org/keycloak/example/CamelHelloProcessor.java new file mode 100644 index 0000000000..ab637fdbb3 --- /dev/null +++ b/examples/fuse/camel-fuse7-undertow/src/main/java/org/keycloak/example/CamelHelloProcessor.java @@ -0,0 +1,48 @@ +/* + * Copyright 2016 Red Hat, Inc. and/or its affiliates + * and other contributors as indicated by the @author tags. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.keycloak.example; + +import org.apache.camel.Exchange; +import org.apache.camel.Processor; +import org.keycloak.KeycloakPrincipal; +import org.keycloak.representations.AccessToken; +import javax.servlet.http.HttpServletRequest; + +/** + * @author Marek Posolda + */ +public class CamelHelloProcessor implements Processor { + + @Override + public void process(Exchange exchange) throws Exception { + // Fuse 7 + KeycloakPrincipal keycloakPrincipal = (KeycloakPrincipal) exchange.getProperty(KeycloakPrincipal.class.getName(), KeycloakPrincipal.class); + + if (keycloakPrincipal == null) { + // Fuse 6.3 + HttpServletRequest req = exchange.getIn().getBody(HttpServletRequest.class); + keycloakPrincipal = (KeycloakPrincipal) req.getUserPrincipal(); + } + + AccessToken accessToken = keycloakPrincipal.getKeycloakSecurityContext().getToken(); + String username = accessToken.getPreferredUsername(); + String fullName = accessToken.getName(); + + exchange.getOut().setBody("Hello " + username + "! Your full name is " + fullName + "."); + } +} diff --git a/examples/fuse/camel-fuse7-undertow/src/main/resources/OSGI-INF/blueprint/blueprint.xml b/examples/fuse/camel-fuse7-undertow/src/main/resources/OSGI-INF/blueprint/blueprint.xml new file mode 100644 index 0000000000..83c9131d21 --- /dev/null +++ b/examples/fuse/camel-fuse7-undertow/src/main/resources/OSGI-INF/blueprint/blueprint.xml @@ -0,0 +1,69 @@ + + + + + + + + + + + + + + + + + + + + + Hello rest service + + Just a hello + + + + + + + + + + + (__This second sentence is returned from a Camel RestDSL endpoint__) + + + + + + + + + + + + + \ No newline at end of file diff --git a/examples/fuse/camel-fuse7-undertow/src/main/resources/WEB-INF/keycloak.json b/examples/fuse/camel-fuse7-undertow/src/main/resources/WEB-INF/keycloak.json new file mode 100644 index 0000000000..a9b9993d63 --- /dev/null +++ b/examples/fuse/camel-fuse7-undertow/src/main/resources/WEB-INF/keycloak.json @@ -0,0 +1,10 @@ +{ + "realm": "demo", + "resource": "admin-camel-endpoint", + "bearer-only": "true", + "auth-server-url": "http://localhost:8080/auth", + "ssl-required" : "external", + "credentials": { + "secret": "password" + } +} diff --git a/examples/fuse/pom.xml b/examples/fuse/pom.xml index 119cb245ce..649098a6c4 100755 --- a/examples/fuse/pom.xml +++ b/examples/fuse/pom.xml @@ -41,6 +41,7 @@ cxf-jaxws cxf-jaxws-fuse7-undertow camel + camel-fuse7-undertow features external-config