You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jmeter.apache.org by pm...@apache.org on 2016/12/29 22:02:08 UTC

svn commit: r1776498 - /jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/sampler/HTTPSamplerBase.java

Author: pmouawad
Date: Thu Dec 29 22:02:07 2016
New Revision: 1776498

URL: http://svn.apache.org/viewvc?rev=1776498&view=rev
Log:
sonar: fix errors 

Modified:
    jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/sampler/HTTPSamplerBase.java

Modified: jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/sampler/HTTPSamplerBase.java
URL: http://svn.apache.org/viewvc/jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/sampler/HTTPSamplerBase.java?rev=1776498&r1=1776497&r2=1776498&view=diff
==============================================================================
--- jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/sampler/HTTPSamplerBase.java (original)
+++ jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/sampler/HTTPSamplerBase.java Thu Dec 29 22:02:07 2016
@@ -264,7 +264,6 @@ public abstract class HTTPSamplerBase ex
     /** A number to indicate that the port has not been set. */
     public static final int UNSPECIFIED_PORT = 0;
     public static final String UNSPECIFIED_PORT_AS_STRING = "0"; // $NON-NLS-1$
-    // TODO - change to use URL version? Will this affect test plans?
 
     /** If the port is not present in a URL, getPort() returns -1 */
     public static final int URL_UNSPECIFIED_PORT = -1;
@@ -274,7 +273,7 @@ public abstract class HTTPSamplerBase ex
 
     protected static final String NON_HTTP_RESPONSE_MESSAGE = "Non HTTP response message";
 
-    public static final String POST_BODY_RAW = "HTTPSampler.postBodyRaw"; // TODO - belongs elsewhere
+    public static final String POST_BODY_RAW = "HTTPSampler.postBodyRaw";
 
     public static final boolean POST_BODY_RAW_DEFAULT = false;
 
@@ -615,7 +614,7 @@ public abstract class HTTPSamplerBase ex
         if (nonEmptyEncoding) {
             try {
                 valueEncoded = arg.getEncodedValue(contentEncoding);
-            } catch (UnsupportedEncodingException e) {
+            } catch (UnsupportedEncodingException e) { // NOSONAR 
                 log.warn("Unable to get encoded value using encoding " + contentEncoding);
                 valueEncoded = arg.getEncodedValue();
             }
@@ -834,18 +833,19 @@ public abstract class HTTPSamplerBase ex
         return (AuthManager) getProperty(AUTH_MANAGER).getObjectValue();
     }
 
-    public void setHeaderManager(HeaderManager value) {
+    public void setHeaderManager(final HeaderManager value) {
         HeaderManager mgr = getHeaderManager();
+        HeaderManager lValue = value;
         if (mgr != null) {
-            value = mgr.merge(value, true);
+            lValue = mgr.merge(value, true);
             if (log.isDebugEnabled()) {
-                log.debug("Existing HeaderManager '" + mgr.getName() + "' merged with '" + value.getName() + "'");
-                for (int i = 0; i < value.getHeaders().size(); i++) {
-                    log.debug("    " + value.getHeader(i).getName() + "=" + value.getHeader(i).getValue());
+                log.debug("Existing HeaderManager '" + mgr.getName() + "' merged with '" + lValue.getName() + "'");
+                for (int i = 0; i < lValue.getHeaders().size(); i++) {
+                    log.debug("    " + lValue.getHeader(i).getName() + "=" + lValue.getHeader(i).getValue());
                 }
             }
         }
-        setProperty(new TestElementProperty(HEADER_MANAGER, value));
+        setProperty(new TestElementProperty(HEADER_MANAGER, lValue));
     }
 
     public HeaderManager getHeaderManager() {
@@ -1013,18 +1013,18 @@ public abstract class HTTPSamplerBase ex
      * @param contentEncoding the encoding to use for encoding parameter values
      * @return the QueryString value
      */
-    public String getQueryString(String contentEncoding) {
+    public String getQueryString(final String contentEncoding) {
         
         CollectionProperty arguments = getArguments().getArguments();
         // Optimisation : avoid building useless objects if empty arguments
         if(arguments.size() == 0) {
             return "";
         }
-        
+        String lContentEncoding = contentEncoding;
         // Check if the sampler has a specified content encoding
-        if (JOrphanUtils.isBlank(contentEncoding)) {
+        if (JOrphanUtils.isBlank(lContentEncoding)) {
             // We use the encoding which should be used according to the HTTP spec, which is UTF-8
-            contentEncoding = EncoderCache.URL_ARGUMENT_ENCODING;
+            lContentEncoding = EncoderCache.URL_ARGUMENT_ENCODING;
         }
         
         StringBuilder buf = new StringBuilder(arguments.size() * 15);
@@ -1041,8 +1041,8 @@ public abstract class HTTPSamplerBase ex
             Object objectValue = iter.next().getObjectValue();
             try {
                 item = (HTTPArgument) objectValue;
-            } catch (ClassCastException e) {
-                log.warn("Unexpected argument type: " + objectValue.getClass().getName());
+            } catch (ClassCastException e) { // NOSONAR
+                log.warn("Unexpected argument type: " + objectValue.getClass().getName() +" cannot be cast to HTTPArgument");
                 item = new HTTPArgument((Argument) objectValue);
             }
             final String encodedName = item.getEncodedName();
@@ -1063,9 +1063,9 @@ public abstract class HTTPSamplerBase ex
 
             // Encode the parameter value in the specified content encoding
             try {
-                buf.append(item.getEncodedValue(contentEncoding));
-            } catch(UnsupportedEncodingException e) {
-                log.warn("Unable to encode parameter in encoding " + contentEncoding + ", parameter value not included in query string");
+                buf.append(item.getEncodedValue(lContentEncoding));
+            } catch(UnsupportedEncodingException e) { // NOSONAR
+                log.warn("Unable to encode parameter in encoding " + lContentEncoding + ", parameter value not included in query string");
             }
         }
         return buf.toString();
@@ -1207,7 +1207,7 @@ public abstract class HTTPSamplerBase ex
     /**
      * Download the resources of an HTML page.
      *
-     * @param res
+     * @param pRes
      *            result of the initial request - must contain an HTML response
      * @param container
      *            for storing the results, if any
@@ -1216,7 +1216,8 @@ public abstract class HTTPSamplerBase ex
      *            prevent infinite recursion.
      * @return res if no resources exist, otherwise the "Container" result with one subsample per request issued
      */
-    protected HTTPSampleResult downloadPageResources(HTTPSampleResult res, HTTPSampleResult container, int frameDepth) {
+    protected HTTPSampleResult downloadPageResources(final HTTPSampleResult pRes, final HTTPSampleResult container, final int frameDepth) {
+        HTTPSampleResult res = pRes;
         Iterator<URL> urls = null;
         try {
             final byte[] responseData = res.getResponseData();
@@ -1232,14 +1233,14 @@ public abstract class HTTPSamplerBase ex
             res.addSubResult(errorResult(e, new HTTPSampleResult(res)));
             setParentSampleSuccess(res, false);
         }
-
+        HTTPSampleResult lContainer = container;
         // Iterate through the URLs and download each image:
         if (urls != null && urls.hasNext()) {
-            if (container == null) {
-                container = new HTTPSampleResult(res);
-                container.addRawSubResult(res);
+            if (lContainer == null) {
+                lContainer = new HTTPSampleResult(res);
+                lContainer.addRawSubResult(res);
             }
-            res = container;
+            res = lContainer;
 
             // Get the URL matcher
             String re = getEmbeddedUrlRE();
@@ -1249,7 +1250,7 @@ public abstract class HTTPSamplerBase ex
                 try {
                     pattern = JMeterUtils.getPattern(re);
                     localMatcher = JMeterUtils.getMatcher();// don't fetch unless pattern compiles
-                } catch (MalformedCachePatternException e) {
+                } catch (MalformedCachePatternException e) { // NOSONAR
                     log.warn("Ignoring embedded URL match string: " + e.getMessage());
                 }
             }
@@ -1284,8 +1285,8 @@ public abstract class HTTPSamplerBase ex
                     } else {
                         try {
                             url = escapeIllegalURLCharacters(url);
-                        } catch (Exception e) {
-                            res.addSubResult(errorResult(new Exception(url.toString() + " is not a correct URI"), new HTTPSampleResult(res)));
+                        } catch (Exception e) { // NOSONAR
+                            res.addSubResult(errorResult(new Exception(url.toString() + " is not a correct URI", e), new HTTPSampleResult(res)));
                             setParentSampleSuccess(res, false);
                             continue;
                         }
@@ -1311,8 +1312,8 @@ public abstract class HTTPSamplerBase ex
                             setParentSampleSuccess(res, res.isSuccessful() && (binRes == null || binRes.isSuccessful()));
                         }
                     }
-                } catch (ClassCastException e) { // TODO can this happen?
-                    res.addSubResult(errorResult(new Exception(binURL + " is not a correct URI"), new HTTPSampleResult(res)));
+                } catch (ClassCastException e) { // NOSONAR
+                    res.addSubResult(errorResult(new Exception(binURL + " is not a correct URI", e), new HTTPSampleResult(res)));
                     setParentSampleSuccess(res, false);
                 }
             }
@@ -1344,6 +1345,7 @@ public abstract class HTTPSamplerBase ex
                     }
                 } catch (InterruptedException ie) {
                     log.warn("Interrupted fetching embedded resources", ie); // $NON-NLS-1$
+                    Thread.currentThread().interrupt();
                 } catch (ExecutionException ee) {
                     log.warn("Execution issue when fetching embedded resources", ee); // $NON-NLS-1$
                 }
@@ -1374,12 +1376,12 @@ public abstract class HTTPSamplerBase ex
      * @return escaped url
      */
     private URL escapeIllegalURLCharacters(java.net.URL url) {
-        if (url == null || url.getProtocol().equals("file")) {
+        if (url == null || "file".equals(url.getProtocol())) {
             return url;
         }
         try {
             return ConversionUtils.sanitizeUrl(url).toURL();
-        } catch (Exception e1) {
+        } catch (Exception e1) { // NOSONAR
             log.error("Error escaping URL:'" + url + "', message:" + e1.getMessage());
             return url;
         }
@@ -1439,7 +1441,6 @@ public abstract class HTTPSamplerBase ex
         }
     }
 
-    // TODO: make static?
     protected String encodeSpaces(String path) {
         return JOrphanUtils.replaceAllChars(path, ' ', "%20"); // $NON-NLS-1$
     }
@@ -1479,8 +1480,7 @@ public abstract class HTTPSamplerBase ex
      */
     @Override
     public Object clone() {
-        HTTPSamplerBase base = (HTTPSamplerBase) super.clone();
-        return base;
+        return (HTTPSamplerBase) super.clone();
     }
 
     /**
@@ -1522,7 +1522,7 @@ public abstract class HTTPSamplerBase ex
             }
             // Change all but HEAD into GET (Bug 55450)
             String method = lastRes.getHTTPMethod();
-            method = computeMethodForRedirect(method, res.getResponseCode());
+            method = computeMethodForRedirect(method);
 
             try {
                 URL url = ConversionUtils.makeRelativeURL(lastRes.getURL(), location);
@@ -1590,12 +1590,11 @@ public abstract class HTTPSamplerBase ex
     /**
      * See <a href="http://tools.ietf.org/html/rfc2616#section-10.3">RFC2616#section-10.3</a>
      * JMeter conforms currently to HttpClient 4.5.2 supported RFC
-     * TODO Update when migrating to HttpClient 5.X
+     * TODO Update when migrating to HttpClient 5.X using response code
      * @param initialMethod the initial HTTP Method
-     * @param responseCode String response code
      * @return the new HTTP Method as per RFC
      */
-    private String computeMethodForRedirect(String initialMethod, String responseCode) {
+    private String computeMethodForRedirect(String initialMethod) {
         if (!HTTPConstants.HEAD.equalsIgnoreCase(initialMethod)) {
             return HTTPConstants.GET;
         }
@@ -1610,13 +1609,15 @@ public abstract class HTTPSamplerBase ex
      * an HTTPSamplerResult container parameter instead of a
      * boolean:areFollowingRedirect.
      *
-     * @param areFollowingRedirect flag whether we are getting a redirect target
+     * @param pAreFollowingRedirect flag whether we are getting a redirect target
      * @param frameDepth Depth of this target in the frame structure. Used only to prevent infinite recursion.
-     * @param res sample result to process
+     * @param pRes sample result to process
      * @return the sample result
      */
-    protected HTTPSampleResult resultProcessing(boolean areFollowingRedirect, int frameDepth, HTTPSampleResult res) {
+    protected HTTPSampleResult resultProcessing(final boolean pAreFollowingRedirect, final int frameDepth, final HTTPSampleResult pRes) {
         boolean wasRedirected = false;
+        boolean areFollowingRedirect = pAreFollowingRedirect;
+        HTTPSampleResult res = pRes;
         if (!areFollowingRedirect && res.isRedirect()) {
             if(log.isDebugEnabled()) {
                 log.debug("Location set to - " + res.getRedirectLocation());