net.sasasin.sreader.commons.util.impl.WgetHttpComponentsImpl.java Source code

Java tutorial

Introduction

Here is the source code for net.sasasin.sreader.commons.util.impl.WgetHttpComponentsImpl.java

Source

/*
 * SReader is RSS/Atom feed reader with full text.
 *
 * Copyright (C) 2011, Shinnosuke Suzuki <sasasin@sasasin.net>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as 
 * published by the Free Software Foundation, either version 3 of
 * the License, or any later version.
 *   
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program.
 * If not, see <http://www.gnu.org/licenses/>.
 */
package net.sasasin.sreader.commons.util.impl;

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

import net.sasasin.sreader.commons.entity.LoginRules;
import net.sasasin.sreader.commons.util.CharDetector;
import net.sasasin.sreader.commons.util.Wget;

import org.apache.commons.io.IOUtils;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpHead;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicHeader;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

public class WgetHttpComponentsImpl implements Wget {

    private URL url;
    private LoginRules loginInfo;
    private String loginId;
    private String loginPassword;

    private final static int DEFAULT_TIMEOUT_MILLISECONDS = 3000;

    public WgetHttpComponentsImpl() {
        setUrl(null);
        setLoginInfo(null);
        setLoginId(null);
        setLoginPassword(null);
    }

    public WgetHttpComponentsImpl(URL url) {
        setUrl(url);
        setLoginInfo(null);
        setLoginId(null);
        setLoginPassword(null);
    }

    @Override
    public void setUrl(URL url) {
        this.url = url;
    }

    @Override
    public URL getUrl() {
        return url;
    }

    @Override
    public LoginRules getLoginInfo() {
        return loginInfo;
    }

    @Override
    public void setLoginInfo(LoginRules loginInfo) {
        this.loginInfo = loginInfo;
    }

    @Override
    public String getLoginId() {
        return loginId;
    }

    @Override
    public void setLoginId(String loginId) {
        this.loginId = loginId;
    }

    @Override
    public String getLoginPassword() {
        return loginPassword;
    }

    @Override
    public void setLoginPassword(String loginPassword) {
        this.loginPassword = loginPassword;
    }

    @Override
    public String read() {

        HttpResponse responce = null;
        try (CloseableHttpClient httpclient = httpClientFactory()) {
            responce = httpclient.execute(new HttpGet(getUrl().toString()));
            return read(responce.getEntity().getContent());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }

    private String read(InputStream is) {
        String result = null;
        byte[] buf = null;
        try {
            // ????
            // UTF8?????String?????????
            // ??String???CharDetector?????
            buf = IOUtils.toByteArray(is);
        } catch (IOException e) {
            e.printStackTrace();
            result = "";
        }
        try {
            // ???
            String enc = CharDetector.detect(buf);
            // ????String??
            result = new String(buf, enc);
        } catch (CharacterCodingException | UnsupportedEncodingException e) {
            // ?????????????String??
            result = new String(buf);
        }
        return result;
    }

    private CloseableHttpClient httpClientFactory() throws IOException {

        // HTTP 30x?
        RequestConfig config = RequestConfig.custom().setRedirectsEnabled(true)
                .setConnectTimeout(DEFAULT_TIMEOUT_MILLISECONDS)
                .setConnectionRequestTimeout(DEFAULT_TIMEOUT_MILLISECONDS).build();

        // UserAgent?MSIE
        List<Header> headers = new ArrayList<Header>();
        headers.add(new BasicHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)"));

        CloseableHttpClient httpclient = HttpClientBuilder.create().setDefaultRequestConfig(config)
                .setDefaultHeaders(headers).build();

        HttpResponse response = null;

        // ??????HttpClient?
        if (getLoginId() != null && getLoginPassword() != null && getLoginInfo() != null) {

            // access top page.
            response = httpclient.execute(new HttpGet("http://" + getLoginInfo().getHostName()));
            EntityUtils.consume(response.getEntity());

            // login
            HttpPost httpost = new HttpPost(getLoginInfo().getPostUrl());
            List<NameValuePair> nvps = new ArrayList<NameValuePair>();
            // ID??????ID
            nvps.add(new BasicNameValuePair(getLoginInfo().getIdBoxName(), getLoginId()));
            // ??????
            nvps.add(new BasicNameValuePair(getLoginInfo().getPasswordBoxName(), getLoginPassword()));
            httpost.setEntity((HttpEntity) new UrlEncodedFormEntity(nvps, Charset.forName("UTF-8")));
            response = httpclient.execute(httpost);
            EntityUtils.consume(response.getEntity());
        }
        return httpclient;
    }

    @Override
    public URL getOriginalUrl() {

        // HTTP 30x???
        RequestConfig config = RequestConfig.custom().setRedirectsEnabled(false)
                .setConnectTimeout(DEFAULT_TIMEOUT_MILLISECONDS)
                .setConnectionRequestTimeout(DEFAULT_TIMEOUT_MILLISECONDS).build();

        // UserAgent?MSIE
        List<Header> headers = new ArrayList<Header>();
        headers.add(new BasicHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)"));

        HttpResponse responce = null;
        try (CloseableHttpClient httpclient = HttpClientBuilder.create().setDefaultRequestConfig(config)
                .setDefaultHeaders(headers).build()) {
            responce = httpclient.execute(new HttpHead(getUrl().toString()));

            int httpStatusCode = responce.getStatusLine().getStatusCode();

            if (httpStatusCode == HttpStatus.SC_MOVED_PERMANENTLY
                    || httpStatusCode == HttpStatus.SC_MOVED_TEMPORARILY) {
                // 301,302???Location??URL??
                Header h = responce.getFirstHeader("Location");
                if (!getUrl().toString().equals(h.getValue())) {
                    // URL????URL?
                    return new URL(h.getValue());
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 30x????????URL?
        return getUrl();
    }

}