public InputStream getInputStream()

in flow/src/java/org/apache/struts/flow/core/source/impl/URLSource.java [225:287]


    public InputStream getInputStream() throws IOException, SourceException
    {
        checkInfos();
        InputStream input = null;
        if (m_connection == null)
        {
            m_connection = m_url.openConnection();

            String userInfo = getUserInfo();
            if (m_url.getProtocol().startsWith("http") && userInfo != null)
            {
                m_connection.setRequestProperty("Authorization", "Basic " + SourceUtil.encodeBASE64(userInfo));
            }

            // do a post operation
            if (m_connection instanceof HttpURLConnection && m_isPost)
            {
                StringBuffer buffer = new StringBuffer(2000);
                String key;
                Iterator i = m_parameters.getParameterNames();
                Iterator values;
                String value;
                boolean first = true;
                while (i.hasNext())
                {
                    key = (String) i.next();
                    values = m_parameters.getParameterValues(key);
                    while (values.hasNext() == true)
                    {
                        value = SourceUtil.encode((String) values.next(), m_encoding);
                        if (first == false)
                            buffer.append('&');
                        first = false;
                        buffer.append(key.toString());
                        buffer.append('=');
                        buffer.append(value);
                    }
                }
                HttpURLConnection httpCon = (HttpURLConnection) m_connection;
                httpCon.setDoInput(true);

                if (buffer.length() > 1)
                { // only post if we have parameters
                    String postString = buffer.toString();
                    httpCon.setRequestMethod("POST"); // this is POST
                    httpCon.setDoOutput(true);
                    httpCon.setRequestProperty("Content-type", "application/x-www-form-urlencoded");

                    // A content-length header must be contained in a POST request
                    httpCon.setRequestProperty("Content-length", Integer.toString(postString.length()));
                    java.io.OutputStream out = new java.io.BufferedOutputStream(httpCon.getOutputStream());
                    out.write(postString.getBytes());
                    out.close();
                }
                input = httpCon.getInputStream();
                m_connection = null; // make sure a new m_connection is created next time
                return input;
            }
        }
        input = m_connection.getInputStream();
        m_connection = null; // make sure a new m_connection is created next time
        return input;
    }