cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r774371 - in /cxf/trunk/rt/frontend/jaxrs: ./ src/main/java/org/apache/cxf/jaxrs/ src/main/java/org/apache/cxf/jaxrs/provider/ src/test/java/org/apache/cxf/jaxrs/ src/test/java/org/apache/cxf/jaxrs/provider/
Date Wed, 13 May 2009 13:59:01 GMT
Author: sergeyb
Date: Wed May 13 13:59:00 2009
New Revision: 774371

URL: http://svn.apache.org/viewvc?rev=774371&view=rev
Log:
JAXRS : adding XPathProvider and JAXRSServerFactoryTest

Added:
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/XPathProvider.java
  (with props)
    cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/JAXRSServerFactoryBeanTest.java
  (with props)
    cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/XPathProviderTest.java
  (with props)
Modified:
    cxf/trunk/rt/frontend/jaxrs/pom.xml
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/JAXRSServiceImpl.java

Modified: cxf/trunk/rt/frontend/jaxrs/pom.xml
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/pom.xml?rev=774371&r1=774370&r2=774371&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/pom.xml (original)
+++ cxf/trunk/rt/frontend/jaxrs/pom.xml Wed May 13 13:59:00 2009
@@ -144,7 +144,13 @@
             <version>${project.version}</version>
             <scope>test</scope>
         </dependency>
-		<dependency>
+        <dependency>
+            <groupId>org.apache.cxf</groupId>
+            <artifactId>cxf-rt-transports-http-jetty</artifactId>
+            <version>${project.version}</version>
+            <scope>test</scope>
+        </dependency> 
+	<dependency>
             <groupId>org.apache.xmlbeans</groupId>
             <artifactId>xmlbeans</artifactId>
         </dependency>

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/JAXRSServiceImpl.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/JAXRSServiceImpl.java?rev=774371&r1=774370&r2=774371&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/JAXRSServiceImpl.java (original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/JAXRSServiceImpl.java Wed
May 13 13:59:00 2009
@@ -67,7 +67,7 @@
         if (address == null) {
             Class primaryClass = classResourceInfos.get(0).getServiceClass();
             String ns = PackageUtils.getNamespace(PackageUtils.getPackageName(primaryClass));
-            return new QName(ns + "/jaxrs", primaryClass.getSimpleName());
+            return new QName(ns + "jaxrs", primaryClass.getSimpleName());
         } else {
             return new QName(address, "WebResource");
         }

Added: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/XPathProvider.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/XPathProvider.java?rev=774371&view=auto
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/XPathProvider.java
(added)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/XPathProvider.java
Wed May 13 13:59:00 2009
@@ -0,0 +1,92 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.apache.cxf.jaxrs.provider;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Type;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+
+import javax.ws.rs.Consumes;
+import javax.ws.rs.WebApplicationException;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.MultivaluedMap;
+import javax.ws.rs.ext.MessageBodyReader;
+import javax.ws.rs.ext.Provider;
+
+import org.apache.cxf.jaxrs.ext.xml.XMLSource;
+
+@Provider
+@Consumes({"text/xml", "application/xml", "application/*+xml" })
+public class XPathProvider implements MessageBodyReader<Object> {
+
+    private List<String> consumeMediaTypes;
+    private Map<String, String> classExpressions;
+    private String globalExpression;
+    private String className;
+    private Map<String, String> globalNamespaces = 
+        Collections.emptyMap();
+    
+    public void setConsumeMediaTypes(List<String> types) {
+        consumeMediaTypes = types;
+    }
+    
+    public List<String> getConsumeMediaTypes() {
+        return consumeMediaTypes;    
+    }
+
+    public void setExpression(String expr) {
+        globalExpression = expr;
+    }
+    
+    public void setClassName(String name) {
+        className = name;
+    }
+    
+    public void setExpressions(Map<String, String> expressions) {
+        classExpressions = expressions;
+    }
+    
+    public void setNamespaces(Map<String, String> nsMap) {
+        globalNamespaces = nsMap;
+    }
+    
+    public boolean isReadable(Class cls, Type genericType, Annotation[] annotations, MediaType
mediaType) {
+        
+        return globalExpression != null && (className == null 
+            || className != null && className.equals(cls.getName()))  
+            || classExpressions != null && classExpressions.containsKey(cls.getName());
+    }
+
+    public Object readFrom(Class<Object> cls, Type type, Annotation[] anns, MediaType
mt, 
+        MultivaluedMap<String, String> hrs, InputStream is) throws IOException, WebApplicationException
{
+        String expression = globalExpression != null ? globalExpression
+            : classExpressions.get(cls.getName());
+        if (expression == null) {
+            // must not happen if isReadable() returned true
+            throw new WebApplicationException(500);
+        }
+        XMLSource source = new XMLSource(is);
+        return source.getNode(expression, globalNamespaces, cls);
+    }
+
+}

Propchange: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/XPathProvider.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/XPathProvider.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/JAXRSServerFactoryBeanTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/JAXRSServerFactoryBeanTest.java?rev=774371&view=auto
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/JAXRSServerFactoryBeanTest.java
(added)
+++ cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/JAXRSServerFactoryBeanTest.java
Wed May 13 13:59:00 2009
@@ -0,0 +1,85 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.apache.cxf.jaxrs;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.ws.rs.core.Response;
+import javax.ws.rs.ext.ExceptionMapper;
+
+import org.apache.cxf.endpoint.Server;
+import org.apache.cxf.jaxrs.provider.ProviderFactory;
+import org.apache.cxf.jaxrs.resources.BookStore;
+import org.apache.cxf.message.MessageImpl;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+public class JAXRSServerFactoryBeanTest extends Assert {
+
+    @Test
+    public void testRegisterProviders() {
+        JAXRSServerFactoryBean bean = new JAXRSServerFactoryBean();
+        bean.setAddress("http://localhost:8080/rest");
+        bean.setStart(false);
+        bean.setResourceClasses(BookStore.class);
+        List<Object> providers = new ArrayList<Object>();
+        Object provider1 = new CustomExceptionMapper();
+        providers.add(provider1);
+        Object provider2 = new RuntimeExceptionMapper();
+        providers.add(provider2);
+        bean.setProviders(providers);
+        Server s = bean.create();
+        
+        ProviderFactory factory = 
+            (ProviderFactory)s.getEndpoint().get(ProviderFactory.class.getName());
+        
+        ExceptionMapper<Exception> mapper1 = 
+            factory.createExceptionMapper(Exception.class, new MessageImpl());
+        assertNotNull(mapper1);
+        ExceptionMapper<RuntimeException> mapper2 = 
+            factory.createExceptionMapper(RuntimeException.class, new MessageImpl());
+        assertNotNull(mapper2);
+        assertNotSame(mapper1, mapper2);
+        assertSame(provider1, mapper1);
+        assertSame(provider2, mapper2);
+        
+        
+    }
+    
+    
+    private static class CustomExceptionMapper implements ExceptionMapper<Exception>
{
+
+        public Response toResponse(Exception exception) {
+            // TODO Auto-generated method stub
+            return null;
+        }
+        
+    }
+    
+    private static class RuntimeExceptionMapper implements ExceptionMapper<RuntimeException>
{
+
+        public Response toResponse(RuntimeException exception) {
+            // TODO Auto-generated method stub
+            return null;
+        }
+        
+    }
+}

Propchange: cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/JAXRSServerFactoryBeanTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/JAXRSServerFactoryBeanTest.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/XPathProviderTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/XPathProviderTest.java?rev=774371&view=auto
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/XPathProviderTest.java
(added)
+++ cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/XPathProviderTest.java
Wed May 13 13:59:00 2009
@@ -0,0 +1,73 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.apache.cxf.jaxrs.provider;
+
+import java.io.ByteArrayInputStream;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.cxf.jaxrs.resources.Book;
+import org.apache.cxf.jaxrs.resources.BookStore;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+public class XPathProviderTest extends Assert {
+
+    @Test
+    public void testIsReadableClassName() {
+        XPathProvider provider = new XPathProvider();
+        provider.setExpression("/");
+        assertTrue(provider.isReadable((Class)Book.class, null, null, null));
+        assertTrue(provider.isReadable((Class)BookStore.class, null, null, null));
+        provider.setClassName(Book.class.getName());
+        assertFalse(provider.isReadable((Class)BookStore.class, null, null, null));
+        assertTrue(provider.isReadable((Class)Book.class, null, null, null));
+        provider.setClassName(null);
+        assertTrue(provider.isReadable((Class)Book.class, null, null, null));
+        assertTrue(provider.isReadable((Class)BookStore.class, null, null, null));
+    }
+    
+    @Test
+    public void testIsReadableClassNames() {
+        XPathProvider provider = new XPathProvider();
+        assertFalse(provider.isReadable((Class)Book.class, null, null, null));
+        assertFalse(provider.isReadable((Class)BookStore.class, null, null, null));
+        Map<String, String> map = new HashMap<String, String>();
+        map.put(Book.class.getName(), "/");
+        provider.setExpressions(map);
+        assertFalse(provider.isReadable((Class)BookStore.class, null, null, null));
+        assertTrue(provider.isReadable((Class)Book.class, null, null, null));
+    }
+    
+    @SuppressWarnings("unchecked")
+    @Test
+    public void testReadFrom() throws Exception {
+        String value = "<Book><name>The Book</name><id>2</id></Book>";
+        XPathProvider provider = new XPathProvider();
+        provider.setExpression("/Book");
+        provider.setClassName(Book.class.getName());
+        Book book = (Book)provider.readFrom((Class)Book.class, null, null, null, null,
+                          new ByteArrayInputStream(value.getBytes()));
+        assertNotNull(book);
+        assertEquals(2L, book.getId());
+        assertEquals("The Book", book.getName());
+    }
+    
+}

Propchange: cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/XPathProviderTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/XPathProviderTest.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date



Mime
View raw message