I have a Java web app made in JSF that connects to a API fills a datatable with info and every row has download button. When I press download button it does another get request with an id taken from the row the button was clicked. But when I press the download button I get this message :
emptyResponse: An empty response was received from the server. Check server error logs.
The server log doesn`t show any error. This is the code:
package com.serban;
import com.google.common.io.ByteStreams;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.Serializable;
import static java.lang.System.out;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.zip.ZipOutputStream;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.StringUtils;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
@ManagedBean(name = "logic", eager = true)
@SessionScoped
public class Logic implements Serializable {
static JSONObject jsonObject = null;
static JSONObject jo = null;
static JSONArray cat = null;
private ArrayList<Mesaj> logics;
StringBuilder sb = new StringBuilder();
String cif2;
String data_creare2;
String id_solicitare2;
String tip2;
String id2;
String detalii2;
String numar2;
/**
*
* @throws ParseException
*/
public void apelareApi() {
try {
URL url = new URL("xxx");
URLConnection yc = url.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream()));
String inputLine;
while((inputLine = in.readLine())!= null){
System.out.println(inputLine);
sb.append(inputLine+"\n");
}
in.close();
}catch(Exception e) {System.out.println(e);}
}
public void incarcareLista() throws ParseException {
JSONParser parser = new JSONParser();
jsonObject = (JSONObject) parser.parse(sb.toString());
cat = (JSONArray) jsonObject.get("mesaje");
logics = new ArrayList<Mesaj>();
for(int i = 0; i < cat.size(); i++) {
Mesaj m = new Mesaj();
jo = (JSONObject) cat.get(i);
cif2 = jo.get("cif").toString();
data_creare2 = jo.get("data_creare").toString();
id_solicitare2 = jo.get("id_solicitare").toString();
tip2 = jo.get("tip").toString();
id2 = jo.get("id").toString();
detalii2 = jo.get("detalii").toString();
numar2 = Integer.toString(i+1);
m.setCif(cif2);
m.setData_creare(data_creare2);
m.setId_solicitare(id_solicitare2);
m.setTip(tip2);
m.setId(id2);
m.setDetalii(detalii2);
m.setNumar(numar2);
logics.add(m);
}
}
/**
*
* @throws ParseException
*/
@PostConstruct
public void apelareSiIncarcare() {
apelareApi();
try {
incarcareLista();
} catch (ParseException ex) {
Logger.getLogger(Logic.class.getName()).log(Level.SEVERE, null, ex);
}
}
public ArrayList<Mesaj> getLogics() {
return logics;
}
public Logic() {
}
String x;
String id;
byte[] rasp;
public void printNumar(String x) throws IOException {
this.x = x;
//System.out.println(x);
int y = Integer.parseInt(x);
jo = (JSONObject) cat.get(y-1);
System.out.println(jo.get("id"));
id = jo.get("id").toString();
rasp = apelareApi2(id);
startDownload(rasp);
}
byte[] byteChunk;
public byte[] apelareApi2(String z) throws IOException {
URL url = new URL("xxx"+z);
URLConnection yc = url.openConnection();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
InputStream is = null;
try {
is = url.openStream ();
byteChunk = new byte[is.available()]; // Or whatever size you want to read in at a time.
int n;
while ( (n = is.read(byteChunk)) > 0 ) {
baos.write(byteChunk, 0, n);
}
}
catch (IOException e) {
System.err.printf ("Failed while reading bytes from %s: %s", url.toExternalForm(),e.getMessage());e.printStackTrace ();}finally {
if (is != null) { is.close(); }
}
return byteChunk;
}
private void startDownload(byte[] continut)
throws IOException {
FacesContext facesContext = FacesContext.getCurrentInstance();
ExternalContext externalContext = facesContext.getExternalContext();
// externalContext.responseReset();
externalContext.setResponseHeader("Cache-Control", "public");
externalContext.setResponseHeader("Pragma", "public");
externalContext.setResponseHeader("Content-Type", "application/pdf");
externalContext.setResponseHeader("Content-Length",
Integer.toString(continut.length));
externalContext.setResponseHeader("Content-Disposition",
"attachment;filename=\"" + "id.pdf" + "\"");
externalContext.getResponseOutputStream().write(continut);
facesContext.responseComplete();
}
}