summaryrefslogtreecommitdiffstats
path: root/pki/base/console/src/com/netscape/admin/certsrv/connection/Response.java
blob: bbb511443be6db6b0cec96b4afec5986acabfb62 (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
// --- 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 ---
package com.netscape.admin.certsrv.connection;

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

/**
 * Response - now use vector to maintain the oredering
 *
 * @author kanda
 * @author thomask
 * @author Jack Pan-Chen
 * @version $Revision$, $Date$
 */
public class Response {
	boolean debug = true;
	boolean test = true;
	boolean testsuccess = true; // test success condition?

	public static final int SUCCESS = 0;
    public static final int RESTART = -1;

	private int mRetcode;
	private String mErrorMsg;
	
	//internal structure is changed to vector to maintain
	//the ordering
	
	private Vector mName = new Vector();
	private Vector mValue = new Vector();

	public Response() {
	// for testing only

	}

	public Response(byte[] resp) throws IOException {
		ByteArrayInputStream bis = new ByteArrayInputStream(resp);
		DataInputStream dis = new DataInputStream(bis);
		mRetcode = dis.readInt();
		byte[] mContents = null;

	if (debug)
		//System.out.println("===in Response===\n");

		if ((mRetcode != SUCCESS) && (mRetcode != RESTART)) {
			mErrorMsg = dis.readUTF();
		} else {
			if (resp.length > 4) {
				mContents = new byte[resp.length - 4];
				dis.read(mContents);
			}
		}
		if (mContents != null) {
			String resultStr = new String(mContents);
			StringTokenizer st = new StringTokenizer(resultStr, 
				"&");
			while (st.hasMoreTokens()) {
				String p = st.nextToken();
				int i = p.indexOf("=");
				if (i == -1) {
					return;
				}
				String t = URLdecode(p.substring(0, i));
				String v = URLdecode(p.substring(i + 1));
				mName.addElement(t);
				mValue.addElement(v);
			}
		}
	}

	public int getReturnCode() { 
		return mRetcode; 
	}

	public String getErrorMessage() { 
		return mErrorMsg; 
	}

	/**
	 * URL decodes the given string.
	 */
	public String URLdecode(String s) {
		if (s == null)
			return null;
		ByteArrayOutputStream out = new ByteArrayOutputStream(s.length());
		for (int i = 0; i < s.length(); i++) {
			int c = (int) s.charAt(i);
			if (c == '+') {
				out.write(' ');
			} else if (c == '%') {
				int c1 = Character.digit(s.charAt(++i), 16);
				int c2 = Character.digit(s.charAt(++i), 16);
				out.write((char) (c1 * 16 + c2));
			} else {
				out.write(c);
			}
		} // end for
		return out.toString();
	}

	public Enumeration getNames() {
		return mName.elements();
	}

	public String get(String name) {
	    int i = mName.indexOf(name);
	    String value;
	    try {
		    value =  (String) mValue.elementAt(i);
		} catch (ArrayIndexOutOfBoundsException e) {
		    value = "";    
		}
		return value;
	}
}