All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.daioware.net.http.client.HttpSender Maven / Gradle / Ivy

package com.daioware.net.http.client;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.UnknownHostException;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import com.daioware.commons.wrapper.WrapperInt;
import com.daioware.commons.wrapper.WrapperString;
import com.daioware.net.http.HttpResponse;
import com.daioware.net.http.items.HttpHeader;
import com.daioware.net.http.request.HttpRequest;
import com.daioware.net.socket.TCPClientSocket;

public class HttpSender {

	public static final int DEFAULT_TIME_OUT_MILLIS=5000;
	
	private HttpRequest request;
	private HttpResponse response;
	private int downloadSpeedInBytes;
	private int uploadSpeedInBytes;
	private ResponseHandler respHandler;
	
	public HttpSender(HttpRequest request) {
		this(request,512,512);
	}
	public HttpSender(HttpRequest request,int downloadSpeedInBytes,int uploadSpeedInBytes) {
		setRequest(request);
		setRespHandler(null);
		setDownloadSpeedInBytes(downloadSpeedInBytes);
		setUploadSpeedInBytes(uploadSpeedInBytes);
	}

	public int getUploadSpeedInBytes() {
		return uploadSpeedInBytes;
	}
	public void setUploadSpeedInBytes(int uploadSpeedInBytes) {
		this.uploadSpeedInBytes = uploadSpeedInBytes;
	}
	public int getDownloadSpeedInBytes() {
		return downloadSpeedInBytes;
	}

	public void setDownloadSpeedInBytes(int downloadSpeedInBytes) {
		this.downloadSpeedInBytes = downloadSpeedInBytes;
	}

	public HttpRequest getRequest() {
		return request;
	}

	public void setRequest(HttpRequest request) {
		this.request = request;
	}
	
	public ResponseHandler getRespHandler() {
		return respHandler;
	}

	public void setRespHandler(ResponseHandler respHandler) {
		this.respHandler=respHandler==null?EmptyHandler.DEFAULT_INSTANCE:respHandler;
	}
	
	public HttpResponse send() throws UnknownHostException, IOException, HandlingException{
		return send(DEFAULT_TIME_OUT_MILLIS);
	}
	public HttpResponse send(int timeOutMillis) throws UnknownHostException, IOException, HandlingException {
		HttpRequest request=getRequest();
		TCPClientSocket socket=new TCPClientSocket(request.getHost(),request.getPort(),
				Charset.forName(request.getEncoding()));
		socket.setSoTimeout(timeOutMillis);
		return send(socket);
	}
	public HttpResponse send(TCPClientSocket socket) throws UnsupportedEncodingException, IOException, HandlingException {
		socket.write(request.getMetadataAsString());
		byte bodyBytes[];
		int length,bytesRead;
		if(request.startReadingBody()) {
			try {
				bodyBytes=new byte[length=getUploadSpeedInBytes()];
				while((bytesRead=request.readBody(bodyBytes, 0, length))>=1) {
					socket.write(bodyBytes, 0, bytesRead);
				}
			}
			finally {
				request.stopReadingBody();
			}
		}
		return parse(socket);		
	}
	protected HttpResponse parse(TCPClientSocket socket) throws IOException, HandlingException {
		List bodyBytes=new LinkedList<>();
		List headersBytes=new LinkedList<>();
		int bytesRead,currentIndexToRead,readingHeaderState;
		boolean readHeaders=true;
		byte bytes[]=new byte[getDownloadSpeedInBytes()];
		Integer totalBytesToRead=null;
		Map headers=new HashMap<>();
		byte byteRead;
		WrapperInt status=new WrapperInt();
		WrapperString statusMessage=new WrapperString();
		WrapperString version=new WrapperString();
		HttpResponse httpResponse;
		try {
			readingHeaderState=0;
			respHandler.open(this);
			do {
				if(socket.getSocket().isClosed()) {
					break;
				}
				bytesRead=socket.readBytes(bytes);
				if(bytesRead==-1 || bytesRead==0) {
					break;
				}
				if(readHeaders) {
					respHandler.handleHeaders(bytes, 0, bytesRead);
					for(currentIndexToRead=0;currentIndexToRead=1);
			httpResponse=createHttpResponse(headers,bodyBytes,version.value,status.value,statusMessage.value,request);
			setResponse(httpResponse);
			return httpResponse;
		}catch (IOException | HandlingException e) {
			throw e;
		}finally {
			socket.close();
			respHandler.close();
		}
	}
	
	protected HttpResponse createHttpResponse(Map headers, List bodyBytes, String version, 
			Integer status,String statusMessage, HttpRequest r) {
		return new HttpResponse(headers,bodyBytes,version,status,statusMessage,request);
	}
	public HttpResponse getResponse() {
		return response;
	}

	public void setResponse(HttpResponse response) {
		this.response = response;
	}

	protected Integer getTotalBytesToRead(Map headers) {
		HttpHeader header=headers.get("Content-Length");
		return header!=null?Integer.parseInt(header.getValue()):null;
	}
	protected void setMetadata(String string,Map headers,WrapperInt status,
			WrapperString statusMessage,WrapperString version) {
		int i,j;
		String line;
		String lines[]=string.split("\r\n");
		String keyAndValue[];
		String firstLine;
		for(i=1,j=lines.length-1;i




© 2015 - 2025 Weber Informatics LLC | Privacy Policy