keycloak-uncached

Details

diff --git a/examples/fuse/pom.xml b/examples/fuse/pom.xml
index 3c2f9fb..6dc473b 100755
--- a/examples/fuse/pom.xml
+++ b/examples/fuse/pom.xml
@@ -35,6 +35,7 @@
     <modules>
         <module>customer-app-fuse</module>
         <module>product-app-fuse</module>
+        <module>product-app-fuse7-undertow</module>
         <module>cxf-jaxrs</module>
         <module>cxf-jaxws</module>
         <module>camel</module>
diff --git a/examples/fuse/product-app-fuse7-undertow/pom.xml b/examples/fuse/product-app-fuse7-undertow/pom.xml
new file mode 100755
index 0000000..ea59251
--- /dev/null
+++ b/examples/fuse/product-app-fuse7-undertow/pom.xml
@@ -0,0 +1,95 @@
+<?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>product-portal-fuse-example-fuse7-undertow</artifactId>
+    <packaging>bundle</packaging>
+
+    <name>Product Portal - Secured in Karaf/Fuse 7.0 on Undertow</name>
+    <description/>
+
+    <properties>
+        <keycloak.osgi.export>
+        </keycloak.osgi.export>
+        <keycloak.osgi.import>
+            javax.wsdl,
+            javax.xml.bind,
+            javax.xml.bind.annotation,
+            javax.xml.namespace,
+            javax.xml.ws,
+            META-INF.cxf;version="[2.7,3.3)",
+            org.apache.cxf.transport.http;version="[2.7,3.3)",
+            org.apache.cxf.*;version="[2.7,3.3)",
+            org.keycloak.*;version="${project.version}",
+            org.keycloak.adapters.authentication;version="${project.version}";resolution:=optional,
+            *;resolution:=optional
+        </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>
+        </dependency>
+        <dependency>
+            <groupId>org.keycloak</groupId>
+            <artifactId>keycloak-core</artifactId>
+        </dependency>
+
+        <!-- Dependency for jaxws client to allow sending request to jaxws endpoint provided by cxf-jaxws-example -->
+        <dependency>
+            <groupId>org.keycloak.example.demo</groupId>
+            <artifactId>cxf-jaxws-example</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/product-app-fuse7-undertow/src/main/java/org/keycloak/example/ProductPortalServlet.java b/examples/fuse/product-app-fuse7-undertow/src/main/java/org/keycloak/example/ProductPortalServlet.java
new file mode 100644
index 0000000..ee023fa
--- /dev/null
+++ b/examples/fuse/product-app-fuse7-undertow/src/main/java/org/keycloak/example/ProductPortalServlet.java
@@ -0,0 +1,119 @@
+/*
+ * 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.cxf.endpoint.Client;
+import org.apache.cxf.frontend.ClientProxy;
+import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
+import org.apache.cxf.message.Message;
+import org.keycloak.KeycloakSecurityContext;
+import org.keycloak.common.util.KeycloakUriBuilder;
+import org.keycloak.constants.ServiceUrlConstants;
+import org.keycloak.example.ws.Product;
+import org.keycloak.example.ws.UnknownProductFault;
+
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import javax.xml.ws.WebServiceException;
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * Servlet for receiving informations about products from backend JAXWS service
+ *
+ * @author <a href="mailto:mposolda@redhat.com">Marek Posolda</a>
+ */
+public class ProductPortalServlet extends HttpServlet {
+
+    @Override
+    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
+        resp.setContentType("text/html");
+
+        // Send jaxws request
+        PrintWriter out = resp.getWriter();
+        out.println("<html><head><title>Product Portal Page</title></head><body>");
+
+        String logoutUri = KeycloakUriBuilder.fromUri("http://localhost:8080/auth").path(ServiceUrlConstants.TOKEN_SERVICE_LOGOUT_PATH)
+                .queryParam("redirect_uri", "http://localhost:8181/product-portal").build("demo").toString();
+        String acctUri = KeycloakUriBuilder.fromUri("http://localhost:8080/auth").path(ServiceUrlConstants.ACCOUNT_SERVICE_PATH)
+                .queryParam("referrer", "product-portal").build("demo").toString();
+
+        out.println("<p>Goto: <a href=\"/customer-portal\">customers</a> | <a href=\"" + logoutUri + "\">logout</a> | <a href=\"" + acctUri + "\">manage acct</a></p>");
+        out.println("Servlet User Principal <b>" + req.getUserPrincipal() + "</b> made this request.");
+
+        String unsecuredWsClientResponse = sendWsReq(req, "1", false);
+        String securedWsClientResponse = sendWsReq(req, "1", true);
+        String securedWsClient2Response = sendWsReq(req, "2", true);
+
+        out.println("<p>Product with ID 1 - unsecured request (it should end with failure): <b>" + unsecuredWsClientResponse + "</b></p><br>");
+        out.println("<p>Product with ID 1 - secured request: <b>" + securedWsClientResponse + "</b></p><br>");
+        out.println("<p>Product with ID 2 - secured request: <b>" + securedWsClient2Response + "</b></p><br>");
+        out.println("</body></html>");
+        out.flush();
+        out.close();
+    }
+
+    private String sendWsReq(HttpServletRequest req, String productId, boolean secured) {
+        Product simpleClient = null;
+//        try {
+            JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
+            factory.setServiceClass(Product.class);
+            factory.setAddress("http://localhost:8282/ProductServiceCF");
+
+            simpleClient = (Product)factory.create();
+/*        } catch (Exception ex) {
+            ex.printStackTrace();
+            String error = "Cannot create WS client: " + ex;
+            return error;
+        }*/
+        java.lang.String _getProduct_productIdVal = productId;
+        javax.xml.ws.Holder<java.lang.String> _getProduct_productId = new javax.xml.ws.Holder<java.lang.String>(_getProduct_productIdVal);
+        javax.xml.ws.Holder<java.lang.String> _getProduct_name = new javax.xml.ws.Holder<java.lang.String>();
+
+        // Attach Authorization header
+        if (secured) {
+            Client clientProxy = ClientProxy.getClient(simpleClient);
+
+            KeycloakSecurityContext session = (KeycloakSecurityContext) req.getAttribute(KeycloakSecurityContext.class.getName());
+            Map<String, List<String>> headers = new HashMap<String, List<String>>();
+            headers.put("Authorization", Arrays.asList("Bearer " + session.getTokenString()));
+
+            clientProxy.getRequestContext().put(Message.PROTOCOL_HEADERS, headers);
+        }
+
+        try {
+            simpleClient.getProduct(_getProduct_productId, _getProduct_name);
+            return String.format("Product received: id=%s, name=%s", _getProduct_productId.value, _getProduct_name.value);
+        } catch (UnknownProductFault upf) {
+            return "UnknownProductFault has occurred. Details: " + upf.toString();
+        } catch (WebServiceException wse) {
+            String error = "Can't receive product. Reason: " + wse.getMessage();
+            if (wse.getCause() != null) {
+                Throwable cause = wse.getCause();
+                error = error + " Details: " + cause.getClass().getName() + ": " + cause.getMessage();
+            }
+            return error;
+        }
+    }
+}
diff --git a/examples/fuse/product-app-fuse7-undertow/src/main/resources/OSGI-INF/blueprint/blueprint.xml b/examples/fuse/product-app-fuse7-undertow/src/main/resources/OSGI-INF/blueprint/blueprint.xml
new file mode 100644
index 0000000..2166764
--- /dev/null
+++ b/examples/fuse/product-app-fuse7-undertow/src/main/resources/OSGI-INF/blueprint/blueprint.xml
@@ -0,0 +1,53 @@
+<?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"
+           xsi:schemaLocation="http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd">
+
+    <bean id="servletConstraintMapping" class="org.keycloak.adapters.osgi.PaxWebSecurityConstraintMapping">
+        <property name="roles">
+            <list>
+                <value>user</value>
+            </list>
+        </property>
+        <property name="authentication" value="true"/>
+        <property name="url" value="/product-portal/*"/>
+    </bean>
+
+    <bean id="keycloakPaxWebIntegration" class="org.keycloak.adapters.osgi.undertow.PaxWebIntegrationService"
+          init-method="start" destroy-method="stop">
+        <property name="bundleContext" ref="blueprintBundleContext" />
+        <property name="constraintMappings">
+            <list>
+                <ref component-id="servletConstraintMapping" />
+            </list>
+        </property>
+    </bean>
+
+    <bean id="productServlet" class="org.keycloak.example.ProductPortalServlet" depends-on="keycloakPaxWebIntegration" />
+
+    <service ref="productServlet" interface="javax.servlet.Servlet">
+        <service-properties>
+            <entry key="alias" value="/product-portal" />
+            <entry key="servlet-name" value="ProductServlet" />
+            <entry key="keycloak.config.file" value="/keycloak.json" />
+        </service-properties>
+    </service>
+
+</blueprint>
\ No newline at end of file
diff --git a/examples/fuse/product-app-fuse7-undertow/src/main/resources/WEB-INF/keycloak.json b/examples/fuse/product-app-fuse7-undertow/src/main/resources/WEB-INF/keycloak.json
new file mode 100644
index 0000000..e90433a
--- /dev/null
+++ b/examples/fuse/product-app-fuse7-undertow/src/main/resources/WEB-INF/keycloak.json
@@ -0,0 +1,9 @@
+{
+    "realm": "demo",
+    "resource": "product-portal",
+    "auth-server-url": "http://localhost:8080/auth",
+    "ssl-required" : "external",
+    "credentials": {
+        "secret": "password"
+    }
+}