OW_URL.cpp

Go to the documentation of this file.
00001 /*******************************************************************************
00002 * Copyright (C) 2001-2004 Vintela, Inc. All rights reserved.
00003 *
00004 * Redistribution and use in source and binary forms, with or without
00005 * modification, are permitted provided that the following conditions are met:
00006 *
00007 *  - Redistributions of source code must retain the above copyright notice,
00008 *    this list of conditions and the following disclaimer.
00009 *
00010 *  - Redistributions in binary form must reproduce the above copyright notice,
00011 *    this list of conditions and the following disclaimer in the documentation
00012 *    and/or other materials provided with the distribution.
00013 *
00014 *  - Neither the name of Vintela, Inc. nor the names of its
00015 *    contributors may be used to endorse or promote products derived from this
00016 *    software without specific prior written permission.
00017 *
00018 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
00019 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00020 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00021 * ARE DISCLAIMED. IN NO EVENT SHALL Vintela, Inc. OR THE CONTRIBUTORS
00022 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00023 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00024 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00025 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00026 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00027 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00028 * POSSIBILITY OF SUCH DAMAGE.
00029 *******************************************************************************/
00030 
00036 #include "OW_config.h"
00037 #include "OW_URL.hpp"
00038 #include "OW_StringBuffer.hpp"
00039 #include "OW_ExceptionIds.hpp"
00040 
00041 namespace OW_NAMESPACE
00042 {
00043 
00044 OW_DEFINE_EXCEPTION_WITH_ID(MalformedURL);
00045 
00047 String
00048 URL::toString() const
00049 {
00050    StringBuffer retval;
00051    if (!scheme.empty())
00052    {
00053       retval += scheme;
00054       retval += "://";
00055    }
00056    if (!principal.empty() || !credential.empty())
00057    {
00058       retval += principal;
00059       if (!credential.empty())
00060       {
00061          retval += ':';
00062          retval += credential;
00063       }
00064       retval += '@';
00065    }
00066    retval += host;
00067    if (!port.empty())
00068    {
00069       retval += ":";
00070       retval += port;
00071    }
00072    if (!namespaceName.empty())
00073    {
00074       retval += '/';
00075       retval += namespaceName;
00076       // can only have a modelPath if we have namespaceName
00077       if (!modelPath.empty())
00078       {
00079          retval += "/:";
00080          retval += modelPath;
00081       }
00082    }
00083    return retval.releaseString();
00084 }
00086 URL::URL() // default for all data members is okay.
00087 {
00088 }
00089 
00091 URL::URL(const String& sUrl) // default for all data members is okay.
00092 {
00093    String sURL = sUrl;
00094    sURL.trim();
00095 
00096    // get the scheme
00097    size_t iBeginIndex = 0;
00098    size_t iEndIndex = sURL.indexOf( "://" );
00099    if ( iEndIndex != String::npos )
00100    {
00101       if ( iEndIndex > 0 )
00102       {
00103          scheme = sURL.substring( 0, iEndIndex ).toLowerCase();
00104       }
00105       iBeginIndex = iEndIndex + 3;
00106    }
00107    // get the userinfo
00108    iEndIndex = sURL.indexOf( '@', iBeginIndex );
00109    if ( iEndIndex != String::npos )
00110    {
00111       String sNamePass = sURL.substring( iBeginIndex, iEndIndex - iBeginIndex );
00112       iBeginIndex = sNamePass.indexOf( ':' );
00113       if ( iBeginIndex != String::npos )
00114       {
00115          if ( iBeginIndex > 0 )
00116          {
00117             principal = sNamePass.substring( 0, iBeginIndex );
00118          }
00119          if ( iBeginIndex < iEndIndex-1 )
00120          {
00121             credential = sNamePass.substring( iBeginIndex + 1 );
00122          }
00123       }
00124       else if ( !sNamePass.empty())
00125       {
00126          principal = sNamePass;
00127       }
00128       iBeginIndex = iEndIndex + 1;
00129    }
00130    // get host[:port]
00131    iEndIndex = sURL.indexOf( '/', iBeginIndex );
00132    if (iEndIndex == String::npos)
00133    {
00134       iEndIndex = sURL.length();
00135    }
00136 
00137    String hostPort = sURL.substring(iBeginIndex, iEndIndex - iBeginIndex);
00138    if (hostPort.empty())
00139    {
00140       OW_THROW(MalformedURLException, String("Invalid URL: " + sUrl).c_str());
00141    }
00142    size_t colonIdx = hostPort.indexOf( ':' );
00143    if ( colonIdx != String::npos )
00144    {
00145       host = hostPort.substring( 0, colonIdx );
00146       port = hostPort.substring( colonIdx + 1 );
00147    }
00148    else
00149    {
00150       host = hostPort;
00151    }
00152    if (host.empty())
00153    {
00154       OW_THROW(MalformedURLException, String("Invalid URL: " + sUrl).c_str());
00155    }
00156 
00157    // get namespaceName
00158    iBeginIndex = sURL.indexOf('/', iBeginIndex);
00159    if ( iBeginIndex != String::npos )
00160    {
00161       iEndIndex = sURL.indexOf("/:",  iBeginIndex);
00162       if (iEndIndex == String::npos)
00163       {
00164          iEndIndex = sURL.length();
00165       }
00166       namespaceName = sURL.substring( iBeginIndex+1, iEndIndex - (iBeginIndex+1) );
00167 
00168       // get modelPath
00169       iBeginIndex = sURL.indexOf("/:", iBeginIndex);
00170       if (iBeginIndex != String::npos)
00171       {
00172          modelPath = sURL.substring(iBeginIndex + 2); // this is the last piece, so grab everything to the end.
00173       }
00174    }
00175 }
00176 
00177 // known schemes
00178 const char* const URL::CIMXML_WBEM = "cimxml.wbem";
00179 const char* const URL::CIMXML_WBEMS = "cimxml.wbems";
00180 const char* const URL::HTTP = "http";
00181 const char* const URL::HTTPS = "https";
00182 const char* const URL::OWBINARY_WBEM = "owbinary.wbem";
00183 const char* const URL::OWBINARY_WBEMS = "owbinary.wbems";
00184 const char* const URL::OWBINARY = "owbinary";
00185 
00186 // special port
00187 const char* const URL::OWIPC = "owipc";
00188 
00189 
00190 } // end namespace OW_NAMESPACE
00191 

Generated on Thu Feb 9 08:48:17 2006 for openwbem by  doxygen 1.4.6