BrightSide Workbench Full Report + Source Code
JamesRemoteManager.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2011 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 package org.turro.elephant.james;
19 
20 import java.io.BufferedReader;
21 import java.io.BufferedWriter;
22 import java.io.InputStream;
23 import java.io.InputStreamReader;
24 import java.io.OutputStream;
25 import java.io.OutputStreamWriter;
26 import java.net.Socket;
27 import java.util.logging.Level;
28 import java.util.logging.Logger;
29 import org.turro.elephant.context.ElephantContext;
30 
35 public class JamesRemoteManager {
36 
37  private String mailHost, user, password;
38  private int port;
39 
40  public JamesRemoteManager(String mailHost, int port, String user, String password) {
41  this.mailHost = mailHost;
42  this.port = port;
43  this.user = user;
44  this.password = password;
45  }
46 
47  public String execute(JamesCommand command) {
48  String returnString = "An unknown error has occurred.";
49  try {
50  Socket socket = new Socket(mailHost, port);
51  InputStream is = socket.getInputStream();
52  InputStreamReader isr = new InputStreamReader(is);
53  BufferedReader br = new BufferedReader(isr);
54  OutputStream os = socket.getOutputStream();
55  OutputStreamWriter osw = new OutputStreamWriter(os);
56  BufferedWriter bw = new BufferedWriter(osw);
57 
58  String total = "";
59  String line = "";
60  String infoString = "OK: \r\n\r\n";
61 
62  while ((line = br.readLine()) != null) {
63  total += line;
64 
65  if (line.indexOf("id") != -1) {
66  //It wants a username.
67  bw.write(user);
68  bw.newLine();//Put in a new line.
69  bw.flush();
70  } else if (line.indexOf("Password") != -1) {
71  //It wants a password.
72  bw.write(password);
73  bw.newLine();//Put in a new line.
74  bw.flush();
75  }
76  if (line.indexOf("Welcome") != -1) {
77  //It wants the command.
78  bw.write(command.getCommand());
79  bw.newLine();//Put in a new line.
80  bw.flush();
81 
82  //Give James a head start.
83  //If James is slow in returning data, br.ready (below) won't be able to collect the status information
84  //from the command. 1.5 seconds should be enough of a delay to let James get a head start in sending back
85  //data. If necessary, increase this value.
86  Thread.sleep(3000);
87 
88  //Acquire resulting data.
89  while (br.ready()) {
90  infoString += br.readLine() + "\r\n";
91  }
92 
93  //And quit neatly.
94  bw.write("quit");
95  bw.newLine();//Put in a new line.
96  bw.flush();
97  }//end if Welcome
98 
99  }//end while
100 
101  returnString = (String) infoString;
102 
103  socket.close();
104 
105  Logger.getLogger(JamesRemoteManager.class.getName()).log(Level.INFO, returnString);
106 
107  }//end try
108  catch (Exception e) {
109  if (command.getCommand().equals("shutdown")) {
110  returnString = "OK: Shutdown";
111  } else {
112  Logger.getLogger(JamesRemoteManager.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(returnString), e);
113  }
114  }
115 
116  return returnString;
117 
118  }
119 }
JamesRemoteManager(String mailHost, int port, String user, String password)