moving an ImapFolder to another Folder - java

In my little e-mail client I try to implement a feature to move an ImapFolder to another ImapFolder so that the first one is the secound one's subfolder.
Because I was not able to find any method which realizes that, I tried to implement the feature on my own like this:
public static boolean moveFolder(Folder move, Folder parent) {
Folder newFolder = FolderUtils.createFolder(parent, move.getName());
if(move == null || newFolder == null)
return false;
if(!(move instanceof IMAPFolder))
return false;
if(FolderUtils.openFolder(move))
try {
((IMAPFolder) move).moveMessages(move.getMessages(), newFolder);
FolderUtils.deleteFolder(move);
return true;
} catch (MessagingException e) {
e.printStackTrace();
}
return false;
}
public static boolean openFolder(Folder folder) {
if(folder == null)
return true;
if(!folder.getStore().isConnected())
return false;
if(!folder.isOpen()) {
try {
if(folder.getType() == Folder.HOLDS_FOLDERS)
return false;
} catch (MessagingException e1) {
}
try {
folder.open(Folder.READ_WRITE);
return true;
} catch (MessagingException e) {
return false;
}
}else
return true;
}
public static boolean deleteFolder(Folder folder) {
try {
if(folder != null){
if(folder.isOpen())
folder.close();
folder.delete(true);
return true;
}
} catch (Exception e) {
}
return false;
}
public static Folder createFolder(Folder parent, String name) {
try {
if(parent != null) {
Folder nF = parent.getFolder(name);
if(!nF.exists()) {
boolean result = nF.create(Folder.HOLDS_FOLDERS + Folder.HOLDS_MESSAGES);
if(result) {
nF.setSubscribed(true);
return nF;
}
}
}
} catch (MessagingException e) {
e.printStackTrace();
}
return null;
}
My problem is that I allways get the following exception when I try to create a new Folder in a subfolder of my inbox:
javax.mail.MessagingException: Unsupported type;
nested exception is:
com.sun.mail.iap.ProtocolException: Unsupported type
at com.sun.mail.imap.IMAPFolder.doCommandIgnoreFailure(IMAPFolder.java:3898)
at com.sun.mail.imap.IMAPFolder.create(IMAPFolder.java:810)
at de.cpu.outlook.utils.FolderUtils.createFolder(FolderUtils.java:33)
at de.cpu.outlook.utils.FolderUtils.moveFolder(FolderUtils.java:63)
at de.cpu.outlook.gui.toolbars.utils.MoveFolderScreen$1$1.run(MoveFolderScreen.java:90)
at java.base/java.lang.Thread.run(Thread.java:833)
Caused by: com.sun.mail.iap.ProtocolException: Unsupported type
at com.sun.mail.imap.IMAPFolder$6.doCommand(IMAPFolder.java:831)
at com.sun.mail.imap.IMAPFolder.doProtocolCommand(IMAPFolder.java:3921)
at com.sun.mail.imap.IMAPFolder.doCommandIgnoreFailure(IMAPFolder.java:3891)
... 5 more
Is there any way to fix this problem?

Related

Reduce My for loop complexity to 4 as per sonar confiuration

I integrated code to sonarqube for quality code and set method complexity is 4.
Please help me to reduce this for loop complexity to 4
for (int i = 1; i <= retryCount + count; i++) {
try {
if (cat!= null)
grps = cat.getAllGroups(category);
flag = true;
} catch (Exception e) {
if (i >= retryCount + count) {
throw new MyException(new Fault(1009,
new Object[] { e.getMessage() }));
} else {
if (e.getMessage().contains("No Records Found")) {
break;
} else {
String status = handleIOAutomationException(ctx, e);
if (status.equalsIgnoreCase("none")) {
throw new MyException(new Fault(1009,
new Object[] { e.getMessage() }));
} else if (status.equalsIgnoreCase("some")) {
}
}
}
}
if (flag) {
break;
}
}
Try to extract your logic into its own sub methods and try make these shorter. You should also try to make your identifiers more readable.
public void method() {
...
int maxLoops = retryCount + count;
for (int i = 1; i <= maxLoops; i++) {
if (tryToGetAllGroups(maxLoops, i))
break;
}
}
private boolean tryToGetAllGroups(int maxLoops, int i) throws MyException {
try {
if (cat != null)
grps = cat.getAllGroups(category);
return true;
} catch (Exception e) { // you should make Exception more specific!
if (i >= maxLoops) {
return throwFinalException(e);
} else {
if (tryToGetCat(e)) return true;
}
}
return false;
}
private boolean tryToGetCat(Exception e) throws MyException {
if (e.getMessage().contains("No Records Found")) {
return true;
} else {
String status = handleIOAutomationException(ctx, e);
if (status.equalsIgnoreCase("none")) {
throwFinalException(e);
} else if (status.equalsIgnoreCase("some")) {
// try to get cat here
}
}
return false;
}
private boolean throwFinalException(Exception e) throws MyException {
throw new MyException(new Fault(1009,
new Object[]{e.getMessage()}));
}

Assert Fails Even java.lang.AssertionError: expected [true] but found [false]

I get this error when i run it. I'm trying to run it and I changed return true and return false later. Do you know why it happens?
public static boolean elementIsPresent(MobileElement element) {
try {
element.isDisplayed();
} catch (org.openqa.selenium.NoSuchElementException e) {
return true;
}
return false;
}
public void checkbox() {
try {
Assert.assertTrue(elementIsPresent(this.CimonCheckBox));
Log.log(driver).info("Passes matches Cimon Name");
Assert.assertTrue(elementIsPresent(this.KurwaCheckbox));
Log.log(driver).info("Passes matches names");
} catch (Exception e) {
Assert.fail("CheckBox: " + e.getMessage());
}
}
The logic in your if statement is backwards. You're returning true if you get a NoSuchElementException and false otherwise. If you want to consider "is displayed" as "present" then I think your method should be:
public static boolean elementIsPresent(MobileElement element) {
try {
return element.isDisplayed();
} catch (org.openqa.selenium.NoSuchElementException e) {
return false;
}
}
or if you simply want to return true if it's present (regardless of whether it is displayed or not) then it can be:
public static boolean elementIsPresent(MobileElement element) {
try {
element.isDisplayed();
} catch (org.openqa.selenium.NoSuchElementException e) {
return false;
}
return true;
}

Search for string in all files inside a Directory

I want to search for particular string inside all files in a Directory.
Ex: Search for "tiger" in path D:/test/chapters/
D:/test/chapters
/chapter1.log
/chapter2.log
/chapter3.log all these sub files under D:/test/chapters/ .
Sample code I have tried :
public class Example {
public Example() {
super();
}
public int plugin_execute() {
boolean foundstring=false;
try {
File dir = new File("D:/test/chapters");
String[] children = dir.list();
if (children == null) {
System.out.println("does not exist is not a directory");
} else {
for (int i = 0; i < children.length; i++) {
String filename = children[i];
System.out.println(filename);
if (filename !=null) {
foundstring = testString(filename, "tiger");
System.out.println("failed");
}
//Search for entry in file
if (!foundstring) {
return //failuremsg
} else {
System.out.println("failed");
return //succes
}
}
}
return 1;
} catch (Exception e) {
return //error mssg
}
}
private boolean teststring(String filePath, String str) {
BufferedReader br = null;
File file = new File(filePath);
boolean result = false;
if(!file.exists())
return false;
try {
br = new BufferedReader(new FileReader(filePath));
String sCurrentLine;
while ((sCurrentLine = br.readLine()) != null) {
if (sCurrentLine.contains(str)) {
result = true;
System.out.println(str);
System.out.println("Found entry ");
break;
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)br.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
return result;
}
}
It only returns the output of last file, means if it search success in last file it return success otherwise failed.
But I want success if string is found in first file. i.e chapter1 should return success, if not found in Chapter1 it should continue search in chapter2 ....
Please suggest how can I modify this code..
Problem: Simple mix-up with ! and true/false locations.
Solution: Change this
if (! foundString)
{
return // failuremsg
}
else
{
System.out.println("failed");
return // success
}
to
if (foundString)
{
// return success message
}
else
{
// return failure message
}
Another problem I believe I see in your code is that the line foundstring = findString(filename, "tiger"); calls the method findString, whereas the other method you posted in your code is testString. I assume this is a name mix up.
public void listFiles(Path dir , String text)
{
try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(dir))
{
for (Path path : directoryStream)
{
if (Files.isRegularFile(path) && Files.isReadable(path))
{
//this.findString(path, text);
}
}
}
catch (IOException ex)
{
ex.printStackTrace();
}
}
private boolean findString(Path file, String text)
{
//Your implementation
return true;
}

ObjectInputStream doesn't appear to be receiving an ArrayList<Object>

I have the methods:
public void sendTroops(ArrayList<Troops> troops){
try {
output.writeObject(troops);
output.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
public void sendTowers(ArrayList<Tower> towers){
try {
output.writeObject(towers);
output.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
public void sendString(ArrayList<String> str){
try {
output.writeObject(str);
output.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
#SuppressWarnings("unchecked")
public ArrayList<Object> receiveObjects(){
try{
return (ArrayList<Object>) input.readObject();
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
return null;
}
}
used here:
private void updateConnection(){
if(isClientActive()){
if(player.getType() == 0){
client.sendTowers(towers);
System.out.println("sent towers: " + towers.size());
}else{
client.sendTroops(troops);
System.out.println("sent troops: " + troops.size());
}
ArrayList<Object> obj = client.receiveObjects();
System.out.println("obj: " +obj.size());
if(obj != null && obj.size() > 0){
if (obj.get(0) instanceof Troops) {
mendTroops(obj);
}else if(obj.get(0) instanceof Troops){
mendTowers(obj);
}
}
}else if(isServerActive()){
if(player.getType() == 0){
server.sendTowers(towers);
System.out.println("sent towers: " + towers.size());
}else{
server.sendTroops(troops);
System.out.println("sent troops: " + troops.size());
}
ArrayList<Object> obj = server.receiveObjects();
System.out.println("obj: " +obj.size());
if(obj != null && obj.size() > 0){
if (obj.get(0) instanceof Troops) {
mendTroops(obj);
}else if(obj.get(0) instanceof Troops){
mendTowers(obj);
}
}
}
}
I was able to determine that the size of the object array from server/client.receiveObjects(); is always resulting in a array of size zero. I am unsure why this is the issue (as no errors are thrown). Either this is terrible coding practice (which it very well may be) or I'm over looking something.
I would appreciate any help, and if more code/ information of how this program works is needed, please let me know.

Is needed Return value or not?, Working with try, catch and Finally: Java

I have this code:
private String Style(String Arg, Vector VctrClass) throws Exception {
if (Verify that Arg is contained into VctrClass)) {
return "Something";
} else {
throw new Exception("Error The argument required \""+Arg+"\" doesn't exist<br>");
}
}
Here my problem, I had this method:
public String GetStylString(String Arg) {
try {
return this.Style(Arg,OneVector);
}
catch(Exception e) {
System.out.println(e.toString());
}
finally {
return "";
}
}
But' I have this message:
Void methods cannot return a value
Then I changed my method to:
public String GetStylString(String Arg) {
try {
return this.Style(Arg,OneVector);
}
catch(Exception e) {
System.out.println(e.toString());
}
}
I have this message:
This method must return a result of type String
Add the return after the println, not in the finally:
public String GetStylString(String Arg) {
try {
return this.Style(Arg,OneVector);
}
catch(Exception e) {
System.out.println(e.toString());
return "";
}
}
Add the return after the catch instead of in the finally:
public String GetStylString(String Arg) {
try {
return this.Style(Arg,OneVector);
}
catch(Exception e) {
System.out.println(e.toString());
}
return "";
}

Categories

Resources