private PasswordAuthentication getAuthInfo() { final HttpConfig.Auth auth = requestConfig.getChainedRequest().actualAuth(); if (auth == null) { return null; } if (auth.getAuthType() == HttpConfig.AuthType.BASIC || auth.getAuthType() == HttpConfig.AuthType.DIGEST) { return new PasswordAuthentication(auth.getUser(), auth.getPassword().toCharArray()); } else { throw new UnsupportedOperationException("HttpURLConnection does not support " + auth.getAuthType() + " authentication"); } }
if (cr.actualBody() != null) { j2s = new JavaToServer(); requestConfig.findEncoder().accept(requestConfig, j2s); contentLog.debug("Request-Body({}): {}", requestConfig.getChainedRequest().actualContentType(), j2s.content());
public static boolean handleRawUpload(final ChainedHttpConfig config, final ToServer ts) { final ChainedHttpConfig.ChainedRequest request = config.getChainedRequest(); final Object body = request.actualBody(); final Charset charset = request.actualCharset(); try { if (body instanceof File) { ts.toServer(new FileInputStream((File) body)); return true; } else if (body instanceof Path) { ts.toServer(Files.newInputStream((Path) body)); return true; } else if (body instanceof byte[]) { ts.toServer(new ByteArrayInputStream((byte[]) body)); return true; } else if (body instanceof InputStream) { ts.toServer((InputStream) body); return true; } else if (body instanceof Reader) { ts.toServer(new ReaderInputStream((Reader) body, charset)); return true; } else { return false; } } catch (IOException e) { throw new RuntimeException(e); } }
final ChainedHttpConfig.ChainedRequest request = config.getChainedRequest(); final Object body = request.actualBody(); if (!(body instanceof MultipartContent)) { throw new IllegalArgumentException("Multipart body content must be MultipartContent."); final String contentType = request.actualContentType(); if (!(contentType.equals(MULTIPART_FORMDATA.getAt(0)) || contentType.equals(MULTIPART_MIXED.getAt(0)))) { throw new IllegalArgumentException("Multipart body content must be multipart/form-data."); multipartBody.writeTo(buffer); request.setContentType("multipart/form-data; boundary=" + multipartBody.boundary());
if (cr.actualBody() != null) { j2s = new JavaToServer(); requestConfig.findEncoder().accept(requestConfig, j2s); contentLog.debug("Request-Body({}): {}", requestConfig.getChainedRequest().actualContentType(), j2s.content());
final ChainedHttpConfig.ChainedRequest request = config.getChainedRequest(); final Object body = request.actualBody(); if (!(body instanceof MultipartContent)) { throw new IllegalArgumentException("Multipart body content must be MultipartContent."); final String contentType = request.actualContentType(); if (!(contentType.equals(MULTIPART_FORMDATA.getAt(0)) || contentType.equals(MULTIPART_MIXED.getAt(0)))) { throw new IllegalArgumentException("Multipart body content must be multipart/form-data."); request.setContentType(boundaryContentType);
final ChainedHttpConfig.ChainedRequest request = config.getChainedRequest(); final Object body = request.actualBody(); if (!(body instanceof MultipartContent)) { throw new IllegalArgumentException("Multipart body content must be MultipartContent."); final String contentType = request.actualContentType(); if (!(contentType.equals(MULTIPART_FORMDATA.getAt(0)) || contentType.equals(MULTIPART_MIXED.getAt(0)))) { throw new IllegalArgumentException("Multipart body content must be multipart/form-data."); request.setContentType(boundaryContentType);
/** * Encodes multipart/form-data where the body content must be an instance of the {@link MultipartContent} class. Individual parts will be * encoded using the encoders available to the {@link ChainedHttpConfig} object. * * @param config the chained configuration object * @param ts the server adapter */ public static void multipart(final ChainedHttpConfig config, final ToServer ts) { try { final ChainedHttpConfig.ChainedRequest request = config.getChainedRequest(); final Object body = request.actualBody(); if (!(body instanceof MultipartContent)) { throw new IllegalArgumentException("Multipart body content must be MultipartContent."); } final String contentType = request.actualContentType(); if (!(contentType.equals(MULTIPART_FORMDATA.getAt(0)) || contentType.equals(MULTIPART_MIXED.getAt(0)))) { throw new IllegalArgumentException("Multipart body content must be multipart/form-data."); } final MimeMultipart mimeMultipart = new MimeMultipart(); for (final MultipartContent.MultipartPart mpe : ((MultipartContent) body).parts()) { mimeMultipart.addBodyPart(part(config, mpe)); } request.setContentType(mimeMultipart.getContentType()); final ByteArrayOutputStream bytesOut = new ByteArrayOutputStream(); mimeMultipart.writeTo(bytesOut); ts.toServer(new ByteArrayInputStream(bytesOut.toByteArray())); } catch (IOException | MessagingException ioe) { ioe.printStackTrace(); } }
/** * Encodes multipart/form-data where the body content must be an instance of the {@link MultipartContent} class. Individual parts will be * encoded using the encoders available to the {@link ChainedHttpConfig} object. * * @param config the chained configuration object * @param ts the server adapter */ public static void multipart(final ChainedHttpConfig config, final ToServer ts) { try { final ChainedHttpConfig.ChainedRequest request = config.getChainedRequest(); final Object body = request.actualBody(); if (!(body instanceof MultipartContent)) { throw new IllegalArgumentException("Multipart body content must be MultipartContent."); } final String contentType = request.actualContentType(); if (!(contentType.equals(MULTIPART_FORMDATA.getAt(0)) || contentType.equals(MULTIPART_MIXED.getAt(0)))) { throw new IllegalArgumentException("Multipart body content must be multipart/form-data."); } final MimeMultipart mimeMultipart = new MimeMultipart(); for (final MultipartContent.MultipartPart mpe : ((MultipartContent) body).parts()) { mimeMultipart.addBodyPart(part(config, mpe)); } request.setContentType(mimeMultipart.getContentType()); final ByteArrayOutputStream bytesOut = new ByteArrayOutputStream(); mimeMultipart.writeTo(bytesOut); ts.toServer(new ByteArrayInputStream(bytesOut.toByteArray())); } catch (IOException | MessagingException ioe) { ioe.printStackTrace(); } }
public static boolean handleRawUpload(final ChainedHttpConfig config, final ToServer ts) { final ChainedHttpConfig.ChainedRequest request = config.getChainedRequest(); final Object body = request.actualBody(); final Charset charset = request.actualCharset(); try { if (body instanceof File) { ts.toServer(new FileInputStream((File) body)); return true; } else if (body instanceof Path) { ts.toServer(Files.newInputStream((Path) body)); return true; } else if (body instanceof byte[]) { ts.toServer(new ByteArrayInputStream((byte[]) body)); return true; } else if (body instanceof InputStream) { ts.toServer((InputStream) body); return true; } else if (body instanceof Reader) { ts.toServer(new ReaderInputStream((Reader) body, charset)); return true; } else { return false; } } catch (IOException e) { throw new TransportingException(e); } }
public static boolean handleRawUpload(final ChainedHttpConfig config, final ToServer ts) { final ChainedHttpConfig.ChainedRequest request = config.getChainedRequest(); final Object body = request.actualBody(); final Charset charset = request.actualCharset(); try { if (body instanceof File) { ts.toServer(new FileInputStream((File) body)); return true; } else if (body instanceof Path) { ts.toServer(Files.newInputStream((Path) body)); return true; } else if (body instanceof byte[]) { ts.toServer(new ByteArrayInputStream((byte[]) body)); return true; } else if (body instanceof InputStream) { ts.toServer((InputStream) body); return true; } else if (body instanceof Reader) { ts.toServer(new ReaderInputStream((Reader) body, charset)); return true; } else { return false; } } catch (IOException e) { throw new TransportingException(e); } }
/** * Standard encoder for requests with an xml body. * <p> * Accepts String and {@link Closure} types. If the body is a String type the method passes the body * to the ToServer parameter as is. If the body is a {@link Closure} then the closure is converted * to xml using Groovy's {@link StreamingMarkupBuilder}. * * @param config Fully configured chained request * @param ts Formatted http body is passed to the ToServer argument */ public static void xml(final ChainedHttpConfig config, final ToServer ts) { final ChainedHttpConfig.ChainedRequest request = config.getChainedRequest(); if (handleRawUpload(config, ts)) { return; } final Object body = checkNull(request.actualBody()); checkTypes(body, XML_TYPES); if (body instanceof String) { ts.toServer(stringToStream((String) body, request.actualCharset())); } else if (body instanceof Closure) { final StreamingMarkupBuilder smb = new StreamingMarkupBuilder(); ts.toServer(stringToStream(smb.bind(body).toString(), request.actualCharset())); } else { throw new UnsupportedOperationException(); } }
/** * Standard encoder for requests with an xml body. * <p> * Accepts String and {@link Closure} types. If the body is a String type the method passes the body * to the ToServer parameter as is. If the body is a {@link Closure} then the closure is converted * to xml using Groovy's {@link StreamingMarkupBuilder}. * * @param config Fully configured chained request * @param ts Formatted http body is passed to the ToServer argument */ public static void xml(final ChainedHttpConfig config, final ToServer ts) { final ChainedHttpConfig.ChainedRequest request = config.getChainedRequest(); if (handleRawUpload(config, ts)) { return; } final Object body = checkNull(request.actualBody()); checkTypes(body, XML_TYPES); if (body instanceof String) { ts.toServer(stringToStream((String) body, request.actualCharset())); } else if (body instanceof Closure) { final StreamingMarkupBuilder smb = new StreamingMarkupBuilder(); ts.toServer(stringToStream(smb.bind(body).toString(), request.actualCharset())); } else { throw new UnsupportedOperationException(); } }
/** * Standard encoder for requests with content type 'application/x-www-form-urlencoded'. * Accepts String and Map types. If the body is a String type the method assumes it is properly * url encoded and is passed to the ToServer parameter as is. If the body is a Map type then * the output is generated by the {@link Form} class. * * @param config Fully configured chained request * @param ts Formatted http body is passed to the ToServer argument */ public static void form(final ChainedHttpConfig config, final ToServer ts) { final ChainedHttpConfig.ChainedRequest request = config.getChainedRequest(); if (handleRawUpload(config, ts)) { return; } final Object body = checkNull(request.actualBody()); checkTypes(body, FORM_TYPES); if (body instanceof String) { ts.toServer(stringToStream((String) body, request.actualCharset())); } else if (body instanceof Map) { final Map<?, ?> params = (Map) body; final String encoded = Form.encode(params, request.actualCharset()); ts.toServer(stringToStream(encoded, request.actualCharset())); } else { throw new UnsupportedOperationException(); } }
/** * Standard encoder for requests with content type 'application/x-www-form-urlencoded'. * Accepts String and Map types. If the body is a String type the method assumes it is properly * url encoded and is passed to the ToServer parameter as is. If the body is a Map type then * the output is generated by the {@link Form} class. * * @param config Fully configured chained request * @param ts Formatted http body is passed to the ToServer argument */ public static void form(final ChainedHttpConfig config, final ToServer ts) { final ChainedHttpConfig.ChainedRequest request = config.getChainedRequest(); if (handleRawUpload(config, ts)) { return; } final Object body = checkNull(request.actualBody()); checkTypes(body, FORM_TYPES); if (body instanceof String) { ts.toServer(stringToStream((String) body, request.actualCharset())); } else if (body instanceof Map) { final Map<?, ?> params = (Map) body; final String encoded = Form.encode(params, request.actualCharset()); ts.toServer(stringToStream(encoded, request.actualCharset())); } else { throw new UnsupportedOperationException(); } }
/** * Standard encoder for requests with content type 'application/x-www-form-urlencoded'. * Accepts String and Map types. If the body is a String type the method assumes it is properly * url encoded and is passed to the ToServer parameter as is. If the body is a Map type then * the output is generated by the {@link Form} class. * * @param config Fully configured chained request * @param ts Formatted http body is passed to the ToServer argument */ public static void form(final ChainedHttpConfig config, final ToServer ts) { final ChainedHttpConfig.ChainedRequest request = config.getChainedRequest(); if (handleRawUpload(config, ts)) { return; } final Object body = checkNull(request.actualBody()); checkTypes(body, FORM_TYPES); if (body instanceof String) { ts.toServer(stringToStream((String) body, request.actualCharset())); } else if (body instanceof Map) { final Map<?, ?> params = (Map) body; final String encoded = Form.encode(params, request.actualCharset()); ts.toServer(stringToStream(encoded, request.actualCharset())); } else { throw new UnsupportedOperationException(); } }
/** * Standard encoder for requests with an xml body. * <p> * Accepts String and {@link Closure} types. If the body is a String type the method passes the body * to the ToServer parameter as is. If the body is a {@link Closure} then the closure is converted * to xml using Groovy's {@link StreamingMarkupBuilder}. * * @param config Fully configured chained request * @param ts Formatted http body is passed to the ToServer argument */ public static void xml(final ChainedHttpConfig config, final ToServer ts) { final ChainedHttpConfig.ChainedRequest request = config.getChainedRequest(); if (handleRawUpload(config, ts)) { return; } final Object body = checkNull(request.actualBody()); checkTypes(body, XML_TYPES); if (body instanceof String) { ts.toServer(stringToStream((String) body, request.actualCharset())); } else if (body instanceof Closure) { final StreamingMarkupBuilder smb = new StreamingMarkupBuilder(); ts.toServer(stringToStream(smb.bind(body).toString(), request.actualCharset())); } else { throw new UnsupportedOperationException(); } }
public Object execute() { try { return ThreadLocalAuth.with(getAuthInfo(), () -> { if (sslContext != null) { HttpsURLConnection https = (HttpsURLConnection) connection; https.setSSLSocketFactory(sslContext.getSocketFactory()); } final ChainedHttpConfig.ChainedRequest cr = requestConfig.getChainedRequest(); JavaToServer j2s = null; if (cr.actualBody() != null) { j2s = new JavaToServer(); requestConfig.findEncoder().accept(requestConfig, j2s); } addHeaders(); connection.connect(); if (j2s != null) { j2s.transfer(); } return handleFromServer(); }); } catch (Exception e) { throw new RuntimeException(e); } }
/** * Standard encoder for requests with a json body. * <p> * Accepts String, {@link GString} and {@link Closure} types. If the body is a String type the method passes the body * to the ToServer parameter as is. If the body is a {@link Closure} then the closure is converted * to json using Groovy's {@link JsonBuilder}. * * @param config Fully configured chained request * @param ts Formatted http body is passed to the ToServer argument */ public static void json(final ChainedHttpConfig config, final ToServer ts) { final ChainedHttpConfig.ChainedRequest request = config.getChainedRequest(); if (handleRawUpload(config, ts)) { return; } final Object body = checkNull(request.actualBody()); final String json = ((body instanceof String || body instanceof GString) ? body.toString() : new JsonBuilder(body).toString()); ts.toServer(stringToStream(json, request.actualCharset())); } }
/** * Used to encode the request content using the OpenCsv writer. * * @param config the configuration * @param ts the server request content accessor */ public static void encode(final ChainedHttpConfig config, final ToServer ts) { if (handleRawUpload(config, ts)) { return; } final ChainedHttpConfig.ChainedRequest request = config.getChainedRequest(); final Csv.Context ctx = (Csv.Context) config.actualContext(request.actualContentType(), Csv.Context.ID); final Object body = checkNull(request.actualBody()); checkTypes(body, new Class[]{Iterable.class}); final StringWriter writer = new StringWriter(); final CSVWriter csvWriter = ctx.makeWriter(new StringWriter()); Iterable<?> iterable = (Iterable<?>) body; for (Object o : iterable) { csvWriter.writeNext((String[]) o); } ts.toServer(stringToStream(writer.toString(), request.actualCharset())); }