summaryrefslogtreecommitdiffstats
path: root/pki/base/common/src/com/netscape/cmscore/connector/HttpPKIMessage.java
blob: b9e32cbd9286057856acd61d2ec24fa32d7d8efd (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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
// --- 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.cmscore.connector;


import com.netscape.certsrv.request.IRequest;
import com.netscape.certsrv.connector.*;
import com.netscape.certsrv.apps.*;
import com.netscape.cmscore.util.Debug;
import java.util.*;
import java.io.*;


/**
 * simple name/value pair message. 
 */
public class HttpPKIMessage implements IHttpPKIMessage {
    // initialized to "" because nulls don't serialize well.
    public String reqType = "";
    public String reqId = "";
    protected String reqStatus = "";
    protected Vector mNameVals = new Vector(); // sequence of name/vals.

    public HttpPKIMessage() {
    }

    public String getReqStatus() {
        return reqStatus;
    }

    public String getReqType() {
        return reqType;
    }

    public String getReqId() {
        return reqId;
    }

    /**
     * copy contents of request to make a simple name/value message.
     */
    public void fromRequest(IRequest r) {
        // actually don't need to copy source id since 
        reqType = r.getRequestType();
        reqId = r.getRequestId().toString();
        reqStatus = r.getRequestStatus().toString();

        CMS.debug("HttpPKIMessage.fromRequest: requestId=" + r.getRequestId().toString() + " requestStatus=" + reqStatus + " instance=" + r);

        String attrs[] = RequestTransfer.getTransferAttributes(r);
        int len = attrs.length;
        String[] names = attrs;
        Object value = null;

        for (int i = 0; i < len; i++) {
            String key = names[i];
            if (r.isSimpleExtDataValue(key)) {
                value = r.getExtDataInString(key);
            } else {
                value = r.getExtDataInHashtable(key);
            }
            if (value != null) {
                mNameVals.addElement(key);
                mNameVals.addElement(value);
            }
        }
    }

    /**
     * copy contents to request.
     */
    public void toRequest(IRequest r) {
        // id, type and status 
        // type had to have been set in instantiation.
        // id is checked but not reset.
        // request status cannot be set, but can be looked at.
        reqStatus = r.getRequestStatus().toString();
        CMS.debug("HttpPKMessage.toRequest: requestStatus=" + reqStatus);

        int len = RequestTransfer.getTransferAttributes(r).length;
        String key;
        Object value;
        Enumeration enum1 = mNameVals.elements();

        while (enum1.hasMoreElements()) {
            key = (String) enum1.nextElement();
            try {
                value = enum1.nextElement();
                if (value instanceof String) {
                    r.setExtData(key, (String) value);
                } else if (value instanceof Hashtable) {
                    r.setExtData(key, (Hashtable) value);
                } else {
                    CMS.debug("HttpPKIMessage.toRequest(): key: " + key +
                    " has unexpected type " + value.getClass().toString());
                }
            } catch (NoSuchElementException e) {
              CMS.debug("Incorrect pairing of name/value for " + key);
            }
        }
    }

    private void writeObject(java.io.ObjectOutputStream out)
        throws IOException {
        CMS.debug("writeObject");
        out.writeObject(reqType);
        if (Debug.ON)
            Debug.trace("read object req type " + reqType);
        out.writeObject(reqId);
        if (Debug.ON)
            Debug.trace("read object req id " + reqId);
        out.writeObject(reqStatus);
        if (Debug.ON)
            Debug.trace("read object req source status " + reqStatus);
        Enumeration enum1 = mNameVals.elements();

        while (enum1.hasMoreElements()) {
            Object key = null;
            Object val = null;
            key = enum1.nextElement();
            try {
              val = enum1.nextElement();
              // test if key and value are serializable
              ObjectOutputStream os = 
                new ObjectOutputStream(new ByteArrayOutputStream());
              os.writeObject(key);
              os.writeObject(val);

              // ok, if we dont have problem serializing the objects,
              // then write the objects into the real object stream
              out.writeObject(key);
              out.writeObject(val);
            } catch (Exception e) {
              // skip not serialiable attribute in DRM
              // DRM does not need to store the enrollment request anymore
              CMS.debug("HttpPKIMessage:skipped key=" + 
                key.getClass().getName());
              if (val == null) {
                CMS.debug("HttpPKIMessage:skipped val= null");
              } else {
                CMS.debug("HttpPKIMessage:skipped val=" + 
                  val.getClass().getName());
              } 
            }
        }
    }

    private void readObject(java.io.ObjectInputStream in)
        throws IOException, ClassNotFoundException, OptionalDataException {
        reqType = (String) in.readObject();
        reqId = (String) in.readObject();
        reqStatus = (String) in.readObject();
        mNameVals = new Vector();
        Object keyorval = null;

        try {
            boolean iskey = true;

            while (true) {
                boolean skipped = false;
                try {
                   keyorval = in.readObject();
                } catch (OptionalDataException e) {
                   throw e;
                } catch (IOException e) {
                   // just skipped parameter
                  CMS.debug("skipped attribute in request e="+e);
                  if (!iskey) {
                     int s = mNameVals.size();
                     if (s > 0) {
                       // remove previous key if this is value
                       mNameVals.removeElementAt(s - 1);
                       skipped = true;
                       keyorval = "";
                     }
                  }
                }
                if (iskey) {
                    if (Debug.ON)
                        Debug.trace("read key " + keyorval);
                    iskey = false;
                } else {
                    if (Debug.ON)
                        Debug.trace("read val " + keyorval);
                    iskey = true;
                }
                if (Debug.ON)
                    Debug.trace("read " + keyorval);
                if (!skipped) {
                  if (keyorval == null) 
                    break;
                  mNameVals.addElement(keyorval);
                }
            }
        } catch (OptionalDataException e) {
            if (e.eof == true) {
                if (Debug.ON)
                    Debug.trace("end of stream");
            } else {
                if (Debug.ON)
                    Debug.trace(" " + e.length);
                throw e;
            }
        }
    }
}