Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

setting request properties for DefaultHttpDataSource. #2174

Closed
ghost opened this issue Dec 9, 2016 · 1 comment
Closed

setting request properties for DefaultHttpDataSource. #2174

ghost opened this issue Dec 9, 2016 · 1 comment
Labels

Comments

@ghost
Copy link

ghost commented Dec 9, 2016

I need to send custom headers to our server while streaming media URL. and I have seen this one. #2166

then I implement my own HttpDataSource.Factory to generating instances of DefaultHttpDataSource like the follow code.

does it work?

private static class VHttpDataSourceFactory implements HttpDataSource.Factory {

        private final String userAgent;
        private final TransferListener<? super DataSource> listener;
        private final int connectTimeoutMillis;
        private final int readTimeoutMillis;
        private final boolean allowCrossProtocolRedirects;
        private Map<String, String> mHeaders = null;
        /**
         * Constructs a DefaultHttpDataSourceFactory. Sets {@link
         * DefaultHttpDataSource#DEFAULT_CONNECT_TIMEOUT_MILLIS} as the connection timeout, {@link
         * DefaultHttpDataSource#DEFAULT_READ_TIMEOUT_MILLIS} as the read timeout and disables
         * cross-protocol redirects.
         *
         * @param userAgent The User-Agent string that should be used.
         */
        public VHttpDataSourceFactory(String userAgent) {
            this(userAgent, null);
        }

        /**
         * Constructs a DefaultHttpDataSourceFactory. Sets {@link
         * DefaultHttpDataSource#DEFAULT_CONNECT_TIMEOUT_MILLIS} as the connection timeout, {@link
         * DefaultHttpDataSource#DEFAULT_READ_TIMEOUT_MILLIS} as the read timeout and disables
         * cross-protocol redirects.
         *
         * @param userAgent The User-Agent string that should be used.
         * @param listener An optional listener.
         * @see #VHttpDataSourceFactory(String, TransferListener, int, int, boolean)
         */
        public VHttpDataSourceFactory(
                String userAgent, TransferListener<? super DataSource> listener) {
            this(userAgent, listener, DefaultHttpDataSource.DEFAULT_CONNECT_TIMEOUT_MILLIS,
                    DefaultHttpDataSource.DEFAULT_READ_TIMEOUT_MILLIS, false);
        }

        /**
         * @param userAgent The User-Agent string that should be used.
         * @param listener An optional listener.
         * @param connectTimeoutMillis The connection timeout that should be used when requesting remote
         *     data, in milliseconds. A timeout of zero is interpreted as an infinite timeout.
         * @param readTimeoutMillis The read timeout that should be used when requesting remote data, in
         *     milliseconds. A timeout of zero is interpreted as an infinite timeout.
         * @param allowCrossProtocolRedirects Whether cross-protocol redirects (i.e. redirects from HTTP
         *     to HTTPS and vice versa) are enabled.
         */
        public VHttpDataSourceFactory(String userAgent,
                                            TransferListener<? super DataSource> listener, int connectTimeoutMillis,
                                            int readTimeoutMillis, boolean allowCrossProtocolRedirects) {
            this.userAgent = userAgent;
            this.listener = listener;
            this.connectTimeoutMillis = connectTimeoutMillis;
            this.readTimeoutMillis = readTimeoutMillis;
            this.allowCrossProtocolRedirects = allowCrossProtocolRedirects;
        }

        public void setHeader(Map<String, String> header) {
            this.mHeaders = header;
        }

        @Override
        public DefaultHttpDataSource createDataSource() {
            DefaultHttpDataSource defaultHttpDataSource = new DefaultHttpDataSource(userAgent, null, listener, connectTimeoutMillis,
                    readTimeoutMillis, allowCrossProtocolRedirects);
            if (mHeaders != null) {
                for (Map.Entry<String, String> entry : mHeaders.entrySet()) {
                    defaultHttpDataSource.setRequestProperty(entry.getKey(), entry.getValue());
                }
            }
            return  defaultHttpDataSource;
        }
    }`
@ojw28
Copy link
Contributor

ojw28 commented Dec 11, 2016

It looks like it should work. It's unclear what the question is here? If you've written some code, it's your responsibility to test whether it works (and debug it if not). The purpose of this tracker is not for us to do it for you. #2166 is tracking making this easier; until then you'll have to do something similar to the code you've written above.

@ojw28 ojw28 closed this as completed Dec 11, 2016
@ojw28 ojw28 added the question label Dec 11, 2016
@google google locked and limited conversation to collaborators Jun 28, 2017
# for free to subscribe to this conversation on GitHub. Already have an account? #.
Labels
Projects
None yet
Development

No branches or pull requests

1 participant