-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCFSSLTransportLayer.java
More file actions
249 lines (224 loc) · 7.95 KB
/
CFSSLTransportLayer.java
File metadata and controls
249 lines (224 loc) · 7.95 KB
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
package edge;
import java.io.*;
import java.net.UnknownHostException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.net.ssl.*;
public class CFSSLTransportLayer
{
final String host = "< host >";
final int port = < port >;
private static SSLSocket sslServerSocket;
static boolean debug = true;
final String keyStoreLocation ="/Library/WebServer/Documents/workspace/.keystore";
final String password = "";
final int socketTimeout = 60000; // Set timeout to one minute as per specs.
SSLSocketFactory factory;
PrintWriter pw;
BufferedReader in = null;
String error = "";
public static void main(String[] args) {
CFSSLTransportLayer tl = new CFSSLTransportLayer();
System.out.println( tl.send( "< Message > ", true ) );
tl.closeConnection();
}
public CFSSLTransportLayer()
{
try
{
// KeyStore myKeyStore = KeyStore.getInstance( "JKS" );
// myKeyStore.load( new FileInputStream( keyStoreLocation ), password.toCharArray() );
// TrustManagerFactory myTrustFactory = TrustManagerFactory.getInstance( "SunX509", "SunJSSE" );
// myTrustFactory.init( myKeyStore );
// TrustManager[] myTrustManager = myTrustFactory.getTrustManagers();
SSLContext ctx = SSLContext.getDefault();
factory = ctx.getSocketFactory();
}
catch ( Exception e )
{
String myError = "Error creating SSL objects.";
System.err.println( myError );
setError( myError );
e.printStackTrace( System.out );
}
try
{
sslServerSocket = ( SSLSocket ) factory.createSocket( host, port );
// Set the timeout
sslServerSocket.setSoTimeout( socketTimeout );
if ( debug )
{
System.out.println( "*************************************************" );
System.out.println( "***********Secure socket made********************" );
System.out.println( "*************************************************" );
System.out.println();
}
pw = new PrintWriter( sslServerSocket.getOutputStream() );
}
catch ( UnknownHostException e )
{
String myError = "Unknown host.";
System.err.println( myError );
setError( myError );
}
catch ( java.net.SocketTimeoutException e )
{
String myError = "The Socket waited " + socketTimeout / 1000 + " seconds for a response.";
System.err.println( myError );
setError( myError );
}
catch ( IOException e )
{
String myError = "I/O Exception.";
System.err.println( myError );
setError( myError );
}
catch ( Exception e )
{
String myError = "An exception has occurred, error message: " + e.getMessage();
System.err.println( myError );
setError( myError );
}
}
private void setError( String error )
{
this.error = this.error + error + "\n";
}
private String getError()
{
return this.error;
}
public String send ( String message, boolean isLast )
{
String response;
String messageToSend = "BDAT " + message.length();
if ( isLast )
{
messageToSend = messageToSend + " LAST";
}
messageToSend = messageToSend + "\r\n" + message;
// Sent the message to EDGE
pw.print( messageToSend );
pw.flush();
response = getResponse();
if ( debug )
{
System.out.println( "Message that was sent to EDGE " + messageToSend );
System.out.println( "Message sent to EDGE." );
response = "response = " + response + getError();
}
return response;
}
private String getResponse()
{
String messageString = "";
try
{
if ( debug )
{
System.out.println( "*************************************************" );
System.out.println( "************Server Output************************" );
System.out.print( "Outputting reply from server: " );
}
in = new BufferedReader( new InputStreamReader( sslServerSocket.getInputStream() ) );
String header = "";
char[] myChar = new char[ 1 ];
// Making sure that we do not enter an infinite loop
int i = 0;
final int MAX_LENGTH_TO_READ = 30;
// Loop until we find the newline character, this should give us the header
while ( !header.endsWith( "\n" ) && i < MAX_LENGTH_TO_READ )
{
in.read( myChar, 0, 1 );
String myStringchar = new String( myChar );
header += myStringchar;
i++;
}
if ( i == MAX_LENGTH_TO_READ )
{
String myError = "Could not read the header!";
System.err.println( myError );
setError( myError );
}
int byteToRead = 0;
// Parse out the message length
Pattern myPattern = Pattern.compile( "\\d+" );
Matcher myMatch = myPattern.matcher( header );
if ( myMatch.find() )
{
byteToRead = Integer.parseInt( header.substring( myMatch.start(), myMatch.end() ) );
}
else
{
String myError = "Message length could not be found in the header!";
System.err.println( myError );
setError( myError );
// take your action in case no integer found in yourString
throw new Exception("MessageLengthNotFound", new Throwable( "The message length could not be found in the header." ) );
}
char[] myMessage = new char[ byteToRead ];
// Try and read the rest of the message
try
{
int remaining = byteToRead;
// Loop untill all bytes are read
while ( remaining > 0 )
{
remaining -= in.read( myMessage, byteToRead - remaining, remaining );
}
}
catch ( Exception e )
{
String myError = "Error reading the rest of the message, byteToRead: " + byteToRead + ". message: " + e.getMessage() + "\n";
System.err.println( myError );
setError( myError );
}
// Convert the message array to a string
messageString = new String( myMessage );
System.out.println( messageString );
if ( debug )
{
System.out.println( "***********Server output complete****************" );
System.out.println( "*************************************************" );
System.out.println();
}
}
catch ( IOException e )
{
String myError = "There was an error reading the Input Stream. The error is " + e.getMessage();
System.err.println( myError );
setError( myError );
}
catch ( Exception e )
{
e.printStackTrace( System.out );
}
return messageString;
}
public void closeConnection()
{
try
{
in.close();
pw.close();
// close socket
sslServerSocket.close();
}
catch ( IOException e )
{
System.out.println( "Error trying to close the socket" + e.getMessage() + "\n" );
}
}
protected void finalize()
throws Throwable
{
try
{
closeConnection();
}
finally
{
super.finalize();
}
}
}