cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject git commit: [CXF-5846] Updating binary and source providers to throw InternalServerError in case of ClassCastException, patch from Iris Ding applied
Date Tue, 05 Aug 2014 10:45:36 GMT
Repository: cxf
Updated Branches:
  refs/heads/2.7.x-fixes 7ef383656 -> 6726f0bd8


[CXF-5846] Updating binary and source providers to throw InternalServerError in case of ClassCastException,
patch from Iris Ding applied


Project: http://git-wip-us.apache.org/repos/asf/cxf/repo
Commit: http://git-wip-us.apache.org/repos/asf/cxf/commit/6726f0bd
Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/6726f0bd
Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/6726f0bd

Branch: refs/heads/2.7.x-fixes
Commit: 6726f0bd8be26492fff1cfae77f262df3f4ecbfb
Parents: 7ef3836
Author: Sergey Beryozkin <sberyozkin@talend.com>
Authored: Tue Aug 5 13:45:12 2014 +0300
Committer: Sergey Beryozkin <sberyozkin@talend.com>
Committed: Tue Aug 5 13:45:12 2014 +0300

----------------------------------------------------------------------
 .../cxf/jaxrs/provider/BinaryDataProvider.java  | 37 ++++++----
 .../cxf/jaxrs/provider/SourceProvider.java      | 72 +++++++++++---------
 2 files changed, 63 insertions(+), 46 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cxf/blob/6726f0bd/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/BinaryDataProvider.java
----------------------------------------------------------------------
diff --git a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/BinaryDataProvider.java
b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/BinaryDataProvider.java
index a670eb8..9698d2a 100644
--- a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/BinaryDataProvider.java
+++ b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/BinaryDataProvider.java
@@ -32,6 +32,7 @@ import java.io.Reader;
 import java.io.Writer;
 import java.lang.annotation.Annotation;
 import java.lang.reflect.Type;
+import java.util.logging.Logger;
 
 import javax.ws.rs.core.HttpHeaders;
 import javax.ws.rs.core.MediaType;
@@ -40,8 +41,10 @@ import javax.ws.rs.core.StreamingOutput;
 import javax.ws.rs.ext.MessageBodyReader;
 import javax.ws.rs.ext.MessageBodyWriter;
 
+import org.apache.cxf.common.logging.LogUtils;
 import org.apache.cxf.helpers.IOUtils;
 import org.apache.cxf.jaxrs.impl.HttpHeadersImpl;
+import org.apache.cxf.jaxrs.utils.ExceptionUtils;
 import org.apache.cxf.message.Message;
 import org.apache.cxf.message.MessageUtils;
 import org.apache.cxf.phase.PhaseInterceptorChain;
@@ -50,7 +53,7 @@ public class BinaryDataProvider<T> extends AbstractConfigurableProvider
     implements MessageBodyReader<T>, MessageBodyWriter<T> {
     
     private static final String HTTP_RANGE_PROPERTY = "http.range.support";
-    
+    private static final Logger LOG = LogUtils.getL7dLogger(BinaryDataProvider.class);
     private static final int BUFFER_SIZE = 4096;
     private boolean reportByteArraySize;
     private boolean closeResponseInputStream = true;
@@ -63,20 +66,26 @@ public class BinaryDataProvider<T> extends AbstractConfigurableProvider
     public T readFrom(Class<T> clazz, Type genericType, Annotation[] annotations, MediaType
type, 
                            MultivaluedMap<String, String> headers, InputStream is)
         throws IOException {
-        if (InputStream.class.isAssignableFrom(clazz)) {
-            return clazz.cast(is);
-        }
-        if (Reader.class.isAssignableFrom(clazz)) {
-            return clazz.cast(new InputStreamReader(is, getEncoding(type)));
-        }
-        if (byte[].class.isAssignableFrom(clazz)) {
-            String enc = getCharset(type);
-            if (enc == null) {
-                return clazz.cast(IOUtils.readBytesFromStream(is));
-            } else {
-                return clazz.cast(IOUtils.toString(is, enc).getBytes(enc));
+        try {
+            if (InputStream.class.isAssignableFrom(clazz)) {
+                return clazz.cast(is);
             }
-        }
+            if (Reader.class.isAssignableFrom(clazz)) {
+                return clazz.cast(new InputStreamReader(is, getEncoding(type)));
+            }
+            if (byte[].class.isAssignableFrom(clazz)) {
+                String enc = getCharset(type);
+                if (enc == null) {
+                    return clazz.cast(IOUtils.readBytesFromStream(is));
+                } else {
+                    return clazz.cast(IOUtils.toString(is, enc).getBytes(enc));
+                }
+            }
+        } catch (ClassCastException e) {
+            String msg = "Unsupported class: " + clazz.getName();
+            LOG.warning(msg);
+            throw ExceptionUtils.toInternalServerErrorException(null, null);
+        }    
         throw new IOException("Unrecognized class");
     }
 

http://git-wip-us.apache.org/repos/asf/cxf/blob/6726f0bd/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/SourceProvider.java
----------------------------------------------------------------------
diff --git a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/SourceProvider.java
b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/SourceProvider.java
index ab3a6e2..82dcbc6 100644
--- a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/SourceProvider.java
+++ b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/SourceProvider.java
@@ -24,6 +24,7 @@ import java.io.InputStream;
 import java.io.OutputStream;
 import java.lang.annotation.Annotation;
 import java.lang.reflect.Type;
+import java.util.logging.Logger;
 
 import javax.ws.rs.Consumes;
 import javax.ws.rs.Produces;
@@ -43,6 +44,7 @@ import javax.xml.transform.stream.StreamSource;
 
 import org.w3c.dom.Document;
 
+import org.apache.cxf.common.logging.LogUtils;
 import org.apache.cxf.io.CachedOutputStream;
 import org.apache.cxf.jaxrs.ext.MessageContext;
 import org.apache.cxf.jaxrs.ext.xml.XMLSource;
@@ -62,6 +64,7 @@ public class SourceProvider<T> extends AbstractConfigurableProvider
implements
     MessageBodyReader<T>, MessageBodyWriter<T> {
 
     private static final String PREFERRED_FORMAT = "source-preferred-format";
+    private static final Logger LOG = LogUtils.getL7dLogger(BinaryDataProvider.class);
     @Context
     private MessageContext context;
     
@@ -88,42 +91,47 @@ public class SourceProvider<T> extends AbstractConfigurableProvider
implements
                 theSource = SAXSource.class;
             }
         }
-        
-        if (DOMSource.class.isAssignableFrom(theSource) || Document.class.isAssignableFrom(theSource))
{
-            
-            boolean docRequired = Document.class.isAssignableFrom(theSource);
-            XMLStreamReader reader = getReader(is);
-            try {
-                Document doc = StaxUtils.read(reader);
-                return source.cast(docRequired ? doc : new DOMSource(doc));
-            } catch (DepthExceededStaxException e) {
-                throw ExceptionUtils.toWebApplicationException(null, JAXRSUtils.toResponse(413));
-            } catch (XMLStreamException e) {
-                if (e.getMessage() != null && e.getMessage().startsWith("Maximum
Number")) {
-                    throw ExceptionUtils.toWebApplicationException(null, JAXRSUtils.toResponse(413));
-                } else {
-                    throw ExceptionUtils.toBadRequestException(e, null);
-                }
-            } catch (Exception e) {
-                IOException ioex = new IOException("Problem creating a Source object");
-                ioex.setStackTrace(e.getStackTrace());
-                throw ioex;
-            } finally {
+        try {
+            if (DOMSource.class.isAssignableFrom(theSource) || Document.class.isAssignableFrom(theSource))
{
+                
+                boolean docRequired = Document.class.isAssignableFrom(theSource);
+                XMLStreamReader reader = getReader(is);
                 try {
-                    reader.close();
+                    Document doc = StaxUtils.read(reader);
+                    return source.cast(docRequired ? doc : new DOMSource(doc));
+                } catch (DepthExceededStaxException e) {
+                    throw ExceptionUtils.toWebApplicationException(null, JAXRSUtils.toResponse(413));
                 } catch (XMLStreamException e) {
-                    //ignore
+                    if (e.getMessage() != null && e.getMessage().startsWith("Maximum
Number")) {
+                        throw ExceptionUtils.toWebApplicationException(null, JAXRSUtils.toResponse(413));
+                    } else {
+                        throw ExceptionUtils.toBadRequestException(e, null);
+                    }
+                } catch (Exception e) {
+                    IOException ioex = new IOException("Problem creating a Source object");
+                    ioex.setStackTrace(e.getStackTrace());
+                    throw ioex;
+                } finally {
+                    try {
+                        reader.close();
+                    } catch (XMLStreamException e) {
+                        //ignore
+                    }
                 }
+            } else if (SAXSource.class.isAssignableFrom(theSource)
+                      || StaxSource.class.isAssignableFrom(theSource)) {
+                return source.cast(new StaxSource(getReader(is)));
+            } else if (StreamSource.class.isAssignableFrom(theSource)
+                       || Source.class.isAssignableFrom(theSource)) {
+                return source.cast(new StreamSource(getRealStream(is)));
+            } else if (XMLSource.class.isAssignableFrom(theSource)) {
+                return source.cast(new XMLSource(getRealStream(is)));
             }
-        } else if (SAXSource.class.isAssignableFrom(theSource)
-                  || StaxSource.class.isAssignableFrom(theSource)) {
-            return source.cast(new StaxSource(getReader(is)));
-        } else if (StreamSource.class.isAssignableFrom(theSource)
-                   || Source.class.isAssignableFrom(theSource)) {
-            return source.cast(new StreamSource(getRealStream(is)));
-        } else if (XMLSource.class.isAssignableFrom(theSource)) {
-            return source.cast(new XMLSource(getRealStream(is)));
-        }
+        } catch (ClassCastException e) {
+            String msg = "Unsupported class: " + source.getName();
+            LOG.warning(msg);
+            throw ExceptionUtils.toInternalServerErrorException(null, null);
+        }    
         
         throw new IOException("Unrecognized source");
     }


Mime
View raw message