summaryrefslogtreecommitdiffstats
path: root/pki/base/silent/src/common/PostQuery.java
blob: b8ab7505ab5d4b68a08221e87568a5afdb6a0fec (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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// --- BEGIN COPYRIGHT BLOCK ---
// 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.
//
// (C) 2007 Red Hat, Inc.
// All rights reserved.
// --- END COPYRIGHT BLOCK ---

import java.net.*;
import java.io.*;
import java.util.*;

import com.netscape.osutil.OSUtil;

/**
 * CMS Test framework .
 * This class submits request to admin server after authenticating with UID and Password. You can get back the response by calling the method. getPage().
 */



public class PostQuery {

    private boolean st;
    private String NmcStatus = "NMC_STATUS: 0";
    private String postQuery = null;
    private String adminID, adminPWD, URLString;

    private StringBuffer stdout = new StringBuffer();

    /**
     * Constructor . Takes the parameters urlstring("http://hostname:<portnumber> , Id for authenticating to the server, password for authentication to the server and query which needs to be submitted to the server 
     */

    public PostQuery(String urlstr, String authid, String authpwd, String querystring) {   

        URLString = urlstr;
        adminID = authid;
        adminPWD = authpwd;
        postQuery = querystring;

    }

    public void setNMCStatus(String m) {
        NmcStatus = m;
    }

    public void setPostQueryString(String querystring) {
        postQuery = querystring;
    }

    public void setAuth(String ID, String Pwd) {
        adminID = ID;
        adminPWD = Pwd;
    }

    public StringBuffer getPage() {
        return stdout;
    }

    public boolean Send() {
        // / This functions connects to the URL and POST HTTP Request . 
        // It compares with NMC_STATUS  and return the status.
        System.out.println(URLString);
        st = false;

        try {

            BufferedReader mbufferedReader = null; 
            URL myUrl = new URL(URLString);
            String userPassword = adminID + ":" + adminPWD;

            System.out.println("adminid=" + adminID);
            System.out.println("adminpwd=" + adminPWD);
            // String encoding = new sun.misc.BASE64Encoder().encode(
            //         userPassword.getBytes());
            String encoding = OSUtil.BtoA(
                    userPassword.getBytes());
            HttpURLConnection URLCon = (HttpURLConnection) myUrl.openConnection();

            URLCon.setRequestProperty("Authorization", "Basic " + encoding);
            URLCon.setDoOutput(true);
            URLCon.setDoInput(true);
            URLCon.setUseCaches(false);
            URLCon.setRequestProperty("Content-type",
                    "application/x-www-form-urlencoded");
            // URLCon.setRequestMethod("POST");
            System.out.println("After post");

            DataOutputStream os = new DataOutputStream(URLCon.getOutputStream()); 

            System.out.println("Query: " + postQuery);

            int querylength = postQuery.length();

            os.writeBytes(postQuery);
            os.flush();
            os.close();
        
            InputStream Content = (InputStream) URLCon.getInputStream();

            System.out.println("Configuring Cert Instance : Return Response");
            BufferedReader in = new BufferedReader(
                    new InputStreamReader(Content));
            String line;

            while ((line = in.readLine()) != null) {
                System.out.println(line);
                stdout.append(line + "\n");
                st = line.startsWith(NmcStatus);
                if (st) {
                    break;
                }
            } 
            URLCon.disconnect();
        } // try 
        catch (MalformedURLException e) {
            System.out.println(URLString + " is not a valid URL.");
	
        } catch (IOException e) {
            System.out.println("exception : " + e.getMessage());
        }
        System.out.println(st);
        return st;
    }

}