/**
* Used for reading a Certificate Signing Request file upload into the certificate request String.
* @param actionEvent is the parameter from the web framework containing the file.
*/
public void uploadActionListener(ActionEvent actionEvent) {
InputFile inputFile = (InputFile) actionEvent.getSource();
FacesContext context = FacesContext.getCurrentInstance();
if (inputFile.getFileInfo().isSaved()) {
// Validate that it is a CSR..
File f = inputFile.getFileInfo().getFile();
// Assume this is a small file.. it should be..
long len = f.length();
if (len < 16*1024L) {
byte[] buf = new byte[(int) len];
try {
FileInputStream in = new FileInputStream(f);
in.read(buf);
in.close();
} catch (IOException e) {
context.addMessage(null /*actionEvent.getComponent().getClientId(context)*/, new FacesMessage(FacesMessage.SEVERITY_ERROR, getMessage("enroll.csrcert.uploadfailed"), null));
log.debug("Rejected uploaded file due to IOException.");
return;
}
try {
// See if it was a PEM
buf = FileTools.getBytesFromPEM(buf, PEM_CSR_BEGIN, PEM_CSR_END);
} catch (IOException e) {
log.debug("Uploaded file was not a PEM.. tryin to parse it as a DER encoded request.");
}
// See if it as a PKCS10
try {
new PKCS10CertificationRequest(buf);
} catch (Exception e) {
context.addMessage(null /*actionEvent.getComponent().getClientId(context)*/, new FacesMessage(FacesMessage.SEVERITY_ERROR, getMessage("enroll.csrcert.uploadfailednotpkcs10"), null));
log.debug("Rejected uploaded file since it's not a valid PKCS#10 request.");
return;
}
// Convert it back to a PEM
String pem = PEM_CSR_BEGIN + "\n" + new String(Base64.encode(buf)) + "\n" + PEM_CSR_END;
certificateRequest = pem;
context.addMessage(null /*actionEvent.getComponent().getClientId(context)*/, new FacesMessage(FacesMessage.SEVERITY_INFO, getMessage("enroll.csrcert.uploadok"), null));
} else {
context.addMessage(null /*actionEvent.getComponent().getClientId(context)*/, new FacesMessage(FacesMessage.SEVERITY_ERROR, getMessage("enroll.csrcert.uploadfailedtoolarge"), null));
}
} else {
log.debug("File upload failed: " + inputFile.getFileInfo().getException().getMessage());
context.addMessage(null /*actionEvent.getComponent().getClientId(context)*/, new FacesMessage(FacesMessage.SEVERITY_ERROR, getMessage("enroll.csrcert.uploadfailed"), null));
}
}