keycloak-aplcache

KEYCLOAK-7279 Camel example

5/19/2018 1:39:38 PM

Details

diff --git a/examples/fuse/camel-fuse7-undertow/pom.xml b/examples/fuse/camel-fuse7-undertow/pom.xml
new file mode 100755
index 0000000..b2d8c32
--- /dev/null
+++ b/examples/fuse/camel-fuse7-undertow/pom.xml
@@ -0,0 +1,107 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  ~ 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.
+  -->
+
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+    <parent>
+        <artifactId>keycloak-examples-fuse-parent</artifactId>
+        <groupId>org.keycloak</groupId>
+        <version>4.0.0.Beta3-SNAPSHOT</version>
+    </parent>
+
+    <modelVersion>4.0.0</modelVersion>
+    <groupId>org.keycloak.example.demo</groupId>
+    <artifactId>camel-endpoint-example-fuse7-undertow</artifactId>
+    <packaging>bundle</packaging>
+    <name>Camel endpoint example - Secured in Karaf/Fuse 7.0 on Undertow</name>
+    <description/>
+
+    <properties>
+
+        <keycloak.osgi.export>
+        </keycloak.osgi.export>
+        <keycloak.osgi.import>
+            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
+        </keycloak.osgi.import>
+        <keycloak.osgi.private>
+            org.keycloak.example.*
+        </keycloak.osgi.private>
+    </properties>
+
+    <dependencies>
+        <dependency>
+            <groupId>org.jboss.spec.javax.servlet</groupId>
+            <artifactId>jboss-servlet-api_3.0_spec</artifactId>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.keycloak</groupId>
+            <artifactId>keycloak-core</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.camel</groupId>
+            <artifactId>camel-core</artifactId>
+            <version>${camel.version}</version>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.camel</groupId>
+            <artifactId>camel-blueprint</artifactId>
+            <version>${camel.version}</version>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.camel</groupId>
+            <artifactId>camel-undertow</artifactId>
+            <version>${camel.version}</version>
+        </dependency>
+        <dependency>
+            <groupId>io.undertow</groupId>
+            <artifactId>undertow-core</artifactId>
+        </dependency>
+    </dependencies>
+
+    <build>
+        <defaultGoal>install</defaultGoal>
+
+        <plugins>
+            <plugin>
+                <groupId>org.apache.felix</groupId>
+                <artifactId>maven-bundle-plugin</artifactId>
+                <extensions>true</extensions>
+                <configuration>
+                    <instructions>
+                        <Bundle-Name>${project.name}</Bundle-Name>
+                        <Bundle-SymbolicName>${project.groupId}.${project.artifactId}</Bundle-SymbolicName>
+                        <Import-Package>${keycloak.osgi.import}</Import-Package>
+                        <Private-Package>${keycloak.osgi.private}</Private-Package>
+                        <Export-Package>${keycloak.osgi.export}</Export-Package>
+                    </instructions>
+                </configuration>
+            </plugin>
+
+        </plugins>
+    </build>
+
+</project>
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 0000000..ab637fd
--- /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 <a href="mailto:mposolda@redhat.com">Marek Posolda</a>
+ */
+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 0000000..83c9131
--- /dev/null
+++ b/examples/fuse/camel-fuse7-undertow/src/main/resources/OSGI-INF/blueprint/blueprint.xml
@@ -0,0 +1,69 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  ~ 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.
+  -->
+
+<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
+           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+           xmlns:camel="http://camel.apache.org/schema/blueprint"
+           xsi:schemaLocation="
+       http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd
+       http://camel.apache.org/schema/blueprint http://camel.apache.org/schema/blueprint/camel-blueprint-2.17.1.xsd">
+
+    <bean id="keycloakConfigResolver" class="org.keycloak.adapters.osgi.BundleBasedKeycloakConfigResolver" >
+        <property name="bundleContext" ref="blueprintBundleContext" />
+    </bean>
+
+    <bean id="helloProcessor" class="org.keycloak.example.CamelHelloProcessor" />
+
+    <camelContext id="blueprintContext"
+                  trace="false"
+                  xmlns="http://camel.apache.org/schema/blueprint">
+
+        <!--the link with Keycloak security handlers happens by using undertow-keycloak component -->
+        <restConfiguration apiComponent="undertow-keycloak" contextPath="/restdsl" port="8484">
+            <endpointProperty key="configResolver" value="#keycloakConfigResolver" />
+            <endpointProperty key="allowedRoles" value="admin,superadmin" />
+        </restConfiguration>
+
+        <rest path="/hello" >
+            <description>Hello rest service</description>
+            <get uri="/{id}" outType="java.lang.String">
+                <description>Just a hello</description>
+                <to uri="direct:justDirect" />
+            </get>
+
+        </rest>
+
+        <route id="justDirect">
+            <from uri="direct:justDirect"/>
+            <process ref="helloProcessor" />
+            <log message="RestDSL correctly invoked ${body}"/>
+            <setBody>
+                <constant>(__This second sentence is returned from a Camel RestDSL endpoint__)</constant>
+            </setBody>
+        </route>
+
+
+        <route id="httpBridge">
+            <from uri="undertow-keycloak:http://0.0.0.0:8383/admin-camel-endpoint?matchOnUriPrefix=true&amp;configResolver=#keycloakConfigResolver&amp;allowedRoles=admin" />
+            <process ref="helloProcessor" />
+            <log message="The message from camel endpoint contains ${body}"/>
+        </route>
+
+    </camelContext>
+
+</blueprint>
\ 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 0000000..a9b9993
--- /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 119cb24..649098a 100755
--- a/examples/fuse/pom.xml
+++ b/examples/fuse/pom.xml
@@ -41,6 +41,7 @@
         <module>cxf-jaxws</module>
         <module>cxf-jaxws-fuse7-undertow</module>
         <module>camel</module>
+        <module>camel-fuse7-undertow</module>
         <module>features</module>
         <module>external-config</module>
     </modules>