Abdul :
Please find the sender class below
private static CloseableHttpClient httpClient() {
try {
TrustStrategy acceptingTrustStrategy = (certificate, authType) -> true;
SSLSocketFactory sf = new SSLSocketFactory(acceptingTrustStrategy, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
.register("http", PlainConnectionSocketFactory.INSTANCE)
.register("https", sf)
.build();
PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(registry);
connectionManager.setMaxTotal(maxTotalConnections);
connectionManager.setDefaultMaxPerRoute(maxConnectionsPerRoute);
CloseableHttpClient defaultHttpClient = HttpClientBuilder.create()
.setConnectionManager(connectionManager)
.setDefaultRequestConfig(RequestConfig.DEFAULT)
.build();
return defaultHttpClient;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
static String authHeaders(String username, String password ){
String auth = username + ":" + password;
String authHeader = "Basic " + auth;
return authHeader;
}
public static void main(String[] args) {
try{
CloseableHttpClient httpClient = httpClient();
HttpPost postRequest = new HttpPost("https://localhost:8018/receiver?filename=data.tar.gz");
//postRequest.addHeader(HttpHeaders.TRANSFER_ENCODING, "chunked");
postRequest.addHeader(HttpHeaders.AUTHORIZATION, authHeaders("test","test") );
postRequest.addHeader(HttpHeaders.CONTENT_ENCODING, "gzip");
File file = new File(Receiver.class.getClassLoader().getResource("data.tar.gz").getFile());
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.addPart("data.tar.gz", new FileBody(file, "application/x-gzip"));
postRequest.setEntity(builder.build());
// MultipartEntity multipart = new MultipartEntity();
// ContentBody fileContent = new FileBody(file); //For tar.gz: "application/x-gzip"
// multipart.addPart("data.tar.gz", fileContent);
// postRequest.setEntity(multipart);
HttpResponse response = httpClient.execute(postRequest);
System.out.println(response);
} catch (Exception e) {
e.printStackTrace();
}
}
I am trying to post gzip file through http client and getting below error
com.filter.GzipFilter|Error on reading gz file:
java.util.zip.ZipException: Not in GZIP format
at java.util.zip.GZIPInputStream.readHeader(GZIPInputStream.java:165)
at java.util.zip.GZIPInputStream.<init>(GZIPInputStream.java:79)
at java.util.zip.GZIPInputStream.<init>(GZIPInputStream.java:91)
at com.filter.GzipFilter.doFilter(GzipFilter.java:46)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:595)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:80)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:624)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:799)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:861)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1455)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:748)
Getting error while posting tar.gz file using http client but If I post the same file using curl command it works fine and executes properly but while passing through http client throws error.
Curl command
curl -u test:test --insecure -T ./data.tar.gz -v -X POST --header "Content-Encoding: gzip" 'https://localhost:8018/receiver?filename=data.tar.gz'
Please check and let me know what I am missing and I tried to search and got some details and implemented using the same but not yet got a solution.
Abdul :
We need to enable chunked to resolve the issue.
File file = new File(CallHttpReceiver.class.getClassLoader().getResource(fileName).getFile());
FileEntity fileEntity = new FileEntity(file);
fileEntity.setChunked(true);
postRequest.setEntity( fileEntity );
We need to make our fileEntity as chunked to pass the gzip as a request and it worked for me . Thanks to all who supported and tried to provide result for me.