예제 #1
0
  /**
   * Constructor, specifying the server host:port combination, login data and an output stream.
   *
   * @param host server name
   * @param port server port
   * @param user user name
   * @param pass password
   * @param output client output; if set to {@code null}, results will be returned as strings.
   * @throws IOException I/O exception
   */
  public ClientSession(
      final String host,
      final int port,
      final String user,
      final String pass,
      final OutputStream output)
      throws IOException {

    super(output);
    ehost = host;
    socket = new Socket();
    try {
      // limit timeout to five seconds
      socket.connect(new InetSocketAddress(host, port), 5000);
    } catch (final IllegalArgumentException ex) {
      throw new BaseXException(ex);
    }
    sin = socket.getInputStream();

    // receive timestamp
    final BufferInput bi = new BufferInput(sin);
    final String ts = bi.readString();

    // send user name and hashed password/timestamp
    sout = PrintOutput.get(socket.getOutputStream());
    send(user);
    send(Token.md5(Token.md5(pass) + ts));
    sout.flush();

    // receive success flag
    if (!ok(bi)) throw new LoginException();
  }
예제 #2
0
  /**
   * Sends the specified command, string arguments and input.
   *
   * @param cmd command
   * @param input input stream
   * @param strings string arguments
   * @throws IOException I/O exception
   */
  protected void send(final ServerCmd cmd, final InputStream input, final String... strings)
      throws IOException {

    sout.write(cmd.code);
    for (final String s : strings) send(s);
    send(input);
  }
예제 #3
0
 /**
  * Unwatches an event.
  *
  * @param name event name
  * @throws IOException I/O exception
  */
 public void unwatch(final String name) throws IOException {
   sout.write(ServerCmd.UNWATCH.code);
   send(name);
   sout.flush();
   receive(null);
   notifiers.remove(name);
 }
예제 #4
0
  /**
   * Executes a command and sends the result to the specified output stream.
   *
   * @param cmd server command
   * @param arg argument
   * @param os target output stream
   * @return string
   * @throws IOException I/O exception
   */
  protected String exec(final ServerCmd cmd, final String arg, final OutputStream os)
      throws IOException {

    final OutputStream o = os == null ? new ArrayOutput() : os;
    sout.write(cmd.code);
    send(arg);
    sout.flush();
    final BufferInput bi = new BufferInput(sin);
    ClientSession.receive(bi, o);
    if (!ClientSession.ok(bi)) throw new BaseXException(bi.readString());
    return o.toString();
  }
예제 #5
0
  /**
   * Watches an event.
   *
   * @param name event name
   * @param notifier event notification
   * @throws IOException I/O exception
   */
  public void watch(final String name, final EventNotifier notifier) throws IOException {

    sout.write(ServerCmd.WATCH.code);
    if (esocket == null) {
      sout.flush();
      final BufferInput bi = new BufferInput(sin);
      final int eport = Integer.parseInt(bi.readString());
      // initialize event socket
      esocket = new Socket();
      esocket.connect(new InetSocketAddress(ehost, eport), 5000);
      final OutputStream so = esocket.getOutputStream();
      so.write(bi.readBytes());
      so.write(0);
      so.flush();
      final InputStream is = esocket.getInputStream();
      is.read();
      listen(is);
    }
    send(name);
    sout.flush();
    receive(null);
    notifiers.put(name, notifier);
  }
예제 #6
0
 @Override
 protected void execute(final String cmd, final OutputStream os) throws IOException {
   send(cmd);
   sout.flush();
   receive(os);
 }
예제 #7
0
 @Override
 public void store(final String path, final InputStream input) throws IOException {
   send(ServerCmd.STORE, input, path);
 }
예제 #8
0
 @Override
 public void replace(final String path, final InputStream input) throws IOException {
   send(ServerCmd.REPLACE, input, path);
 }
예제 #9
0
 @Override
 public void add(final String path, final InputStream input) throws IOException {
   send(ServerCmd.ADD, input, path);
 }
예제 #10
0
 @Override
 public void create(final String name, final InputStream input) throws IOException {
   send(ServerCmd.CREATE, input, name);
 }