Esempio n. 1
0
  /**
   * Handle the response from a request.
   *
   * @throws ResponseException if there is a problem reading the response
   */
  private void handleResponse() throws ResponseException, CommandAbortedException {
    try {
      LoggedDataInputStream dis = connection.getInputStream();
      loggedDataInputStream = dis;

      int ch = -1;
      try {
        ch = dis.read();
      } catch (InterruptedIOException ex) {
        abort();
      }

      while (!abort && ch != -1) {
        StringBuffer responseNameBuffer = new StringBuffer();
        // read in the response name
        while (ch != -1 && (char) ch != '\n' && (char) ch != ' ') {
          responseNameBuffer.append((char) ch);
          try {
            ch = dis.read();
          } catch (InterruptedIOException ex) {
            abort();
            break;
          }
        }

        String responseString = responseNameBuffer.toString();
        Response response = getResponseFactory().createResponse(responseString);
        // Logger.logInput(new String("<" + responseString + " processing start>\n").getBytes()); //
        // NOI18N
        response.process(dis, this);
        boolean terminal = response.isTerminalResponse();

        // handle SpecialResponses
        if (terminal && response instanceof ErrorMessageResponse) {
          ErrorMessageResponse errorResponce = (ErrorMessageResponse) response;
          String errMsg = errorResponce.getMessage();
          throw new CommandAbortedException(errMsg, errMsg);
        }
        // Logger.logInput(new String("<" + responseString + " processed " + terminal +
        // ">\n").getBytes()); // NOI18N
        if (terminal || abort) {
          break;
        }

        try {
          ch = dis.read();
        } catch (InterruptedIOException ex) {
          abort();
          break;
        }
      }

      if (abort) {
        String localMsg = CommandException.getLocalMessage("Client.commandAborted", null); // NOI18N
        throw new CommandAbortedException("Aborted during request processing", localMsg); // NOI18N
      }
    } catch (EOFException ex) {
      throw new ResponseException(
          ex, ResponseException.getLocalMessage("CommandException.EndOfFile", null)); // NOI18N
    } catch (IOException ex) {
      throw new ResponseException(ex);
    }
  }
Esempio n. 2
0
  /**
   * Ensures, that the connection is open.
   *
   * @throws AuthenticationException if it wasn't possible to connect
   */
  public void ensureConnection() throws AuthenticationException {
    BugLog.getInstance().assertNotNull(getConnection());

    if (getConnection().isOpen()) {
      return;
    }

    // #69689 detect silent servers, possibly caused by proxy errors
    final Throwable ex[] = new Throwable[1];
    final boolean opened[] = new boolean[] {false};
    Runnable probe =
        new Runnable() {
          public void run() {
            try {
              getConnection().open();
              synchronized (opened) {
                opened[0] = true;
              }
            } catch (Throwable e) {
              synchronized (ex) {
                ex[0] = e;
              }
            }
          }
        };

    Thread probeThread = new Thread(probe, "CVS Server Probe"); // NOI18N
    probeThread.start();
    try {

      probeThread.join(60 * 1000); // 1 min

      Throwable wasEx;
      synchronized (ex) {
        wasEx = ex[0];
      }
      if (wasEx != null) {
        if (wasEx instanceof CommandAbortedException) {
          // User cancelled
          abort();
          return;
        } else if (wasEx instanceof AuthenticationException) {
          throw (AuthenticationException) wasEx;
        } else if (wasEx instanceof RuntimeException) {
          throw (RuntimeException) wasEx;
        } else if (wasEx instanceof Error) {
          throw (Error) wasEx;
        } else {
          assert false : wasEx;
        }
      }

      boolean wasOpened;
      synchronized (opened) {
        wasOpened = opened[0];
      }
      if (wasOpened == false) {
        probeThread.interrupt();
        throw new AuthenticationException(
            "Timeout, no response from server.", "Timeout, no response from server.");
      }

    } catch (InterruptedException e) {

      // User cancelled
      probeThread.interrupt();
      abort();
    }
  }