KEYCLOAK-3678 - Fuse examples, added Camel RestDSL

This commit is contained in:
Paolo Antinori 2016-10-07 14:41:57 +02:00
parent 7413f46e1b
commit 0485f5c43d
4 changed files with 86 additions and 9 deletions

View file

@ -18,9 +18,10 @@
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0" <blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:camel="http://camel.apache.org/schema/blueprint"
xsi:schemaLocation=" xsi:schemaLocation="
http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd 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.xsd"> http://camel.apache.org/schema/blueprint http://camel.apache.org/schema/blueprint/camel-blueprint-2.17.1.xsd">
<bean id="kcAdapterConfig" class="org.keycloak.representations.adapters.config.AdapterConfig"> <bean id="kcAdapterConfig" class="org.keycloak.representations.adapters.config.AdapterConfig">
<property name="realm" value="demo"/> <property name="realm" value="demo"/>
@ -61,20 +62,64 @@
<property name="realmName" value="does-not-matter"/> <property name="realmName" value="does-not-matter"/>
</bean> </bean>
<bean id="securityHandlerRest" class="org.eclipse.jetty.security.ConstraintSecurityHandler">
<property name="authenticator" ref="keycloakAuthenticator" />
<property name="constraintMappings">
<list>
<ref component-id="constraintMapping" />
</list>
</property>
<property name="authMethod" value="BASIC"/>
<property name="realmName" value="does-not-matter"/>
</bean>
<!--we need 2 independent instance of sessionHandler, since jetty will try to start both-->
<bean id="sessionHandler" class="org.keycloak.adapters.jetty.spi.WrappingSessionHandler"> <bean id="sessionHandler" class="org.keycloak.adapters.jetty.spi.WrappingSessionHandler">
<property name="handler" ref="securityHandler" /> <property name="handler" ref="securityHandler" />
</bean> </bean>
<bean id="sessionHandlerRest" class="org.keycloak.adapters.jetty.spi.WrappingSessionHandler">
<property name="handler" ref="securityHandlerRest" />
</bean>
<bean id="helloProcessor" class="org.keycloak.example.CamelHelloProcessor" /> <bean id="helloProcessor" class="org.keycloak.example.CamelHelloProcessor" />
<camelContext id="blueprintContext" <camelContext id="blueprintContext"
trace="false" trace="false"
xmlns="http://camel.apache.org/schema/blueprint"> xmlns="http://camel.apache.org/schema/blueprint">
<restConfiguration component="jetty" contextPath="/restdsl"
port="8484">
<!--the link with Keycloak security handlers happens here-->
<endpointProperty key="handlers" value="sessionHandlerRest"></endpointProperty>
<endpointProperty key="matchOnUriPrefix" value="true"></endpointProperty>
</restConfiguration>
<rest path="/hello" >
<description>Hello rest service</description>
<get uri="/{id}" outType="java.lang.String">
<description>Just an helllo</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"> <route id="httpBridge">
<!--note that we are passing to the endoint the security handlers we have defined above-->
<from uri="jetty:http://0.0.0.0:8383/admin-camel-endpoint?handlers=sessionHandler&amp;matchOnUriPrefix=true" /> <from uri="jetty:http://0.0.0.0:8383/admin-camel-endpoint?handlers=sessionHandler&amp;matchOnUriPrefix=true" />
<process ref="helloProcessor" /> <process ref="helloProcessor" />
<log message="The message from camel endpoint contains ${body}"/> <log message="The message from camel endpoint contains ${body}"/>
</route> </route>
</camelContext> </camelContext>
</blueprint> </blueprint>

View file

@ -17,6 +17,13 @@
package org.keycloak.example; package org.keycloak.example;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import javax.servlet.http.HttpServletRequest;
import org.apache.http.HttpEntity; import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse; import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient; import org.apache.http.client.HttpClient;
@ -24,12 +31,6 @@ import org.apache.http.client.methods.HttpGet;
import org.keycloak.KeycloakSecurityContext; import org.keycloak.KeycloakSecurityContext;
import org.keycloak.adapters.HttpClientBuilder; import org.keycloak.adapters.HttpClientBuilder;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
/** /**
* @author <a href="mailto:mposolda@redhat.com">Marek Posolda</a> * @author <a href="mailto:mposolda@redhat.com">Marek Posolda</a>
*/ */
@ -40,7 +41,10 @@ public class CamelClient {
HttpClient client = new HttpClientBuilder() HttpClient client = new HttpClientBuilder()
.disableTrustManager().build(); .disableTrustManager().build();
StringBuilder sb = new StringBuilder();
try { try {
// Initially let's invoke a simple Camel-Jetty exposed endpoint
HttpGet get = new HttpGet("http://localhost:8383/admin-camel-endpoint"); HttpGet get = new HttpGet("http://localhost:8383/admin-camel-endpoint");
get.addHeader("Authorization", "Bearer " + session.getTokenString()); get.addHeader("Authorization", "Bearer " + session.getTokenString());
try { try {
@ -52,7 +56,26 @@ public class CamelClient {
HttpEntity entity = response.getEntity(); HttpEntity entity = response.getEntity();
InputStream is = entity.getContent(); InputStream is = entity.getContent();
try { try {
return getStringFromInputStream(is); sb.append(getStringFromInputStream(is));
} finally {
is.close();
}
} catch (IOException e) {
throw new RuntimeException(e);
}
// Here we invoke a Jetty endpoint, published using Camel RestDSL
get = new HttpGet("http://localhost:8484/restdsl/hello/world");
get.addHeader("Authorization", "Bearer " + session.getTokenString());
try {
HttpResponse response = client.execute(get);
if (response.getStatusLine().getStatusCode() != 200) {
return "There was a failure processing request with the RestDSL endpoint. You either didn't configure Keycloak properly or you don't have admin permission? Status code is "
+ response.getStatusLine().getStatusCode();
}
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();
try {
sb.append(getStringFromInputStream(is));
} finally { } finally {
is.close(); is.close();
} }
@ -62,6 +85,8 @@ public class CamelClient {
} finally { } finally {
client.getConnectionManager().shutdown(); client.getConnectionManager().shutdown();
} }
return sb.toString();
} }
private static String getStringFromInputStream(InputStream is) { private static String getStringFromInputStream(InputStream is) {

View file

@ -230,6 +230,13 @@
"baseUrl": "http://localhost:8383/admin-camel-endpoint", "baseUrl": "http://localhost:8383/admin-camel-endpoint",
"bearerOnly": true "bearerOnly": true
}, },
{
"clientId": "admin-camel-restdsl",
"enabled": true,
"adminUrl": "http://localhost:8484/restdsl",
"baseUrl": "http://localhost:8484/restdsl",
"bearerOnly": true
},
{ {
"clientId": "ssh-jmx-admin-client", "clientId": "ssh-jmx-admin-client",
"enabled": true, "enabled": true,

View file

@ -30,7 +30,7 @@
<artifactId>keycloak-examples-fuse-parent</artifactId> <artifactId>keycloak-examples-fuse-parent</artifactId>
<packaging>pom</packaging> <packaging>pom</packaging>
<properties> <properties>
<camel.version>2.16.1</camel.version> <camel.version>2.17.0</camel.version>
</properties> </properties>
<modules> <modules>
<module>customer-app-fuse</module> <module>customer-app-fuse</module>