void buildFilePart(final KeyDataPair pairWithFile, final MultipartEntityBuilder builder) { String mimeType = pairWithFile.getMimeType(); if (mimeType == null) { mimeType = "application/octet-stream"; final File file = pairWithFile.getFile(); if (pairWithFile.getData() != null) { final String filename; if (file == null) { filename = pairWithFile.getValue(); else if (pairWithFile.getFileName() != null) { filename = pairWithFile.getFileName(); builder.addBinaryBody(pairWithFile.getName(), new ByteArrayInputStream(pairWithFile.getData()), contentType, filename); return; builder.addPart(pairWithFile.getName(), new InputStreamBody(new ByteArrayInputStream(new byte[0]), contentType, pairWithFile.getValue()) { @Override public long getContentLength() { if (pairWithFile.getFile() == null) { filename = pairWithFile.getValue(); else if (pairWithFile.getFileName() != null) { filename = pairWithFile.getFileName();
private static Charset getCharset(final Charset charset, final List<NameValuePair> pairs) { for (final NameValuePair pair : pairs) { if (pair instanceof KeyDataPair) { final KeyDataPair pairWithFile = (KeyDataPair) pair; if (pairWithFile.getData() == null && pairWithFile.getFile() != null) { final String fileName = pairWithFile.getFile().getName(); for (int i = 0; i < fileName.length(); i++) { if (fileName.codePointAt(i) > 127) { return charset; } } } } } return null; }
/** * {@inheritDoc} */ @Override public NameValuePair[] getSubmitNameValuePairs() { if (files_ == null || files_.length == 0) { return new NameValuePair[] {new KeyDataPair(getNameAttribute(), null, null, null, (Charset) null)}; } final List<NameValuePair> list = new ArrayList<>(); for (File file : files_) { String contentType; if (contentType_ == null) { contentType = getPage().getWebClient().getBrowserVersion().getUploadMimeType(file); if (StringUtils.isEmpty(contentType)) { contentType = "application/octet-stream"; } } else { contentType = contentType_; } final Charset charset = getPage().getCharset(); final KeyDataPair keyDataPair = new KeyDataPair(getNameAttribute(), file, null, contentType, charset); keyDataPair.setData(data_); list.add(keyDataPair); } return list.toArray(new NameValuePair[list.size()]); }
new KeyDataPair(name, file.getFile(), fileName, file.getType(), (Charset) null));
private static Charset getCharset(final Charset charset, final List<NameValuePair> pairs) { for (final NameValuePair pair : pairs) { if (pair instanceof KeyDataPair) { final KeyDataPair pairWithFile = (KeyDataPair) pair; if (pairWithFile.getData() == null && pairWithFile.getFile() != null) { final String fileName = pairWithFile.getFile().getName(); for (int i = 0; i < fileName.length(); i++) { if (fileName.codePointAt(i) > 127) { return charset; } } } } } return null; }
/** * {@inheritDoc} */ @Override public NameValuePair[] getSubmitNameValuePairs() { if (files_ == null || files_.length == 0) { return new NameValuePair[] {new KeyDataPair(getNameAttribute(), null, null, null, (Charset) null)}; } final List<NameValuePair> list = new ArrayList<>(); for (File file : files_) { String contentType; if (contentType_ == null) { contentType = getPage().getWebClient().getBrowserVersion().getUploadMimeType(file); if (StringUtils.isEmpty(contentType)) { contentType = "application/octet-stream"; } } else { contentType = contentType_; } final Charset charset = getPage().getCharset(); final KeyDataPair keyDataPair = new KeyDataPair(getNameAttribute(), file, null, contentType, charset); keyDataPair.setData(data_); list.add(keyDataPair); } return list.toArray(new NameValuePair[0]); }
/** * Appends a new value onto an existing key inside a {@code FormData} object, * or adds the key if it does not already exist. * @param name the name of the field whose data is contained in {@code value} * @param value the field's value * @param filename the filename reported to the server (optional) */ @JsxFunction public void append(final String name, final Object value, final Object filename) { if (value instanceof File) { final File file = (File) value; String fileName = null; String contentType = null; if (filename instanceof String) { fileName = (String) filename; } contentType = file.getType(); if (StringUtils.isEmpty(contentType)) { contentType = "application/octet-stream"; } requestParameters_.add(new KeyDataPair(name, file.getFile(), fileName, contentType, (Charset) null)); } else { requestParameters_.add(new NameValuePair(name, Context.toString(value))); } }
void buildFilePart(final KeyDataPair pairWithFile, final MultipartEntityBuilder builder) { String mimeType = pairWithFile.getMimeType(); if (mimeType == null) { mimeType = "application/octet-stream"; final File file = pairWithFile.getFile(); if (pairWithFile.getData() != null) { final String filename; if (file == null) { filename = pairWithFile.getValue(); else if (pairWithFile.getFileName() != null) { filename = pairWithFile.getFileName(); builder.addBinaryBody(pairWithFile.getName(), new ByteArrayInputStream(pairWithFile.getData()), contentType, filename); return; builder.addPart(pairWithFile.getName(), new InputStreamBody(new ByteArrayInputStream(new byte[0]), contentType, pairWithFile.getValue()) { @Override public long getContentLength() { if (pairWithFile.getFile() == null) { filename = pairWithFile.getValue(); else if (pairWithFile.getFileName() != null) { filename = pairWithFile.getFileName();
new KeyDataPair(name, file.getFile(), fileName, file.getType(), (Charset) null));
/** * Appends a new value onto an existing key inside a {@code FormData} object, * or adds the key if it does not already exist. * @param name the name of the field whose data is contained in {@code value} * @param value the field's value * @param filename the filename reported to the server (optional) */ @JsxFunction public void append(final String name, final Object value, final Object filename) { if (value instanceof File) { final File file = (File) value; String fileName = null; String contentType = null; if (filename instanceof String) { fileName = (String) filename; } contentType = file.getType(); if (StringUtils.isEmpty(contentType)) { contentType = "application/octet-stream"; } requestParameters_.add(new KeyDataPair(name, file.getFile(), fileName, contentType, (Charset) null)); } else { requestParameters_.add(new NameValuePair(name, Context.toString(value))); } }