OW_CIMUrl.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_CIMUrl.hpp"
00038 #include "OW_String.hpp"
00039 #include "OW_StrictWeakOrdering.hpp"
00040 #include "OW_Bool.hpp"
00041 #include "OW_COWIntrusiveCountableBase.hpp"
00042 #include "OW_StringBuffer.hpp"
00043 
00044 #if defined(OW_HAVE_ISTREAM) && defined(OW_HAVE_OSTREAM)
00045 #include <istream>
00046 #include <ostream>
00047 #else
00048 #include <iostream>
00049 #endif
00050 
00051 namespace OW_NAMESPACE
00052 {
00053 
00054 using std::istream;
00055 using std::ostream;
00056 
00057 struct CIMUrl::URLData : public COWIntrusiveCountableBase
00058 {
00059    URLData() :
00060       m_port(0),
00061       m_localHost(true) {}
00062    String m_spec;
00063    String m_protocol;
00064    String m_host;
00065    Int32 m_port;
00066    String m_file;
00067    String m_ref;
00068    Bool m_localHost;
00069    URLData* clone() { return new URLData(*this); }
00070 };
00072 bool operator<(const CIMUrl::URLData& x, const CIMUrl::URLData& y)
00073 {
00074    return x.m_spec < y.m_spec;
00075 }
00077 // PUBLIC
00078 CIMUrl::CIMUrl() :
00079    CIMBase(), m_pdata(new URLData)
00080 {
00081    setDefaultValues();
00082 }
00084 // PUBLIC
00085 CIMUrl::CIMUrl(CIMNULL_t) :
00086    CIMBase(), m_pdata(0)
00087 {
00088 }
00090 // PUBLIC
00091 CIMUrl::CIMUrl(const String& spec) :
00092    CIMBase(), m_pdata(new URLData)
00093 {
00094    m_pdata->m_spec = spec;
00095    setComponents();
00096 }
00098 // PUBLIC
00099 CIMUrl::CIMUrl(const String& protocol, const String& host,
00100    const String& file, Int32 port) :
00101    CIMBase(), m_pdata(new URLData)
00102 {
00103    m_pdata->m_protocol = protocol;
00104    m_pdata->m_host = host;
00105    m_pdata->m_port = port;
00106    m_pdata->m_file = file;
00107    setDefaultValues();
00108 }
00110 // PUBLIC
00111 CIMUrl::CIMUrl(const CIMUrl& context, const String& spec) :
00112    CIMBase(), m_pdata(new URLData)
00113 {
00114    m_pdata->m_spec = spec;
00115    setComponents();
00116    if (m_pdata->m_protocol.empty())
00117    {
00118       m_pdata->m_protocol = context.getProtocol();
00119    }
00120    else
00121    {
00122       if (m_pdata->m_protocol == context.m_pdata->m_protocol)
00123       {
00124          m_pdata->m_host = context.m_pdata->m_host;
00125          m_pdata->m_port = context.m_pdata->m_port;
00126          m_pdata->m_file = context.m_pdata->m_file;
00127       }
00128    }
00129    setDefaultValues();
00130 }
00132 CIMUrl::CIMUrl(const CIMUrl& x)
00133    : CIMBase() , m_pdata(x.m_pdata)
00134 {
00135 }
00137 CIMUrl::~CIMUrl()
00138 {
00139 }
00141 CIMUrl&
00142 CIMUrl::operator= (const CIMUrl& x)
00143 {
00144    m_pdata = x.m_pdata;
00145    return *this;
00146 }
00148 void
00149 CIMUrl::setNull()
00150 {
00151    m_pdata = NULL;
00152 }
00154 String
00155 CIMUrl::getSpec() const {  return m_pdata->m_spec; }
00157 String
00158 CIMUrl::getProtocol() const {  return m_pdata->m_protocol; }
00160 String
00161 CIMUrl::getHost() const {  return m_pdata->m_host; }
00163 Int32
00164 CIMUrl::getPort() const {  return m_pdata->m_port; }
00166 String
00167 CIMUrl::getFile() const {  return m_pdata->m_file; }
00169 String
00170 CIMUrl::getRef() const {  return m_pdata->m_ref; }
00172 bool
00173 CIMUrl::isLocal() const {  return m_pdata->m_localHost; }
00175 String
00176 CIMUrl::toString() const {  return String(m_pdata->m_spec); }
00178 // PRIVATE
00179 void
00180 CIMUrl::setLocalHost()
00181 {
00182    m_pdata->m_localHost = false;
00183    m_pdata->m_host.trim();
00184    if (m_pdata->m_host.empty()
00185       || m_pdata->m_host.equals("127.0.0.1")
00186       || m_pdata->m_host.equalsIgnoreCase("localhost"))
00187    {
00188       m_pdata->m_localHost = true;
00189       m_pdata->m_host = "127.0.0.1";
00190    }
00191 }
00193 // PRIVATE
00194 void
00195 CIMUrl::setDefaultValues()
00196 {
00197    m_pdata->m_protocol.trim();
00198    if (m_pdata->m_protocol.empty())
00199    {
00200       m_pdata->m_protocol = "http";
00201    }
00202    setLocalHost();
00203    if (m_pdata->m_port <= 0)
00204    {
00205       m_pdata->m_port = 5988;
00206    }
00207    m_pdata->m_file.trim();
00208    if (m_pdata->m_file.empty())
00209    {
00210       m_pdata->m_file = "cimom";
00211       m_pdata->m_ref = String();
00212    }
00213    buildSpec();
00214 }
00216 // PUBLIC
00217 CIMUrl&
00218 CIMUrl::setHost(const String& host)
00219 {
00220    m_pdata->m_host = host;
00221    setLocalHost();
00222    buildSpec();
00223    return *this;
00224 }
00226 CIMUrl&
00227 CIMUrl::setProtocol(const String& protocol)
00228 {
00229    m_pdata->m_protocol = protocol;
00230    buildSpec();
00231    return *this;
00232 }
00234 // PUBLIC
00235 bool
00236 CIMUrl::equals(const CIMUrl& arg) const
00237 {
00238    return (m_pdata->m_protocol == arg.m_pdata->m_protocol
00239       && m_pdata->m_host == arg.m_pdata->m_host
00240       && m_pdata->m_port == arg.m_pdata->m_port
00241       && m_pdata->m_file == arg.m_pdata->m_file
00242       && m_pdata->m_ref == arg.m_pdata->m_ref);
00243 }
00245 // PUBLIC
00246 bool
00247 CIMUrl::sameFile(const CIMUrl& arg) const
00248 {
00249    return (m_pdata->m_protocol == arg.m_pdata->m_protocol
00250       && m_pdata->m_host == arg.m_pdata->m_host
00251       && m_pdata->m_port == arg.m_pdata->m_port
00252       && m_pdata->m_file == arg.m_pdata->m_file);
00253 }
00255 // PRIVATE
00256 void
00257 CIMUrl::setComponents()
00258 {
00259    if (m_pdata->m_spec.empty())
00260    {
00261       return;
00262    }
00263    String spec(m_pdata->m_spec);
00264    m_pdata->m_protocol = String();
00265    m_pdata->m_host = String();
00266    m_pdata->m_port = 0;
00267    m_pdata->m_file = String();
00268    m_pdata->m_ref = String();
00269    m_pdata->m_localHost = true;
00270    size_t ndx = spec.indexOf("://");
00271    if (ndx != String::npos)
00272    {
00273       m_pdata->m_protocol = spec.substring(0, ndx);
00274       spec = spec.substring(ndx+3);
00275    }
00276    // parse and remove name and password
00277    ndx = spec.indexOf('@');
00278    if (ndx != String::npos)
00279    {
00280       spec = spec.substring(ndx + 1);
00281    }
00282    ndx = spec.indexOf('/');
00283    if (ndx != String::npos)
00284    {
00285       m_pdata->m_host = spec.substring(0, ndx);
00286       m_pdata->m_file = spec.substring(ndx+1);
00287       checkRef();
00288    }
00289    else
00290    {
00291       m_pdata->m_host = spec.substring(0);
00292    }
00293    ndx = m_pdata->m_host.indexOf(':');
00294    if (ndx != String::npos)
00295    {
00296       String sport = m_pdata->m_host.substring(ndx+1);
00297       try
00298       {
00299          m_pdata->m_port = sport.toInt32();
00300       }
00301       catch (StringConversionException&)
00302       {
00303          m_pdata->m_port = 5988;
00304       }
00305       m_pdata->m_host = m_pdata->m_host.substring(0, ndx);
00306    }
00307    checkRef();
00308    setDefaultValues();
00309 }
00311 // PRIVATE
00312 void
00313 CIMUrl::checkRef()
00314 {
00315    if (!m_pdata->m_file.empty())
00316    {
00317       size_t ndx = m_pdata->m_file.indexOf('#');
00318       if (ndx != String::npos)
00319       {
00320          m_pdata->m_ref = m_pdata->m_file.substring(ndx+1);
00321          m_pdata->m_file = m_pdata->m_file.substring(0, ndx);
00322       }
00323    }
00324 }
00326 // PRIVATE
00327 void
00328 CIMUrl::buildSpec()
00329 {
00330    StringBuffer tmp(m_pdata->m_protocol);
00331    tmp += "://";
00332    tmp += m_pdata->m_host;
00333    if (m_pdata->m_port > 0)
00334    {
00335       tmp += ":";
00336       tmp += String(m_pdata->m_port);
00337    }
00338    if (!m_pdata->m_file.empty())
00339    {
00340       tmp += '/';
00341       tmp += m_pdata->m_file;
00342    }
00343    if (!m_pdata->m_ref.empty())
00344    {
00345       tmp += '#';
00346       tmp += m_pdata->m_ref;
00347    }
00348 
00349    m_pdata->m_spec = tmp.releaseString();
00350 }
00352 // PUBLIC
00353 void
00354 CIMUrl::readObject(istream &istrm)
00355 {
00356    CIMBase::readSig( istrm, OW_CIMURLSIG );
00357    String spec;
00358    spec.readObject(istrm);
00359    
00360    if (!m_pdata)
00361    {
00362       m_pdata = new URLData;
00363    }
00364    m_pdata->m_spec = spec;
00365    setComponents();
00366 }
00368 // PUBLIC
00369 void
00370 CIMUrl::writeObject(ostream &ostrm) const
00371 {
00372    CIMBase::writeSig( ostrm, OW_CIMURLSIG );
00373    m_pdata->m_spec.writeObject(ostrm);
00374 }
00376 String
00377 CIMUrl::toMOF() const {  return "UNIMPLEMENTED"; }
00379 bool operator<(const CIMUrl& lhs, const CIMUrl& rhs)
00380 {
00381    return *lhs.m_pdata < *rhs.m_pdata;
00382 }
00383 
00384 } // end namespace OW_NAMESPACE
00385 

Generated on Thu Feb 9 08:47:55 2006 for openwbem by  doxygen 1.4.6