I am trying to download a PDF file available at one of the rest URL using JAX RS and Jersey with authorization .
import org.apache.commons.io.IOUtils;
import javax.net.ssl.*;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Invocation;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Response;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.File;
import java.security.SecureRandom;
import java.security.cert.X509Certificate;
public class ReportView {
public void process(String authStringEnc) {
System.setProperty("javax.net.ssl.trustStore","C:\\Users\\alim\\Desktop\\my-app\\stage\\npkeystore.jks");
System.setProperty("javax.net.ssl.trustStorePassword","changeit");
System.setProperty("javax.net.ssl.trustAnchors","C:\\Users\\alim\\Desktop\\my-app\\stage\\npkeystore.jks");
Client client = ClientBuilder.newClient();
// WebTarget target = client.target("https://x.x.x.x/api/profiler/1.0/reporting/reports/751252/").path("view");
WebTarget target = client.target("https://x.x.x.x/api/profiler/1.0/reporting/reports/751252/view");
Response resp = target.request("application/pdf,image/jpeg,application/xml,application/vnd.ms-excel").header("Authorization", authStringEnc).get(Response.class);
System.out.println("Code : " + resp.getStatus());
if(resp.getStatus() == Response.Status.OK.getStatusCode()) {
InputStream is = resp.readEntity(InputStream.class);
File downloadfile = new File("C://Users/alim/Downloads/view.pdf");
try {
byte[] byteArray = IOUtils.toByteArray(is);
FileOutputStream fos = new FileOutputStream(downloadfile);
fos.write(byteArray);
fos.flush();
fos.close();
}catch(Exception e){
e.getMessage();
}
IOUtils.closeQuietly(is);
System.out.println("the file details after call:"+ downloadfile.getAbsolutePath()+", size is "+downloadfile.length());
}
else{
throw new WebApplicationException("Http Call failed. response code is"+resp.getStatus()+". Error reported is"+resp.getStatusInfo());
}
}
But the above code snippet returns a 400 Bad Request . Not sure if I have specified the URL incorrectly . Using the same URL in Postman returns a PDF file .
Exception in thread "main" javax.ws.rs.WebApplicationException: Http Call failed. response code is 400. Error reported is Bad Request
Also removing the certificate block returns me PKIX Certification Exception while I have already defined it in main class and using it in one of the subclass .
Caused by: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
JAX-RS and Jersey concepts are pretty new to me. Not Sure where I am going wrong in terms of specifying URL with authentication,certificate and request.
Any help/guidance over same would really help.