<commit_before>package com.sequenceiq.cloud.azure.client

import groovyx.net.http.HttpResponseDecorator
import groovyx.net.http.RESTClient
import org.apache.commons.logging.Log
import org.apache.commons.logging.LogFactory

import javax.crypto.Mac
import javax.crypto.spec.SecretKeySpec
import java.security.InvalidKeyException

import static groovyx.net.http.ContentType.TEXT

class AzureClientUtil {

    private static final Log log = LogFactory.getLog( getClass() );

    static def createBlobContainer(String authKey, String targetBlobContainerUri) {
        def RESTClient client = new RESTClient(targetBlobContainerUri)

        def msDateString = getMsDateString()

        def rawAuthString = getRawAuthStringForBlobApi(
                'PUT',
                'x-ms-date:' + msDateString + '\nx-ms-version:' + '2012-02-12',
                getCanonicalizedResource(targetBlobContainerUri) + '\nrestype:container'
        )

        def authHash = getAuthHashForBlobApi(authKey, rawAuthString)

        client.setHeaders(
                'x-ms-date': msDateString,
                'x-ms-version': '2012-02-12',
                'authorization': 'SharedKey ' + getStorageAccountNameFromUri(targetBlobContainerUri) + ':' + authHash,
                // 'Content-Length': 0
                // need to add 'Content-Length' since the underlying HTTP client does not add it for the HEAD request
                // but we assume so when we generate auth string
        )
        def HttpResponseDecorator response = client.put(
                path: targetBlobContainerUri,
                requestContentType: TEXT,
                query: [ restype: 'container' ]
        )
    }

    static private getMsDateString() {
        Calendar cal = Calendar.getInstance();
        cal.setTimeZone(TimeZone.getTimeZone("GMT"));
        cal.format("EEE, dd MMM yyyy HH:mm:ss z")
    }

    static def copyOsImage(String authKey, String sourceImageUri, String targetImageUri) {
        def RESTClient client = new RESTClient(targetImageUri)

        def msDateString = getMsDateString()

        def rawAuthString = getRawAuthStringForBlobApi(
                'PUT',
                'x-ms-copy-source:' + sourceImageUri + '\nx-ms-date:' + msDateString + '\nx-ms-version:' + '2012-02-12',
                getCanonicalizedResource(targetImageUri)
        )

        def authHash = getAuthHashForBlobApi(authKey, rawAuthString)

        client.setHeaders(
                'x-ms-copy-source': sourceImageUri,
                'x-ms-date': msDateString,
                'x-ms-version': '2012-02-12',
                'authorization': 'SharedKey ' + getStorageAccountNameFromUri(targetImageUri) + ':' + authHash
        )
        client.put(
                path: targetImageUri,
                requestContentType: TEXT
                // can't use XML here, because Azure REST Server returns some extra characters
                // and causes the XML parser to throw "org.xml.sax.SAXParseException: Content is not allowed in prolog"
        )
    }

    static def imageCopyProgress(storageAccountKey, targetImageUri) {
        def copyStatus = 'pending'
        while (copyStatus == 'pending') {
            def copyStatusFromServer = getCopyOsImageProgress(storageAccountKey, targetImageUri)
            copyStatus = copyStatusFromServer.status
            log.info 'copy status=' + copyStatusFromServer.status
            log.info 'copy progress=' + copyStatusFromServer.copiedBytes + ' / ' + copyStatusFromServer.totalBytes + ' percentage: ' + (copyStatusFromServer.copiedBytes.toLong() / copyStatusFromServer.totalBytes.toLong() * 100).intValue() + '%'
            sleep(5000)
        }
        if (copyStatus != 'success') {<commit_msg>CLOUD-888 added content type for the image creation request to avoid sax parser exception<commit_after>package com.sequenceiq.cloud.azure.client
import groovyx.net.http.HttpResponseDecorator
import groovyx.net.http.RESTClient
import org.apache.commons.logging.Log
import org.apache.commons.logging.LogFactory

import javax.crypto.Mac
import javax.crypto.spec.SecretKeySpec
import java.security.InvalidKeyException

import static groovyx.net.http.ContentType.TEXT

class AzureClientUtil {

    private static final Log log = LogFactory.getLog( getClass() );

    static def createBlobContainer(String authKey, String targetBlobContainerUri) {
        def RESTClient client = new RESTClient(targetBlobContainerUri)

        def msDateString = getMsDateString()

        def rawAuthString = getRawAuthStringForBlobApi(
                'PUT',
                'x-ms-date:' + msDateString + '\nx-ms-version:' + '2012-02-12',
                getCanonicalizedResource(targetBlobContainerUri) + '\nrestype:container'
        )

        def authHash = getAuthHashForBlobApi(authKey, rawAuthString)

        client.setHeaders(
                'x-ms-date': msDateString,
                'x-ms-version': '2012-02-12',
                'authorization': 'SharedKey ' + getStorageAccountNameFromUri(targetBlobContainerUri) + ':' + authHash,
                // 'Content-Length': 0
                // need to add 'Content-Length' since the underlying HTTP client does not add it for the HEAD request
                // but we assume so when we generate auth string
        )
        def HttpResponseDecorator response = client.put(
                path: targetBlobContainerUri,
                requestContentType: TEXT,
                query: [ restype: 'container' ]
        )
    }

    static private getMsDateString() {
        Calendar cal = Calendar.getInstance();
        cal.setTimeZone(TimeZone.getTimeZone("GMT"));
        cal.format("EEE, dd MMM yyyy HH:mm:ss z")
    }

    static def copyOsImage(String authKey, String sourceImageUri, String targetImageUri) {
        def RESTClient client = new RESTClient(targetImageUri)

        def msDateString = getMsDateString()

        def rawAuthString = getRawAuthStringForBlobApi(
                'PUT',
                'x-ms-copy-source:' + sourceImageUri + '\nx-ms-date:' + msDateString + '\nx-ms-version:' + '2012-02-12',
                getCanonicalizedResource(targetImageUri)
        )

        def authHash = getAuthHashForBlobApi(authKey, rawAuthString)

        client.setHeaders(
                'x-ms-copy-source': sourceImageUri,
                'x-ms-date': msDateString,
                'x-ms-version': '2012-02-12',
                'authorization': 'SharedKey ' + getStorageAccountNameFromUri(targetImageUri) + ':' + authHash
        )
        client.put(
                path: targetImageUri,
                requestContentType: TEXT,
                contentType: TEXT
                // can't use XML here, because Azure REST Server returns some extra characters (the BOM)
                // and causes the XML parser to throw "org.xml.sax.SAXParseException: Content is not allowed in prolog"
        )
    }

    static def imageCopyProgress(storageAccountKey, targetImageUri) {
        def copyStatus = 'pending'
        while (copyStatus == 'pending') {
            def copyStatusFromServer = getCopyOsImageProgress(storageAccountKey, targetImageUri)
            copyStatus = copyStatusFromServer.status
            log.info 'copy status=' + copyStatusFromServer.status
            log.info 'copy progress=' + copyStatusFromServer.copiedBytes + ' / ' + copyStatusFromServer.totalBytes + ' percentage: ' + (copyStatusFromServer.copiedBytes.toLong() / copyStatusFromServer.totalBytes.toLong() * 100).intValue() + '%'
            sleep(5000)
        }
        if (copyStatus != 'success') {
{'fraction_non_alphanumeric': 0.09416270470153196, 'fraction_numerical': 0.010961436872688853, 'mean_word_length': 2.4706691109074246, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '260294', 'n_tokens_mistral': 2156, 'n_tokens_neox': 1996, 'n_words': 463}