use of net.i2p.util.Log in project i2p.i2p-bote by i2p.
the class JSPHelper method mergeRecipientFields.
public static List<RecipientAddress> mergeRecipientFields(ServletRequest request) {
Log log = new Log(GeneralHelper.class);
// Convert request.getParameterMap() to a Map<String, String>
Map<String, String[]> parameterArrayMap = request.getParameterMap();
Map<String, String> parameterStringMap = new HashMap<String, String>();
for (Map.Entry<String, String[]> parameter : parameterArrayMap.entrySet()) {
String[] value = parameter.getValue();
if (value != null && value.length > 0)
parameterStringMap.put(parameter.getKey(), value[0]);
else
parameterStringMap.put(parameter.getKey(), "");
}
Map<String, String> oldAddresses = getSortedRecipientParams(parameterStringMap);
String action = request.getParameter("action");
int indexToRemove = -1;
if (action != null && action.startsWith("removeRecipient")) {
String indexString = action.substring("removeRecipient".length());
if (isNumeric(indexString))
indexToRemove = Integer.valueOf(indexString);
}
// make an Iterator over the selectedContact values
String[] newAddressesArray = request.getParameterValues("nofilter_selectedContact");
Iterator<String> newAddresses;
if (newAddressesArray == null)
newAddresses = new ArrayList<String>().iterator();
else
newAddresses = Arrays.asList(newAddressesArray).iterator();
// make selectedContact values and oldAddresses into one List
List<RecipientAddress> mergedAddresses = new ArrayList<RecipientAddress>();
int i = 0;
for (String address : oldAddresses.values()) {
// don't add it if it needs to be removed
if (i == indexToRemove) {
i++;
continue;
}
String typeKey = "recipientType" + i;
String type;
if (parameterStringMap.containsKey(typeKey))
type = parameterStringMap.get(typeKey);
else {
log.error("Request contains a parameter named recipient" + i + ", but no parameter named recipientType" + i + ".");
type = "to";
}
if (!address.trim().isEmpty())
mergedAddresses.add(new RecipientAddress(type, address));
else // if an existing address field is empty and a selectedContact is available, put the selectedContact into the address field
if (newAddresses.hasNext())
mergedAddresses.add(new RecipientAddress(type, newAddresses.next()));
else
mergedAddresses.add(new RecipientAddress(type, ""));
i++;
}
// add any remaining selectedContacts
while ((newAddresses.hasNext())) mergedAddresses.add(new RecipientAddress("to", newAddresses.next()));
if ("addRecipientField".equalsIgnoreCase(action))
mergedAddresses.add(new RecipientAddress("to", ""));
else // Make sure there is a blank recipient field at the end so all non-empty fields have a remove button next to them
if (mergedAddresses.isEmpty() || !mergedAddresses.get(mergedAddresses.size() - 1).getAddress().isEmpty())
mergedAddresses.add(new RecipientAddress("to", ""));
return mergedAddresses;
}
use of net.i2p.util.Log in project i2p.i2p-bote by i2p.
the class SeedlessParameters method init.
private void init() {
Log log = new Log(SeedlessParameters.class);
ContextHelper ctx = new ContextHelper(null);
// 1: Get the console IP:port
addr = ctx.getConsoleAddress();
if (addr == null) {
log.error("No router console found, trying default host/port: " + DEFAULT_ADDR);
addr = DEFAULT_ADDR;
} else {
ready = false;
}
// 2: Get console password
String apass = ctx.getConsolePassword();
log.info("Testing Seedless API");
// 3: Check for the console API, if it exists, wait 'till it's status is ready.
// and set the needed settings. Repeat test 10 times with some delay between when it fails.
String url = "http://" + addr + "/SeedlessConsole/";
String svcurl = url + "Service";
int tries = 10;
HttpURLConnection h;
int i;
while (tries > 0) {
try {
ProxyRequest proxy = new ProxyRequest();
h = proxy.doURLRequest(url, null, null, -1, "admin", apass);
if (h != null) {
i = h.getResponseCode();
if (i == 200) {
log.info("Seedless, API says OK");
break;
}
}
} catch (IOException ex) {
}
tries--;
}
if (tries > 0) {
// Now wait for it to be ready.
// but not forever!
log.info("Waiting for Seedless to become ready...");
// ~2 minutes.
tries = 60;
String foo;
while (!ready && tries > 0) {
tries--;
try {
ProxyRequest proxy = new ProxyRequest();
h = proxy.doURLRequest(svcurl, "stat ping!", null, -1, "admin", apass);
if (h != null) {
i = h.getResponseCode();
if (i == 200) {
foo = h.getHeaderField("X-Seedless");
ready = Boolean.parseBoolean(foo);
}
}
} catch (IOException ex) {
}
if (!ready) {
try {
// sleep for 2 seconds
Thread.sleep(2000);
} catch (InterruptedException ex) {
ready = false;
Thread.currentThread().interrupt();
return;
}
}
}
}
if (ready) {
svcURL = svcurl;
cpass = apass;
peersReqHeader = "scan " + Base64.encode("i2p-bote X" + I2PBote.PROTOCOL_VERSION + "X");
peersLocHeader = "locate " + Base64.encode("i2p-bote X" + I2PBote.PROTOCOL_VERSION + "X");
serversLocHeader = "locate " + Base64.encode("seedless i2p-bote");
}
}
use of net.i2p.util.Log in project i2p.i2p-bote by i2p.
the class Util method readLines.
/**
* Opens a <code>File</code> and reads one line at a time.
* Returns the lines as a <code>List</code> of <code>String</code>s,
* or an empty <code>List</code> if an error occurred.
* @param file
* @see #readLines(URL)
*/
public static List<String> readLines(File file) {
Log log = new Log(Util.class);
log.info("Reading file: <" + file.getAbsolutePath() + ">");
InputStream stream = null;
try {
stream = new FileInputStream(file);
return readLines(stream);
} catch (IOException e) {
log.error("Error reading file.", e);
return Collections.emptyList();
} finally {
if (stream != null)
try {
stream.close();
} catch (IOException e) {
log.error("Can't close input stream.", e);
}
}
}
use of net.i2p.util.Log in project i2p.i2p-bote by i2p.
the class Util method readLines.
/**
* Opens an <code>InputStream</code> and reads one line at a time.
* Returns the lines as a <code>List</code> of <code>String</code>s.
* or an empty <code>List</code> if an error occurred.
* @param inputStream
* @see #readLines(URL)
*/
public static List<String> readLines(InputStream inputStream) throws IOException {
Log log = new Log(Util.class);
BufferedReader inputBuffer = new BufferedReader(new InputStreamReader(inputStream));
List<String> lines = new ArrayList<String>();
while (true) {
String line = null;
line = inputBuffer.readLine();
if (line == null)
break;
lines.add(line);
}
log.info(lines.size() + " lines read.");
return lines;
}
use of net.i2p.util.Log in project i2p.i2p-bote by i2p.
the class CryptoFactory method init.
private static void init() {
instances = Collections.synchronizedList(new ArrayList<CryptoImplementation>());
Log log = new Log(CryptoFactory.class);
try {
instances.add(new ElGamal2048_DSA1024());
} catch (GeneralSecurityException e) {
log.error("Error creating ElGamal2048_DSA1024.", e);
}
try {
instances.add(new ECDH256_ECDSA256());
instances.add(new ECDH521_ECDSA521());
} catch (GeneralSecurityException e) {
log.error("Error creating ECDH256_ECDSA256 or ECDH521_ECDSA521.", e);
}
try {
instances.add(new NTRUEncrypt1087_GMSS512());
} catch (GeneralSecurityException e) {
log.error("Error creating NTRUEncrypt1087_GMSS512.", e);
}
}
Aggregations