import org.apache.http.HttpHeaders
import org.apache.http.client.methods.HttpPost
import org.apache.http.entity.ContentType
import org.apache.http.entity.mime.HttpMultipartMode
import org.apache.http.entity.mime.MultipartEntityBuilder
import org.apache.http.impl.client.HttpClients
import org.apache.http.message.BasicHeader
import org.apache.http.entity.mime.content.FileBody
import org.apache.http.entity.mime.content.StringBody
import org.apache.http.entity.mime.FormBodyPart
import org.apache.http.entity.mime.FormBodyPartBuilder
import org.apache.commons.codec.digest.DigestUtils
import org.apache.commons.io.FileUtils
import org.apache.http.util.EntityUtils
//String archiveFileName1='C:\\Users\\Yakov.Danilchuk\\Downloads\\export.zip';
String archiveFileName= vars.get("import_folder_svodki") + vars.get("import_subfolder_svodki") + "export.zip";
String urlRequest='http://' + '${host_svodki}' + ':8080' + '/web-api/synchronization/import-from-lower-level';
def postRequest = new HttpPost(urlRequest);
def file = new File(archiveFileName);
def builder = MultipartEntityBuilder.create();
def hashCode = DigestUtils.sha256Hex(FileUtils.readFileToByteArray(file))
FormBodyPart jsonPart = FormBodyPartBuilder.create().addField("Content-Disposition", "form-data;name=packageInfo").setBody(new StringBody(vars.get("jsonPart"), ContentType.APPLICATION_JSON)).setName("packageInfo").build();
FormBodyPart bodyFilePart = FormBodyPartBuilder.create().addField("Content-Disposition", "form-data;filename=export.zip").setBody(new FileBody(file, ContentType.TEXT_PLAIN)).setName("export.zip").build();
FormBodyPart hashPart = FormBodyPartBuilder.create().addField("Content-Disposition", "form-data;name=packageHash").setBody(new StringBody(hashCode, ContentType.APPLICATION_JSON)).setName("packageHash").build();
//builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
//builder.addBinaryBody("filename", file, ContentType.DEFAULT_BINARY, archiveFileName);
def entity = builder.setBoundary(vars.get("boundary")).addPart(jsonPart).setBoundary(vars.get("boundary")).addPart(bodyFilePart).setBoundary(vars.get("boundary")).addPart(hashPart).setBoundary(vars.get("boundary")).build();
postRequest.setEntity(entity);
String rqHeaderContentType = "multipart/form-data; boundary=" + "\"" + vars.get("boundary") + "\"";
def header = new BasicHeader(HttpHeaders.CONTENT_TYPE, rqHeaderContentType);
def header_auth = new BasicHeader("Authorization", "Bearer ${access_token}");
def headers = Arrays.asList(header, header_auth);
def client = HttpClients.custom().setDefaultHeaders(headers).build();
def response = client.execute(postRequest);
String responseBody = EntityUtils.toString(response.getEntity());
int respCode = response.getStatusLine().getStatusCode();
SampleResult.setResponseCode(respCode as String);
SampleResult.setResponseMessage(responseBody);
if(respCode==200){
SampleResult.setSuccessful(true);
}
else {
SampleResult.setSuccessful(false);
}