/*
 * Copyright 2014 Google Inc. All rights reserved.
 *
 * 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 org.physical_web.physicalweb;

import android.os.AsyncTask;
import com.google.api.client.extensions.android.http.AndroidHttp;
import com.google.api.client.extensions.android.json.AndroidJsonFactory;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.services.urlshortener.Urlshortener;
import com.google.api.services.urlshortener.UrlshortenerRequestInitializer;
import com.google.api.services.urlshortener.model.Url;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.ExecutionException;

/**
 * This class shortens urls and also expands those short urls
 * to their original url.
 * Currently this class only supports google url shortener
 * TODO: support other url shorteners
 */
class UrlShortener {

  private static final String TAG = "UrlShortener";

  /**
   * Create the shortened form
   * of the given url.
   *
   * @param longUrl The url that will be shortened
   * @return The short url for the given longUrl
   */
  public static String shortenUrl(String longUrl) {
    String shortUrl = null;
    try {
      shortUrl = (String) new ShortenUrlTask().execute(longUrl).get();
    } catch (InterruptedException | ExecutionException e) {
      e.printStackTrace();
    }
    return shortUrl;
  }

  /**
   * Create a google url shortener interface object
   * and make a request to shorten the given url
   */
  private static class ShortenUrlTask extends AsyncTask {
    @Override
    protected String doInBackground(Object[] params) {
      String longUrl = (String) params[0];
      Urlshortener urlshortener = createGoogleUrlShortener();
      Url url = new Url();
      url.setLongUrl(longUrl);
      try {
        Url response = urlshortener.url().insert(url).execute();
        return response.getId();
      } catch (IOException e) {
        e.printStackTrace();
      }
      return null;
    }
  }

  /**
   * Create an instance of the google url shortener object
   * and return it.
   *
   * @return The created shortener object
   */
  private static Urlshortener createGoogleUrlShortener() {
    HttpTransport httpTransport = AndroidHttp.newCompatibleTransport();
    JsonFactory jsonFactory = AndroidJsonFactory.getDefaultInstance();
    UrlshortenerRequestInitializer urlshortenerRequestInitializer = new UrlshortenerRequestInitializer();
    return new Urlshortener.Builder(httpTransport, jsonFactory, null)
        .setApplicationName("PhysicalWeb")
        .setUrlshortenerRequestInitializer(urlshortenerRequestInitializer)
        .build();
  }

  /**
   * Check if the given url is a short url.
   *
   * @param url The url that will be tested to see if it is short
   * @return The value that indicates if the given url is short
   */
  public static boolean isShortUrl(String url) {
    return url.startsWith("http://goo.gl/") || url.startsWith("https://goo.gl/");
  }

  /**
   * Takes any short url and converts it to the long url that is being pointed to.
   * Note: this method will work for all types of shortened urls as it inspect the
   * returned headers for the location.
   *
   * @param shortUrl The short url that will be lengthened
   * @return The lengthened url for the given short url
   */
  public static String lengthenShortUrl(String shortUrl) {
    String longUrl = null;
    try {
      longUrl = (String) new LengthenShortUrlTask().execute(shortUrl).get();
    } catch (InterruptedException | ExecutionException e) {
      e.printStackTrace();
    }
    return longUrl;
  }

  private static class LengthenShortUrlTask extends AsyncTask {
    @Override
    protected String doInBackground(Object[] params) {
      String shortUrl = (String) params[0];
      String longUrl;
      URL url = null;
      try {
        url = new URL(shortUrl);
      } catch (MalformedURLException e) {
        e.printStackTrace();
      }
      HttpURLConnection httpURLConnection = null;
      try {
        httpURLConnection = (HttpURLConnection) url.openConnection();
      } catch (IOException e) {
        e.printStackTrace();
      }
      httpURLConnection.setInstanceFollowRedirects(false);
      longUrl = httpURLConnection.getHeaderField("location");
      if (longUrl == null) {
        longUrl = shortUrl;
      }
      return longUrl;
    }
  }
}

@@ -1135,103 +1135,8 @@
 on;%0A
-import java.net.HttpURLConnection;%0Aimport java.net.MalformedURLException;%0Aimport java.net.URL;%0A
 impo
@@ -1179,16 +1179,16 @@
 eption;%0A
+
 %0A/**%0A * 
@@ -1399,61 +1399,8 @@
  %7B%0A%0A
-  private static final String TAG = %22UrlShortener%22;%0A%0A
   /*
@@ -1668,33 +1668,24 @@
   shortUrl =
- (String)
  new Shorten
@@ -1997,32 +1997,54 @@
 xtends AsyncTask
+%3CObject, Void, String%3E
  %7B%0A    @Override
@@ -2335,24 +2335,68 @@
 .execute();%0A
+        if(response!=null) %7B//avoid NPE%0A    
         retu
@@ -2412,24 +2412,34 @@
 se.getId();%0A
+        %7D%0A
       %7D catc
@@ -3186,1726 +3186,6 @@
  %7D%0A%0A
-  /**%0A   * Check if the given url is a short url.%0A   *%0A   * @param url The url that will be tested to see if it is short%0A   * @return The value that indicates if the given url is short%0A   */%0A  public static boolean isShortUrl(String url) %7B%0A    return url.startsWith(%22http://goo.gl/%22) %7C%7C url.startsWith(%22https://goo.gl/%22);%0A  %7D%0A%0A  /**%0A   * Takes any short url and converts it to the long url that is being pointed to.%0A   * Note: this method will work for all types of shortened urls as it inspect the%0A   * returned headers for the location.%0A   *%0A   * @param shortUrl The short url that will be lengthened%0A   * @return The lengthened url for the given short url%0A   */%0A  public static String lengthenShortUrl(String shortUrl) %7B%0A    String longUrl = null;%0A    try %7B%0A      longUrl = (String) new LengthenShortUrlTask().execute(shortUrl).get();%0A    %7D catch (InterruptedException %7C ExecutionException e) %7B%0A      e.printStackTrace();%0A    %7D%0A    return longUrl;%0A  %7D%0A%0A  private static class LengthenShortUrlTask extends AsyncTask %7B%0A    @Override%0A    protected String doInBackground(Object%5B%5D params) %7B%0A      String shortUrl = (String) params%5B0%5D;%0A      String longUrl;%0A      URL url = null;%0A      try %7B%0A        url = new URL(shortUrl);%0A      %7D catch (MalformedURLException e) %7B%0A        e.printStackTrace();%0A      %7D%0A      HttpURLConnection httpURLConnection = null;%0A      try %7B%0A        httpURLConnection = (HttpURLConnection) url.openConnection();%0A      %7D catch (IOException e) %7B%0A        e.printStackTrace();%0A      %7D%0A      httpURLConnection.setInstanceFollowRedirects(false);%0A      longUrl = httpURLConnection.getHeaderField(%22location%22);%0A      if (longUrl == null) %7B%0A        longUrl = shortUrl;%0A      %7D%0A      return longUrl;%0A    %7D%0A  %7D%0A
 %7D%0A

Fix inspection results discussed in #67 - UrlShortener.java