protected void doExecute()

in core/src/main/java/org/apache/struts2/result/StreamResult.java [212:293]


    protected void doExecute(String finalLocation, ActionInvocation invocation) throws Exception {
        LOG.debug("Find the Response in context");

        OutputStream oOutput = null;

        try {
            String parsedInputName = conditionalParse(inputName, invocation);
            boolean evaluated = parsedInputName != null && !parsedInputName.equals(inputName);
            boolean reevaluate = !evaluated || isAcceptableExpression(parsedInputName);
            if (inputStream == null && reevaluate) {
                LOG.debug("Find the inputstream from the invocation variable stack");
                inputStream = (InputStream) invocation.getStack().findValue(parsedInputName);
            }

            if (inputStream == null) {
                String msg = ("Can not find a java.io.InputStream with the name [" + parsedInputName + "] in the invocation stack. " +
                    "Check the <param name=\"inputName\"> tag specified for this action is correct, not excluded and accepted.");
                LOG.error(msg);
                throw new IllegalArgumentException(msg);
            }


            HttpServletResponse oResponse = invocation.getInvocationContext().getServletResponse();

            LOG.debug("Set the content type: {};charset{}", contentType, contentCharSet);
            if (contentCharSet != null && !contentCharSet.isEmpty()) {
                oResponse.setContentType(conditionalParse(contentType, invocation) + ";charset=" + conditionalParse(contentCharSet, invocation));
            } else {
                oResponse.setContentType(conditionalParse(contentType, invocation));
            }

            LOG.debug("Set the content length: {}", contentLength);
            if (contentLength != null) {
                String translatedContentLength = conditionalParse(contentLength, invocation);
                int contentLengthAsInt;
                try {
                    contentLengthAsInt = Integer.parseInt(translatedContentLength);
                    if (contentLengthAsInt >= 0) {
                        oResponse.setContentLength(contentLengthAsInt);
                    }
                } catch (NumberFormatException e) {
                    LOG.warn("failed to recognize {} as a number, contentLength header will not be set",
                            translatedContentLength, e);
                }
            }

            LOG.debug("Set the content-disposition: {}", contentDisposition);
            if (contentDisposition != null) {
                oResponse.addHeader("Content-Disposition", conditionalParse(contentDisposition, invocation));
            }

            LOG.debug("Set the cache control headers if necessary: {}", allowCaching);
            if (!allowCaching) {
                oResponse.addHeader("Pragma", "no-cache");
                oResponse.addHeader("Cache-Control", "no-cache");
            }

            oOutput = oResponse.getOutputStream();

            LOG.debug("Streaming result [{}] type=[{}] length=[{}] content-disposition=[{}] charset=[{}]",
                inputName, contentType, contentLength, contentDisposition, contentCharSet);

            LOG.debug("Streaming to output buffer +++ START +++");
            byte[] oBuff = new byte[bufferSize];
            int iSize;
            while (-1 != (iSize = inputStream.read(oBuff))) {
                LOG.debug("Sending stream ... {}", iSize);
                oOutput.write(oBuff, 0, iSize);
            }
            LOG.debug("Streaming to output buffer +++ END +++");

            // Flush
            oOutput.flush();
        } finally {
            if (inputStream != null) {
                inputStream.close();
            }
            if (oOutput != null) {
                oOutput.close();
            }
        }
    }