com.eviware.soapui.impl.wsdl.submit.transports.http.TimeablePostMethod.java Source code

Java tutorial

Introduction

Here is the source code for com.eviware.soapui.impl.wsdl.submit.transports.http.TimeablePostMethod.java

Source

/*
 *  soapui, copyright (C) 2006 eviware.com 
 *
 *  SoapUI is free software; you can redistribute it and/or modify it under the 
 *  terms of the GNU Lesser General Public License as published by the Free Software Foundation; 
 *  either version 2.1 of the License, or (at your option) any later version.
 *
 *  SoapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without 
 *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
 *  See the GNU Lesser General Public License for more details at gnu.org.
 */

package com.eviware.soapui.impl.wsdl.submit.transports.http;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.httpclient.HttpConnection;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpState;
import org.apache.commons.httpclient.methods.PostMethod;

import com.eviware.soapui.impl.wsdl.support.http.HttpClientSupport;
import com.eviware.soapui.support.Tools;

/**
 * Extended PostMethod that supports limiting of response size and detailed
 * timestamps
 * 
 * @author Ole.Matzura
 */

public final class TimeablePostMethod extends PostMethod {
    private long timeTaken;

    private long startTime;

    private long maxSize;

    private byte[] responseBody;

    public TimeablePostMethod() {
    }

    protected void readResponse(HttpState arg0, HttpConnection arg1) throws IOException, HttpException {
        super.readResponse(arg0, arg1);
        timeTaken = getTimeTakenUntilNow();
    }

    public long getMaxSize() {
        return maxSize;
    }

    public void setMaxSize(long maxSize) {
        this.maxSize = maxSize;
    }

    public long getTimeTakenUntilNow() {
        long nanoTime = System.nanoTime();
        long result = ((nanoTime - startTime) + 500000) / 1000000;

        if (result == 0) {
            System.out.println("time taken = 0 ms; " + (nanoTime - startTime) + " ns");
            result = 1;
        }

        return result;
    }

    protected void writeRequest(HttpState arg0, HttpConnection arg1) throws IOException, HttpException {
        super.writeRequest(arg0, arg1);

        if (startTime == 0)
            startTime = System.nanoTime();
    }

    public void initStartTime() {
        startTime = System.nanoTime();
    }

    public long getTimeTaken() {
        return timeTaken;
    }

    public long getStartTime() {
        return startTime;
    }

    public byte[] getResponseBody() throws IOException {
        if (responseBody != null)
            return responseBody;

        long contentLength = getResponseContentLength();

        if (maxSize == 0 || (contentLength >= 0 && contentLength <= maxSize))
            return super.getResponseBody();

        InputStream instream = getResponseBodyAsStream();

        ByteArrayOutputStream outstream = Tools.readAll(instream, maxSize);
        responseBody = outstream.toByteArray();

        if (HttpClientSupport.isZippedResponse(this)) {
            responseBody = HttpClientSupport.decompress(responseBody);
        }

        return responseBody;
    }
}