Esempio n. 1
0
ProcessResponseEnum XmlClientHandler::ProcessResponse(char* response, size_t length, Value& result, unsigned requestId, bool notification)
{
    log_trace();
    ProcessResponseEnum processResponse = ProcessResponseErrorClose;
    try
    {
        Document doc;

        InSituStringStream sstream(response, length);
        XmlReader reader(sstream);

        reader.ParseResponse(doc);
        if (reader.HasParseError())
        {
            std::stringstream message;
            message << "Response parse error, offset=" << reader.GetErrorOffset();
            message << ", code=" << reader.GetParseErrorCode();
            message << ", message=" << reader.GetParseErrorStr();
            GenerateFaultResult(AnyRpcErrorResponseParseError, message.str(), result);
        }
        else if (doc.GetValue().IsMap())
        {
            // looks like a fault response - verify that it is properly formated
            result.Assign( doc.GetValue() );
            if (result.HasMember("faultCode") && result.HasMember("faultString"))
            {
                // standard xmlrpc fault codes - copy fault codes to the standard names
                GenerateFaultResult(result["faultCode"].GetInt(), result["faultString"].GetString(), result);
                // keep the connection open
                processResponse =  ProcessResponseErrorKeepOpen;
            }
            else
                GenerateFaultResult(AnyRpcErrorInvalidResponse, "Invalid response, wrong fault fields", result);
        }
        else if (!doc.GetValue().IsArray() || (doc.GetValue().Size() != 1))
            GenerateFaultResult(AnyRpcErrorInvalidResponse, "Invalid response, wrong field types", result);
        else
        {
            result.Assign( doc.GetValue()[0] );
            processResponse = ProcessResponseSuccess;
        }
    }
    catch (AnyRpcException &fault)
    {
        std::stringstream buffer;
        buffer << "Unhandled system error, Code:" << fault.GetCode() << ", Message:" << fault.GetMessage();
        GenerateFaultResult(AnyRpcErrorSystemError, buffer.str(), result);
    }


    return processResponse;
}
Esempio n. 2
0
bool EdiComposer::ComposeDetail(const Document& doc) {
    bool result = true;

    GetTrailer().TotalRecords = doc.GetNumberRows();
    bool fillTrailer = true;

    for (size_t i=0; i<doc.GetNumberRows(); i++) {

        if ( fillTrailer )  {
            UpdateTrailer(doc.GetValue(i,0));
        }
        BufferClean();
        for (size_t j=0; j<doc.GetColCount(); j++) {
            WriteData(doc.GetColumnDescriptor(j), doc.GetValue(i,j));
        }
        BufferFlush();
    }
    return result;
};
Esempio n. 3
0
static string ReadWriteData(char* inString)
{
    ReadStringStream is(inString);
    XmlReader reader(is);
    Document doc;
    reader >> doc;

    WriteStringStream os;
    XmlWriter writer(os);
    writer << doc.GetValue();

    return os.GetString();
}
Esempio n. 4
0
bool XmlRpcHandler(MethodManager* manager, char* request, size_t length, Stream &response)
{
    InSituStringStream sstream(request, length);
    XmlReader reader(sstream);
    Document doc;

    std::string methodName = reader.ParseRequest(doc);
    log_info("MethodName=" << methodName);

    if (reader.HasParseError())
        XmlGenerateFaultResponse(AnyRpcErrorParseError, "Parse error", response);
    else
    {
        Value params;
        Value result;

        params.Assign(doc.GetValue());
        log_info("Execute method");
        if (methodName == "system.multicall")
        {
            if (!params.IsArray() || (params.Size() != 1) || !params[0].IsArray())
                XmlGenerateFaultResponse(AnyRpcErrorInvalidParams, "Invalid method parameters", response);
            else
            {
                XmlExecuteMultiCall(manager,params[0],result);
                if (result.IsInvalid())
                    result.SetString("");
                XmlGenerateResponse(result, response);
           }
        }
        else
        {
            try
            {
                if (manager->ExecuteMethod(methodName,params,result))
                {
                    if (result.IsInvalid())
                        result = "";
                    XmlGenerateResponse(result, response);
                }
                else
                    XmlGenerateFaultResponse(AnyRpcErrorMethodNotFound, "Method not found", response);
            }
            catch (const AnyRpcException& fault)
            {
                XmlGenerateFaultResponse(fault.GetCode(), fault.GetMessage(), response);
            }
        }
    }
    return true;
}
Esempio n. 5
0
static void WriteReadValue(Value& value, Value& outValue)
{
    WriteStringStream os;
    XmlWriter writer(os);
    writer << value;

    //cout << "Xml: " << os.GetBuffer() << endl;

    ReadStringStream is(os.GetBuffer());
    XmlReader reader(is);
    Document doc;
    reader >> doc;
    outValue.Assign(doc.GetValue());
}
Esempio n. 6
0
static string ParseResponseWriteData(const char* inString)
{
    ReadStringStream is(inString);
    XmlReader reader(is);
    Document doc;
    reader.ParseResponse(doc);

    WriteStringStream os;
    XmlWriter writer(os);
    writer << doc.GetValue();

    if (reader.GetParseErrorCode() != 0)
        os.Put("<<<Parse Error>>>");

    return os.GetString();
}
Esempio n. 7
0
static string ParseRequestWriteData(const char* inString)
{
    ReadStringStream is(inString);
    XmlReader reader(is);
    Document doc;
    std::string methodName = reader.ParseRequest(doc);

    WriteStringStream os;
    XmlWriter writer(os);
    writer.String(methodName.c_str(),methodName.length());
    writer << doc.GetValue();

    if (reader.GetParseErrorCode() != 0)
        os.Put("<<<Parse Error>>>");

    return os.GetString();
}
Esempio n. 8
0
int main()
{
    // Start the logger up first so all other code can be logged
    InitializeLogger();

    // Log the time for the executing the function
    log_time(INFO);

    Value value;
    Document doc;

    // Create map with various values of different types
    value["integer"] = 32;
    value["string"] = "test string\nsecond line";
    value["double"] = 5.532e-5;
    value["dateTime"].SetDateTime(time(NULL));

    Value binary;
    char* binData = (char*)"\x0a\x0b\x0c\x0d\xff\x00\xee\xdd";
    binary.SetBinary( (unsigned char*)binData, 8);
    value.AddMember( "binary", binary );

    Value array;
    array.SetSize(4);
    array[0] = 5;
    array[1] = 10;
    array[2] = 15;
    array[3] = 20;
    value["array"] = array;

    // Write data to stdout
    cout << "Json data to stdout: " << endl;
    JsonWriter jsonWriter(stdoutStream);
    jsonWriter << value;
    cout << endl;

    // Write data to a string
    WriteStringStream strStream;
    JsonWriter jsonStrWriter(strStream);
    jsonStrWriter << value;

    const char *json = strStream.GetBuffer();
    int jsonLength = strStream.Length();
    cout << "Json data in string:" << endl << json << endl;

    // Use string data and parse in place.
    // This will modify the string and reference value strings directly from the input string.
    InSituStringStream sstream((char*)json, jsonLength);
    JsonReader jsonreader(sstream);
    jsonreader >> doc;
    if (jsonreader.HasParseError())
    {
        cout << "Parse Error: " << jsonreader.GetParseErrorStr();
        cout << "; at position " << jsonreader.GetErrorOffset() << endl;
    }
    else
    {
        // Display value using internal converter - close to Json format but not exact
        cout << "Value traversal after reading: " << endl;
        cout << doc.GetValue() << endl;
    }

    return 0;
}