Coverage Report - org.apache.commons.performance.http.HttpClientThread
 
Classes in this File Line Coverage Branch Coverage Complexity
HttpClientThread
0%
0/20
0%
0/8
2.25
 
 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 3  
  * contributor license agreements.  See the NOTICE file distributed with
 4  
  * this work for additional information regarding copyright ownership.
 5  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 6  
  * (the "License"); you may not use this file except in compliance with
 7  
  * the License.  You may obtain a copy of the License at
 8  
  * 
 9  
  *      http://www.apache.org/licenses/LICENSE-2.0
 10  
  * 
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 
 18  
 package org.apache.commons.performance.http;
 19  
 
 20  
 import java.util.logging.Logger;
 21  
 
 22  
 import org.apache.commons.httpclient.HttpClient;
 23  
 import org.apache.commons.httpclient.HttpException;
 24  
 import org.apache.commons.httpclient.HttpMethod;
 25  
 import org.apache.commons.httpclient.HttpStatus;
 26  
 import org.apache.commons.httpclient.methods.GetMethod;
 27  
 import org.apache.commons.httpclient.methods.PostMethod;
 28  
 
 29  
 import org.apache.commons.performance.ClientThread;
 30  
 import org.apache.commons.performance.Statistics;
 31  
 
 32  
 /**
 33  
  * Client thread that executes http requests in a loop against a configured
 34  
  * url, with the number of requests, time between requests and 
 35  
  * query strings governed by constructor parameters. See 
 36  
  * {@link ClientThread ClientThread javadoc} for a description
 37  
  * of how times between requests are computed.
 38  
  *
 39  
  */
 40  
 public class HttpClientThread extends ClientThread {
 41  
 
 42  0
     private HttpClient httpClient = new HttpClient();
 43  0
     private HttpMethod httpMethod = null;
 44  0
     private String successKey = null;
 45  
     
 46  
     public HttpClientThread(long iterations, long minDelay, long maxDelay,
 47  
             double sigma, String delayType, long rampPeriod,
 48  
             long peakPeriod, long troughPeriod, String cycleType,
 49  
             String rampType, Logger logger,
 50  
             Statistics stats, String url, String method,
 51  
             int socketTimeout, String successKey)  {
 52  
         
 53  0
         super(iterations, minDelay, maxDelay, sigma, delayType, rampPeriod,
 54  
                 peakPeriod, troughPeriod, cycleType, rampType, logger,
 55  
                 stats);
 56  
         
 57  0
         httpClient.getParams().setSoTimeout(socketTimeout);
 58  0
         if (method.trim().toUpperCase().equals("POST")) {
 59  0
             httpMethod = new PostMethod(url);
 60  
         } else {
 61  0
             httpMethod = new GetMethod(url);
 62  
         }
 63  0
         this.successKey = successKey;
 64  0
     }
 65  
 
 66  
     /** Nothing to do here at this point */
 67  0
     public void setUp() throws Exception {}
 68  
     
 69  
     /**
 70  
      * Execute the http method against the target url.
 71  
      * Throws HttpException if something other than 200 status is returned
 72  
      * or if there is a successKey configured and the response does not contain
 73  
      * the specified key.
 74  
      */
 75  
     public void execute() throws Exception {
 76  0
         int statusCode = httpClient.executeMethod(httpMethod);
 77  0
         if (statusCode != HttpStatus.SC_OK) {
 78  0
             throw new HttpException("Request failed with status code: "
 79  
                     + statusCode);
 80  
         }
 81  
         // Should use stream here - for now assume body is small
 82  0
         String responseBody = httpMethod.getResponseBodyAsString();
 83  0
         if (successKey != null && responseBody.indexOf(successKey) < 0 ) {
 84  0
             throw new HttpException("Response did not include success key: "
 85  
                     + successKey);
 86  
         }
 87  0
     }
 88  
     
 89  
     /** Release http connection */
 90  
     public void cleanUp() throws Exception {
 91  0
          httpMethod.releaseConnection();
 92  0
     }
 93  
 
 94  
 }