Exemplo n.º 1
0
  private JobInfo createOrUpdateJob(JobInfo job, String endpoint) throws AsyncApiException {
    try {
      Transport transport = config.createTransport();
      OutputStream out = transport.connect(endpoint, getHeaders(XML_CONTENT_TYPE), true);
      XmlOutputStream xout = new AsyncXmlOutputStream(out, true);
      job.write(JOB_QNAME, xout, typeMapper);
      xout.close();

      InputStream in = transport.getContent();

      if (transport.isSuccessful()) {
        XmlInputStream xin = new XmlInputStream();
        xin.setInput(in, "UTF-8");
        JobInfo result = new JobInfo();
        result.load(xin, typeMapper);
        return result;
      } else {
        parseAndThrowException(in);
      }
    } catch (PullParserException e) {
      throw new AsyncApiException("Failed to create job ", AsyncExceptionCode.ClientInputError, e);
    } catch (IOException e) {
      throw new AsyncApiException("Failed to create job ", AsyncExceptionCode.ClientInputError, e);
    } catch (ConnectionException e) {
      throw new AsyncApiException("Failed to create job ", AsyncExceptionCode.ClientInputError, e);
    }
    return null;
  }
Exemplo n.º 2
0
  private BatchInfo createBatchFromStreamImpl(JobInfo jobInfo, InputStream input, boolean isZip)
      throws AsyncApiException {
    try {
      String endpoint = getRestEndpoint();
      Transport transport = config.createTransport();
      endpoint = endpoint + "job/" + jobInfo.getId() + "/batch";
      String contentType = getContentTypeString(jobInfo.getContentType(), isZip);
      HashMap<String, String> httpHeaders = getHeaders(contentType);
      // TODO do we want to allow the zip content to be gzipped
      boolean allowZipToBeGzipped = false;
      OutputStream out = transport.connect(endpoint, httpHeaders, allowZipToBeGzipped || !isZip);

      FileUtil.copy(input, out);

      InputStream result = transport.getContent();
      if (!transport.isSuccessful()) parseAndThrowException(result);
      return BatchRequest.loadBatchInfo(result);
    } catch (IOException e) {
      throw new AsyncApiException("Failed to create batch", AsyncExceptionCode.ClientInputError, e);
    } catch (PullParserException e) {
      throw new AsyncApiException("Failed to create batch", AsyncExceptionCode.ClientInputError, e);
    } catch (ConnectionException e) {
      throw new AsyncApiException("Failed to create batch", AsyncExceptionCode.ClientInputError, e);
    }
  }
Exemplo n.º 3
0
  /**
   * @param jobInfo Parent job for new batch.
   * @param batchContent InputStream containing the xml or csv content of the batch, or null only if
   *     request.txt is contained in attachments map
   * @param attachments Map of attachments where the key is the filename to be used in the zip file
   *     and the value is the InputStream representing that file.
   * @return BatchInfo of uploaded batch.
   */
  public BatchInfo createBatchWithInputStreamAttachments(
      JobInfo jobInfo, InputStream batchContent, Map<String, InputStream> attachments)
      throws AsyncApiException {

    if (batchContent != null && attachments.get("request.txt") != null)
      throw new AsyncApiException(
          "Request content cannot be included as both input stream and attachment",
          AsyncExceptionCode.ClientInputError);
    try {
      String endpoint = getRestEndpoint();
      endpoint = endpoint + "job/" + jobInfo.getId() + "/batch";
      Transport transport = config.createTransport();
      ZipOutputStream zipOut =
          new ZipOutputStream(
              transport.connect(
                  endpoint,
                  getHeaders(getContentTypeString(jobInfo.getContentType(), true)),
                  false));

      try {
        if (batchContent != null) {
          zipOut.putNextEntry(new ZipEntry("request.txt"));
          FileUtil.copy(batchContent, zipOut, false);
        }
        for (Map.Entry<String, InputStream> entry : attachments.entrySet()) {
          zipOut.putNextEntry(new ZipEntry(entry.getKey()));
          FileUtil.copy(entry.getValue(), zipOut, false);
        }
      } finally {
        zipOut.close();
      }

      InputStream result = transport.getContent();
      return BatchRequest.loadBatchInfo(result);
    } catch (IOException e) {
      throw new AsyncApiException("Failed to create batch", AsyncExceptionCode.ClientInputError, e);
    } catch (PullParserException e) {
      throw new AsyncApiException("Failed to create batch", AsyncExceptionCode.ClientInputError, e);
    } catch (ConnectionException e) {
      throw new AsyncApiException("Failed to create batch", AsyncExceptionCode.ClientInputError, e);
    }
  }
Exemplo n.º 4
0
  public BatchRequest createBatch(JobInfo job) throws AsyncApiException {
    try {
      String endpoint = getRestEndpoint();
      Transport transport = config.createTransport();
      endpoint = endpoint + "job/" + job.getId() + "/batch";
      ContentType ct = job.getContentType();
      if (ct != null && ct != ContentType.XML) {
        throw new AsyncApiException(
            "This method can only be used with xml content type",
            AsyncExceptionCode.ClientInputError);
      }

      OutputStream out = transport.connect(endpoint, getHeaders(XML_CONTENT_TYPE), true);
      return new BatchRequest(transport, out);
    } catch (IOException e) {
      throw new AsyncApiException("Failed to create batch", AsyncExceptionCode.ClientInputError, e);
    } catch (ConnectionException x) {
      throw new AsyncApiException("Failed to create batch", AsyncExceptionCode.ClientInputError, x);
    }
  }
Exemplo n.º 5
0
  public BatchInfo completeRequest() throws AsyncApiException {
    try {
      xmlStream.writeEndTag(BulkConnection.NAMESPACE, "sObjects");
      xmlStream.endDocument();
      xmlStream.close();
      InputStream in = transport.getContent();

      if (transport.isSuccessful()) {
        return loadBatchInfo(in);
      } else {
        BulkConnection.parseAndThrowException(in);
      }
    } catch (IOException e) {
      throw new AsyncApiException(
          "Failed to complete request", AsyncExceptionCode.ClientInputError, e);
    } catch (PullParserException e) {
      throw new AsyncApiException(
          "Failed to complete request", AsyncExceptionCode.ClientInputError, e);
    } catch (ConnectionException e) {
      throw new AsyncApiException(
          "Failed to complete request", AsyncExceptionCode.ClientInputError, e);
    }
    return null;
  }