cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cschnei...@apache.org
Subject cxf git commit: [CXF-6436] Fix for NPE
Date Tue, 09 Jun 2015 11:14:42 GMT
Repository: cxf
Updated Branches:
  refs/heads/master dcaf6ecd5 -> d4de0ced8


[CXF-6436] Fix for NPE


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

Branch: refs/heads/master
Commit: d4de0ced8b53599229a7a3e43ad3bb5cf70b401b
Parents: dcaf6ec
Author: Christian Schneider <chris@die-schneider.net>
Authored: Tue Jun 9 13:11:14 2015 +0200
Committer: Christian Schneider <chris@die-schneider.net>
Committed: Tue Jun 9 13:14:35 2015 +0200

----------------------------------------------------------------------
 .../logging/event/DefaultLogEventMapper.java    | 78 +++++++++---------
 .../ext/logging/DefaultLogEventMapperTest.java  | 85 ++++++++++++++++++++
 2 files changed, 123 insertions(+), 40 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cxf/blob/d4de0ced/rt/features/logging/src/main/java/org/apache/cxf/ext/logging/event/DefaultLogEventMapper.java
----------------------------------------------------------------------
diff --git a/rt/features/logging/src/main/java/org/apache/cxf/ext/logging/event/DefaultLogEventMapper.java
b/rt/features/logging/src/main/java/org/apache/cxf/ext/logging/event/DefaultLogEventMapper.java
index 8aa86c5..a9fe043 100644
--- a/rt/features/logging/src/main/java/org/apache/cxf/ext/logging/event/DefaultLogEventMapper.java
+++ b/rt/features/logging/src/main/java/org/apache/cxf/ext/logging/event/DefaultLogEventMapper.java
@@ -29,7 +29,6 @@ import java.util.Set;
 import java.util.UUID;
 
 import javax.security.auth.Subject;
-import javax.xml.namespace.QName;
 import javax.xml.stream.XMLStreamReader;
 
 import org.apache.cxf.binding.Binding;
@@ -70,19 +69,10 @@ public class DefaultLogEventMapper implements LogEventMapper {
             }
         }
 
-        String encoding = (String)message.get(Message.ENCODING);
+        event.setEncoding(safeGet(message, Message.ENCODING));
+        event.setHttpMethod(safeGet(message, Message.HTTP_REQUEST_METHOD));
+        event.setContentType(safeGet(message, Message.CONTENT_TYPE));
 
-        if (encoding != null) {
-            event.setEncoding(encoding);
-        }
-        String httpMethod = (String)message.get(Message.HTTP_REQUEST_METHOD);
-        if (httpMethod != null) {
-            event.setHttpMethod(httpMethod);
-        }
-        String ct = (String)message.get(Message.CONTENT_TYPE);
-        if (ct != null) {
-            event.setContentType(ct);
-        }
         Map<String, String> headerMap = getHeaders(message);
         event.setHeaders(headerMap);
 
@@ -138,6 +128,9 @@ public class DefaultLogEventMapper implements LogEventMapper {
     private Map<String, String> getHeaders(Message message) {
         Map<String, List<String>> headers = CastUtils.cast((Map<?, ?>)message.get(Message.PROTOCOL_HEADERS));
         Map<String, String> result = new HashMap<>();
+        if (headers == null) {
+            return result;
+        }
         for (Map.Entry<String, List<String>> entry : headers.entrySet()) {
             if (entry.getValue().size() == 1) {
                 result.put(entry.getKey(), entry.getValue().get(0));
@@ -150,10 +143,10 @@ public class DefaultLogEventMapper implements LogEventMapper {
     }
 
     private String getUri(Message message) {
-        String uri = (String)message.get(Message.REQUEST_URL);
+        String uri = safeGet(message, Message.REQUEST_URL);
         if (uri == null) {
-            String address = (String)message.get(Message.ENDPOINT_ADDRESS);
-            uri = (String)message.get(Message.REQUEST_URI);
+            String address = safeGet(message, Message.ENDPOINT_ADDRESS);
+            uri = safeGet(message, Message.REQUEST_URI);
             if (uri != null && uri.startsWith("/")) {
                 if (address != null && !address.startsWith(uri)) {
                     if (address.endsWith("/") && address.length() > 1) {
@@ -165,7 +158,7 @@ public class DefaultLogEventMapper implements LogEventMapper {
                 uri = address;
             }
         }
-        String query = (String)message.get(Message.QUERY_STRING);
+        String query = safeGet(message, Message.QUERY_STRING);
         if (query != null) {
             return uri + "?" + query;
         } else {
@@ -174,7 +167,7 @@ public class DefaultLogEventMapper implements LogEventMapper {
     }
 
     private boolean isBinaryContent(Message message) {
-        String contentType = (String)message.get(Message.CONTENT_TYPE);
+        String contentType = safeGet(message, Message.CONTENT_TYPE);
         return contentType != null && BINARY_CONTENT_MEDIA_TYPES.contains(contentType);
     }
 
@@ -228,13 +221,12 @@ public class DefaultLogEventMapper implements LogEventMapper {
     }
 
     private BindingOperationInfo getOperationFromContent(Message message) {
-        BindingOperationInfo boi = null;
         XMLStreamReader xmlReader = message.getContent(XMLStreamReader.class);
-        if (null != xmlReader) {
-            QName qName = xmlReader.getName();
-            boi = ServiceModelUtil.getOperation(message.getExchange(), qName);
+        if (xmlReader != null) {
+            return ServiceModelUtil.getOperation(message.getExchange(), xmlReader.getName());
+        } else {
+            return null;
         }
-        return boi;
     }
 
     private Message getEffectiveMessage(Message message) {
@@ -249,23 +241,30 @@ public class DefaultLogEventMapper implements LogEventMapper {
 
     private String getRestOperationName(Message curMessage) {
         Message message = getEffectiveMessage(curMessage);
-        if (message.containsKey(Message.HTTP_REQUEST_METHOD)) {
-            String httpMethod = message.get(Message.HTTP_REQUEST_METHOD).toString();
-
-            String path = "";
-            if (message.containsKey(Message.REQUEST_URI)) {
-                String requestUri = message.get(Message.REQUEST_URI).toString();
-                int baseUriLength = (message.containsKey(Message.BASE_PATH)) ? message.get(Message.BASE_PATH)
-                    .toString().length() : 0;
-                path = requestUri.substring(baseUriLength);
-                if (path.isEmpty()) {
-                    path = "/";
-                }
-            }
+        String httpMethod = safeGet(message, Message.HTTP_REQUEST_METHOD);
+        if (httpMethod == null) {
+            return "";
+        }
 
-            return new StringBuffer().append(httpMethod).append('[').append(path).append(']').toString();
+        String path = "";
+        String requestUri = safeGet(message, Message.REQUEST_URI);
+        if (requestUri != null) {
+            String basePath = safeGet(message, Message.BASE_PATH);
+            int baseUriLength = (basePath != null) ? basePath.length() : 0;
+            path = requestUri.substring(baseUriLength);
+            if (path.isEmpty()) {
+                path = "/";
+            }
+        }
+        return new StringBuffer().append(httpMethod).append('[').append(path).append(']').toString();
+    }
+    
+    private String safeGet(Message message, String key) {
+        if (!message.containsKey(key)) {
+            return null;
         }
-        return "";
+        Object value = message.get(key);
+        return (value instanceof String) ? value.toString() : null;
     }
 
     /**
@@ -317,7 +316,6 @@ public class DefaultLogEventMapper implements LogEventMapper {
         EndpointInfo endpoint = getEPInfo(message);
         event.setPortName(endpoint.getName());
         event.setPortTypeName(endpoint.getName());
-        event.setOperationName(getRestOperationName(message));
         String opName = isSOAPMessage(message) ? getOperationName(message) : getRestOperationName(message);
         event.setOperationName(opName);
         if (endpoint.getService() != null) {
@@ -333,7 +331,7 @@ public class DefaultLogEventMapper implements LogEventMapper {
 
     private EndpointInfo getEPInfo(Message message) {
         Endpoint ep = message.getExchange().getEndpoint();
-        return (ep == null) ? null : ep.getEndpointInfo();
+        return (ep == null) ? new EndpointInfo() : ep.getEndpointInfo();
     }
 
 }

http://git-wip-us.apache.org/repos/asf/cxf/blob/d4de0ced/rt/features/logging/src/test/java/org/apache/cxf/ext/logging/DefaultLogEventMapperTest.java
----------------------------------------------------------------------
diff --git a/rt/features/logging/src/test/java/org/apache/cxf/ext/logging/DefaultLogEventMapperTest.java
b/rt/features/logging/src/test/java/org/apache/cxf/ext/logging/DefaultLogEventMapperTest.java
new file mode 100644
index 0000000..0d64e64
--- /dev/null
+++ b/rt/features/logging/src/test/java/org/apache/cxf/ext/logging/DefaultLogEventMapperTest.java
@@ -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.ext.logging;
+
+import javax.xml.namespace.QName;
+
+import org.apache.cxf.binding.Binding;
+import org.apache.cxf.binding.soap.SoapBinding;
+import org.apache.cxf.ext.logging.event.DefaultLogEventMapper;
+import org.apache.cxf.ext.logging.event.LogEvent;
+import org.apache.cxf.message.Exchange;
+import org.apache.cxf.message.ExchangeImpl;
+import org.apache.cxf.message.Message;
+import org.apache.cxf.message.MessageImpl;
+import org.apache.cxf.service.model.BindingInfo;
+import org.apache.cxf.service.model.BindingOperationInfo;
+import org.apache.cxf.service.model.OperationInfo;
+import org.apache.cxf.service.model.ServiceInfo;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class DefaultLogEventMapperTest {
+
+    @Test
+    public void testRest() {
+        DefaultLogEventMapper mapper = new DefaultLogEventMapper();
+        Message message = new MessageImpl();
+        message.put(Message.HTTP_REQUEST_METHOD, "GET");
+        message.put(Message.REQUEST_URI, "test");
+        Exchange exchange = new ExchangeImpl();
+        message.setExchange(exchange);
+        LogEvent event = mapper.map(message);
+        Assert.assertEquals("GET[test]", event.getOperationName());
+    }
+    
+    /**
+     * Test for NPE described in CXF-6436
+     */
+    @Test
+    public void testNullValues() {
+        DefaultLogEventMapper mapper = new DefaultLogEventMapper();
+        Message message = new MessageImpl();
+        message.put(Message.HTTP_REQUEST_METHOD, null);
+        message.put(Message.REQUEST_URI, null);
+        Exchange exchange = new ExchangeImpl();
+        message.setExchange(exchange);
+        LogEvent event = mapper.map(message);
+        Assert.assertEquals("", event.getOperationName());
+    }
+    
+    @Test
+    public void testSoap() {
+        DefaultLogEventMapper mapper = new DefaultLogEventMapper();
+        Message message = new MessageImpl();
+        ExchangeImpl exchange = new ExchangeImpl();
+        ServiceInfo service = new ServiceInfo();
+        BindingInfo info = new BindingInfo(service, "bindingId");
+        SoapBinding value = new SoapBinding(info);
+        exchange.put(Binding.class, value);
+        OperationInfo opInfo = new OperationInfo();
+        opInfo.setName(new QName("http://my", "Operation"));
+        BindingOperationInfo boi = new BindingOperationInfo(info, opInfo);
+        exchange.put(BindingOperationInfo.class, boi);
+        message.setExchange(exchange);
+        LogEvent event = mapper.map(message);
+        Assert.assertEquals("{http://my}Operation", event.getOperationName());
+    }
+
+}


Mime
View raw message