summaryrefslogtreecommitdiffstats
path: root/base/tps/src/org/dogtagpki/server/tps/TPSPhoneHome.java
blob: e0b3b6b878739cdf54a51ef40a5d5202aa0cd762 (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
89
90
91
92
93
94
95
package org.dogtagpki.server.tps;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.dogtagpki.tps.main.TPSBuffer;

import com.netscape.certsrv.apps.CMS;

public class TPSPhoneHome extends HttpServlet {

    private static final long serialVersionUID = 1864386666927370987L;
    private static String phoneHomeName = "phoneHome.xml";

    public void service(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        //Simply return xml file to the client
        //In the future we could get this info from elsewhere such as LDAP

        CMS.debug("TPSPhoneHome entering.");

        renderPhoneHome(request, response);
    }

    private void renderPhoneHome(HttpServletRequest request, HttpServletResponse response) throws ServletException,
            IOException {
        ServletOutputStream stream = null;
        BufferedInputStream buf = null;
        FileInputStream input = null;

        try {

            stream = response.getOutputStream();
            response.setContentType("application/xml");

            String confPath = getConfigPath();

            confPath += File.separator + phoneHomeName;

            input = new FileInputStream(confPath);
            // InputStream input = ctx.getResourceAsStream(phoneHomeName);
            buf = new BufferedInputStream(input);

            int readBytes = 0;
            TPSBuffer readData = new TPSBuffer();
            while ((readBytes = buf.read()) != -1) {
                stream.write(readBytes);
                readData.add((byte) readBytes);
            }

            CMS.debug("TPSPhoneHome.renderPhoneHome: data: " + readData.toHexString());

        } catch (IOException e) {
            CMS.debug("TPSPhoneHome.renderPhoneHome: Error encountered:  " + e);
            throw new ServletException("TPSPhoneHome.renderPhoneHome: Error encountered:  " + e);
        } finally {
            if (stream != null)
                stream.close();
            if (buf != null)
                buf.close();
            if (input != null)
                input.close();
        }

    }

    private String getConfigPath() {

        String path = null;
        String context = getServletContext().getContextPath();

        // get subsystem name by removing the / prefix from the context
        String subsystem = context.startsWith("/") ? context.substring(1) : context;

        // catalina.base points to instance dir
        String instanceDir = System.getProperty("catalina.base");

        //Finish off path of conf directory
        path = instanceDir + File.separator + "conf" + File.separator +
                subsystem + File.separator;

        CMS.debug("TPSPhoneHome.getConfigPath: returning: " + path);

        return path;

    }

}