// Path: http-builder-ng-core/src/main/java/groovyx/net/http/util/Misc.java
// public static String randomString(final int size) {
//     final StringBuilder buffer = new StringBuilder(size);
//     for(int i = 0; i < size; ++i) {
//         buffer.append(theChars[current().nextInt(theChars.length)]);
//     }
// 
//     return buffer.toString();
// }


import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import java.io.IOException;
import static groovyx.net.http.ContentTypes.MULTIPART_FORMDATA;
import static groovyx.net.http.ContentTypes.MULTIPART_MIXED;
import static org.apache.http.entity.ContentType.parse;
import static groovyx.net.http.util.Misc.randomString;
/**
 * Copyright (C) 2017 HttpBuilder-NG Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package groovyx.net.http;



/**
 * Request content encoders specific to the Apache client implementation.
 *
 * See the {@link MultipartContent} class documentation for more configuration details.
 */
public class ApacheEncoders {

    /**
     *  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.");
            }

// Path: http-builder-ng-core/src/main/java/groovyx/net/http/util/Misc.java
// public static String randomString(final int size) {
//     final StringBuilder buffer = new StringBuilder(size);
//     for(int i = 0; i < size; ++i) {
//         buffer.append(theChars[current().nextInt(theChars.length)]);
//     }
// 
//     return buffer.toString();
// }


// Path: http-builder-ng-apache/src/main/java/groovyx/net/http/ApacheEncoders.java
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import java.io.IOException;
import static groovyx.net.http.ContentTypes.MULTIPART_FORMDATA;
import static groovyx.net.http.ContentTypes.MULTIPART_MIXED;
import static org.apache.http.entity.ContentType.parse;
import static groovyx.net.http.util.Misc.randomString;

/**
 * Copyright (C) 2017 HttpBuilder-NG Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package groovyx.net.http;



/**
 * Request content encoders specific to the Apache client implementation.
 *
 * See the {@link MultipartContent} class documentation for more configuration details.
 */
public class ApacheEncoders {

    /**
     *  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 String boundary = randomString(10);