提交 e9e80c8c 编写于 作者: N ngmr

7105640: Unix printing does not check the result of exec'd lpr/lp command

Summary: Add checking, exception for spool process failure
Reviewed-by: prr, jgodinez
Contributed-by: NNeil Richards <neil.richards@ngmr.net>
上级 1bb03fc3
......@@ -68,14 +68,18 @@ import javax.print.attribute.standard.Sides;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.CharConversionException;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Enumeration;
......@@ -673,15 +677,38 @@ public class PSPrinterJob extends RasterPrinterJob {
private class PrinterSpooler implements java.security.PrivilegedAction {
PrinterException pex;
private void handleProcessFailure(final Process failedProcess,
final String[] execCmd, final int result) throws IOException {
try (StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw)) {
pw.append("error=").append(Integer.toString(result));
pw.append(" running:");
for (String arg: execCmd) {
pw.append(" '").append(arg).append("'");
}
try (InputStream is = failedProcess.getErrorStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr)) {
while (br.ready()) {
pw.println();
pw.append("\t\t").append(br.readLine());
}
} finally {
pw.flush();
throw new IOException(sw.toString());
}
}
}
public Object run() {
if (spoolFile == null || !spoolFile.exists()) {
pex = new PrinterException("No spool file");
return null;
}
try {
/**
* Spool to the printer.
*/
if (spoolFile == null || !spoolFile.exists()) {
pex = new PrinterException("No spool file");
return null;
}
String fileName = spoolFile.getAbsolutePath();
String execCmd[] = printExecCmd(mDestination, mOptions,
mNoJobSheet, getJobNameInt(),
......@@ -689,12 +716,16 @@ public class PSPrinterJob extends RasterPrinterJob {
Process process = Runtime.getRuntime().exec(execCmd);
process.waitFor();
spoolFile.delete();
final int result = process.exitValue();
if (0 != result) {
handleProcessFailure(process, execCmd, result);
}
} catch (IOException ex) {
pex = new PrinterIOException(ex);
} catch (InterruptedException ie) {
pex = new PrinterException(ie.toString());
} finally {
spoolFile.delete();
}
return null;
}
......
......@@ -38,7 +38,9 @@ import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Reader;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.util.Vector;
......@@ -955,23 +957,49 @@ public class UnixPrintJob implements CancelablePrintJob {
private class PrinterSpooler implements java.security.PrivilegedAction {
PrintException pex;
private void handleProcessFailure(final Process failedProcess,
final String[] execCmd, final int result) throws IOException {
try (StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw)) {
pw.append("error=").append(Integer.toString(result));
pw.append(" running:");
for (String arg: execCmd) {
pw.append(" '").append(arg).append("'");
}
try (InputStream is = failedProcess.getErrorStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr)) {
while (br.ready()) {
pw.println();
pw.append("\t\t").append(br.readLine());
}
} finally {
pw.flush();
throw new IOException(sw.toString());
}
}
}
public Object run() {
if (spoolFile == null || !spoolFile.exists()) {
pex = new PrintException("No spool file");
notifyEvent(PrintJobEvent.JOB_FAILED);
return null;
}
try {
/**
* Spool to the printer.
*/
if (spoolFile == null || !spoolFile.exists()) {
pex = new PrintException("No spool file");
notifyEvent(PrintJobEvent.JOB_FAILED);
return null;
}
String fileName = spoolFile.getAbsolutePath();
String execCmd[] = printExecCmd(mDestination, mOptions,
mNoJobSheet, jobName, copies, fileName);
Process process = Runtime.getRuntime().exec(execCmd);
process.waitFor();
spoolFile.delete();
final int result = process.exitValue();
if (0 != result) {
handleProcessFailure(process, execCmd, result);
}
notifyEvent(PrintJobEvent.DATA_TRANSFER_COMPLETE);
} catch (IOException ex) {
notifyEvent(PrintJobEvent.JOB_FAILED);
......@@ -981,6 +1009,7 @@ public class UnixPrintJob implements CancelablePrintJob {
notifyEvent(PrintJobEvent.JOB_FAILED);
pex = new PrintException(ie);
} finally {
spoolFile.delete();
notifyEvent(PrintJobEvent.NO_MORE_EVENTS);
}
return null;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册