use of de.janrufmonitor.fritzbox.firmware.exception.DoBlockException in project janrufmonitor by tbrandt77.
the class UnitymediaFirmware method doBlock.
public void doBlock(String number) throws DoBlockException, IOException {
if (!this.isInitialized())
throw new DoBlockException("Could not block number " + number + " on FritzBox: FritzBox firmware not initialized.");
String urlstr = getProtocol() + this.m_address + ":" + this.m_port + "/fon_num/sperre_edit.lua";
String postdata = ("mode_call=_in&rule_kind=rufnummer&rule_number=$NUMBER¤t_rule=¤t_mode=_new&backend_validation=false&apply=&sid=".replaceAll("\\$NUMBER", number) + this.m_sid);
executeURL(urlstr, postdata, false);
if (this.m_logger.isLoggable(Level.INFO))
this.m_logger.info("Successfully added numer " + number + " to FritzBox block list.");
}
use of de.janrufmonitor.fritzbox.firmware.exception.DoBlockException in project janrufmonitor by tbrandt77.
the class Block method doBlock.
private boolean doBlock(IPhonenumber n) {
String dial = n.getTelephoneNumber();
if (!n.getIntAreaCode().equalsIgnoreCase(this.getRuntime().getConfigManagerFactory().getConfigManager().getProperty(IJAMConst.GLOBAL_NAMESPACE, IJAMConst.GLOBAL_INTAREA))) {
dial = "00" + n.getIntAreaCode() + dial;
}
if (!dial.startsWith("0"))
dial = "0" + dial;
String text = getI18nManager().getString(getNamespace(), "block", "description", getLanguage());
text = StringUtils.replaceString(text, "{%1}", dial);
if (MessageDialog.openConfirm(new Shell(DisplayManager.getDefaultDisplay()), this.getI18nManager().getString(this.getNamespace(), "block", "label", this.getLanguage()), text)) {
FirmwareManager fwm = FirmwareManager.getInstance();
try {
if (!fwm.isLoggedIn())
fwm.login();
fwm.doBlock(dial);
FritzBoxBlockedListManager.invalidate();
} catch (IOException e) {
this.m_logger.warning(e.toString());
PropagationFactory.getInstance().fire(new Message(Message.ERROR, getNamespace(), "failedblock", e));
} catch (FritzBoxLoginException e) {
this.m_logger.warning(e.toString());
} catch (DoBlockException e) {
this.m_logger.warning(e.toString());
PropagationFactory.getInstance().fire(new Message(Message.ERROR, getNamespace(), "failedblock", e));
}
return true;
}
return false;
}
use of de.janrufmonitor.fritzbox.firmware.exception.DoBlockException in project janrufmonitor by tbrandt77.
the class AbstractFritzBoxFirmware method getBlockCount.
private int getBlockCount() throws DoBlockException, IOException {
if (!this.isInitialized())
throw new DoBlockException("Could get blocked caller list from FritzBox: FritzBox firmware not initialized.");
InputStream in = null;
try {
in = this.getBlockedListAsStream();
} catch (IOException e) {
throw new DoBlockException(e.getMessage());
}
if (in == null)
throw new DoBlockException("Could get blocked caller list from FritzBox: Could not read count from page.");
int c = 0;
StringBuffer parseBuffer = new StringBuffer();
InputStreamReader inr = new InputStreamReader(in);
BufferedReader bufReader = new BufferedReader(inr);
// drop header
String line = bufReader.readLine();
// fasten the processing
bufReader.skip(4096);
while (bufReader.ready()) {
line = bufReader.readLine();
parseBuffer.append(line);
parseBuffer.append(IJAMConst.CRLF);
}
bufReader.close();
in.close();
Pattern p = Pattern.compile("(telcfg:settings/CallerIDActions\\d+)");
Matcher m = p.matcher(parseBuffer);
while (m.find() && m.groupCount() == 1) {
c++;
}
if (this.m_logger.isLoggable(Level.INFO))
this.m_logger.info("Found " + c + " blocked caller numbers in FritzBox.");
return c;
}
use of de.janrufmonitor.fritzbox.firmware.exception.DoBlockException in project janrufmonitor by tbrandt77.
the class AbstractFritzBoxFirmware method getBlockedListAsStream.
private InputStream getBlockedListAsStream() throws DoBlockException, IOException {
long start = System.currentTimeMillis();
this.m_logger.info("Starting retrieving blocked list...");
// The list should be updated now
// Get the csv file for processing
String urlstr = getProtocol() + this.m_address + ":" + this.m_port + getBaseURL();
URL url;
URLConnection urlConn;
DataOutputStream printout;
try {
this.m_logger.info("Calling FritzBox URL: " + urlstr);
url = new URL(urlstr);
} catch (MalformedURLException e) {
this.m_logger.log(Level.SEVERE, e.getMessage(), e);
throw new DoBlockException("Invalid URL: " + urlstr);
}
// If the url is valid load the data
if (url != null && getFetchBlockedListPOSTData().trim().length() > 0) {
urlConn = url.openConnection();
urlConn.setDoInput(true);
urlConn.setDoOutput(true);
urlConn.setUseCaches(false);
// Sending postdata to the fritz box
urlConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
printout = new DataOutputStream(urlConn.getOutputStream());
printout.writeBytes(getFetchBlockedListPOSTData().replaceAll("\\$LANG", this.m_language) + getFetchBlockedListURLAuthenticator().getAuthenticationToken());
printout.flush();
printout.close();
try {
Thread.sleep(1000);
} catch (InterruptedException e2) {
this.m_logger.log(Level.SEVERE, e2.getMessage(), e2);
}
try {
// Get response data from the box
ByteArrayOutputStream bos = new ByteArrayOutputStream();
this.m_logger.info("Fetching blocked list from FritzBox took " + (System.currentTimeMillis() - start) + "ms");
Stream.copy(urlConn.getInputStream(), bos);
ByteArrayInputStream bin = new ByteArrayInputStream(bos.toByteArray());
// this.m_logger.info(bos.toString());
this.m_logger.info("Finished retrieving blocked list took " + (System.currentTimeMillis() - start) + "ms");
urlConn.getInputStream().close();
return bin;
} catch (IOException e1) {
this.m_logger.log(Level.SEVERE, e1.getMessage(), e1);
throw new DoBlockException(e1.getMessage());
}
}
return null;
}
Aggregations