View | Details | Raw Unified | Return to bug 51919
Collapse All | Expand All

(-)src/protocol/http/org/apache/jmeter/protocol/http/sampler/HTTPSamplerBase.java (-17 / +82 lines)
Lines 45-50 Link Here
45
import org.apache.jmeter.engine.event.LoopIterationEvent;
45
import org.apache.jmeter.engine.event.LoopIterationEvent;
46
import org.apache.jmeter.protocol.http.control.AuthManager;
46
import org.apache.jmeter.protocol.http.control.AuthManager;
47
import org.apache.jmeter.protocol.http.control.CacheManager;
47
import org.apache.jmeter.protocol.http.control.CacheManager;
48
import org.apache.jmeter.protocol.http.control.Cookie;
48
import org.apache.jmeter.protocol.http.control.CookieManager;
49
import org.apache.jmeter.protocol.http.control.CookieManager;
49
import org.apache.jmeter.protocol.http.control.HeaderManager;
50
import org.apache.jmeter.protocol.http.control.HeaderManager;
50
import org.apache.jmeter.protocol.http.parser.HTMLParseException;
51
import org.apache.jmeter.protocol.http.parser.HTMLParseException;
Lines 62-67 Link Here
62
import org.apache.jmeter.testelement.TestListener;
63
import org.apache.jmeter.testelement.TestListener;
63
import org.apache.jmeter.testelement.ThreadListener;
64
import org.apache.jmeter.testelement.ThreadListener;
64
import org.apache.jmeter.testelement.property.BooleanProperty;
65
import org.apache.jmeter.testelement.property.BooleanProperty;
66
import org.apache.jmeter.testelement.property.CollectionProperty;
65
import org.apache.jmeter.testelement.property.IntegerProperty;
67
import org.apache.jmeter.testelement.property.IntegerProperty;
66
import org.apache.jmeter.testelement.property.JMeterProperty;
68
import org.apache.jmeter.testelement.property.JMeterProperty;
67
import org.apache.jmeter.testelement.property.PropertyIterator;
69
import org.apache.jmeter.testelement.property.PropertyIterator;
Lines 275-282 Link Here
275
277
276
    private boolean dynamicPath = false;// Set false if spaces are already encoded
278
    private boolean dynamicPath = false;// Set false if spaces are already encoded
277
279
278
280
    /**
279
281
     * Thread Local holder for CookieManager when downloading embedded resources in Concurrent Download Mode
282
     */
283
    static ThreadLocal<CookieManager> cookieManagerTL = new ThreadLocal<CookieManager>(); 
280
    ////////////////////// Code ///////////////////////////
284
    ////////////////////// Code ///////////////////////////
281
285
282
    public HTTPSamplerBase() {
286
    public HTTPSamplerBase() {
Lines 739-745 Link Here
739
    }
743
    }
740
744
741
    public CookieManager getCookieManager() {
745
    public CookieManager getCookieManager() {
742
        return (CookieManager) getProperty(COOKIE_MANAGER).getObjectValue();
746
        if(cookieManagerTL != null && cookieManagerTL.get() != null) {
747
            if(log.isDebugEnabled()) {
748
                log.debug("Thread "+ Thread.currentThread().getName()+" is using TL CookieManager:"+cookieManagerTL.get());
749
            }
750
            return cookieManagerTL.get();
751
        } else {
752
            if(log.isDebugEnabled()) {
753
                log.debug("Thread "+ Thread.currentThread().getName()+" is using Non TL CookieManager");
754
            }
755
            return (CookieManager) getProperty(COOKIE_MANAGER).getObjectValue();
756
        }
743
    }
757
    }
744
758
745
    public void setCacheManager(CacheManager value) {
759
    public void setCacheManager(CacheManager value) {
Lines 1021-1027 Link Here
1021
    public SampleResult sample() {
1035
    public SampleResult sample() {
1022
        SampleResult res = null;
1036
        SampleResult res = null;
1023
        try {
1037
        try {
1024
            res = sample(getUrl(), getMethod(), false, 0);
1038
                res = sample(getUrl(), getMethod(), false, 0);
1025
            res.setSampleLabel(getName());
1039
            res.setSampleLabel(getName());
1026
            return res;
1040
            return res;
1027
        } catch (Exception e) {
1041
        } catch (Exception e) {
Lines 1110-1116 Link Here
1110
            }
1124
            }
1111
            
1125
            
1112
            // For concurrent get resources
1126
            // For concurrent get resources
1113
            final List<Callable<HTTPSampleResult>> liste = new ArrayList<Callable<HTTPSampleResult>>();
1127
            final List<Callable<AsynSamplerResultHolder>> liste = new ArrayList<Callable<AsynSamplerResultHolder>>();
1114
            
1128
            
1115
            while (urls.hasNext()) {
1129
            while (urls.hasNext()) {
1116
                Object binURL = urls.next(); // See catch clause below
1130
                Object binURL = urls.next(); // See catch clause below
Lines 1137-1143 Link Here
1137
                        
1151
                        
1138
                        if (isConcurrentDwn()) {
1152
                        if (isConcurrentDwn()) {
1139
                            // if concurrent download emb. resources, add to a list for async gets later
1153
                            // if concurrent download emb. resources, add to a list for async gets later
1140
                            liste.add(new ASyncSample(url, GET, false, frameDepth + 1));
1154
                            liste.add(new ASyncSample(url, GET, false, frameDepth + 1, getCookieManager()));
1141
                        } else {
1155
                        } else {
1142
                            // default: serial download embedded resources
1156
                            // default: serial download embedded resources
1143
                            HTTPSampleResult binRes = sample(url, GET, false, frameDepth + 1);
1157
                            HTTPSampleResult binRes = sample(url, GET, false, frameDepth + 1);
Lines 1171-1189 Link Here
1171
                boolean tasksCompleted = false;
1185
                boolean tasksCompleted = false;
1172
                try {
1186
                try {
1173
                    // sample all resources with threadpool
1187
                    // sample all resources with threadpool
1174
                    final List<Future<HTTPSampleResult>> retExec = exec.invokeAll(liste);
1188
                    final List<Future<AsynSamplerResultHolder>> retExec = exec.invokeAll(liste);
1175
                    // call normal shutdown (wait ending all tasks)
1189
                    // call normal shutdown (wait ending all tasks)
1176
                    exec.shutdown();
1190
                    exec.shutdown();
1177
                    // put a timeout if tasks couldn't terminate
1191
                    // put a timeout if tasks couldn't terminate
1178
                    exec.awaitTermination(AWAIT_TERMINATION_TIMEOUT, TimeUnit.SECONDS);
1192
                    exec.awaitTermination(AWAIT_TERMINATION_TIMEOUT, TimeUnit.SECONDS);
1193
                    CookieManager cookieManager = getCookieManager();
1179
1194
1180
                    // add result to main sampleResult
1195
                    // add result to main sampleResult
1181
                    for (Future<HTTPSampleResult> future : retExec) {
1196
                    for (Future<AsynSamplerResultHolder> future : retExec) {
1182
                        HTTPSampleResult binRes;
1197
                        AsynSamplerResultHolder binRes;
1183
                        try {
1198
                        try {
1184
                            binRes = future.get(1, TimeUnit.MILLISECONDS);
1199
                            binRes = future.get(1, TimeUnit.MILLISECONDS);
1185
                            res.addSubResult(binRes);
1200
                            if(cookieManager != null) {
1186
                            res.setSuccessful(res.isSuccessful() && binRes.isSuccessful());
1201
                                CollectionProperty cookies = binRes.getCookies();
1202
                                PropertyIterator iter = cookies.iterator();
1203
                                while (iter.hasNext()) {
1204
                                    Cookie cookie = (Cookie) iter.next().getObjectValue();
1205
                                    cookieManager.add(cookie) ;
1206
                                }
1207
                            }
1208
                            res.addSubResult(binRes.getResult());
1209
                            res.setSuccessful(res.isSuccessful() && binRes.getResult().isSuccessful());
1187
                        } catch (TimeoutException e) {
1210
                        } catch (TimeoutException e) {
1188
                            errorResult(e, res);
1211
                            errorResult(e, res);
1189
                        }
1212
                        }
Lines 1674-1696 Link Here
1674
     * Callable class to sample asynchronously resources embedded
1697
     * Callable class to sample asynchronously resources embedded
1675
     *
1698
     *
1676
     */
1699
     */
1677
    public class ASyncSample implements Callable<HTTPSampleResult> {
1700
    public class ASyncSample implements Callable<AsynSamplerResultHolder> {
1678
        final private URL url;
1701
        final private URL url;
1679
        final private String method;
1702
        final private String method;
1680
        final private boolean areFollowingRedirect;
1703
        final private boolean areFollowingRedirect;
1681
        final private int depth;
1704
        final private int depth;
1705
        private CookieManager cookieManager;
1682
1706
1683
        public ASyncSample(URL url, String method,
1707
        public ASyncSample(URL url, String method,
1684
                boolean areFollowingRedirect, int depth){
1708
                boolean areFollowingRedirect, int depth, CookieManager cookieManager){
1685
            this.url = url;
1709
            this.url = url;
1686
            this.method = method;
1710
            this.method = method;
1687
            this.areFollowingRedirect = areFollowingRedirect;
1711
            this.areFollowingRedirect = areFollowingRedirect;
1688
            this.depth = depth;
1712
            this.depth = depth;
1713
            if(cookieManager != null) {
1714
                this.cookieManager = (CookieManager) cookieManager.clone();
1715
            } 
1689
        }
1716
        }
1690
1717
1691
        public HTTPSampleResult call() {
1718
        public AsynSamplerResultHolder call() {
1692
            return sample(url, method, areFollowingRedirect, depth);
1719
            try {
1720
                if(cookieManager != null) {
1721
                    cookieManagerTL.set(this.cookieManager);
1722
                }
1723
                HTTPSampleResult httpSampleResult = HTTPSamplerBase.this.sample(url, method, areFollowingRedirect, depth);
1724
                if(cookieManager != null) {
1725
                    CollectionProperty cookies = cookieManager.getCookies();
1726
                    return new AsynSamplerResultHolder(httpSampleResult, cookies);
1727
                } else {
1728
                    return new AsynSamplerResultHolder(httpSampleResult, new CollectionProperty());
1729
                }
1730
            } finally {
1731
                cookieManagerTL.remove();
1732
            }   
1693
        }
1733
        }
1694
    }
1734
    }
1695
}
1735
    
1696
1736
    private static class AsynSamplerResultHolder {
1737
        private HTTPSampleResult result;
1738
        private CollectionProperty cookies;
1739
        /**
1740
         * @param result
1741
         * @param cookies
1742
         */
1743
        public AsynSamplerResultHolder(HTTPSampleResult result, CollectionProperty cookies) {
1744
            super();
1745
            this.result = result;
1746
            this.cookies = cookies;
1747
        }
1748
        /**
1749
         * @return the result
1750
         */
1751
        public HTTPSampleResult getResult() {
1752
            return result;
1753
        }
1754
        /**
1755
         * @return the cookies
1756
         */
1757
        public CollectionProperty getCookies() {
1758
            return cookies;
1759
        }
1760
    }
1761
}

Return to bug 51919