示例#1
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);
    }
  }
示例#2
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);
    }
  }
示例#3
0
 public BatchInfo createBatchFromDir(JobInfo job, InputStream batchContent, File attachmentDir)
     throws AsyncApiException {
   final List<File> files = FileUtil.listFilesRecursive(attachmentDir, false);
   final Map<String, File> fileMap = new HashMap<String, File>(files.size());
   final String rootPath = attachmentDir.getAbsolutePath() + "/";
   for (File f : files) {
     String name = f.getAbsolutePath().replace(rootPath, "");
     fileMap.put(name, f);
   }
   return createBatchWithFileAttachments(job, batchContent, fileMap);
 }
示例#4
0
  protected File generate(String packageName, String fileName, Object gen, ST template, File dir)
      throws IOException {
    template.add("gen", gen);
    File source = new File(FileUtil.mkdirs(packageName, dir), fileName);
    PrintWriter out = null;
    out = new PrintWriter(new BufferedWriter(newSourceWriter(source), BUFFER_SIZE_16_KIB));
    try {
      out.print(template.render());
    } finally {
      out.close();
    }

    return source;
  }
示例#5
0
  @Override
  public InputStream getContent() throws IOException {
    InputStream in;

    try {
      successful = true;
      in = connection.getInputStream();
    } catch (IOException e) {
      successful = false;
      in = connection.getErrorStream();
      if (in == null) {
        throw e;
      }
    }

    String encoding = connection.getHeaderField("Content-Encoding");

    if (config.getMaxResponseSize() > 0) {
      in = new LimitingInputStream(config.getMaxResponseSize(), in);
    }

    if ("gzip".equals(encoding)) {
      in = new GZIPInputStream(in);
    }

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

      if (config.hasMessageHandlers()) {
        Iterator<MessageHandler> it = config.getMessagerHandlers();
        while (it.hasNext()) {
          MessageHandler handler = it.next();
          if (handler instanceof MessageHandlerWithHeaders) {
            ((MessageHandlerWithHeaders) handler)
                .handleResponse(url, bytes, connection.getHeaderFields());
          } else {
            handler.handleResponse(url, bytes);
          }
        }
      }

      if (config.isTraceMessage()) {
        new TeeInputStream(config, bytes);
      }
    }

    return in;
  }
  /**
   * Convert the specified input object into an output object of the specified type.
   *
   * @param type Data type to which this value should be converted
   * @param value The input value to be converted
   * @exception ConversionException if conversion cannot be performed successfully
   */
  @Override
  public Object convert(Class type, Object value) {

    if (value == null || String.valueOf(value).length() == 0) {
      return null;
    }

    try {
      final ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
      // just in case the file is not found we want to display the absolute file name to the user
      final String absolutePath = new File(String.valueOf(value.toString())).getAbsolutePath();
      FileUtil.copy(new FileInputStream(absolutePath), byteStream);
      return byteStream.toByteArray();
    } catch (Exception e) {
      if (useDefault) {
        return (defaultValue);
      } else {
        throw new ConversionException(e);
      }
    }
  }
示例#7
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;
  }
 /** @returns A list containing all files in dataloader source folders */
 private List<File> getSourceFiles() {
   final List<File> allFiles = new ArrayList<File>();
   allFiles.addAll(FileUtil.listFilesRecursive(getResource(DATALOADER_SRC), false));
   allFiles.addAll(FileUtil.listFilesRecursive(getResource(TEST_SRC), false));
   return allFiles;
 }