/**
   * Transmit a text file to the server, using the standard CVS protocol conventions. CR/LFs are
   * converted to the Unix format.
   *
   * @param file the file to transmit
   * @param dos the data outputstream on which to transmit the file
   */
  @Override
  public void transmitTextFile(File file, LoggedDataOutputStream dos) throws IOException {
    if (file == null || !file.exists()) {
      throw new IllegalArgumentException(
          "File is either null or " + "does not exist. Cannot transmit.");
    }

    File fileToSend = file;

    final TransmitTextFilePreprocessor transmitTextFilePreprocessor =
        getTransmitTextFilePreprocessor();

    if (transmitTextFilePreprocessor != null) {
      fileToSend = transmitTextFilePreprocessor.getPreprocessedTextFile(file);
    }

    BufferedInputStream bis = null;
    try {
      // first write the length of the file
      long length = fileToSend.length();
      dos.writeBytes(getLengthString(length), "US-ASCII");

      bis = new BufferedInputStream(new FileInputStream(fileToSend));
      // now transmit the file itself
      byte[] chunk = new byte[CHUNK_SIZE];
      while (length > 0) {
        int bytesToRead = length >= CHUNK_SIZE ? CHUNK_SIZE : (int) length;
        int count = bis.read(chunk, 0, bytesToRead);
        if (count == -1) {
          throw new IOException("Unexpected end of stream from " + fileToSend + ".");
        }
        length -= count;
        dos.write(chunk, 0, count);
      }
      dos.flush();
    } finally {
      if (bis != null) {
        try {
          bis.close();
        } catch (IOException ex) {
          // ignore
        }
      }
      if (transmitTextFilePreprocessor != null) {
        transmitTextFilePreprocessor.cleanup(fileToSend);
      }
    }
  }
Esempio n. 2
0
 /**
  * Counts {@link #processRequests(java.util.List)}. send and received bytes.
  *
  * @thread it assumes that client is not run in paralel.
  */
 public long getCounter() {
   long ret = 0;
   if (loggedDataInputStream != null) {
     ret += loggedDataInputStream.getCounter();
   }
   if (loggedDataOutputStream != null) {
     ret += loggedDataOutputStream.getCounter();
   }
   return ret;
 }
Esempio n. 3
0
  /**
   * Process all the requests. The connection must have been opened and set first.
   *
   * @param requests the requets to process
   */
  public void processRequests(List requests)
      throws IOException, UnconfiguredRequestException, ResponseException, CommandAbortedException {

    if (requests == null || requests.size() == 0) {
      throw new IllegalArgumentException(
          "[processRequests] requests "
              + // NOI18N
              "was either null or empty."); // NOI18N
    }

    if (abort) {
      throw new CommandAbortedException(
          "Aborted during request processing", // NOI18N
          CommandException.getLocalMessage("Client.commandAborted", null)); // NOI18N
    }

    loggedDataInputStream = null;
    loggedDataOutputStream = null;

    // send the initialisation requests if we are handling the first
    // command
    boolean filterRootRequest = true;
    if (isFirstCommand()) {
      setIsFirstCommand(false);
      int pos = 0;
      if (!initialRequestsSent) {
        pos = fillInitialRequests(requests);
        initialRequestsSent = true;
        filterRootRequest = false;
      }
      if (globalOptions != null) {
        // sends the global options that are to be sent to server (-q, -Q, -t, -n, l)
        for (Iterator it = globalOptions.createRequestList().iterator(); it.hasNext(); ) {
          Request request = (Request) it.next();
          requests.add(pos++, request);
        }

        if (globalOptions.isUseGzip() && globalOptions.getCompressionLevel() != 0) {
          requests.add(pos++, new GzipFileContentsRequest(globalOptions.getCompressionLevel()));
        }
      }
    } else if (printConnectionReuseWarning) {
      if (System.getProperty("javacvs.multiple_commands_warning") == null) { // NOI18N
        System.err.println("WARNING TO DEVELOPERS:"); // NOI18N
        System.err.println(
            "Please be warned that attempting to reuse one open connection for more commands is not supported by cvs servers very well."); // NOI18N
        System.err.println("You are advised to open a new Connection each time."); // NOI18N
        System.err.println(
            "If you still want to proceed, please do: System.setProperty(\"javacvs.multiple_commands_warning\", \"false\")"); // NOI18N
        System.err.println("That will disable this message."); // NOI18N
      }
    }

    if (!ALLOWED_CONNECTION_REUSE_REQUESTS.contains(requests.get(requests.size() - 1).getClass())) {
      printConnectionReuseWarning = true;
    }

    final boolean fireEnhancedEvents = getEventManager().isFireEnhancedEventSet();
    int fileDetailRequestCount = 0;

    if (fireEnhancedEvents) {
      for (Iterator it = requests.iterator(); it.hasNext(); ) {
        Request request = (Request) it.next();

        FileDetails fileDetails = request.getFileForTransmission();
        if (fileDetails != null && fileDetails.getFile().exists()) {
          fileDetailRequestCount++;
        }
      }
      CVSEvent event =
          new EnhancedMessageEvent(
              this, EnhancedMessageEvent.REQUESTS_COUNT, new Integer(fileDetailRequestCount));
      getEventManager().fireCVSEvent(event);
    }

    LoggedDataOutputStream dos = connection.getOutputStream();
    loggedDataOutputStream = dos;

    // this list stores stream modification requests, each to be called
    // to modify the input stream the next time we need to process a
    // response
    List streamModifierRequests = new LinkedList();

    // sending files does not seem to allow compression
    transmitFileHandler = getUncompressedFileHandler();

    for (Iterator it = requests.iterator(); it.hasNext(); ) {
      if (abort) {
        throw new CommandAbortedException(
            "Aborted during request processing", // NOI18N
            CommandException.getLocalMessage("Client.commandAborted", null)); // NOI18N
      }

      final Request request = (Request) it.next();

      if (request instanceof GzipFileContentsRequest) {
        if (dontUseGzipFileHandler) {
          stderr.println(
              "Warning: The server is not supporting gzip-file-contents request, no compression is used.");
          continue;
        }
      }

      // skip the root request if already sent
      if (request instanceof RootRequest) {
        if (filterRootRequest) {
          continue;
        } else { // Even if we should not filter the RootRequest now, we must filter all successive
                 // RootRequests
          filterRootRequest = true;
        }
      }
      // send request to server
      String requestString = request.getRequestString();
      dos.writeBytes(requestString);

      // we must modify the outputstream now, but defer modification
      // of the inputstream until we are about to read a response.
      // This is because some modifiers (e.g. gzip) read the header
      // on construction, and obviously no header is present when
      // no response has been sent
      request.modifyOutputStream(connection);
      if (request.modifiesInputStream()) {
        streamModifierRequests.add(request);
      }
      dos = connection.getOutputStream();

      FileDetails fileDetails = request.getFileForTransmission();
      if (fileDetails != null) {
        final File file = fileDetails.getFile();
        // only transmit the file if it exists! When committing
        // a remove request you cannot transmit the file
        if (file.exists()) {
          Logger.logOutput(
              new String(
                      "<Sending file: "
                          + // NOI18N
                          file.getAbsolutePath()
                          + ">\n")
                  .getBytes("utf8")); // NOI18N

          if (fireEnhancedEvents) {
            CVSEvent event =
                new EnhancedMessageEvent(this, EnhancedMessageEvent.FILE_SENDING, file);
            getEventManager().fireCVSEvent(event);

            fileDetailRequestCount--;
          }

          if (fileDetails.isBinary()) {
            transmitFileHandler.transmitBinaryFile(file, dos);
          } else {
            transmitFileHandler.transmitTextFile(file, dos);
          }

          if (fireEnhancedEvents && fileDetailRequestCount == 0) {
            CVSEvent event =
                new EnhancedMessageEvent(this, EnhancedMessageEvent.REQUESTS_SENT, "Ok"); // NOI18N
            getEventManager().fireCVSEvent(event);
          }
        }
      }
      if (request.isResponseExpected()) {
        dos.flush();

        // now perform the deferred modification of the input stream
        Iterator modifiers = streamModifierRequests.iterator();
        while (modifiers.hasNext()) {
          System.err.println("Modifying the inputstream..."); // NOI18N
          final Request smRequest = (Request) modifiers.next();
          System.err.println(
              "Request is a: "
                  + // NOI18N
                  smRequest.getClass().getName());
          smRequest.modifyInputStream(connection);
        }
        streamModifierRequests.clear();

        handleResponse();
      }
    }
    dos.flush();

    transmitFileHandler = null;
  }