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

com.alogic.remote.httpclient.HttpClientResponse Maven / Gradle / Ivy

There is a newer version: 1.6.16
Show newest version
package com.alogic.remote.httpclient;

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

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.client.entity.GzipDecompressingEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.protocol.HTTP;

import com.alogic.remote.Response;
import com.anysoft.util.IOTools;

/**
 * Response
 * 
 * @author yyduan
 * @since 1.6.8.12
 * 
 * @version 1.6.8.15 [20170511 duanyy] 
* - 增加绝对路径调用功能
* * @version 1.6.13.21 [20201021 duanyy]
* - 优化代码结构
* - 支持gzip压缩
*/ public class HttpClientResponse implements Response{ protected CloseableHttpResponse httpResponse = null; protected String encoding = "utf-8"; protected boolean closed = false; public HttpClientResponse(CloseableHttpResponse response,String encoding){ this.httpResponse = response; this.encoding = encoding; } @Override public String getHeader(String name, String dft) { String value = dft; if (this.httpResponse != null){ Header header = this.httpResponse.getFirstHeader(name); if (header != null){ value = header.getValue(); } } return value; } @Override public int getStatusCode() { return this.httpResponse == null ? -1 : this.httpResponse.getStatusLine().getStatusCode(); } @Override public String getReasonPhrase() { return this.httpResponse == null ? "" : this.httpResponse.getStatusLine().getReasonPhrase(); } @Override public String asString() throws IOException { byte [] bytes = asBytes(); return new String(bytes,getEncoding()); } @Override public byte[] asBytes() throws IOException { InputStream in = asStream(); if (in != null){ try { ByteArrayOutputStream out = new ByteArrayOutputStream(10240); byte[] buffer = new byte[10240]; int read = 0; while ((read = in.read(buffer,0,buffer.length)) != -1){ out.write(buffer, 0, read); } return out.toByteArray(); }finally{ IOTools.close(in); closed = true; } } return new byte[0]; } @Override public InputStream asStream() throws IOException { InputStream in = null; if (this.httpResponse != null){ HttpEntity entity = this.httpResponse.getEntity(); if (entity != null){ in = entity.getContent(); } } return in; } @Override public void discard(){ if (this.httpResponse != null && !closed){ HttpEntity entity = this.httpResponse.getEntity(); if (entity != null){ try { InputStream in = entity.getContent(); if (in.available() > 0){ IOTools.close(in); } }catch (Exception ex){ //ex.printStackTrace(); } } } } @Override public void close() throws IOException{ if (httpResponse != null){ closed = true; httpResponse.close(); } } protected String getEncoding(){ if (this.httpResponse != null){ HttpEntity entity = this.httpResponse.getEntity(); if (entity != null){ Header contentEncoding = entity.getContentEncoding(); if (contentEncoding != null){ return contentEncoding.getValue(); } } } return this.encoding; } @Override public String getContentType() { if (this.httpResponse != null){ Header header = this.httpResponse.getFirstHeader(HTTP.CONTENT_TYPE); if (header != null){ return header.getValue(); } } return null; } @Override public int getContentLength() { if (this.httpResponse != null){ Header header = this.httpResponse.getFirstHeader(HTTP.CONTENT_LEN); if (header != null){ try { return Integer.parseInt(header.getValue()); }catch (NumberFormatException ex){ return 0; } } } return 0; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy