/**
 * 
 */
package org.lodder.subtools.sublibrary.util.http;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import org.lodder.subtools.sublibrary.logging.Logger;

import com.dropbox.core.DbxClient;
import com.dropbox.core.DbxException;
import com.dropbox.core.DbxRequestConfig;
import com.dropbox.core.DbxWriteMode;

/**
 * @author lodder
 * 
 */
public class DropBoxClient {

  private static DropBoxClient dbc;
  private DbxClient dbxClient;
  private String locationOffset = "/Ondertitels/PrivateRepo/";
  private String unSortedLocationOffset = "/Ondertitels/Unsorted/";

  DropBoxClient() {
    dropboxInit();
  }

  public static DropBoxClient getDropBoxClient() {
    if (dbc == null) dbc = new DropBoxClient();
    return dbc;
  }

  private void dropboxInit() {
    final String accessToken = "3x5qOT-XdxgAAAAAAAAAAVa2Hrj23e7EiO98AZqw-UqGEr7I4lJG6eL8M1s9LlG0";

    DbxRequestConfig config =
        new DbxRequestConfig("PersonalDownload/1.0", java.util.Locale.getDefault().toString());

    dbxClient = new DbxClient(config, accessToken);
  }

  public boolean doDownloadFile(String location, File output) {
    boolean success = false;
    try (FileOutputStream outputStream = new FileOutputStream(output)){
      dbxClient.getFile(locationOffset + location, null, outputStream);
      success = true;
      outputStream.close();
    } catch (DbxException e) {
      Logger.instance.error(Logger.stack2String(e));
    } catch (IOException e) {
      Logger.instance.error(Logger.stack2String(e));
    }
    return success;
  }

  public void put(File inputFile, String filename, String languageCode) {
    FileInputStream inputStream = null;
    try {
      inputStream = new FileInputStream(inputFile);
      dbxClient.uploadFile(unSortedLocationOffset + "/" + languageCode + "/" + filename,
          DbxWriteMode.add(), inputFile.length(), inputStream);
    } catch (DbxException e) {
      Logger.instance.error(Logger.stack2String(e));
    } catch (IOException e) {
      Logger.instance.error(Logger.stack2String(e));
    } finally {
      if (inputStream != null) {
        try {
          inputStream.close();
        } catch (IOException e) {
          Logger.instance.error(Logger.stack2String(e));
        }
      }
    }
  }

  /**
   * @param location
   * @return
   */
  public String getFile(String location) {
    String content = "";
    try {
      ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
      dbxClient.getFile(location, null, outputStream);
      content = outputStream.toString("UTF-8");
      outputStream.close();
    } catch (DbxException e) {
      Logger.instance.error(Logger.stack2String(e));
    } catch (IOException e) {
      Logger.instance.error(Logger.stack2String(e));
    }
    return content;
  }

}

@@ -235,49 +235,53 @@
 org.
-lodder.subtools.sublibrary.logging.Logger
+slf4j.Logger;%0D%0Aimport org.slf4j.LoggerFactory
 ;%0D%0A%0D
@@ -710,16 +710,104 @@
 d/%22;%0D%0A%0D%0A
+  private static final Logger LOGGER = LoggerFactory.getLogger(DropBoxClient.class);%0D%0A%0D%0A
   DropBo
@@ -1460,16 +1460,17 @@
 output))
+ 
 %7B%0D%0A     
@@ -1619,149 +1619,66 @@
 ion 
-e) %7B%0D%0A      Logger.instance.error(Logger.stack2String(e));%0D%0A    %7D catch (IOException e) %7B%0D%0A      Logger.instance.error(Logger.stack2String(e)
+%7C IOException e) %7B%0D%0A      LOGGER.error(%22doDownloadFile%22, e
 );%0D%0A
@@ -2077,149 +2077,55 @@
 ion 
-e) %7B%0D%0A      Logger.instance.error(Logger.stack2String(e));%0D%0A    %7D catch (IOException e) %7B%0D%0A      Logger.instance.error(Logger.stack2String(e)
+%7C IOException e) %7B%0D%0A      LOGGER.error(%22put%22, e
 );%0D%0A
@@ -2272,51 +2272,46 @@
    L
-ogger.instance.error(Logger.stack2String(e)
+OGGER.error(%22put close inputStream%22, e
 );%0D%0A
@@ -2712,149 +2712,59 @@
 ion 
-e) %7B%0D%0A      Logger.instance.error(Logger.stack2String(e));%0D%0A    %7D catch (IOException e) %7B%0D%0A      Logger.instance.error(Logger.stack2String(e)
+%7C IOException e) %7B%0D%0A      LOGGER.error(%22getFile%22, e
 );%0D%0A

Convert DropBoxClient from old logger to new LOGGER slf4j