Пример #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 stream to the server.
  *
  * @param input input stream
  * @throws IOException I/O exception
  */
 private void send(final InputStream input) throws IOException {
   final EncodingOutput eo = new EncodingOutput(sout);
   for (int b; (b = input.read()) != -1; ) eo.write(b);
   sout.write(0);
   sout.flush();
   receive(null);
 }
Пример #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
  /**
   * Runs the command without permission, data and concurrency checks.
   *
   * @param ctx database context
   * @param os output stream
   * @return result of check
   */
  public boolean run(final Context ctx, final OutputStream os) {
    perf = new Performance();
    context = ctx;
    prop = ctx.prop;
    mprop = ctx.mprop;
    out = PrintOutput.get(os);

    try {
      return run();
    } catch (final ProgressException ex) {
      // process was interrupted by the user or server
      abort();
      return error(INTERRUPTED);
    } catch (final Throwable ex) {
      // unexpected error
      Performance.gc(2);
      abort();
      if (ex instanceof OutOfMemoryError) {
        Util.debug(ex);
        return error(OUT_OF_MEM + (createWrite() ? H_OUT_OF_MEM : ""));
      }
      return error(Util.bug(ex) + NL + info.toString());
    } finally {
      // flushes the output
      try {
        if (out != null) out.flush();
      } catch (final IOException ignored) {
      }
    }
  }
Пример #5
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();
  }
Пример #6
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);
  }
Пример #7
0
 @Override
 protected void execute(final String cmd, final OutputStream os) throws IOException {
   send(cmd);
   sout.flush();
   receive(os);
 }
Пример #8
0
 @Override
 public void close() throws IOException {
   out.flush();
 }