📄 inprotocol.java
字号:
} //parse body parts headers //it it has different bodypart's boundary than the actual boundary then its encapsulated email type String tmpBoundary = parseBodyPartHeader(actuallyParsedBodyPart.getHeader()); if (DEBUG) System.out.println("DEBUG InProtocol.parseBodyOfMultipartMessage - tmpBoundary=" + tmpBoundary); if (tmpBoundary != null && !tmpBoundary.equals(bodyBoundary)) { // we have new boundary // 1.) add old boundary to "stack" if (DEBUG) System.out.println( "DEBUG InProtocol.parseBodyOfMultipartMessage - adding boundary: " + bodyBoundary ); boundaries.push( bodyBoundary ); bodyBoundary = tmpBoundary; } } else { if (redownloaded) { break; } redownloaded = true; //if its IMAP and redownloading a particular bodypart //then by executing BODY"+"["+reDownloadMode+"] we reach to the right bodypart actualBPNumber = reDownloadMode; //make a copy, don't call parseBodyPartHeader(bp) as IMAP protocol reach to data directly already if (runMode == CONVERT_BODY) { actuallyParsedBodyPart = header.getBodyPart(actualBPNumber); actuallyParsedBodyPart.createConvertedContentStorage(); actuallyParsedBodyPart.switchToConvertedContent(); if (DEBUG) { System.out.print("DEBUG InProtocol.parseBodyOfMultipartMessage - CONTENTSTORAGE==null: "); System.out.println(actuallyParsedBodyPart.getStorage() == null); } } else { actuallyParsedBodyPart = new BodyPart(header, header.getBodyPart(actualBPNumber).getHeader()); } connection.unGetLine(); //return back the first bodypart's data line } //now its bodyparts content, fetch to the boundary line = connection.getLine(); if (DEBUG) { System.out.print("DEBUG InProtocol.parseBodyOfMultipartMessage - line='" + line + "'"); System.out.println(", bodyBoundary " + bodyBoundary); } wasStopped = false; long bodyPartSize = 0; while ( ! line.startsWith(bodyBoundary) && !line.startsWith(END_OF_MAIL) && (line.indexOf("OK Success") == -1)) { ++linesCount; bodyPartSize += line.length(); if (stopDownloadingBodyPart(progress, bodyPartSize, linesCount)) { handleNotSuccesfullyDownloaded(actuallyParsedBodyPart); wasStopped = true; break; } try { if (runMode != CONVERT_BODY) { actuallyParsedBodyPart.getStorage().addToContentBuffered(line); } else { System.out.print("DEBUG InProtocol.parseBodyOfMultipartMessage - adding to converted storage"); // set temporary encoding for converted bodypart //byte enc = actuallyParsedBodyPart.getHeader().getEncoding(); // TODO: maybe when it is pdf bodypart, it should be not saved as raw //actuallyParsedBodyPart.getHeader().setEncoding(BodyPart.ENC_NORMAL); // save converted bodypart content actuallyParsedBodyPart.getStorage().addToContentBuffered(line); // set original encoding for bodypart //actuallyParsedBodyPart.getHeader().setEncoding(enc); } } catch (Throwable t) { t.printStackTrace(); if (DEBUG) { System.out.println("DEBUG InProtocol.parseBodyOfMultipartMessage - Error 1"); } //something's gone wrong during the saving process displayAlertIfFetchingBodyFailed(actuallyParsedBodyPart, progress); handleNotSuccesfullyDownloaded(actuallyParsedBodyPart); break; } if (runMode != GET_NEW_MAILS) { progress.incActual(line.length()); } line = connection.getLine(); if (DEBUG) { System.out.print("DEBUG InProtocol.parseBodyOfMultipartMessage - line='" + line + "'"); System.out.println(", bodyBoundary " + bodyBoundary); } } if ( line.startsWith( bodyBoundary + "--" ) && !boundaries.isEmpty() ) { if (DEBUG) System.out.println( "DEBUG InProtocol.parseBodyOfMultipartMessage - removing boundary: " + bodyBoundary ); bodyBoundary = (String)boundaries.pop(); if (DEBUG) System.out.println( "DEBUG InProtocol.parseBodyOfMultipartMessage - new body boundary: " + bodyBoundary ); } try { if (!wasStopped) { if (DEBUG) { System.out.println("DEBUG InProtocol.parseBodyOfMultipartMessage - FLUSHING BUFFER"); } actuallyParsedBodyPart.getStorage().flushBuffer(); } if (runMode == CONVERT_BODY ) { if (DEBUG) { System.out.println("DEBUG InProtocol.parseBodyOfMultipartMessage - hash="+actuallyParsedBodyPart.hashCode()); } synchronized (actuallyParsedBodyPart) { if (DEBUG) { System.out.println("DEBUG InProtocol.parseBodyOfMultipartMessage - NOTIFYING"); } actuallyParsedBodyPart.notify(); } } } catch (Throwable t) { t.printStackTrace(); if (DEBUG) { System.out.println("DEBUG InProtocol.parseBodyOfMultipartMessage - Error 2"); } displayAlertIfFetchingBodyFailed(actuallyParsedBodyPart, progress); handleNotSuccesfullyDownloaded(actuallyParsedBodyPart); } //now lets save the bodypart\\ actuallyParsedBodyPart.setOrder(actualBPNumber); //redownloading and new bodypart that was successfully saved to the DB and replacing the old bodypart if (runMode == REDOWNLOAD_BODY && actuallyParsedBodyPart.atLeastPartial() && isBigger(header, actuallyParsedBodyPart)) { byte old = 0; //markAsDeleted the old bodypart from DB for (old = (byte) (header.getBodyPartCount() - 1); old >= 0; --old) { if (header.getBpOriginalOrder(old) == actuallyParsedBodyPart.getOrder()) { header.getBodyPart(old).getStorage().deleteContent(); break; } } header.replaceBodyPart(actuallyParsedBodyPart, old); //replace the old one in vector bodyParts } else if ((runMode != CONVERT_BODY) && (runMode != REDOWNLOAD_BODY || !isInMail(header, actuallyParsedBodyPart))) { header.insertBodyPartAt(actuallyParsedBodyPart, actualBPNumber); //accept any bodypart - partial, empty, complete } else if (runMode == REDOWNLOAD_BODY){ actuallyParsedBodyPart.getStorage().deleteContent(); } if (line.startsWith(endBoundary)) { //In some bizarre cases END_OF_MAIL can be at the //end of the boundary string and not in the next //line. That's why we need the following //if-statement. if (line.indexOf(END_OF_MAIL) == -1) { line = connection.getLine(); if (DEBUG) System.out.print("DEBUG InProtocol.parseBodyOfMultipartMessage - line='" + line + "'"); while ( ! line.startsWith(END_OF_MAIL) ) { line = connection.getLine(); if (DEBUG) System.out.print("DEBUG InProtocol.parseBodyOfMultipartMessage - line='" + line + "'"); } } break; } //if it wasn't end boundary but just separating boundary, //return the boundary line back the buffer //so it could be detected in the next iteration connection.unGetLine(); if (stopDownloadingBodyPart(progress, 0, linesCount)) { wasStopped = true; break; } // get next line line = connection.getLine(); if (DEBUG) System.out.print("DEBUG InProtocol.parseBodyOfMultipartMessage - line='" + line + "'"); if (runMode == CONVERT_BODY) { actuallyParsedBodyPart.switchToNotConvertedContent(); } } if (runMode == CONVERT_BODY) { actuallyParsedBodyPart.switchToNotConvertedContent(); } if (!wasStopped && " FLAGS (".equals(END_OF_MAIL)) //parse FLAGS parameter { connection.unGetLine(); line = connection.getLine(); if (DEBUG) System.out.print("DEBUG InProtocol.parseBodyOfMultipartMessage - line='" + line + "'"); handleLineWithFlags(line); } } private void parseBodyOfPlainMessage(MessageHeader header, StoppableProgress progress) throws MyException, MyException { String line; int linesCount = 0; BodyPart.Header bpHeader; //body part header was created already by parseHeader() - when retrieving header and body at once if (header.getBodyPartCount() != 0 && runMode != REDOWNLOAD_BODY) { actuallyParsedBodyPart = header.getBodyPart((byte) 0); } else if (account.getType() == MailAccount.IMAP && reDownloadMode != -1) { actuallyParsedBodyPart = new BodyPart(header, header.getBodyPart(0).getHeader()); } else { //downloading or redownloading the whole mail bpHeader = new BodyPart.Header(); actuallyParsedBodyPart = new BodyPart(header, bpHeader); parseBodyPartHeader(bpHeader); if (DEBUG) { System.out.println("DEBUG InProtocol.parseBodyOfPlainMessage - body part header parsed 1"); } } //take all the rest long bodyPartSize = 0; boolean fetchingStopped = false; // true if fetching of body was stopped while (!(line = connection.getLine()).startsWith(END_OF_MAIL) && (line.indexOf("OK Success") == -1)) { ++linesCount; bodyPartSize += line.length(); if (stopDownloadingBodyPart(progress, bodyPartSize, linesCount)) { handleNotSuccesfullyDownloaded(actuallyParsedBodyPart); fetchingStopped = true; break; } if (DEBUG) System.out.println("DEBUG InProtocol.parseBodyOfPlainMessage - this bodypart should be saved."); try { actuallyParsedBodyPart.getStorage().addToContentBuffered(line); } catch (Exception e) { if (DEBUG) { System.out.println("DEBUG InProtocol.parseBodyOfPlainMessage - body part parsed 4"); } //something's gone wrong during the saving process displayAlertIfFetchingBodyFailed(actuallyParsedBodyPart, progress); handleNotSuccesfullyDownloaded(actuallyParsedBodyPart); } //increase the counter of downloaded mails only when we're downloading mail body if (runMode != GET_NEW_MAILS) { progress.incActual(line.length()); } } if (!fetchingStopped && " FLAGS (".equals(END_OF_MAIL)) //parse FLAGS parameter { handleLineWithFlags(line); } try { actuallyParsedBodyPart.getStorage().flushBuffer(); } catch (Exception ex) { ex.printStackTrace(); displayAlertIfFetchingBodyFailed(actuallyParsedBodyPart, progress); handleNotSuccesfullyDownloaded(actuallyParsedBodyPart); } //redownloading the mail and its body part is saved to the DB if (runMode == REDOWNLOAD_BODY && actuallyParsedBodyPart.atLeastPartial() && isBigger(header, actuallyParsedBodyPart)) { //markAsDeleted the old bodypart from DB header.getBodyPart((byte) 0).getStorage().deleteContent(); header.replaceBody(actuallyParsedBodyPart); // replace the old one } else if (header.getBodyPartCount() == 0) { header.addBodyPart(actuallyParsedBodyPart); } else { actuallyParsedBodyPart.getStorage().deleteContent(); } if (DEBUG) { System.out.println("DEBUG InProtocol.parseBodyOfPlainMessage - body part parsed 4"); } } /** * <p>Returns value for the "boundary" parameter in passed line staring * at the position <code>index</code> in the line.</p> * * <p>Handles cases like: * <ul> * <li>boundary=value (returns value)</li> * <li>boundary="value" (returns value)</li> * <li>boundary="value"; (returns value)</li> * </ul> * </p> * * @param index position in line where the boundary parameter name starts * @param line where to find the boundary parameter value in * @return boundary parameter value */ private String getBoundaryParamValue( final int index, final String line ) { String boundaryValue = line.substring( index + 9 ).trim(); // 9="boundary=".length() // if boundary value is quoted: "value" -> value if (boundaryValue.charAt(0) == '"') { boundaryValue = boundaryValue.substring(1, boundaryValue.indexOf('"', 2)); } // if there is semicolon at the end, remove it int semicolonIndex = boundaryValue.indexOf(';'); if (semicolonIndex != -1) { boundaryValue = boundaryValue.substring(0, semicolonIndex); } return boundaryValue; } /** * <p>Parses the lines in the header of email which begins with the word * content.</p>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -