set pagesize  100
set linesize  132
set long     20000
set echo on
-- conn sys/oracle as sysdba

create or replace procedure addServletMapping (pattern varchar2, 
                             servletname varchar2, 
                             dispname varchar2, 
                             servletclass varchar2,
                             servletschema varchar2, 
                             language varchar2,
                             description varchar2,
                             securityRole xmltype) as
  xdbconfig xmltype; 
begin
   xdbconfig := dbms_xdb.cfg_get();
   select deleteXML
          (
            xdbconfig,
            '/xdbconfig/sysconfig/protocolconfig/httpconfig/webappconfig/servletconfig/servlet-list/servlet[servlet-name="' || servletname || '"]'
          )
          into xdbconfig
          from dual;

   if (language = 'C') then
     select insertChildXML
            (
              xdbconfig,
              '/xdbconfig/sysconfig/protocolconfig/httpconfig/webappconfig/servletconfig/servlet-list',
              'servlet',
              xmlElement
              (
                "servlet",
                xmlAttributes('http://xmlns.oracle.com/xdb/xdbconfig.xsd' as "xmlns"),
                xmlForest
                (
                   servletname as "servlet-name",
                   language as "servlet-language",
                   dispname as "display-name",
                   description as "description"
                ),
                securityRole            
              )
            )
       into xdbconfig
       from dual;
   else
     select insertChildXML
            (
              xdbconfig,
              '/xdbconfig/sysconfig/protocolconfig/httpconfig/webappconfig/servletconfig/servlet-list',
              'servlet',
              xmlElement
              (
                "servlet",
                xmlAttributes('http://xmlns.oracle.com/xdb/xdbconfig.xsd' as "xmlns"),
                xmlForest
                (
                   servletname as "servlet-name",
                   language as "servlet-language",
                   dispname as "display-name",
                   description as "description",
                   servletclass as "servlet-class",
                   servletschema as "servlet-schema"
                )            
              )
            )
       into xdbconfig
       from dual;
   end if;
     
   select deleteXML
          (
            xdbconfig,
            '/xdbconfig/sysconfig/protocolconfig/httpconfig/webappconfig/servletconfig/servlet-mappings/servlet-mapping[servlet-name="' || servletname || '"]'
          )
          into xdbconfig
          from dual;
   
   select insertChildXML
          (
            xdbconfig,
            '/xdbconfig/sysconfig/protocolconfig/httpconfig/webappconfig/servletconfig/servlet-mappings',
            'servlet-mapping',
            xmltype
            (
               '<servlet-mapping xmlns="http://xmlns.oracle.com/xdb/xdbconfig.xsd">
                  <servlet-pattern>'||pattern||'</servlet-pattern>
                  <servlet-name>'||servletname||'</servlet-name>
                </servlet-mapping>'
            )            
          )
     into xdbconfig
     from dual;

  dbms_xdb.cfg_update(xdbconfig);

end;
/

call addServletMapping(
       '/orawsv/*',
       'orawsv',
       'Oracle Query Web Service',
       null,
       null,
       'C',
       'Web Services Servlet',
       xmltype(
         '<security-role-ref>
            <role-name>XDB_WEBSERVICES</role-name>
            <role-link>XDB_WEBSERVICES</role-link>
          </security-role-ref>'
       )
)
/

call addServletMapping(
       '/orawsdl/*',
       'orawsdl',
       'Oracle WSDLs',
       null,
       null,
       'C',
       'WSDL Servlet',
       xmltype(
         '<security-role-ref>
            <role-name>XDB_WEBSERVICES</role-name>
            <role-link>XDB_WEBSERVICES</role-link>
          </security-role-ref>'
       )
)
/

grant XDB_WEBSERVICES to oe
/

-- For 11g only
grant XDB_WEBSERVICES_OVER_HTTP to oe
/
grant XDB_WEBSERVICES_WITH_PUBLIC to oe
/

-- Clean up afterward
drop procedure addServletMapping
/

