summaryrefslogtreecommitdiffstats
path: root/base/server/upgrade/10.0.1/03-AddRestServlet
blob: c0b9c8ca29915eed0e1686519b61fe374b5d3bd4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/usr/bin/python
# Authors:
#     Ade Lee <alee@redhat.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
# Copyright (C) 2013 Red Hat, Inc.
# All rights reserved.
#

from __future__ import absolute_import
import os
from lxml import etree as ET

import pki
import pki.server.upgrade

class AddRestServlet(pki.server.upgrade.PKIServerUpgradeScriptlet):

    restServicesServletData = """
        <servlet>
            <servlet-name> rest-services </servlet-name>
            <servlet-class> com.netscape.cms.servlet.base.RESTServlet </servlet-class>
        </servlet>"""

    restServicesMappingData = """
        <servlet-mapping>
            <servlet-name>  rest-services </servlet-name>
            <url-pattern>   /rest/*  </url-pattern>
        </servlet-mapping> """

    def __init__(self):
        super(AddRestServlet, self).__init__()
        self.message = 'Add dummy REST servlet to upgraded Dogtag 9 instances'
        self.doc = None
        self.root = None

    def upgrade_subsystem(self, instance, subsystem):
        if subsystem.type >= 10:
            return

        web_xml = os.path.join(
            instance.base_dir,
            'webapps', subsystem.name,
            'WEB-INF', 'web.xml')

        self.backup(web_xml)

        self.doc = ET.parse(web_xml)
        self.root = self.doc.getroot()
        self.add_rest_services_servlet()

        self.doc.write(web_xml)

    def add_rest_services_servlet(self):
        #add rest-services servlet and mapping
        found = False
        for servlet in self.doc.findall('.//servlet'):
            name = servlet.find('servlet-name').text.strip()
            if name == 'rest-services':
                found = True
            if name == 'services':
                index = self.root.index(servlet) + 1
        if not found:
            servlet = ET.fromstring(self.restServicesServletData)
            self.root.insert(index, servlet)

        found = False
        for mapping in self.doc.findall('.//servlet-mapping'):
            name = mapping.find('servlet-name').text.strip()
            if name == 'rest-services':
                found = True
            if name == 'services':
                index = self.root.index(mapping) + 1
        if not found:
            mapping = ET.fromstring(self.restServicesMappingData)
            self.root.insert(index, mapping)