1 /*****************************************************************
2 * Licensed to the Apache Software Foundation (ASF) under one *
3 * or more contributor license agreements. See the NOTICE file *
4 * distributed with this work for additional information *
5 * regarding copyright ownership. The ASF licenses this file *
6 * to you under the Apache License, Version 2.0 (the *
7 * "License"); you may not use this file except in compliance *
8 * with the License. You may obtain a copy of the License at *
9 * *
10 * http://www.apache.org/licenses/LICENSE-2.0 *
11 * *
12 * Unless required by applicable law or agreed to in writing, *
13 * software distributed under the License is distributed on an *
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
15 * KIND, either express or implied. See the License for the *
16 * specific language governing permissions and limitations *
17 * under the License. *
18 ****************************************************************/
19
20
21 package org.apache.james.postage.client;
22
23 import org.apache.commons.net.telnet.TelnetClient;
24
25 import java.io.BufferedInputStream;
26 import java.io.BufferedReader;
27 import java.io.IOException;
28 import java.io.InputStreamReader;
29 import java.io.OutputStreamWriter;
30 import java.io.Writer;
31 import java.util.ArrayList;
32 import java.util.List;
33
34 /***
35 * Apache James Server specific client to access its Remote Manager.<br/>
36 * This is used for creating user accounts.
37 */
38 public class RemoteManagerClient {
39
40 private String m_host = null;
41 private int m_port = -1;
42 private String m_username = null;
43 private String m_password = null;
44 private TelnetClient m_telnetClient;
45 private BufferedReader m_reader;
46 private Writer m_writer;
47
48 public RemoteManagerClient(String host, int port, String username, String password) {
49 m_host = host;
50 m_port = port;
51 m_username = username;
52 m_password = password;
53 }
54
55 public boolean login() {
56 m_telnetClient = new TelnetClient();
57 try {
58 m_telnetClient.connect(m_host, m_port);
59 m_reader = new BufferedReader(new InputStreamReader(new BufferedInputStream(m_telnetClient.getInputStream(), 1024), "ASCII"));
60 m_writer = new OutputStreamWriter(m_telnetClient.getOutputStream());
61 } catch (IOException e) {
62 e.printStackTrace();
63 return false;
64 }
65
66 try {
67 sendCommand(m_username);
68 delay();
69 sendCommand(m_password);
70 } catch (IOException e) {
71 e.printStackTrace();
72 }
73
74 List answers = readAnswer();
75 if (answers == null || answers.size() == 0 || !((String)answers.get(answers.size() - 1)).startsWith("Welcome"))
76 {
77 disconnect();
78 return false;
79 }
80 return true;
81 }
82
83 public void disconnect() {
84 if (m_telnetClient == null) return;
85 try {
86 m_telnetClient.disconnect();
87 } catch (IOException e) {
88 e.printStackTrace();
89 } finally{
90 m_telnetClient = null;
91 m_reader = null;
92 m_writer = null;
93 }
94 }
95
96 public List executeCommand(String command) {
97 try {
98 sendCommand(command);
99 } catch (IOException e) {
100 e.printStackTrace();
101 return null;
102 }
103
104 List list = readAnswer();
105 return list;
106 }
107
108 protected void sendCommand(String command) throws IOException {
109 m_writer.write(command + "\n");
110 m_writer.flush();
111 }
112
113 public List readAnswer() {
114 return readAnswer(0);
115 }
116
117 protected List readAnswer(int numLines) {
118 List allAnswerLines = new ArrayList();
119 try {
120 delay();
121 if (numLines > 0) {
122 for (int i = 0; i < numLines; i++) {
123 readline(allAnswerLines);
124 }
125 } else {
126 readline(allAnswerLines);
127
128 while (m_reader.ready()) {
129 while (m_reader.ready()) {
130 readline(allAnswerLines);
131 }
132 delay();
133 }
134 }
135 return allAnswerLines;
136 } catch (NullPointerException e) {
137 return null;
138 } catch (IOException e) {
139 return null;
140 }
141 }
142
143 private void readline(List allAnswerLines) throws IOException {
144 if (!m_reader.ready()) return;
145 String line = m_reader.readLine();
146 if (line != null) allAnswerLines.add(line);
147 }
148
149 private void delay() {
150 try {
151 Thread.sleep(100);
152 } catch (InterruptedException e) {
153 }
154 }
155
156
157 }