Example #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();
  }
Example #2
0
  /**
   * Stores the specified source to the specified file.
   *
   * @param in input source
   * @param file target file
   * @throws IOException I/O exception
   */
  public static void store(final InputSource in, final IOFile file) throws IOException {
    // add directory if it does not exist anyway
    file.dir().md();

    final PrintOutput po = new PrintOutput(file.path());
    try {
      final Reader r = in.getCharacterStream();
      final InputStream is = in.getByteStream();
      final String id = in.getSystemId();
      if (r != null) {
        for (int c; (c = r.read()) != -1; ) po.utf8(c);
      } else if (is != null) {
        for (int b; (b = is.read()) != -1; ) po.write(b);
      } else if (id != null) {
        final BufferInput bi = new BufferInput(IO.get(id));
        try {
          for (int b; (b = bi.read()) != -1; ) po.write(b);
        } finally {
          bi.close();
        }
      }
    } finally {
      po.close();
    }
  }
Example #3
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();
  }
Example #4
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);
  }
Example #5
0
 /**
  * Checks the next success flag.
  *
  * @param bi buffer input
  * @return value of check
  * @throws IOException I/O exception
  */
 protected static boolean ok(final BufferInput bi) throws IOException {
   return bi.read() == 0;
 }
Example #6
0
 /**
  * Receives the info string.
  *
  * @param os output stream to send result to. If {@code null}, no result will be requested
  * @throws IOException I/O exception
  */
 private void receive(final OutputStream os) throws IOException {
   final BufferInput bi = new BufferInput(sin);
   if (os != null) receive(bi, os);
   info = bi.readString();
   if (!ok(bi)) throw new BaseXException(info);
 }