summaryrefslogtreecommitdiffstats
path: root/base/tps/src/org/dogtagpki/server/tps/TPSSession.java
blob: 6f175e2076b5d23c6eee6a1a1601ea061104ac7e (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
// --- 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) 2013 Red Hat, Inc.
// All rights reserved.
// --- END COPYRIGHT BLOCK ---
package org.dogtagpki.server.tps;

import java.io.IOException;

import org.dogtagpki.server.tps.dbs.TokenRecord;
import org.dogtagpki.server.tps.main.ExternalRegAttrs;
import org.dogtagpki.server.tps.processor.TPSEnrollProcessor;
import org.dogtagpki.server.tps.processor.TPSPinResetProcessor;
import org.dogtagpki.server.tps.processor.TPSProcessor;
import org.dogtagpki.tps.TPSConnection;
import org.dogtagpki.tps.main.TPSException;
import org.dogtagpki.tps.msg.BeginOpMsg;
import org.dogtagpki.tps.msg.EndOpMsg;
import org.dogtagpki.tps.msg.TPSMessage;

import com.netscape.certsrv.apps.CMS;

public class TPSSession {

    private TPSConnection connection;
    private String ipAddress; /* remote IP */
    private TokenRecord tokenRecord;

    private ExternalRegAttrs extRegAttrs;

    public TPSSession(TPSConnection conn, String ip) {

        if (conn == null) {
            throw new NullPointerException("TPSSession incoming connection is null!");
        }

        CMS.debug("TPSSession constructor conn: " + conn);
        connection = conn;

        if (ip == null) {
         // probably unlikely to happen; log it and continue anyway
            CMS.debug("TPSSession constructor remote ipAddress null");
        } else {
            CMS.debug("TPSSession constructor remote ipAddress: " + getIpAddress());
        }
        setIpAddress(ip);

    }

    public TPSConnection getConnection() {
        return connection;
    }

    public TPSMessage read() throws IOException {
        TPSMessage message = null;

        CMS.debug("TPSSession.process() about to call read on connection : " + connection);

        try {
            message = connection.read();
            CMS.debug("TPSSession.process() created message " + message);

        } catch (IOException e) {
            //Catch here so we can log
            CMS.debug("TPSSession.process: Exception reading from the client: " + e.toString());
            throw e;
        }

        return message;
    }

    public void write(TPSMessage msg) throws IOException {

        try {
            connection.write(msg);
        } catch (Exception e) {
            //Catch here so we can log
            CMS.debug("Exception writing to client: " + e.toString());
            throw e;
        }

    }

    public void process() throws IOException {
        EndOpMsg.TPSStatus status = EndOpMsg.TPSStatus.STATUS_NO_ERROR;
        CMS.debug("In TPSSession.process()");

        TPSMessage firstMsg = read();

        TPSMessage.MsgType msg_type = firstMsg.getType();
        TPSMessage.OpType op_type = firstMsg.getOpType();

        if (msg_type != TPSMessage.MsgType.MSG_BEGIN_OP) {
            throw new IOException("Wrong first message type read in TPSSession.process!");
        }

        int result = EndOpMsg.RESULT_ERROR;
        BeginOpMsg beginOp = (BeginOpMsg) firstMsg;
        try {
            switch (op_type) {
            case OP_FORMAT:

                //Assume success, processor will indicate otherwise
                result = EndOpMsg.RESULT_GOOD;
                TPSProcessor processor = new TPSProcessor(this);
                processor.process(beginOp);
                break;

            case OP_ENROLL:
                //Assume success, processor will indicate otherwise
                result = EndOpMsg.RESULT_GOOD;
                TPSEnrollProcessor enrollProcessor = new TPSEnrollProcessor(this);
                enrollProcessor.process(beginOp);
                break;
            case OP_RENEW:
                break;
            case OP_RESET_PIN:
                result = EndOpMsg.RESULT_GOOD;
                TPSPinResetProcessor pinResetProcessor = new TPSPinResetProcessor(this);
                pinResetProcessor.process(beginOp);
                break;
            case OP_UNBLOCK:
                break;
            case OP_UNDEFINED:
                break;
            default:
                break;

            }
        } catch (TPSException e) {
            //Get the status from the exception and return it to the client.
            CMS.debug("TPSSession.process: Message processing failed: " + e);
            status = e.getStatus();
            result = EndOpMsg.RESULT_ERROR;
        } catch (IOException e) {
            CMS.debug("TPSSession.process: IO error happened during processing: " + e);
            // We get here we are done.
            throw e;

        }

        EndOpMsg endOp = new EndOpMsg(firstMsg.getOpType(), result, status);
        write(endOp);

        CMS.debug("TPSSession.process: leaving: result: " + result + " status: " + status);

    }

    public TokenRecord getTokenRecord() {
        return tokenRecord;
    }

    public void setTokenRecord(TokenRecord tokenRecord) {
        this.tokenRecord = tokenRecord;
    }

    public String getIpAddress() {
        return ipAddress;
    }

    private void setIpAddress(String ipAddress) {
        this.ipAddress = ipAddress;
    }

    public void setExternalRegAttrs(ExternalRegAttrs erAttrs) {
        extRegAttrs = erAttrs;
    }

    public ExternalRegAttrs getExternalRegAttrs() {
        return extRegAttrs;
    }
}