1 14 15 package org.quickserver.net.server; 16 17 import java.io.*; 18 import java.net.SocketTimeoutException ; 19 import org.quickserver.net.AppException; 20 import org.quickserver.net.ConnectionLostException; 21 import org.quickserver.util.io.*; 22 23 59 public abstract class QuickAuthenticator 60 implements Authenticator { 61 62 public abstract boolean askAuthorisation(ClientHandler clientHandler) 63 throws IOException, AppException; 64 65 66 71 public void sendString(ClientHandler clientHandler, String msg) 72 throws IOException { 73 if(msg!=null) { 74 if(clientHandler.getDataMode(DataType.OUT)!=DataMode.STRING) 75 clientHandler.setDataMode(DataMode.STRING, DataType.OUT); 76 clientHandler.sendClientMsg(msg); 77 } 78 } 79 80 88 public String askStringInput(ClientHandler clientHandler, String msg) 89 throws IOException { 90 if(msg!=null) { 91 if(clientHandler.getDataMode(DataType.OUT)!=DataMode.STRING) 92 clientHandler.setDataMode(DataMode.STRING, DataType.OUT); 93 clientHandler.sendClientMsg(msg); 94 } 95 if(clientHandler.getDataMode(DataType.IN)!=DataMode.STRING) 96 clientHandler.setDataMode(DataMode.STRING, DataType.IN); 97 98 String data = null; 99 if(clientHandler.hasEvent(ClientEvent.RUN_BLOCKING)) { 100 data = clientHandler.getBufferedReader().readLine(); 101 } else { 102 ByteBufferInputStream bbin = (ByteBufferInputStream) 103 clientHandler.getInputStream(); 104 data = bbin.readLine(); 105 } 106 107 if(data!=null) 108 return data; 109 else 110 throw new ConnectionLostException(); 111 } 112 113 118 public void sendObject(ClientHandler clientHandler, Object msg) 119 throws IOException { 120 if(msg!=null) { 121 if(clientHandler.getDataMode(DataType.OUT)!=DataMode.OBJECT) 122 clientHandler.setDataMode(DataMode.OBJECT, DataType.OUT); 123 clientHandler.sendClientObject(msg); 124 } 125 } 126 127 135 public Object askObjectInput(ClientHandler clientHandler, Object msg) 136 throws IOException, ClassNotFoundException { 137 if(msg!=null) { 138 if(clientHandler.getDataMode(DataType.OUT)!=DataMode.OBJECT) 139 clientHandler.setDataMode(DataMode.OBJECT, DataType.OUT); 140 clientHandler.sendClientObject(msg); 141 } 142 if(clientHandler.getDataMode(DataType.IN)!=DataMode.OBJECT) 143 clientHandler.setDataMode(DataMode.OBJECT, DataType.IN); 144 Object data = clientHandler.getObjectInputStream().readObject(); 145 if(data!=null) 146 return data; 147 else 148 throw new ConnectionLostException(); 149 } 150 151 157 public void sendByte(ClientHandler clientHandler, String msg) 158 throws IOException { 159 if(msg!=null) { 160 if(clientHandler.getDataMode(DataType.OUT)!=DataMode.BYTE) 161 clientHandler.setDataMode(DataMode.BYTE, DataType.OUT); 162 clientHandler.sendClientBytes(msg); 163 } 164 } 165 166 175 public String askByteInput(ClientHandler clientHandler, String msg) 176 throws IOException { 177 if(msg!=null) { 178 if(clientHandler.getDataMode(DataType.OUT)!=DataMode.BYTE) 179 clientHandler.setDataMode(DataMode.BYTE, DataType.OUT); 180 clientHandler.sendClientBytes(msg); 181 } 182 if(clientHandler.getDataMode(DataType.IN)!=DataMode.BYTE) 183 clientHandler.setDataMode(DataMode.BYTE, DataType.IN); 184 String data = clientHandler.readBytes(); 185 if(data!=null) 186 return data; 187 else 188 throw new ConnectionLostException(); 189 } 190 191 197 public void sendBinary(ClientHandler clientHandler, byte msg[]) 198 throws IOException { 199 if(msg!=null) { 200 if(clientHandler.getDataMode(DataType.OUT)!=DataMode.BINARY) 201 clientHandler.setDataMode(DataMode.BINARY, DataType.OUT); 202 clientHandler.sendClientBinary(msg); 203 } 204 } 205 206 215 public byte[] askBinaryInput(ClientHandler clientHandler, byte msg[]) 216 throws IOException { 217 if(msg!=null) { 218 if(clientHandler.getDataMode(DataType.OUT)!=DataMode.BINARY) 219 clientHandler.setDataMode(DataMode.BINARY, DataType.OUT); 220 clientHandler.sendClientBinary(msg); 221 } 222 if(clientHandler.getDataMode(DataType.IN)!=DataMode.BINARY) 223 clientHandler.setDataMode(DataMode.BINARY, DataType.IN); 224 byte[] data = clientHandler.readBinary(); 225 if(data!=null) 226 return data; 227 else 228 throw new ConnectionLostException(); 229 } 230 } 231 | Popular Tags |