All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.julienlavergne.core.helpers.URLHelper Maven / Gradle / Ivy

The newest version!
package com.julienlavergne.core.helpers;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.joran.JoranConfigurator;
import ch.qos.logback.core.joran.spi.JoranException;
import ch.qos.logback.core.util.Loader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import org.slf4j.MDC;

import static com.google.common.base.Preconditions.checkNotNull;

/**
 * Created with IntelliJ IDEA.
 * com.julienlavergne.core.User: julien
 * Date: 5/26/13
 * Time: 18:41
 * To change this template use File | Settings | File Templates.
 */
public class URLHelper {


    static LoggerContext loggerContext;
    static Logger logger;



    static{

        loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();

        JoranConfigurator configurator = new JoranConfigurator();
        configurator.setContext(loggerContext);
        loggerContext.reset();
        URL url = Loader.getResourceBySelfClassLoader("logback.xml");
        try {
            configurator.doConfigure(url);
        } catch (JoranException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }

        logger = loggerContext.getLogger(URLHelper.class);


    }

        public static String getLongUrl(String fromUrl)  {
            String result = checkNotNull(fromUrl);
            String header;
            try {
                do {
                    URL toUrl = new URL(result);
                    HttpURLConnection.setFollowRedirects(false);
                    URLConnection conn = toUrl.openConnection();
                    header = conn.getHeaderField(null);
                    String location = conn.getHeaderField("location");
                    if (location != null) {
                        result = location;
                    }
                } while (header.contains("301"));
            }
            catch(Exception e)
            {
                logger.debug("error in redirect url get",e);
            }
            finally {
                return result;
            }
        }


}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy