cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r736726 - in /cxf/trunk: rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/ rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/tl/ rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ rt/frontend/jaxrs/src/main/java/...
Date Thu, 22 Jan 2009 17:49:45 GMT
Author: sergeyb
Date: Thu Jan 22 09:49:43 2009
New Revision: 736726

URL: http://svn.apache.org/viewvc?rev=736726&view=rev
Log:
JAXRS: context injection into base jaxb provider through setters, minor changes to message
context api

Modified:
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/MessageContext.java
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/MessageContextImpl.java
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/tl/ThreadLocalMessageContext.java
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractJAXBProvider.java
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JAXBElementProvider.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/utils/AttachmentUtils.java
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/Messages.properties
    cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSMultipartTest.java
    cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/MultipartStore.java

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/MessageContext.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/MessageContext.java?rev=736726&r1=736725&r2=736726&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/MessageContext.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/MessageContext.java
Thu Jan 22 09:49:43 2009
@@ -19,9 +19,6 @@
 
 package org.apache.cxf.jaxrs.ext;
 
-import java.util.Map;
-
-import javax.activation.DataHandler;
 import javax.servlet.ServletConfig;
 import javax.servlet.ServletContext;
 import javax.servlet.http.HttpServletRequest;
@@ -44,8 +41,6 @@
     Object get(Object key);
     void put(Object key, Object value);
     
-    Map<String, DataHandler> getAttachments();
-    
     UriInfo getUriInfo();
     Request getRequest();
     HttpHeaders getHttpHeaders();
@@ -58,4 +53,5 @@
     ServletConfig getServletConfig();
     
     <T> T getContext(Class<T> contextClass);
+    <T, E> T getResolver(Class<T> resolverClass, Class<E> resolveClazz);
 }

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/MessageContextImpl.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/MessageContextImpl.java?rev=736726&r1=736725&r2=736726&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/MessageContextImpl.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/MessageContextImpl.java
Thu Jan 22 09:49:43 2009
@@ -20,6 +20,7 @@
 
 import java.lang.reflect.Type;
 import java.util.Collection;
+import java.util.Collections;
 import java.util.Map;
 
 import javax.activation.DataHandler;
@@ -55,10 +56,6 @@
         return m.get(key);
     }
 
-    public Map<String, DataHandler> getAttachments() {
-        return createAttachments(MessageContext.INBOUND_MESSAGE_ATTACHMENTS);
-    } 
-    
     public <T> T getContext(Class<T> contextClass) {
         return getContext(contextClass, contextClass);
     }
@@ -68,8 +65,11 @@
     }
     
     @SuppressWarnings("unchecked")
-    public <T> ContextResolver<T> getContextResolver(Class<T> resolveClazz)
{
-        return getContext(resolveClazz, ContextResolver.class);
+    public <T, E> T getResolver(Class<T> resolverClazz, Class<E> resolveClazz)
{
+        if (ContextResolver.class == resolverClazz) {
+            return resolverClazz.cast(getContext(resolveClazz, ContextResolver.class));
+        }
+        return null;
     }
     
     public Request getRequest() {
@@ -118,6 +118,9 @@
             return CastUtils.cast((Map)o);
         }
         Collection<Attachment> attachments = m.getAttachments();
+        if (attachments == null) {
+            return Collections.emptyMap();
+        }
         attachments.size();
         Map<String, DataHandler> dataHandlers = AttachmentUtil.getDHMap(attachments);
         m.put(propertyName, dataHandlers);

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/tl/ThreadLocalMessageContext.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/tl/ThreadLocalMessageContext.java?rev=736726&r1=736725&r2=736726&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/tl/ThreadLocalMessageContext.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/tl/ThreadLocalMessageContext.java
Thu Jan 22 09:49:43 2009
@@ -19,9 +19,6 @@
 
 package org.apache.cxf.jaxrs.impl.tl;
 
-import java.util.Map;
-
-import javax.activation.DataHandler;
 import javax.servlet.ServletConfig;
 import javax.servlet.ServletContext;
 import javax.servlet.http.HttpServletRequest;
@@ -81,10 +78,6 @@
         return get() != null ? get().getRequest() : null;
     }
 
-    public Map<String, DataHandler> getAttachments() {
-        return get() != null ? get().getAttachments() : null;
-    }
-
     public void put(Object key, Object value) {
         if (get() != null) {
             get().put(key, value);
@@ -93,4 +86,8 @@
         
     }
 
+    public <T, E> T getResolver(Class<T> resolverClass, Class<E> resolveClazz)
{
+        return get() != null ? get().getResolver(resolverClass, resolveClazz) : null;
+    }
+
 }

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractJAXBProvider.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractJAXBProvider.java?rev=736726&r1=736725&r2=736726&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractJAXBProvider.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractJAXBProvider.java
Thu Jan 22 09:49:43 2009
@@ -19,6 +19,8 @@
 
 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.List;
@@ -30,6 +32,7 @@
 import javax.ws.rs.WebApplicationException;
 import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.Response;
+import javax.ws.rs.ext.ContextResolver;
 import javax.ws.rs.ext.MessageBodyReader;
 import javax.ws.rs.ext.MessageBodyWriter;
 import javax.xml.bind.JAXBContext;
@@ -47,6 +50,7 @@
 import org.apache.cxf.common.util.PackageUtils;
 import org.apache.cxf.jaxrs.ext.MessageContext;
 import org.apache.cxf.jaxrs.utils.AnnotationUtils;
+import org.apache.cxf.jaxrs.utils.AttachmentUtils;
 import org.apache.cxf.jaxrs.utils.InjectionUtils;
 import org.apache.cxf.jaxrs.utils.schemas.SchemaHandler;
 
@@ -54,16 +58,22 @@
     implements MessageBodyReader<Object>, MessageBodyWriter<Object> {
     
     protected static final ResourceBundle BUNDLE = BundleUtils.getBundle(AbstractJAXBProvider.class);
-       
-    private static final Logger LOG = LogUtils.getL7dLogger(AbstractJAXBProvider.class);
 
+    private static final MediaType MULTIPART_RELATED_TYPE = 
+        MediaType.valueOf("multipart/related");
+    private static final Logger LOG = LogUtils.getL7dLogger(AbstractJAXBProvider.class);
     private static final String CHARSET_PARAMETER = "charset"; 
         
     private static Map<String, JAXBContext> packageContexts = new WeakHashMap<String,
JAXBContext>();
     private static Map<Class<?>, JAXBContext> classContexts = new WeakHashMap<Class<?>,
JAXBContext>();
-        
+   
+    private MessageContext mc;
     private Schema schema;
             
+    protected void setContext(MessageContext context) {
+        mc = context;
+    }
+    
     public boolean isWriteable(Class<?> type, Type genericType, Annotation[] anns,
MediaType mt) {
         return isSupported(type, genericType, anns)
                || AnnotationUtils.getAnnotation(anns, XmlJavaTypeAdapter.class) != null;
@@ -85,15 +95,31 @@
         return -1;
     }
 
-    // TODO : no contexts can be inhected in superclasses
-    protected abstract MessageContext getContext(); 
+    protected MessageContext getContext() {
+        return mc;
+    }
     
+    public InputStream getInputStream(Class<Object> type, Annotation[] anns, MediaType
mt, 
+                                      InputStream is) throws IOException {
+        if (mt.isCompatible(MULTIPART_RELATED_TYPE)) {
+            is = (InputStream)AttachmentUtils.getMultipart(type, anns, mt, getContext(),
is);
+            if (is == null) {
+                throw new WebApplicationException(404);
+            }
+        }
+        return is;
+    }
+    
+    @SuppressWarnings("unchecked")
     protected JAXBContext getJAXBContext(Class<?> type, Type genericType) throws JAXBException
{
-        MessageContext mc = getContext();
         if (mc != null) {
-            JAXBContext customContext = mc.getContext(JAXBContext.class);
-            if (customContext != null) {
-                return customContext;
+            ContextResolver<JAXBContext> resolver = 
+                mc.getResolver(ContextResolver.class, JAXBContext.class);
+            if (resolver != null) {
+                JAXBContext customContext = resolver.getContext(type);
+                if (customContext != null) {
+                    return customContext;
+                }
             }
         }
         

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JAXBElementProvider.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JAXBElementProvider.java?rev=736726&r1=736725&r2=736726&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JAXBElementProvider.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JAXBElementProvider.java
Thu Jan 22 09:49:43 2009
@@ -42,7 +42,6 @@
 import javax.xml.transform.stream.StreamSource;
 
 import org.apache.cxf.jaxrs.ext.MessageContext;
-import org.apache.cxf.jaxrs.utils.AttachmentUtils;
 import org.apache.cxf.jaxrs.utils.schemas.SchemaHandler;
 
 @Produces({"application/xml", "text/xml" })
@@ -52,14 +51,11 @@
 @Provider
 public class JAXBElementProvider extends AbstractJAXBProvider  {
     
-    private static final MediaType MULTIPART_RELATED_TYPE = 
-        MediaType.valueOf("multipart/related");
-    
     private Map<String, Object> mProperties = new HashMap<String, Object>();
-    @Context private MessageContext mc;
     
-    protected MessageContext getContext() {
-        return mc;
+    @Context
+    public void setMessageContext(MessageContext mc) {
+        super.setContext(mc);
     }
     
     public void setConsumeMediaTypes(List<String> types) {
@@ -87,15 +83,10 @@
     }
     
     public Object readFrom(Class<Object> type, Type genericType, Annotation[] anns,
MediaType mt, 
-        MultivaluedMap<String, String> headers, InputStream is) 
+        MultivaluedMap<String, String> headers, InputStream stream) 
         throws IOException {
         try {
-            if (mt.isCompatible(MULTIPART_RELATED_TYPE)) {
-                is = (InputStream)AttachmentUtils.getMultipart(type, anns, mt, mc, is);
-                if (is == null) {
-                    throw new WebApplicationException(404);
-                }
-            }
+            InputStream is = getInputStream(type, anns, mt, stream);
             Class<?> theType = getActualType(type, genericType);
             Unmarshaller unmarshaller = createUnmarshaller(theType, genericType);
             
@@ -107,6 +98,8 @@
             
         } catch (JAXBException e) {
             handleJAXBException(e);
+        } catch (WebApplicationException e) {
+            throw e;
         } catch (Exception e) {
             throw new WebApplicationException(e);        
         }

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=736726&r1=736725&r2=736726&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
Thu Jan 22 09:49:43 2009
@@ -65,10 +65,10 @@
     private Map<String, String> namespaceMap = new HashMap<String, String>();
     private boolean serializeAsArray;
     private List<String> arrayKeys;
-    @Context private MessageContext mc;
     
-    protected MessageContext getContext() {
-        return mc;
+    @Context
+    public void setMessageContext(MessageContext mc) {
+        super.setContext(mc);
     }
     
     public void setConsumeMediaTypes(List<String> types) {
@@ -99,11 +99,12 @@
         this.namespaceMap = namespaceMap;
     }
 
-    public Object readFrom(Class<Object> type, Type genericType, Annotation[] annotations,
MediaType m, 
-        MultivaluedMap<String, String> headers, InputStream is) 
+    public Object readFrom(Class<Object> type, Type genericType, Annotation[] anns,
MediaType mt, 
+        MultivaluedMap<String, String> headers, InputStream stream) 
         throws IOException {
         
         try {
+            InputStream is = getInputStream(type, anns, mt, stream);
             Class<?> theType = getActualType(type, genericType);
             Unmarshaller unmarshaller = createUnmarshaller(theType, genericType);
             
@@ -121,6 +122,8 @@
             handleJAXBException(e);
         } catch (XMLStreamException e) {
             throw new WebApplicationException(e);
+        } catch (WebApplicationException e) {
+            throw e;
         } catch (Exception e) {
             throw new WebApplicationException(e);
         }

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/AttachmentUtils.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/AttachmentUtils.java?rev=736726&r1=736725&r2=736726&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/AttachmentUtils.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/AttachmentUtils.java
Thu Jan 22 09:49:43 2009
@@ -23,19 +23,31 @@
 import java.io.InputStream;
 import java.lang.annotation.Annotation;
 import java.util.Map;
+import java.util.ResourceBundle;
+import java.util.logging.Logger;
 
 import javax.activation.DataHandler;
 import javax.activation.DataSource;
 import javax.mail.util.ByteArrayDataSource;
 import javax.ws.rs.core.MediaType;
 
+import org.apache.cxf.common.i18n.BundleUtils;
+import org.apache.cxf.common.logging.LogUtils;
+import org.apache.cxf.helpers.CastUtils;
 import org.apache.cxf.jaxrs.ext.MessageContext;
 import org.apache.cxf.jaxrs.ext.MultipartID;
 
 public final class AttachmentUtils {
+    private static final Logger LOG = LogUtils.getL7dLogger(AttachmentUtils.class);
+    private static final ResourceBundle BUNDLE = BundleUtils.getBundle(AttachmentUtils.class);
+    
     private AttachmentUtils() {
     }
     
+    public static Map<String, DataHandler> getAttachments(MessageContext mc) {
+        return CastUtils.cast((Map)mc.get(MessageContext.INBOUND_MESSAGE_ATTACHMENTS));
+    }
+    
     public static Object getMultipart(Class<Object> c, Annotation[] anns, 
          MediaType mt, MessageContext mc, InputStream is) throws IOException {
         InputStream stream = null;
@@ -51,7 +63,7 @@
             }
             if (stream == null) {
                 // TODO: looks like the lazy attachments collection can only be accessed
this way
-                for (Map.Entry<String, DataHandler> entry : mc.getAttachments().entrySet())
{
+                for (Map.Entry<String, DataHandler> entry : getAttachments(mc).entrySet())
{
                     if (entry.getKey().equals(contentId)) {
                         DataHandler dh = entry.getValue();
                         return DataHandler.class.isAssignableFrom(c) ? dh 
@@ -59,6 +71,12 @@
                             : dh.getInputStream();
                     }
                 }
+                org.apache.cxf.common.i18n.Message errorMsg = 
+                    new org.apache.cxf.common.i18n.Message("MULTTIPART_ID_NOT_FOUND", 
+                                                           BUNDLE, 
+                                                           contentId,
+                                                           mt.toString());
+                LOG.warning(errorMsg.toString());
             }
         } else {
             stream = is;

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/Messages.properties
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/Messages.properties?rev=736726&r1=736725&r2=736726&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/Messages.properties
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/Messages.properties
Thu Jan 22 09:49:43 2009
@@ -30,3 +30,4 @@
 NO_MSG_READER =.No message body reader found for request class : {0}, ContentType : {1}.
 NO_SUBRESOURCE_METHOD_FOUND=No operation matching request path {0} is found on subresource,
ContentType : {1}, Accept : {2}.
 NO_OP_EXC =.No operation matching request path {0} is found, ContentType : {1}, Accept :
{2}.
+MULTTIPART_ID_NOT_FOUND=No multipart with content id {0} found, request content type : {1}
\ No newline at end of file

Modified: cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSMultipartTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSMultipartTest.java?rev=736726&r1=736725&r2=736726&view=diff
==============================================================================
--- cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSMultipartTest.java
(original)
+++ cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSMultipartTest.java
Thu Jan 22 09:49:43 2009
@@ -37,7 +37,7 @@
     @BeforeClass
     public static void startServers() throws Exception {
         assertTrue("server did not launch correctly",
-                   launchServer(MultipartServer.class));
+                   launchServer(MultipartServer.class, true));
     }
     
     @Test

Modified: cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/MultipartStore.java
URL: http://svn.apache.org/viewvc/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/MultipartStore.java?rev=736726&r1=736725&r2=736726&view=diff
==============================================================================
--- cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/MultipartStore.java (original)
+++ cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/MultipartStore.java Thu
Jan 22 09:49:43 2009
@@ -37,6 +37,7 @@
 
 import org.apache.cxf.jaxrs.ext.MessageContext;
 import org.apache.cxf.jaxrs.ext.MultipartID;
+import org.apache.cxf.jaxrs.utils.AttachmentUtils;
 
 @Path("/bookstore")
 public class MultipartStore {
@@ -118,7 +119,7 @@
     @Path("/books/mchandlers")
     @Produces("text/xml")
     public Response addBookFromMessageContext() throws Exception {
-        Map<String, DataHandler> handlers = context.getAttachments();
+        Map<String, DataHandler> handlers = AttachmentUtils.getAttachments(context);
         for (Map.Entry<String, DataHandler> entry : handlers.entrySet()) {
             if (entry.getKey().equals("book2")) {
                 return readBookFromInputStream(entry.getValue().getInputStream());



Mime
View raw message