summaryrefslogtreecommitdiffstats
path: root/pki/base/common/src/com/netscape/cms/servlet/request/model/KeyRequestDAO.java
blob: b15e17c6dac39ef2164db184970b2b413bcd5bf3 (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
// --- 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) 2011 Red Hat, Inc.
// All rights reserved.
// --- END COPYRIGHT BLOCK ---
package com.netscape.cms.servlet.request.model;

import java.util.ArrayList;
import java.util.List;

import javax.ws.rs.core.UriBuilder;
import javax.ws.rs.core.UriInfo;

import com.netscape.certsrv.apps.CMS;
import com.netscape.certsrv.base.EBaseException;
import com.netscape.certsrv.kra.IKeyRecoveryAuthority;
import com.netscape.certsrv.request.IRequest;
import com.netscape.certsrv.request.IRequestList;
import com.netscape.certsrv.request.IRequestQueue;
import com.netscape.certsrv.request.RequestId;
import com.netscape.certsrv.request.RequestStatus;

/**
 * @author alee
 *
 */
public class KeyRequestDAO {
    private IRequestQueue queue;
    
    public KeyRequestDAO() {
        IKeyRecoveryAuthority kra = null;
        kra = ( IKeyRecoveryAuthority ) CMS.getSubsystem( "kra" );
        queue = kra.getRequestQueue();
    }

    /**
     * This will find the requests in the database matching the specified search parameters
     * Needs input validation and probably paging, maybe using the vlv functions
     * @throws EBaseException 
     */
    public List<KeyRequestInfo> listRequests(String filter, UriInfo uriInfo) throws EBaseException {
        List <KeyRequestInfo> list = new ArrayList<KeyRequestInfo>();  
        IRequestList requests = queue.listRequestsByFilter(filter);
        while (requests.hasMoreElements()) {
            RequestId rid = (RequestId) requests.nextElement();
            IRequest request;
            request = queue.findRequest(rid);
            list.add(createKeyRequestInfo(request, uriInfo));
        }
        return list;
    }
    
    /**
     * Gets info for a specific request
     * @param id
     * @return info for specific request
     * @throws EBaseException 
     */
    public KeyRequestInfo getRequest(String id, UriInfo uriInfo) throws EBaseException {
        IRequest request = queue.findRequest(new RequestId(id));
        if (request == null) {
            return null;
        }
        KeyRequestInfo info = createKeyRequestInfo(request, uriInfo);
        return info;
    }
    /**
     * Submits an archival request and processes it.
     * @param data
     * @return info for the request submitted.
     * @throws EBaseException 
     */
    public KeyRequestInfo submitRequest(ArchivalRequestData data, UriInfo uriInfo) throws EBaseException {
        IRequest request = queue.newRequest(IRequest.SECURITY_DATA_ENROLLMENT_REQUEST);
        //TODO : 
        //set data using request.setExtData(field, data)
        queue.processRequest(request);
        return createKeyRequestInfo(request, uriInfo);
    }
    /**
     * Submits a key recovery request.
     * @param data
     * @return info on the recovery request created
     * @throws EBaseException 
     */
    public KeyRequestInfo submitRequest(RecoveryRequestData data, UriInfo uriInfo) throws EBaseException { 
        IRequest request = queue.newRequest(IRequest.SECURITY_DATA_RECOVERY_REQUEST);
        // set data using request.setExtData(field, data)
        queue.processRequest(request);
        return createKeyRequestInfo(request, uriInfo);
    }

    public void approveRequest(String id) throws EBaseException {
        IRequest request = queue.findRequest(new RequestId(id));
        request.setRequestStatus(RequestStatus.APPROVED);
    }
    
    public void rejectRequest(String id) throws EBaseException {
        IRequest request = queue.findRequest(new RequestId(id));
        request.setRequestStatus(RequestStatus.CANCELED);
    }
    
    public void cancelRequest(String id) throws EBaseException {
        IRequest request = queue.findRequest(new RequestId(id));
        request.setRequestStatus(RequestStatus.REJECTED);
    }
    
    public KeyRequestInfo createKeyRequestInfo(IRequest request, UriInfo uriInfo) {
        KeyRequestInfo ret = new KeyRequestInfo();
        
        ret.setRequestType(request.getRequestType());
        ret.setRequestStatus(request.getRequestStatus().toString());
        
        String rid = request.getRequestId().toString();
        UriBuilder reqBuilder = uriInfo.getBaseUriBuilder();
        reqBuilder.path("/keyrequest/" + rid);
        ret.setRequestURL(reqBuilder.build().toString());
        
        String kid = request.getExtDataInString("keyrecord");
        UriBuilder keyBuilder = uriInfo.getBaseUriBuilder();
        keyBuilder.path("/key/" + kid);
        ret.setKeyURL(keyBuilder.build().toString());
        
        return ret;
    }
}