/**
   * Sends a specific <tt>Request</tt> to the STUN server associated with this
   * <tt>StunCandidateHarvest</tt>.
   *
   * @param request the <tt>Request</tt> to send to the STUN server associated with this
   *     <tt>StunCandidateHarvest</tt>
   * @param firstRequest <tt>true</tt> if the specified <tt>request</tt> should be sent as the first
   *     request in the terms of STUN; otherwise, <tt>false</tt>
   * @return the <tt>TransactionID</tt> of the STUN client transaction through which the specified
   *     <tt>Request</tt> has been sent to the STUN server associated with this
   *     <tt>StunCandidateHarvest</tt>
   * @param transactionID the <tt>TransactionID</tt> of <tt>request</tt> because <tt>request</tt>
   *     only has it as a <tt>byte</tt> array and <tt>TransactionID</tt> is required for the
   *     <tt>applicationData</tt> property value
   * @throws StunException if anything goes wrong while sending the specified <tt>Request</tt> to
   *     the STUN server associated with this <tt>StunCandidateHarvest</tt>
   */
  protected TransactionID sendRequest(
      Request request, boolean firstRequest, TransactionID transactionID) throws StunException {
    if (!firstRequest && (longTermCredentialSession != null))
      longTermCredentialSession.addAttributes(request);

    StunStack stunStack = harvester.getStunStack();
    TransportAddress stunServer = harvester.stunServer;
    TransportAddress hostCandidateTransportAddress = hostCandidate.getTransportAddress();

    if (transactionID == null) {
      byte[] transactionIDAsBytes = request.getTransactionID();

      transactionID =
          (transactionIDAsBytes == null)
              ? TransactionID.createNewTransactionID()
              : TransactionID.createTransactionID(harvester.getStunStack(), transactionIDAsBytes);
    }
    synchronized (requests) {
      try {
        transactionID =
            stunStack.sendRequest(
                request, stunServer, hostCandidateTransportAddress, this, transactionID);
      } catch (IllegalArgumentException iaex) {
        if (logger.isLoggable(Level.INFO)) {
          logger.log(
              Level.INFO,
              "Failed to send "
                  + request
                  + " through "
                  + hostCandidateTransportAddress
                  + " to "
                  + stunServer,
              iaex);
        }
        throw new StunException(StunException.ILLEGAL_ARGUMENT, iaex.getMessage(), iaex);
      } catch (IOException ioex) {
        if (logger.isLoggable(Level.INFO)) {
          logger.log(
              Level.INFO,
              "Failed to send "
                  + request
                  + " through "
                  + hostCandidateTransportAddress
                  + " to "
                  + stunServer,
              ioex);
        }
        throw new StunException(StunException.NETWORK_ERROR, ioex.getMessage(), ioex);
      }

      requests.put(transactionID, request);
    }
    return transactionID;
  }
    @Override
    protected String[] doInBackground(String... parms) {
      String[] resultarr = new String[1];
      String intentstr = parms[0];

      // int count = urls.length;
      // long totalSize = 0; for (int i = 0; i < count; i++) { totalSize +=
      // Downloader.downloadFile(urls[i]);
      // publishProgress((int) ((i / (float) count) * 100));
      // Escape early if cancel() is called
      // if (isCancelled())
      //		break;

      try {
        // Connect to API and authenticate
        publishProgress("Connecting and authenticating API session...");

        ApiWrapper wrapper;
        // wrapper = Api.wrapper;
        wrapper = new ApiWrapper(Api.mClientID, Api.mClientSecret, null, null);
        wrapper.login("un1tz3r0", "Farscap3");

        publishProgress("Resolving url...");

        String resolvedurl = resolveURL(wrapper, intentstr);

        publishProgress("Getting metadata...");

        HttpResponse resp = wrapper.get(Request.to(resolvedurl));

        JSONObject jso = Http.getJSON(resp);
        // resultstr = jso.toString();

        if (jso.getString("kind").equals("track")) {
          if (jso.getBoolean("downloadable")) {
            publishProgress("Getting download redirect URL...");

            String dlrurl =
                wrapper
                    .getURI(
                        Request.to(jso.getString("download_url")).add("allow_redirect", false),
                        false,
                        false)
                    .toString();
            HttpResponse dlrresp = wrapper.get(Request.to(dlrurl));
            String dlstr = dlrurl;
            if (dlrresp.getStatusLine().getStatusCode() == HttpStatus.SC_MOVED_TEMPORARILY) {
              Header dlloch = dlrresp.getFirstHeader("location");
              if ((dlloch == null) || (dlloch.getValue() == null))
                throw new RuntimeException("Download url HEAD response has no location header");

              dlstr = wrapper.getURI(Request.to(dlloch.getValue()), false, false).toString();
            } else if (dlrresp.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
              dlstr = dlrurl;
            } else {
              throw new RuntimeException(
                  "Download url HEAD response has wrong status ("
                      + String.valueOf(dlrresp.getStatusLine().getStatusCode())
                      + " "
                      + dlrresp.getStatusLine().getReasonPhrase()
                      + ")");
            }
            // String dlstr = Request.to( dlloch.getValue() ).add("CLIENT_ID",
            // Api.mClientID).toString();

            //												if(dlresp2.getStatusLine().getStatusCode() !=
            // HttpStatus.SC_MOVED_TEMPORARILY)
            //														throw new RuntimeException("Download redirect url HEAD response has
            // wrong status: " + dlresp2.getStatusLine().toString());
            //												Header dlloc2 = dlresp2.getFirstHeader("location");
            //												if((dlloc2 == null) || (dlloc2.getValue() == null))
            //														throw new RuntimeException("Download redirect url HEAD response has no
            // location header");
            //

            resultarr = new String[2];
            resultarr[1] =
                jso.getString("title").replaceAll("[^A-Za-z0-9 -]*", "")
                    + "."
                    + jso.getString("original_format");
            resultarr[0] = dlstr;
          } else {
            Stream st = wrapper.resolveStreamUrl(jso.getString("stream_url"), true);
            resultarr = new String[2];
            resultarr[1] = jso.getString("title").replaceAll("[^A-Za-z0-9 -]*", "").concat(".mp3");
            resultarr[0] = st.streamUrl;
          }
        }
      } catch (JSONException e) {
        resultarr = new String[1];
        resultarr[0] = e.toString();
      } catch (IOException e) {
        resultarr = new String[1];
        resultarr[0] = e.toString();
      } catch (Exception e) {
        resultarr = new String[1];
        resultarr[0] = e.toString();
      }

      return resultarr;
    }