HttpURLConnectionBuilder.java 5.72 KB
package net.hockeyapp.android.utils;

import android.content.ContentResolver;
import android.content.Context;
import android.net.Uri;
import android.os.Build.VERSION;
import android.text.TextUtils;
import java.io.BufferedOutputStream;
import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class HttpURLConnectionBuilder
{
  public static final String DEFAULT_CHARSET = "UTF-8";
  private final String a;
  private String b;
  private String c;
  private SimpleMultipartEntity d;
  private int e = 120000;
  private final Map<String, String> f;
  
  public HttpURLConnectionBuilder(String paramString)
  {
    this.a = paramString;
    this.f = new HashMap();
  }
  
  public HttpURLConnection build()
    throws IOException
  {
    HttpURLConnection localHttpURLConnection = (HttpURLConnection)new URL(this.a).openConnection();
    localHttpURLConnection.setConnectTimeout(this.e);
    localHttpURLConnection.setReadTimeout(this.e);
    if (Build.VERSION.SDK_INT <= 9) {
      localHttpURLConnection.setRequestProperty("Connection", "close");
    }
    if (!TextUtils.isEmpty(this.b))
    {
      localHttpURLConnection.setRequestMethod(this.b);
      if ((!TextUtils.isEmpty(this.c)) || (this.b.equalsIgnoreCase("POST")) || (this.b.equalsIgnoreCase("PUT"))) {
        localHttpURLConnection.setDoOutput(true);
      }
    }
    Object localObject = this.f.keySet().iterator();
    while (((Iterator)localObject).hasNext())
    {
      String str = (String)((Iterator)localObject).next();
      localHttpURLConnection.setRequestProperty(str, (String)this.f.get(str));
    }
    if (!TextUtils.isEmpty(this.c))
    {
      localObject = new BufferedWriter(new OutputStreamWriter(localHttpURLConnection.getOutputStream(), "UTF-8"));
      ((BufferedWriter)localObject).write(this.c);
      ((BufferedWriter)localObject).flush();
      ((BufferedWriter)localObject).close();
    }
    if (this.d != null)
    {
      localHttpURLConnection.setRequestProperty("Content-Length", String.valueOf(this.d.getContentLength()));
      localObject = new BufferedOutputStream(localHttpURLConnection.getOutputStream());
      ((BufferedOutputStream)localObject).write(this.d.getOutputStream().toByteArray());
      ((BufferedOutputStream)localObject).flush();
      ((BufferedOutputStream)localObject).close();
    }
    return localHttpURLConnection;
  }
  
  public HttpURLConnectionBuilder setBasicAuthorization(String paramString1, String paramString2)
  {
    setHeader("Authorization", "Basic " + Base64.encodeToString(new StringBuilder().append(paramString1).append(":").append(paramString2).toString().getBytes(), 2));
    return this;
  }
  
  public HttpURLConnectionBuilder setHeader(String paramString1, String paramString2)
  {
    this.f.put(paramString1, paramString2);
    return this;
  }
  
  public HttpURLConnectionBuilder setRequestBody(String paramString)
  {
    this.c = paramString;
    return this;
  }
  
  public HttpURLConnectionBuilder setRequestMethod(String paramString)
  {
    this.b = paramString;
    return this;
  }
  
  public HttpURLConnectionBuilder setTimeout(int paramInt)
  {
    if (paramInt < 0) {
      throw new IllegalArgumentException("Timeout has to be positive.");
    }
    this.e = paramInt;
    return this;
  }
  
  public HttpURLConnectionBuilder writeFormFields(Map<String, String> paramMap)
  {
    try
    {
      ArrayList localArrayList = new ArrayList();
      Iterator localIterator = paramMap.keySet().iterator();
      while (localIterator.hasNext())
      {
        String str2 = (String)localIterator.next();
        String str1 = (String)paramMap.get(str2);
        str2 = URLEncoder.encode(str2, "UTF-8");
        str1 = URLEncoder.encode(str1, "UTF-8");
        localArrayList.add(str2 + "=" + str1);
      }
      paramMap = TextUtils.join("&", localArrayList);
    }
    catch (UnsupportedEncodingException paramMap)
    {
      throw new RuntimeException(paramMap);
    }
    setHeader("Content-Type", "application/x-www-form-urlencoded");
    setRequestBody(paramMap);
    return this;
  }
  
  public HttpURLConnectionBuilder writeMultipartData(Map<String, String> paramMap, Context paramContext, List<Uri> paramList)
  {
    Object localObject;
    try
    {
      this.d = new SimpleMultipartEntity();
      this.d.writeFirstBoundaryIfNeeds();
      localObject = paramMap.keySet().iterator();
      while (((Iterator)localObject).hasNext())
      {
        String str = (String)((Iterator)localObject).next();
        this.d.addPart(str, (String)paramMap.get(str));
      }
      i = 0;
    }
    catch (IOException paramMap)
    {
      throw new RuntimeException(paramMap);
    }
    int i;
    if (i < paramList.size())
    {
      localObject = (Uri)paramList.get(i);
      if (i != paramList.size() - 1) {
        break label225;
      }
    }
    label225:
    for (boolean bool = true;; bool = false)
    {
      paramMap = paramContext.getContentResolver().openInputStream((Uri)localObject);
      localObject = ((Uri)localObject).getLastPathSegment();
      this.d.addPart("attachment" + i, (String)localObject, paramMap, bool);
      i += 1;
      break;
      this.d.writeLastBoundaryIfNeeds();
      setHeader("Content-Type", "multipart/form-data; boundary=" + this.d.getBoundary());
      return this;
    }
  }
}


/* Location:              /home/merong/decompile/hackery-dex2jar.jar!/net/hockeyapp/android/utils/HttpURLConnectionBuilder.class
 * Java compiler version: 6 (50.0)
 * JD-Core Version:       0.7.1
 */