private long calcMultipartFormDataBodyLength(final MultipartFormData<?> multipartFormData) { final String boundaryToContentTypeStart = MultipartFormatter.boundaryToContentType(""); final String boundary = getHeaders().get(HeaderNames.CONTENT_TYPE) .filter(ct -> ct.startsWith(boundaryToContentTypeStart)) .map(ct -> "\r\n--" + ct.substring(boundaryToContentTypeStart.length())) .orElseThrow(() -> new RuntimeException(("Content-Type header starting with \"" + boundaryToContentTypeStart + "\" needs to be present"))); long dataSizeSum = multipartFormData.asFormUrlEncoded().entrySet().stream().mapToLong(dataPart -> Arrays.stream(dataPart.getValue()).mapToLong(value -> partLength(boundary, "form-data", dataPart.getKey() + (dataPart.getValue().length > 1 ? "[]" : ""), null, null, value) ).sum()).sum(); long fileHeadersSizeSum = multipartFormData.getFiles().stream() .mapToLong(filePart -> // Pass empty body because we add the file size sum later instead anyway (see next assignment below) partLength(boundary, filePart.getDispositionType(), filePart.getKey(), filePart.getFilename(), filePart.getContentType(), "") ).sum(); long fileSizeSum = multipartFormData.getFiles().stream().mapToLong(filePart -> filePart.getFileSize()).sum(); long length = dataSizeSum + fileHeadersSizeSum + fileSizeSum; if(length > 0) { // Remove trailing "\r\n" from first boundary length -= 2; // Add last boundary with double dash (--) at the end length += (boundary + "--").getBytes(StandardCharsets.UTF_8).length; } return length; }
private long calcMultipartFormDataBodyLength(final MultipartFormData<?> multipartFormData) { final String boundaryToContentTypeStart = MultipartFormatter.boundaryToContentType(""); final String boundary = getHeaders().get(HeaderNames.CONTENT_TYPE) .filter(ct -> ct.startsWith(boundaryToContentTypeStart)) .map(ct -> "\r\n--" + ct.substring(boundaryToContentTypeStart.length())) .orElseThrow(() -> new RuntimeException(("Content-Type header starting with \"" + boundaryToContentTypeStart + "\" needs to be present"))); long dataSizeSum = multipartFormData.asFormUrlEncoded().entrySet().stream().mapToLong(dataPart -> Arrays.stream(dataPart.getValue()).mapToLong(value -> partLength(boundary, "form-data", dataPart.getKey() + (dataPart.getValue().length > 1 ? "[]" : ""), null, null, value) ).sum()).sum(); long fileHeadersSizeSum = multipartFormData.getFiles().stream() .mapToLong(filePart -> // Pass empty body because we add the file size sum later instead anyway (see next assignment below) partLength(boundary, filePart.getDispositionType(), filePart.getKey(), filePart.getFilename(), filePart.getContentType(), "") ).sum(); long fileSizeSum = multipartFormData.getFiles().stream().mapToLong(filePart -> filePart.getFileSize()).sum(); long length = dataSizeSum + fileHeadersSizeSum + fileSizeSum; if(length > 0) { // Remove trailing "\r\n" from first boundary length -= 2; // Add last boundary with double dash (--) at the end length += (boundary + "--").getBytes(StandardCharsets.UTF_8).length; } return length; }
private long calcMultipartFormDataBodyLength(final MultipartFormData<?> multipartFormData) { final String boundaryToContentTypeStart = MultipartFormatter.boundaryToContentType(""); final String boundary = getHeaders().get(HeaderNames.CONTENT_TYPE) .filter(ct -> ct.startsWith(boundaryToContentTypeStart)) .map(ct -> "\r\n--" + ct.substring(boundaryToContentTypeStart.length())) .orElseThrow(() -> new RuntimeException(("Content-Type header starting with \"" + boundaryToContentTypeStart + "\" needs to be present"))); long dataSizeSum = multipartFormData.asFormUrlEncoded().entrySet().stream().mapToLong(dataPart -> Arrays.stream(dataPart.getValue()).mapToLong(value -> partLength(boundary, "form-data", dataPart.getKey() + (dataPart.getValue().length > 1 ? "[]" : ""), null, null, value) ).sum()).sum(); long fileHeadersSizeSum = multipartFormData.getFiles().stream() .mapToLong(filePart -> // Pass empty body because we add the file size sum later instead anyway (see next assignment below) partLength(boundary, filePart.getDispositionType(), filePart.getKey(), filePart.getFilename(), filePart.getContentType(), "") ).sum(); long fileSizeSum = multipartFormData.getFiles().stream().mapToLong(filePart -> filePart.getFileSize()).sum(); long length = dataSizeSum + fileHeadersSizeSum + fileSizeSum; if(length > 0) { // Remove trailing "\r\n" from first boundary length -= 2; // Add last boundary with double dash (--) at the end length += (boundary + "--").getBytes(StandardCharsets.UTF_8).length; } return length; }
/** * Set the body of the request. * * @param body The body. * @return the modified builder */ protected RequestBuilder body(RequestBody body) { if (body == null || body.as(Object.class) == null) { // assume null signifies no body; RequestBody is a wrapper for the actual body content headers(getHeaders().remove(HeaderNames.CONTENT_LENGTH).remove(HeaderNames.TRANSFER_ENCODING)); } else { if (!getHeaders().get(HeaderNames.TRANSFER_ENCODING).isPresent()) { final MultipartFormData<?> multipartFormData = body.asMultipartFormData(); if (multipartFormData != null) { header(HeaderNames.CONTENT_LENGTH, Long.toString(calcMultipartFormDataBodyLength(multipartFormData))); } else { int length = body.asBytes().length(); header(HeaderNames.CONTENT_LENGTH, Integer.toString(length)); } } } req = req.withBody(body); return this; }
/** * Set the body of the request. * * @param body The body. * @return the modified builder */ protected RequestBuilder body(RequestBody body) { if (body == null || body.as(Object.class) == null) { // assume null signifies no body; RequestBody is a wrapper for the actual body content headers(getHeaders().remove(HeaderNames.CONTENT_LENGTH).remove(HeaderNames.TRANSFER_ENCODING)); } else { if (!getHeaders().get(HeaderNames.TRANSFER_ENCODING).isPresent()) { final MultipartFormData<?> multipartFormData = body.asMultipartFormData(); if (multipartFormData != null) { header(HeaderNames.CONTENT_LENGTH, Long.toString(calcMultipartFormDataBodyLength(multipartFormData))); } else { int length = body.asBytes().length(); header(HeaderNames.CONTENT_LENGTH, Integer.toString(length)); } } } req = req.withBody(body); return this; }
/** * Set the body of the request. * * @param body The body. * @return the modified builder */ protected RequestBuilder body(RequestBody body) { if (body == null || body.as(Object.class) == null) { // assume null signifies no body; RequestBody is a wrapper for the actual body content headers(getHeaders().remove(HeaderNames.CONTENT_LENGTH).remove(HeaderNames.TRANSFER_ENCODING)); } else { if (!getHeaders().get(HeaderNames.TRANSFER_ENCODING).isPresent()) { final MultipartFormData<?> multipartFormData = body.asMultipartFormData(); if (multipartFormData != null) { header(HeaderNames.CONTENT_LENGTH, Long.toString(calcMultipartFormDataBodyLength(multipartFormData))); } else { int length = body.asBytes().length(); header(HeaderNames.CONTENT_LENGTH, Integer.toString(length)); } } } req = req.withBody(body); return this; }
/** * @param key the key for in the header * @param value the value (one) associated with the key * @return the builder instance */ public RequestBuilder header(String key, String value) { return this.headers(getHeaders().addHeader(key, value)); }
/** * @param key the key for in the header * @param values the values associated with the key * @return the builder instance */ public RequestBuilder header(String key, List<String> values) { return this.headers(getHeaders().addHeader(key, values)); }
/** * @return the host name from the header */ public String host() { return getHeaders().get(HeaderNames.HOST).orElse(null); }
/** * @param key the key for in the header * @param values the values associated with the key * @return the builder instance */ public RequestBuilder header(String key, List<String> values) { return this.headers(getHeaders().addHeader(key, values)); }
/** * @param key the key for in the header * @param value the value (one) associated with the key * @return the builder instance */ public RequestBuilder header(String key, String value) { return this.headers(getHeaders().addHeader(key, value)); }
/** * @param key the key for in the header * @param value the value (one) associated with the key * @return the builder instance */ public RequestBuilder header(String key, String value) { return this.headers(getHeaders().addHeader(key, value)); }
/** * @return the host name from the header */ public String host() { return getHeaders().get(HeaderNames.HOST).orElse(null); }
/** * @param key the key for in the header * @param values the values associated with the key * @return the builder instance */ public RequestBuilder header(String key, List<String> values) { return this.headers(getHeaders().addHeader(key, values)); }
/** * @return the host name from the header */ public String host() { return getHeaders().get(HeaderNames.HOST).orElse(null); }