#!/usr/bin/python
from lxml import etree
import tempfile

#Disabling filters in  web.xml

web_xml_path = '/var/lib/pki-ca/webapps/ca/WEB-INF/web.xml';

print("opening "+web_xml_path)
infile = open(web_xml_path, 'rw')
doc  = etree.parse(infile)
infile.close

init_param_names = doc.xpath('//web-app/filter/init-param/param-name')

print ('init-param_names ')

for name in init_param_names:
    text = name.text
    text.strip
    if (name.text == 'active'):
        values = name.xpath('../param-value')
        for value in values:
            value.text = "false"

ofile = open(web_xml_path, 'w')
doc.write(ofile)

print("saving "+web_xml_path)
ofile.close


#adding  <Connector port="8009" protocol="AJP/1.3" redirectPort="9444" /> to server.xml

server_xml_path ="/etc/pki-ca/server.xml"; 

infile = open(server_xml_path, 'rw')
doc  = etree.parse(infile)
infile.close()

catalina =doc.xpath('Service[@name="Catalina"]')

port8009 = catalina[0].xpath('Connector[@port=8009]'  )
if (len(port8009) > 0):
    print ("Port 8009 found " )
else:
    print ("No Port 8009 defined " )
    port8009 = etree.XML(" <Connector port=\"8009\" protocol=\"AJP/1.3\" redirectPort=\"9444\" />") 
    catalina[0].append(port8009)

    ofile = open(server_xml_path, 'w')
    doc.write(ofile)
    ofile.close


