net.uytrewq.jogp.impl.ClientImpl.java Source code

Java tutorial

Introduction

Here is the source code for net.uytrewq.jogp.impl.ClientImpl.java

Source

/**
 *    Copyright 2011 Masanori Matsumoto
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 */
package net.uytrewq.jogp.impl;

import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.net.URL;
import java.net.URLConnection;

import net.uytrewq.jogp.HtmlException;
import net.uytrewq.jogp.OgpClient;
import net.uytrewq.jogp.OgpValues;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

/**
 * Implementation of the {@link OgpClient}
 */
public class ClientImpl implements OgpClient {
    private final Logger logger = LoggerFactory.getLogger(getClass());
    private HttpClient http;

    public ClientImpl() {
        this.http = new DefaultHttpClient();
    }

    private HttpEntity getEntity(String url) throws ClientProtocolException, IOException {
        HttpGet get = new HttpGet(url);
        HttpResponse resp = http.execute(get);
        return resp.getEntity();
    }

    private OgpValues parse(String url, InputSource source) throws IOException, HtmlException {
        try {
            OgpSaxParser parser = new OgpSaxParser(url);
            parser.setProperty("http://cyberneko.org/html/properties/default-encoding", "UTF-8");
            parser.parse(source);
            return new OgpValues(parser.getOgnsPrefix(), parser.getOgValues(), parser.getHtmlTitle(),
                    parser.getImages());
        } catch (SAXException e) {
            throw new HtmlException(e);
        }
    }

    public OgpValues parseURL(String url) throws IOException, HtmlException {
        HttpEntity entity = null;
        try {
            entity = getEntity(url);
            return parse(url, new InputSource(entity.getContent()));
        } catch (ClientProtocolException e) {
            throw new IOException(e);
        } finally {
            try {
                EntityUtils.consume(entity);
            } catch (IOException e) {
                // ignore
                logger.error("failed to consume entity", e);
            }
        }
    }

    public OgpValues parseURL(URL url) throws IOException, HtmlException {
        URLConnection conn = null;
        InputStream is = null;
        try {
            conn = url.openConnection();
            is = conn.getInputStream();
            return parse("", new InputSource(is));
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                // ignore
                logger.error("failed to close input stream", e);
            }
        }
    }

    public OgpValues parseText(String html) throws HtmlException {
        try {
            return parse("", new InputSource(new StringReader(html)));
        } catch (IOException e) {
            // IO exception should not be thrown.
            logger.error("string input source should not throw io exception", e);
            throw new AssertionError(e);
        }
    }
}