Merge pull request #3299 from paoloantinori/fuse_rest_dsl_example

KEYCLOAK-3678 - Fuse examples: added Camel RestDSL
This commit is contained in:
Marek Posolda 2016-11-22 11:35:53 +01:00 committed by GitHub
commit 99f12ff720
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"
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.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">
<property name="realm" value="demo"/>
@ -61,20 +62,64 @@
<property name="realmName" value="does-not-matter"/>
</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">
<property name="handler" ref="securityHandler" />
</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" />
<camelContext id="blueprintContext"
trace="false"
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">
<!--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" />
<process ref="helloProcessor" />
<log message="The message from camel endpoint contains ${body}"/>
</route>
</camelContext>
</blueprint>

View file

@ -17,6 +17,13 @@
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.HttpResponse;
import org.apache.http.client.HttpClient;
@ -24,12 +31,6 @@ import org.apache.http.client.methods.HttpGet;
import org.keycloak.KeycloakSecurityContext;
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>
*/
@ -40,7 +41,10 @@ public class CamelClient {
HttpClient client = new HttpClientBuilder()
.disableTrustManager().build();
StringBuilder sb = new StringBuilder();
try {
// Initially let's invoke a simple Camel-Jetty exposed endpoint
HttpGet get = new HttpGet("http://localhost:8383/admin-camel-endpoint");
get.addHeader("Authorization", "Bearer " + session.getTokenString());
try {
@ -52,7 +56,26 @@ public class CamelClient {
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();
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 {
is.close();
}
@ -62,6 +85,8 @@ public class CamelClient {
} finally {
client.getConnectionManager().shutdown();
}
return sb.toString();
}
private static String getStringFromInputStream(InputStream is) {

View file

@ -230,6 +230,13 @@
"baseUrl": "http://localhost:8383/admin-camel-endpoint",
"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",
"enabled": true,

View file

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