All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.lyncode.test.http.method.PostMethodBuilder Maven / Gradle / Ivy

/**
 * 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 com.lyncode.test.http.method;

import org.apache.http.NameValuePair;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.entity.ContentType;
import org.apache.http.message.BasicHeader;
import org.apache.http.message.BasicNameValuePair;

import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

public class PostMethodBuilder extends MethodBuilder {
    private String body = null;
    private List parameters = new ArrayList();
    private ContentType contentType;

    public static PostMethodBuilder post (String relativeUrl) {
        return new PostMethodBuilder(relativeUrl);
    }

    public PostMethodBuilder(String relativeUrl) {
        super(relativeUrl);
    }


    public PostMethodBuilder withBody (String body, ContentType contentType) {
        this.body = body;
        this.contentType = contentType;
        return with(new BasicHeader("content-type", contentType.toString()));
    }

    public PostMethodBuilder withForm (String name, String value) {
        this.parameters.add(new BasicNameValuePair(name, value));
        return this;
    }

    @Override
    HttpPost build(String url) {
        HttpPost httpPost = new HttpPost(url);
        if (body != null) {
            httpPost.setEntity(new ByteArrayEntity(body.getBytes(), contentType));
        } else if (!parameters.isEmpty()) {
            final ContentType contentType = ContentType.create(URLEncodedUtils.CONTENT_TYPE, "UTF-8");
            final String s = URLEncodedUtils.format(parameters, "UTF-8");
            httpPost.setEntity(bodyString(s, contentType));
        }

        return httpPost;
    }

    private ByteArrayEntity bodyString(final String s, final ContentType contentType) {
        final Charset charset = contentType != null ? contentType.getCharset() : null;
        byte[] raw;
        try {
            raw = charset != null ? s.getBytes(charset.name()) : s.getBytes();
        } catch (UnsupportedEncodingException ex) {
            raw = s.getBytes();
        }
        return new ByteArrayEntity(raw, contentType);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy