Thursday, November 15, 2012

use script component to extract url parameter value during ETL process

    public override void Input0_ProcessInputRow(Input0Buffer Row)
    {
        int name_length_limit = 255;
        int value_length_limit = 2000;
        string name, value;
        string[] arr = BlobToString(Row.parameter).Split('&');
        foreach (string str in arr)
        {
            if (str.Trim().Length > 0)
            {
                ResultBuffer.AddRow();
                ResultBuffer.id = Row.id;
                ResultBuffer.ts = Row.ts;

                string[] values = str.Trim().Split('=');
                name = decode(values[0]);
                if(name.Length > name_length_limit)
                    name = name.Substring(0,name_length_limit);

                ResultBuffer.parametername = name;

                if (values.Length > 1)
                {
                    value = decode(values[1]);
                    if(value.Length > value_length_limit)
                        value = value.Substring(0,value_length_limit);
                    ResultBuffer.parametervalue = value;
                }
            }
        }
    }

    private string BlobToString(BlobColumn blob)
    {
        string result = "";
        try
        {
            if (blob != null)
            {
                result = System.Text.Encoding.ASCII.GetString(blob.GetBlobData(0, Convert.ToInt32(blob.Length)));
            }
        }
        catch (Exception ex)
        {
            result = ex.Message;
        }
        return result;
    }

    public string decode(string s)
    {
        int charValue;
        int b1, b2, b3;
        Decoder utf8Decoder = Encoding.UTF8.GetDecoder();
        Char[] chars = new Char[1];
        s = s.Replace("+", " ");
        Byte[] threeByte = new Byte[] { 99, 99, 99 };
        for (int i = 0; i < s.Length; i++)
        {
            if (s[i] == '%' && s.Length >= i+3 )
            {
                b1 = int.Parse(s.Substring(i + 1, 2), System.Globalization.NumberStyles.HexNumber);
                if (b1 < 127)
                {
                    s = s.Remove(i, 3);
                    s = s.Insert(i, Convert.ToChar(b1).ToString());
                }
                if ((b1 > 193) && (b1 < 224))
                {
                    threeByte[0] = byte.Parse(b1.ToString());
                    s = s.Remove(i, 3);
                    b2 = int.Parse(s.Substring(i + 1, 2), System.Globalization.NumberStyles.HexNumber);
                    threeByte[1] = byte.Parse(b2.ToString());
                    s = s.Remove(i, 3);
                    charValue = utf8Decoder.GetChars(threeByte, 0, 2, chars, 0);
                    s = s.Insert(i, chars[0].ToString());

                }
                if ((b1 > 223) && (b1 < 240))
                {
                    threeByte[0] = byte.Parse(b1.ToString());
                    s = s.Remove(i, 3);
                    b2 = int.Parse(s.Substring(i + 1, 2), System.Globalization.NumberStyles.HexNumber);
                    threeByte[1] = byte.Parse(b2.ToString());
                    s = s.Remove(i, 3);
                    b3 = int.Parse(s.Substring(i + 1, 2), System.Globalization.NumberStyles.HexNumber);
                    threeByte[2] = byte.Parse(b3.ToString());
                    s = s.Remove(i, 3);
                    charValue = utf8Decoder.GetChars(threeByte, 0, 3, chars, 0);
                    s = s.Insert(i, chars[0].ToString());
                }
            }
        }
        return s.Trim();
    }