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

io.imqa.injector.util.MappingUploader Maven / Gradle / Ivy

There is a newer version: 2.25.11
Show newest version
package io.imqa.injector.util;

import java.io.ByteArrayInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.Map;

import static io.imqa.injector.util.HttpHelper.*;

public class MappingUploader {

    public static String postJson(String serverUrl) throws IOException {
        return postJson(serverUrl, new HashMap(), new HashMap());
    }

    public static String postJson(String serverUrl, Map headers) throws IOException {
        return postJson(serverUrl, headers, new HashMap());
    }

    public static String postJson(String serverUrl, Map headers, Map body) throws IOException {
        headers.put("Content-Type", "application/json");
        headers.put("Content-Encoding", "gzip");

        HttpURLConnection connection = (HttpURLConnection) setHeader(serverUrl, headers);
        DataOutputStream dos = new DataOutputStream(connection.getOutputStream());

        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("{");
        for (String key : body.keySet()) {
            stringBuilder.append("\"");
            stringBuilder.append(key);
            stringBuilder.append("\":\"");
            stringBuilder.append(body.get(key));
            stringBuilder.append("\"");
        }
        stringBuilder.append("}");
        InputStream stream = new ByteArrayInputStream(stringBuilder.toString().getBytes());
        sendData(dos, stream);

        String response = finish(dos, connection);
        return "[status : "+connection.getResponseCode()+"] " +response;
    }

    public static String postFile(String serverUrl, String filename, File uploadFile) throws IOException {
        return postFile(serverUrl, new HashMap(), filename, uploadFile, new HashMap());
    }

    public static String postFile(String serverUrl, Map headers, String filename, File uploadFile) throws IOException {
        return postFile(serverUrl, headers, filename, uploadFile, new HashMap());
    }

    public static String postFile(String serverUrl, Map headers, String filename, File uploadFile, Map body) throws IOException {

        headers.put("Content-Type", "multipart/form-data;boundary=" + HttpHelper.boundary);

        HttpURLConnection connection = (HttpURLConnection) setHeader(serverUrl, headers);
        DataOutputStream dos = new DataOutputStream(connection.getOutputStream());

        for (String key : body.keySet()) {
            addFormField(dos, key, body.get(key));
        }
        addFilePart(dos, filename, uploadFile);
        endFile(dos);

        String response = finish(dos, connection);
        return "[status : "+connection.getResponseCode()+"] " +response;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy