Skip to content

Commit

Permalink
Stash of random changes #474
Browse files Browse the repository at this point in the history
Signed-off-by: jmehrens [email protected]
  • Loading branch information
jmehrens committed Feb 16, 2024
1 parent 42ed152 commit 0f07c0d
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,18 @@
import jakarta.mail.Store;
import jakarta.mail.search.SearchException;
import jakarta.mail.search.SubjectTerm;
import org.eclipse.angus.mail.imap.protocol.IMAPProtocol;
import org.eclipse.angus.mail.imap.protocol.SearchSequence;
import org.eclipse.angus.mail.test.TestServer;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.Timeout;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.util.Properties;

import static org.junit.Assert.fail;
Expand Down Expand Up @@ -131,15 +137,30 @@ public void search(String line) throws IOException {
final Session session = Session.getInstance(properties);
//session.setDebug(true);

SubjectTerm term = new SubjectTerm(find);
InputStream in = new ByteArrayInputStream(new byte[0]);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream out = new PrintStream(baos, true, "UTF-8");

IMAPProtocol p = new IMAPProtocol(in, out, properties, session.getDebug()) {
public boolean supportsUtf8() {
return true;
}
};
SearchSequence ss = new SearchSequence(p);
try {
ss.generateSequence(term, "UTF-8").write(p);
} catch(IOException ignore) {
}
System.out.println(baos.toString("UTF-8"));

final Store store = session.getStore("imap");
Folder folder = null;
try {
store.connect("test", "test");
folder = store.getFolder("INBOX");
folder.open(Folder.READ_ONLY);
//U+7CFB
//U+7EDF
Message[] msgs = folder.search(new SubjectTerm(find));
Message[] msgs = folder.search(term);
} catch (Exception ex) {
System.out.println(ex);
//ex.printStackTrace();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1997, 2023 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1997, 2024 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand Down Expand Up @@ -40,11 +40,13 @@
import jakarta.mail.search.StringTerm;
import jakarta.mail.search.SubjectTerm;
import org.eclipse.angus.mail.iap.Argument;
import org.eclipse.angus.mail.iap.Literal;
import org.eclipse.angus.mail.imap.ModifiedSinceTerm;
import org.eclipse.angus.mail.imap.OlderTerm;
import org.eclipse.angus.mail.imap.YoungerTerm;

import java.io.IOException;
import java.io.OutputStream;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
Expand Down Expand Up @@ -77,7 +79,7 @@ public SearchSequence(IMAPProtocol p) {
/**
* Create a SearchSequence.
*/
@Deprecated
@Deprecated(since="2.0.3", forRemoval=true)
public SearchSequence() {
}

Expand Down Expand Up @@ -362,7 +364,12 @@ protected Argument subject(SubjectTerm term, String charset)
Argument result = new Argument();

result.writeAtom("SUBJECT");
result.writeString(term.getPattern(), charset);
String pattern = term.getPattern();
if (protocol != null && protocol.supportsUtf8() && !isAscii(term)) {
result.writeBytes(new Utf8Literal(pattern, charset));
} else {
result.writeString(pattern, charset);
}
return result;
}

Expand Down Expand Up @@ -543,4 +550,22 @@ protected Argument modifiedSince(ModifiedSinceTerm term)
result.writeNumber(term.getModSeq());
return result;
}

private static final class Utf8Literal implements Literal {
private final byte[] bytes;

Utf8Literal(String data, String charset) throws IOException {
this.bytes = data.getBytes(charset == null ? "UTF-8" : charset);
}

@Override
public int size() {
return bytes.length;
}

@Override
public void writeTo(OutputStream os) throws IOException {
os.write(this.bytes);
}
}
}

0 comments on commit 0f07c0d

Please sign in to comment.