tomee-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rmannibu...@apache.org
Subject svn commit: r1528735 - in /tomee/tomee/trunk/server/openejb-cxf-rs/src: main/java/org/apache/openejb/server/cxf/rs/CxfRsHttpListener.java test/java/org/apache/openejb/server/cxf/rs/RsJMXTest.java
Date Thu, 03 Oct 2013 07:18:46 GMT
Author: rmannibucau
Date: Thu Oct  3 07:18:46 2013
New Revision: 1528735

URL: http://svn.apache.org/r1528735
Log:
adding getWadl in cxf-rs mbeans

Modified:
    tomee/tomee/trunk/server/openejb-cxf-rs/src/main/java/org/apache/openejb/server/cxf/rs/CxfRsHttpListener.java
    tomee/tomee/trunk/server/openejb-cxf-rs/src/test/java/org/apache/openejb/server/cxf/rs/RsJMXTest.java

Modified: tomee/tomee/trunk/server/openejb-cxf-rs/src/main/java/org/apache/openejb/server/cxf/rs/CxfRsHttpListener.java
URL: http://svn.apache.org/viewvc/tomee/tomee/trunk/server/openejb-cxf-rs/src/main/java/org/apache/openejb/server/cxf/rs/CxfRsHttpListener.java?rev=1528735&r1=1528734&r2=1528735&view=diff
==============================================================================
--- tomee/tomee/trunk/server/openejb-cxf-rs/src/main/java/org/apache/openejb/server/cxf/rs/CxfRsHttpListener.java
(original)
+++ tomee/tomee/trunk/server/openejb-cxf-rs/src/main/java/org/apache/openejb/server/cxf/rs/CxfRsHttpListener.java
Thu Oct  3 07:18:46 2013
@@ -20,12 +20,15 @@ import org.apache.cxf.endpoint.Server;
 import org.apache.cxf.helpers.IOUtils;
 import org.apache.cxf.jaxrs.JAXRSServerFactoryBean;
 import org.apache.cxf.jaxrs.JAXRSServiceImpl;
+import org.apache.cxf.jaxrs.ext.RequestHandler;
 import org.apache.cxf.jaxrs.ext.ResourceComparator;
 import org.apache.cxf.jaxrs.lifecycle.ResourceProvider;
 import org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider;
 import org.apache.cxf.jaxrs.model.ClassResourceInfo;
 import org.apache.cxf.jaxrs.model.MethodDispatcher;
 import org.apache.cxf.jaxrs.model.OperationResourceInfo;
+import org.apache.cxf.jaxrs.model.ProviderInfo;
+import org.apache.cxf.jaxrs.model.wadl.WadlGenerator;
 import org.apache.cxf.jaxrs.provider.JAXBElementProvider;
 import org.apache.cxf.jaxrs.provider.json.JSONProvider;
 import org.apache.cxf.service.invoker.Invoker;
@@ -38,9 +41,11 @@ import org.apache.openejb.api.internal.I
 import org.apache.openejb.api.jmx.Description;
 import org.apache.openejb.api.jmx.MBean;
 import org.apache.openejb.api.jmx.ManagedAttribute;
+import org.apache.openejb.api.jmx.ManagedOperation;
 import org.apache.openejb.assembler.classic.ServiceInfo;
 import org.apache.openejb.assembler.classic.util.ServiceConfiguration;
 import org.apache.openejb.assembler.classic.util.ServiceInfos;
+import org.apache.openejb.loader.IO;
 import org.apache.openejb.loader.SystemInstance;
 import org.apache.openejb.monitoring.LocalMBeanServer;
 import org.apache.openejb.monitoring.ObjectNameBuilder;
@@ -70,6 +75,8 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.lang.annotation.Annotation;
 import java.lang.reflect.Constructor;
+import java.net.HttpURLConnection;
+import java.net.URL;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
@@ -114,6 +121,16 @@ public class CxfRsHttpListener implement
         STATIC_CONTENT_TYPES.put("ico", "image/ico");
         STATIC_CONTENT_TYPES.put("pdf", "application/pdf");
         STATIC_CONTENT_TYPES.put("xsd", "application/xml");
+
+        // bug in CXF? it prevents to get the wadl as json otherwise
+        if ("true".equalsIgnoreCase(SystemInstance.get().getProperty("openejb.cxf-rs.wadl-generator.ignoreMessageWriters",
"true"))) {
+            for (final ProviderInfo<RequestHandler> rh : org.apache.cxf.jaxrs.provider.ProviderFactory.getSharedInstance().getRequestHandlers())
{
+                final RequestHandler provider = rh.getProvider();
+                if (WadlGenerator.class.isInstance(provider)) {
+                    WadlGenerator.class.cast(provider).setIgnoreMessageWriters(false);
+                }
+            }
+        }
     }
 
     public CxfRsHttpListener(final HTTPTransportFactory httpTransportFactory, final String
star) {
@@ -648,6 +665,41 @@ public class CxfRsHttpListener implement
 
         @ManagedAttribute
         @Description("The type of the REST service")
+        public String getWadlUrl() {
+            if (address.endsWith("?_wadl")) {
+                return address;
+            }
+            return address + "?_wadl";
+        }
+
+        @ManagedOperation
+        @Description("The type of the REST service")
+        public String getWadl(final String format) {
+            if (format != null && format.toLowerCase().contains("json")) {
+                InputStream inputStream = null;
+                try {
+                    final URL url = new URL(getWadlUrl() + "&_type=json");
+                    final HttpURLConnection connection = HttpURLConnection.class.cast(url.openConnection());
+                    connection.setRequestProperty("Accept", "application/json");
+                    connection.setRequestProperty("Content-type", "application/json");
+                    inputStream = connection.getInputStream();
+                    return IO.slurp(inputStream);
+                } catch (final Exception e) {
+                    return e.getMessage();
+                } finally {
+                    IO.close(inputStream);
+                }
+            } else { // xml
+                try {
+                    return IO.slurp(new URL(getWadlUrl()));
+                } catch (final IOException e) {
+                    return e.getMessage();
+                }
+            }
+        }
+
+        @ManagedAttribute
+        @Description("The type of the REST service")
         public String getType() {
             return type;
         }

Modified: tomee/tomee/trunk/server/openejb-cxf-rs/src/test/java/org/apache/openejb/server/cxf/rs/RsJMXTest.java
URL: http://svn.apache.org/viewvc/tomee/tomee/trunk/server/openejb-cxf-rs/src/test/java/org/apache/openejb/server/cxf/rs/RsJMXTest.java?rev=1528735&r1=1528734&r2=1528735&view=diff
==============================================================================
--- tomee/tomee/trunk/server/openejb-cxf-rs/src/test/java/org/apache/openejb/server/cxf/rs/RsJMXTest.java
(original)
+++ tomee/tomee/trunk/server/openejb-cxf-rs/src/test/java/org/apache/openejb/server/cxf/rs/RsJMXTest.java
Thu Oct  3 07:18:46 2013
@@ -22,6 +22,7 @@ import org.apache.openejb.monitoring.Loc
 import org.apache.openejb.testing.Classes;
 import org.apache.openejb.testing.EnableServices;
 import org.apache.openejb.testing.Module;
+import org.hamcrest.CoreMatchers;
 import org.junit.AfterClass;
 import org.junit.BeforeClass;
 import org.junit.Test;
@@ -33,6 +34,7 @@ import javax.ws.rs.GET;
 import javax.ws.rs.Path;
 
 import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertThat;
 import static org.junit.Assert.assertTrue;
 
 @EnableServices("jax-rs")
@@ -54,6 +56,12 @@ public class RsJMXTest {
     @Test
     public void checkServiceWasDeployed() throws Exception {
         assertTrue(LocalMBeanServer.get().isRegistered(name));
+
+        final String wadlXml = String.class.cast(LocalMBeanServer.get().invoke(name, "getWadl",
new Object[]{null}, new String[0]));
+        assertThat(wadlXml, wadlXml, CoreMatchers.containsString("<resources base=\"http://127.0.0.1:4204/app/foo/"));
+
+        final String wadlJson = String.class.cast(LocalMBeanServer.get().invoke(name, "getWadl",
new Object[]{"json"}, new String[0]));
+        assertThat(wadlJson, wadlJson, CoreMatchers.containsString("{\"application\":{"));
     }
 
     @AfterClass
@@ -61,11 +69,17 @@ public class RsJMXTest {
         assertFalse(LocalMBeanServer.get().isRegistered(name));
     }
 
-    @Path("/foo")
+    @Path("foo")
     public static class AnEndpoint {
         @GET
         public String bar() {
             return "bar";
         }
+
+        @GET
+        @Path("babar")
+        public String babar() {
+            return "babar";
+        }
     }
 }



Mime
View raw message