Example #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;
  }
Example #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);
    }
  }
Example #3
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;
  }
Example #4
0
  private InputStream doHttpGet(URL url) throws IOException, AsyncApiException {
    HttpURLConnection connection = JdkHttpTransport.createConnection(config, url, null);
    connection.setRequestProperty(SESSION_ID, config.getSessionId());

    boolean success = true;
    InputStream in;
    try {
      in = connection.getInputStream();
    } catch (IOException e) {
      success = false;
      in = connection.getErrorStream();
    }

    String encoding = connection.getHeaderField("Content-Encoding");
    if ("gzip".equals(encoding)) {
      in = new GZIPInputStream(in);
    }

    if (config.isTraceMessage() || config.hasMessageHandlers()) {
      byte[] bytes = FileUtil.toBytes(in);
      in = new ByteArrayInputStream(bytes);

      if (config.hasMessageHandlers()) {
        for (MessageHandler handler : config.getMessagerHandlers()) {
          if (handler instanceof MessageHandlerWithHeaders) {
            ((MessageHandlerWithHeaders) handler).handleRequest(url, new byte[0], null);
            ((MessageHandlerWithHeaders) handler)
                .handleResponse(url, bytes, connection.getHeaderFields());
          } else {
            handler.handleRequest(url, new byte[0]);
            handler.handleResponse(url, bytes);
          }
        }
      }

      if (config.isTraceMessage()) {
        config.getTraceStream().println(url.toExternalForm());

        Map<String, List<String>> headers = connection.getHeaderFields();
        for (Map.Entry<String, List<String>> entry : headers.entrySet()) {
          StringBuffer sb = new StringBuffer();
          List<String> values = entry.getValue();

          if (values != null) {
            for (String v : values) {
              sb.append(v);
            }
          }

          config.getTraceStream().println(entry.getKey() + ": " + sb.toString());
        }

        new XmlTraceHelper(config.getTraceStream(), config.isPrettyPrintXml()).trace(bytes);
      }
    }

    if (!success) {
      parseAndThrowException(in);
    }

    return in;
  }