Beispiel #1
0
void OSInfoCommand::getOSInfo(ostream& outPrintWriter,
                              ostream& errPrintWriter)
{

    CIMClient client;
    client.setTimeout( _timeout );

    try
    {
        _connectToServer( client, outPrintWriter);

        Boolean deepInheritance = true;
        Boolean localOnly = false;
        Boolean includeQualifiers = false;
        Boolean includeClassOrigin = false;
        Uint32 numberInstances;
        Array<CIMInstance> cimNInstances =
               client.enumerateInstances(NAMESPACE, CLASSNAME,
                                         deepInheritance,
                                         localOnly,  includeQualifiers,
                                         includeClassOrigin );

        numberInstances = cimNInstances.size();

        // while we only have one instance (the running OS), we can take the
        // first instance.  When the OSProvider supports installed OSs as well,
        // will need to select the runningOS instance

        for (Uint32 i = 0; i < cimNInstances.size(); i++)
        {
           CIMObjectPath instanceRef = cimNInstances[i].getPath ();
           if ( !(instanceRef.getClassName().equal (CIMName (CLASSNAME))))
           {
              errorExit(errPrintWriter, "EnumerateInstances failed");
           }

           // first gather the interesting properties
           gatherProperties(cimNInstances[i], _useRawDateTimeFormat);
           // then display them
           displayProperties(outPrintWriter);

      }   // end for looping through instances

    }  // end try

    catch(const Exception& e)
    {
      errorExit(errPrintWriter, e.getMessage());
    }

}
/**

    Executes the command using HTTP.  A CIM request encoded in XML is read
    from the input, and encapsulated in an HTTP request message.  A channel
    is obtained for an HTTP connection, and the message is written to the
    channel.  The response is written to the specified outPrintWriter, and
    consists of the CIM response encoded in XML.

    @param   outPrintWriter     the ostream to which output should be
                                written
    @param   errPrintWriter     the ostream to which error output should be
                                written

    @exception  WbemExecException  if an error is encountered in executing
                                   the command

 */
void WbemExecCommand::_executeHttp (ostream& outPrintWriter,
                                    ostream& errPrintWriter)
{
    Uint32                       size;
    Buffer                    content;
    Buffer                    contentCopy;
    Buffer                    message;
    Buffer                    httpHeaders;
    Buffer                    httpResponse;
#ifdef PEGASUS_WMIMAPPER
    WMIWbemExecClient client;
#else
    WbemExecClient client;
#endif

    client.setTimeout( _timeout );

    //
    //  Check for invalid combination of options
    //  The M-POST method may not be used with HTTP/1.0
    //
    if ((!_useHTTP11) && (_useMPost))
    {
        throw WbemExecException(WbemExecException::MPOST_HTTP10_INVALID);
    }

    //
    //  If no hostName specified
    //  Default to local host
    //
    if (!_hostNameSet)
    {
      _hostName = System::getHostName();
    }
    if( !_portNumberSet )
    {
        if( _useSSL )
        {
            _portNumber = System::lookupPort( WBEM_HTTPS_SERVICE_NAME,
                              WBEM_DEFAULT_HTTPS_PORT );
        }
        else
        {
            _portNumber = System::lookupPort( WBEM_HTTP_SERVICE_NAME,
                              WBEM_DEFAULT_HTTP_PORT );
        }
        char buffer[32];
        sprintf( buffer, "%lu", (unsigned long) _portNumber );
        _portNumberStr = buffer;
    }

    //
    //  Get XML request from input file
    //
    if (_inputFilePathSet)
    {
        //
        //  Check that input file exists
        //
        if (!FileSystem::exists (_inputFilePath))
        {
            throw WbemExecException(WbemExecException::INPUT_FILE_NONEXISTENT);
        }

        //
        //  Check that input file is readable
        //
        if (!FileSystem::canRead (_inputFilePath))
        {
            throw WbemExecException(WbemExecException::INPUT_FILE_NOT_READABLE);
        }

        //
        //  Check that file is not empty
        //
        FileSystem::getFileSize (_inputFilePath, size);
        if (size == 0)
        {
            throw WbemExecException(WbemExecException::NO_INPUT);
        }

        //
        //  Read from input file
        //
        try
        {
            FileSystem::loadFileToMemory (content, _inputFilePath);
        }
        catch (const CannotOpenFile&)
        {
            throw WbemExecException(WbemExecException::INPUT_FILE_CANNOT_OPEN);
        }
    }
    else
    {
        //
        //  Read from cin
        //
        //  (GetLine is defined in Pegasus/Common/String.[h,cpp], but is
        //  not a class member.)
        //
        String line;

        while (GetLine (cin, line))
        {
            content << line << '\n';
        }

        if (content.size () == 0)
        {
            //
            //  No input
            //
            throw WbemExecException(WbemExecException::NO_INPUT);
        }
    }

    //
    //  Make a copy of the content because the XmlParser constructor
    //  modifies the text
    //
    contentCopy << content;

    XmlParser parser ((char*) contentCopy.getData ());

    try
    {
        _connectToServer( client, outPrintWriter );

        //
        //  Encapsulate XML request in an HTTP request
        //

        String hostName;
        if (_hostNameSet && _hostName.size())
        {
            hostName = _hostName + String(":") + _portNumberStr;
        }

        message = XMLProcess::encapsulate( parser, hostName,
                                           _useMPost, _useHTTP11,
                                           content, httpHeaders );

        if (_debugOutput1)
        {
            outPrintWriter << message.getData () << endl;
        }
    }
    catch (const XmlException& xe)
    {
        throw WbemExecException(
            WbemExecException::INVALID_XML, xe.getMessage());
    }
    catch (const WbemExecException&)
    {
        throw;
    }
    catch (const Exception& ex)
    {
        throw WbemExecException(
            WbemExecException::CONNECT_FAIL, ex.getMessage());
    }

    try
    {
        httpResponse = client.issueRequest( message );
    }
    catch (const ConnectionTimeoutException&)
    {
        throw WbemExecException(WbemExecException::TIMED_OUT);
    }
    catch (const UnauthorizedAccess& ex)
    {
        throw WbemExecException(
            WbemExecException::CONNECT_FAIL, ex.getMessage());
    }
    catch (const Exception& ex)
    {
        throw WbemExecException(
            WbemExecException::CONNECT_FAIL, ex.getMessage());
    }

    //
    // Process the response message
    //
    _handleResponse( httpResponse, outPrintWriter, errPrintWriter );
}