summaryrefslogtreecommitdiffstats
path: root/pki/base/common/src/com/netscape/cms/servlet/common/CMSRequest.java
blob: 61c77287c6081d379e825ee8c56644b7c2140cf3 (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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
// --- 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.cms.servlet.common;


import java.util.Vector;
import java.util.Hashtable;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletContext;
import javax.servlet.ServletConfig;

import com.netscape.certsrv.base.*;
import com.netscape.certsrv.request.IRequest;
import com.netscape.certsrv.request.RequestStatus;
import com.netscape.certsrv.request.RequestId;
import com.netscape.certsrv.apps.*;

/**
 * This represents a user request.
 *
 * @version $Revision$, $Date$
 */
public class CMSRequest {
    // statuses. the first two are out of band.
    public static final Integer UNAUTHORIZED = Integer.valueOf(1);
    public static final Integer SUCCESS = Integer.valueOf(2);
    public static final Integer PENDING = Integer.valueOf(3);
    public static final Integer SVC_PENDING = Integer.valueOf(4);
    public static final Integer REJECTED = Integer.valueOf(5);
    public static final Integer ERROR = Integer.valueOf(6);
    public static final Integer EXCEPTION = Integer.valueOf(7); // unexpected error.

    private static final String RESULT = "cmsRequestResult";

    // Reason message for request failure
    private String reason = null;

    // http parameters - handier than getting directly from http request.
    private IArgBlock mHttpParams = null;

    // http headers & other info.
    private HttpServletRequest mHttpReq = null;

    // http response. 
    private HttpServletResponse mHttpResp = null;

    // http servlet config.
    private ServletConfig mServletConfig = null;

    // http servlet context.
    private ServletContext mServletContext = null;

    // permanent request in request queue. 
    private IRequest mRequest = null;

    // whether request processed successfully
    private Integer mStatus = SUCCESS; 

    // exception message containing error that occured.
    // note exception could also be thrown seperately.
    private String mError = null;

    // any error description.
    private Vector mErrorDescr = null;

    // any request resulting data;
    Object mResult = null;
    Hashtable mResults = new Hashtable();

	/**
     * Constructor
     */
    public CMSRequest() {
    }

    // set methods use by servlets. 

    /**
     * set the HTTP parameters
     */
    public void setHttpParams(IArgBlock httpParams) {
        mHttpParams = httpParams;
    }

    /**
     * set the Request aobject associated with this session
     */
    public void setIRequest(IRequest request) {
        mRequest = request;
    }

    /**
     * set the HTTP Request object associated with this session
     */
    public void setHttpReq(HttpServletRequest httpReq) {
        mHttpReq = httpReq;
    }

    /**
     * set the HTTP Response object which is used to create the
     * HTTP response which is sent back to the user
     */
    public void setHttpResp(HttpServletResponse httpResp) {
        mHttpResp = httpResp;
    }

    /**
     * set the servlet configuration. The servlet configuration is
     * read from the WEB-APPS/web.xml file under the <servlet>
     * XML definition. The parameters are delimited by init-param
     * param-name/param-value options as described in the servlet
     * documentation.
     */
    public void setServletConfig(ServletConfig servletConfig) {
        mServletConfig = servletConfig;
    }

	/* 
     * set the servlet context. the servletcontext has detail
     * about the currently running request
     */
    public void setServletContext(ServletContext servletContext) {
        mServletContext = servletContext;
    }

	/**
	 * Set request status. 
	 * @param status request status. Allowed values are
     * UNAUTHORIZED, SUCCESS, REJECTED, PENDING,  ERROR, SVC_PENDING
     * @throws IllegalArgumentException if status is not one of the above values
     */
    public void setStatus(Integer status) {
        if ( !status.equals( UNAUTHORIZED ) && 
             !status.equals( SUCCESS )      &&
             !status.equals( REJECTED )     && 
             !status.equals( PENDING )      &&
             !status.equals( ERROR )        && 
             !status.equals( SVC_PENDING )  &&
             !status.equals( EXCEPTION ) ) { 
            throw new IllegalArgumentException(CMS.getLogMessage("CMSGW_BAD_REQ_STATUS"));
        }
        mStatus = status;
    }

    public void setError(EBaseException error) {
        mError = error.toString();
    }

    public void setError(String error) {
        mError = error;
    }

    public void setErrorDescription(String descr) {
        if (mErrorDescr == null) 
            mErrorDescr = new Vector();
        mErrorDescr.addElement(descr); 
    }

    public void setResult(Object result) {
        mResult = result;
        mResults.put(RESULT, result);
    }

    public void setResult(String name, Object result) {
        mResults.put(name, result);
    }

    public IArgBlock getHttpParams() {
        return mHttpParams;
    }

    public HttpServletRequest getHttpReq() {
        return mHttpReq;
    }

    public HttpServletResponse getHttpResp() {
        return mHttpResp;
    }

    public ServletConfig getServletConfig() {
        return mServletConfig;
    }

    public ServletContext getServletContext() {
        return mServletContext;
    }

    public IRequest getIRequest() {
        return mRequest;
    }

    public Integer getStatus() {
        return mStatus;
    }

    public String getError() {
        return mError;
    }

    public Vector getErrorDescr() {
        return mErrorDescr;
    }

    public Object getResult() {
        return mResult;
    }

    public Object getResult(String name) {
        return mResults.get(name);
    }

    public void setReason(String reason) {
        this.reason = reason;
    }

    public String getReason() {
        return reason;
    }

    // handy routines for IRequest. 

    public void setExtData(String type, String value) {
        if (mRequest != null) {
            mRequest.setExtData(type, value);
        }
    }

    public String getExtData(String type) {
        if (mRequest != null) {
            return mRequest.getExtDataInString(type);
        } else {
            return null;
        }
    }

    // policy errors; set on rejection or possibly deferral. 
    public Vector getPolicyMessages() {
        if (mRequest != null) {
            return mRequest.getExtDataInStringVector(IRequest.ERRORS);
        }
        return null;
    }

    /** 
     * set default CMS status according to IRequest status. 
     */
    public void setIRequestStatus() throws EBaseException {
        if (mRequest == null) {
            EBaseException e = 
                new ECMSGWException(CMS.getLogMessage("CMSGW_MISSING_REQUEST"));

            throw e;
        }

        RequestStatus status = mRequest.getRequestStatus();

        // completed equivalent to success by default.
        if (status == RequestStatus.COMPLETE) {
            mStatus = CMSRequest.SUCCESS;
            return;
        }
        // unexpected resulting request status. 
        if (status == RequestStatus.REJECTED) {
            mStatus = CMSRequest.REJECTED;
            return;
        } // pending or service pending. 
        else if (status == RequestStatus.PENDING) {
            mStatus = CMSRequest.PENDING;
            return;
        } else if (status == RequestStatus.SVC_PENDING) {
            mStatus = CMSRequest.SVC_PENDING;
            return;
        } else {
            RequestId reqId = mRequest.getRequestId();

            throw new ECMSGWException(
                    CMS.getLogMessage("CMSGW_UNEXPECTED_REQUEST_STATUS_2", 
                        status.toString(), reqId.toString()));
        }
    }

}