Package org.wiztools.commons.feed

Source Code of org.wiztools.commons.feed.HttpUtil

package org.wiztools.commons.feed;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Map;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.AbstractHttpMessage;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;

/**
*
* @author subhash
*/
final class HttpUtil {
    static InputStream getConnectionStream(URL url, Options options) throws IOException{
        if(options != null){
            DefaultHttpClient httpclient = new DefaultHttpClient();
            HttpContext localContext = new BasicHttpContext();
            AbstractHttpMessage method = new HttpGet(url.toString());
            if(options.isHttpAuthEnabled()){
                String username = options.getHttpAuthUsername();
                String password = options.getHttpAuthPassword();
                httpclient.getCredentialsProvider().setCredentials(
                        AuthScope.ANY, new UsernamePasswordCredentials(username, password));
            }
            if(options.isCookiesSet()){
                Map<String, String> cookies = options.getHttpCookies();
                StringBuilder sb = new StringBuilder();
                for(String name: cookies.keySet()){
                    sb.append(name)
                            .append("=")
                            .append(cookies.get(name))
                            .append(";");
                }
                method.addHeader("Cookie", sb.toString());
            }

            HttpResponse http_res = httpclient.execute((HttpUriRequest) method,
                    localContext);
            return new WrappedHttpClientInputStream(http_res, httpclient);
        }
        else{
            return url.openStream();
        }
    }
}
TOP

Related Classes of org.wiztools.commons.feed.HttpUtil

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.