Upload component: Proper way to check MIME type

A simple question: I’m using the upload component to upload an Excel spreadsheet. To make sure it’s the right Excel format, I want to check that the MIME type is OpenXML.

Since MIME type is a parameter in the receiveUpload method, I would assume that that’s the best place to implement the check. My problem is that regardless of the result of this check, an output stream always has to be returned, otherwise an exception will occur.

    @Override
    public OutputStream receiveUpload(String filename, String mimeType) {

        for (ViewListener listener : listeners) {
            try {
                return listener.receiveUpload(filename, mimeType);
            } catch (Exception e) { // Wrong MIME Type
                upload.interruptUpload();
                return null;
//                return new ByteArrayOutputStream();
            }
        }
        return null;
    }

The above code catches an Exception if the wrong MIME type is detected, then interrupts the upload, and then goes to the uploadFailed event. But if I return null for the outputstream, the application still generates a NoOutputStreamException. I can work around this by returning a new outputStream, but since we’re already in an exception flow, the proper return here would be null. Is there a way to return null here and not get the NoOutputStreamException?

The exception is thrown from FileUploadHandler… Lines 545 - 546. I guess you’d have to write a custom upload handler.