cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r999636 - in /cxf/trunk/rt/frontend/jaxrs/src: main/java/org/apache/cxf/jaxrs/impl/ main/java/org/apache/cxf/jaxrs/provider/ main/java/org/apache/cxf/jaxrs/utils/ test/java/org/apache/cxf/jaxrs/impl/ test/java/org/apache/cxf/jaxrs/provider/
Date Tue, 21 Sep 2010 20:57:16 GMT
Author: sergeyb
Date: Tue Sep 21 20:57:15 2010
New Revision: 999636

URL: http://svn.apache.org/viewvc?rev=999636&view=rev
Log:
Updating JSONProvider to support TypeConverters and fixing SecurityContextImpl to check Authorization
headers

Added:
    cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/SecurityContextImplTest.java
  (with props)
Modified:
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/SecurityContextImpl.java
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AegisJSONProvider.java
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/DataBindingJSONProvider.java
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JSONProvider.java
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JSONUtils.java
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/JAXRSUtils.java
    cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/JSONProviderTest.java

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/SecurityContextImpl.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/SecurityContextImpl.java?rev=999636&r1=999635&r2=999636&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/SecurityContextImpl.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/SecurityContextImpl.java
Tue Sep 21 20:57:15 2010
@@ -20,6 +20,7 @@
 package org.apache.cxf.jaxrs.impl;
 
 import java.security.Principal;
+import java.util.List;
 
 import javax.ws.rs.core.SecurityContext;
 
@@ -34,12 +35,13 @@ public class SecurityContextImpl impleme
         this.m = m;
     }
 
-    // TODO
     public String getAuthenticationScheme() {
         if (m.get(AuthorizationPolicy.class) != null) {
             return SecurityContext.BASIC_AUTH;
         }
-        return "Unknown scheme";
+        List<String> authorizationValues = new HttpHeadersImpl(m).getRequestHeader("Authorization");
+        
+        return authorizationValues.size() > 0 ? authorizationValues.get(0) : "Unknown
scheme";
     }
 
     public Principal getUserPrincipal() {

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AegisJSONProvider.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AegisJSONProvider.java?rev=999636&r1=999635&r2=999636&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AegisJSONProvider.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AegisJSONProvider.java
Tue Sep 21 20:57:15 2010
@@ -45,6 +45,7 @@ import org.apache.cxf.aegis.AegisWriter;
 import org.apache.cxf.aegis.type.AegisType;
 import org.apache.cxf.staxutils.StaxUtils;
 import org.apache.cxf.staxutils.W3CDOMStreamWriter;
+import org.codehaus.jettison.mapped.Configuration;
 
 @Provider
 @Produces({"application/json" })
@@ -125,8 +126,10 @@ public final class AegisJSONProvider<T> 
     @Override
     protected XMLStreamWriter createStreamWriter(QName typeQName, OutputStream os) throws
Exception {
         
+        Configuration config = 
+            JSONUtils.createConfiguration(namespaceMap, writeXsiType && !ignoreNamespaces,
null);
         XMLStreamWriter writer = JSONUtils.createStreamWriter(os, typeQName, 
-             writeXsiType && !ignoreNamespaces, namespaceMap, serializeAsArray, arrayKeys,
dropRootElement);
+             writeXsiType && !ignoreNamespaces, config, serializeAsArray, arrayKeys,
dropRootElement);
         return JSONUtils.createIgnoreNsWriterIfNeeded(writer, ignoreNamespaces);
     }
 

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/DataBindingJSONProvider.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/DataBindingJSONProvider.java?rev=999636&r1=999635&r2=999636&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/DataBindingJSONProvider.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/DataBindingJSONProvider.java
Tue Sep 21 20:57:15 2010
@@ -36,6 +36,7 @@ import javax.xml.stream.XMLStreamWriter;
 
 import org.apache.cxf.jaxrs.utils.InjectionUtils;
 import org.apache.cxf.jaxrs.utils.JAXRSUtils;
+import org.codehaus.jettison.mapped.Configuration;
 
 @Provider
 @Produces("application/json")
@@ -93,8 +94,10 @@ public class DataBindingJSONProvider<T> 
         } else {
             qname = getQName(InjectionUtils.getActualType(genericType));
         }
+        Configuration config = 
+            JSONUtils.createConfiguration(namespaceMap, writeXsiType && !ignoreNamespaces,
null);
         XMLStreamWriter writer = JSONUtils.createStreamWriter(os, qname, 
-             writeXsiType && !ignoreNamespaces, namespaceMap, serializeAsArray, arrayKeys,
dropRootElement);
+             writeXsiType && !ignoreNamespaces, config, serializeAsArray, arrayKeys,
dropRootElement);
         writer = JSONUtils.createIgnoreMixedContentWriterIfNeeded(writer, ignoreMixedContent);
         return JSONUtils.createIgnoreNsWriterIfNeeded(writer, ignoreNamespaces);
     }

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JSONProvider.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JSONProvider.java?rev=999636&r1=999635&r2=999636&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JSONProvider.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JSONProvider.java
Tue Sep 21 20:57:15 2010
@@ -54,6 +54,9 @@ import org.apache.cxf.jaxrs.ext.MessageC
 import org.apache.cxf.jaxrs.utils.HttpUtils;
 import org.apache.cxf.jaxrs.utils.InjectionUtils;
 import org.apache.cxf.jaxrs.utils.schemas.SchemaHandler;
+import org.codehaus.jettison.mapped.Configuration;
+import org.codehaus.jettison.mapped.SimpleConverter;
+import org.codehaus.jettison.mapped.TypeConverter;
 
 @Produces("application/json")
 @Consumes("application/json")
@@ -77,6 +80,7 @@ public class JSONProvider extends Abstra
     private boolean readXsiType = true;
     private boolean ignoreNamespaces;
     private String convention = MAPPED_CONVENTION;
+    private TypeConverter typeConverter;
     
     public void setConvention(String value) {
         if (!MAPPED_CONVENTION.equals(value) && !BADGER_FISH_CONVENTION.equals(value))
{
@@ -85,6 +89,16 @@ public class JSONProvider extends Abstra
         convention = value;
     }
     
+    public void setConvertTypesToStrings(boolean convert) {
+        if (convert) {
+            this.setTypeConverter(new SimpleConverter());
+        }
+    }
+    
+    public void setTypeConverter(TypeConverter converter) {
+        this.typeConverter = converter;
+    }
+    
     public void setIgnoreNamespaces(boolean ignoreNamespaces) {
         this.ignoreNamespaces = ignoreNamespaces;
     }
@@ -339,8 +353,12 @@ public class JSONProvider extends Abstra
         }
         
         QName qname = getQName(actualClass, genericType, actualObject, true);
+        
+        Configuration config = 
+            JSONUtils.createConfiguration(namespaceMap, writeXsiType && !ignoreNamespaces,
typeConverter);
+        
         XMLStreamWriter writer = JSONUtils.createStreamWriter(os, qname, 
-             writeXsiType && !ignoreNamespaces, namespaceMap, serializeAsArray, arrayKeys,
+             writeXsiType && !ignoreNamespaces, config, serializeAsArray, arrayKeys,
              isCollection || dropRootElement);
         writer = JSONUtils.createIgnoreMixedContentWriterIfNeeded(writer, ignoreMixedContent);
         writer = JSONUtils.createIgnoreNsWriterIfNeeded(writer, ignoreNamespaces);

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JSONUtils.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JSONUtils.java?rev=999636&r1=999635&r2=999636&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JSONUtils.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JSONUtils.java
Tue Sep 21 20:57:15 2010
@@ -48,6 +48,7 @@ import org.codehaus.jettison.mapped.Conf
 import org.codehaus.jettison.mapped.MappedNamespaceConvention;
 import org.codehaus.jettison.mapped.MappedXMLInputFactory;
 import org.codehaus.jettison.mapped.MappedXMLStreamWriter;
+import org.codehaus.jettison.mapped.TypeConverter;
 
 public final class JSONUtils {
 
@@ -67,19 +68,16 @@ public final class JSONUtils {
         XMLInputFactory factory = new BadgerFishXMLInputFactory();
         return factory.createXMLStreamReader(is);
     }
-    
+        
     public static XMLStreamWriter createStreamWriter(OutputStream os, 
                                                      QName qname, 
                                                      boolean writeXsiType,
-                                                     ConcurrentHashMap<String, String>
namespaceMap,
+                                                     Configuration config,
                                                      boolean serializeAsArray,
                                                      List<String> arrayKeys,
                                                      boolean dropRootElement) throws Exception
{
-        if (writeXsiType) {
-            namespaceMap.putIfAbsent(XSI_URI, XSI_PREFIX);
-        }
-        Configuration c = new Configuration(namespaceMap);
-        MappedNamespaceConvention convention = new PrefixRespectingMappedNamespaceConvention(c);
+        
+        MappedNamespaceConvention convention = new PrefixRespectingMappedNamespaceConvention(config);
         AbstractXMLStreamWriter xsw = new MappedXMLStreamWriter(
                                             convention, 
                                             new OutputStreamWriter(os, UTF8));
@@ -100,6 +98,19 @@ public final class JSONUtils {
         return writer;
     }    
     
+    public static Configuration createConfiguration(ConcurrentHashMap<String, String>
namespaceMap,
+                                                    boolean writeXsiType,
+                                                    TypeConverter converter) {
+        if (writeXsiType) {
+            namespaceMap.putIfAbsent(XSI_URI, XSI_PREFIX);
+        }
+        Configuration c = new Configuration(namespaceMap);
+        if (converter != null) {
+            c.setTypeConverter(converter);
+        }
+        return c;
+    }
+    
     public static XMLStreamWriter createIgnoreMixedContentWriterIfNeeded(XMLStreamWriter
writer, 
                                                                          boolean ignoreMixedContent)
{
         return ignoreMixedContent ? new IgnoreMixedContentWriter(writer) : writer; 

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/JAXRSUtils.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/JAXRSUtils.java?rev=999636&r1=999635&r2=999636&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/JAXRSUtils.java (original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/JAXRSUtils.java Tue
Sep 21 20:57:15 2010
@@ -791,7 +791,8 @@ public final class JAXRSUtils {
         } else if (Request.class.isAssignableFrom(clazz)) {
             o = new RequestImpl(contextMessage);
         } else if (SecurityContext.class.isAssignableFrom(clazz)) {
-            o = new SecurityContextImpl(contextMessage);
+            SecurityContext customContext = contextMessage.get(SecurityContext.class);
+            o = customContext == null ? new SecurityContextImpl(contextMessage) : customContext;
         } else if (Providers.class.isAssignableFrom(clazz)) {
             o = new ProvidersImpl(contextMessage);
         } else if (ContextResolver.class.isAssignableFrom(clazz)) {

Added: cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/SecurityContextImplTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/SecurityContextImplTest.java?rev=999636&view=auto
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/SecurityContextImplTest.java
(added)
+++ cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/SecurityContextImplTest.java
Tue Sep 21 20:57:15 2010
@@ -0,0 +1,45 @@
+/**
+ * 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.impl;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.cxf.message.Message;
+import org.apache.cxf.message.MessageImpl;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+public class SecurityContextImplTest extends Assert {
+
+    @Test
+    public void testAuthenticationScheme() {
+        Message m = new MessageImpl();
+        Map<String, List<String>> requestHeaders = new HashMap<String, List<String>>();
+        List<String> values = new ArrayList<String>();
+        values.add("Digest realm=\"custom\"");
+        requestHeaders.put("Authorization", values);
+        m.put(Message.PROTOCOL_HEADERS, requestHeaders);
+        String scheme = new SecurityContextImpl(m).getAuthenticationScheme();
+        assertEquals("Digest", scheme);
+    }
+}

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

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

Modified: cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/JSONProviderTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/JSONProviderTest.java?rev=999636&r1=999635&r2=999636&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/JSONProviderTest.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/JSONProviderTest.java
Tue Sep 21 20:57:15 2010
@@ -198,6 +198,22 @@ public class JSONProviderTest extends As
     }
     
     @Test
+    public void testWriteBookWithStringConverter() throws Exception {
+        JSONProvider p = new JSONProvider();
+        p.setConvertTypesToStrings(true);
+        Book book = new Book("CXF", 125);
+        
+        ByteArrayOutputStream os = new ByteArrayOutputStream();
+        
+        p.writeTo(book, (Class)Book.class, Book.class, Book.class.getAnnotations(), 
+                  MediaType.APPLICATION_JSON_TYPE, new MetadataMap<String, Object>(),
os);
+        
+        String s = os.toString();
+        assertEquals("{\"Book\":{\"id\":\"125\",\"name\":\"CXF\",\"state\":\"\"}}", s);
+        
+    }
+    
+    @Test
     public void testWriteToSingleTagBadgerFish() throws Exception {
         JSONProvider p = new JSONProvider();
         p.setConvention("badgerfish");



Mime
View raw message