use of com.zimbra.common.util.Pair in project zm-mailbox by Zimbra.
the class ImapHandler method resolvePath.
private static Pair<String, Pattern> resolvePath(String referenceName, String mailboxName) {
int startWildcards = referenceName.length();
String resolved = mailboxName;
if (!mailboxName.startsWith("/") && !referenceName.trim().equals("")) {
if (referenceName.endsWith("/")) {
resolved = referenceName + mailboxName;
} else {
resolved = referenceName + '/' + mailboxName;
}
} else {
startWildcards = 0;
}
String unescaped = resolved.toUpperCase();
StringBuffer escaped = new StringBuffer();
boolean previousStar = false;
/* use for simple optimization */
boolean previousPercent = false;
/* use for simple optimization */
for (int i = 0; i < unescaped.length(); i++) {
char c = unescaped.charAt(i);
// The character "%" is similar to "*", but it does not match a hierarchy delimiter."
if (c == '*' && i >= startWildcards) {
if (!previousStar) {
escaped.append(".*");
}
} else if (c == '%' && i >= startWildcards) {
if (!previousPercent) {
escaped.append("[^/]*");
}
} else if (c > 0x7f || !REGEXP_ESCAPED[c]) {
escaped.append(c);
} else {
escaped.append('\\').append(c);
}
previousStar = (c == '*');
previousPercent = (c == '%');
}
return new Pair<String, Pattern>(resolved, Pattern.compile(escaped.toString()));
}
use of com.zimbra.common.util.Pair in project zm-mailbox by Zimbra.
the class ChartUtil method getAllChartSettings.
private List<ChartSettings> getAllChartSettings(File[] chartDefs) throws Exception {
List<ChartSettings> allSettings = new ArrayList<ChartSettings>();
for (File def : chartDefs) {
List<ChartSettings> settings = XMLChartConfig.load(def);
if (settings.size() == 0) {
System.err.println("No chart settings found in " + def.getAbsolutePath());
System.exit(1);
}
// Figure out which columns in which input files are being charted.
for (ChartSettings cs : settings) {
ArrayList<PlotSettings> plots = new ArrayList<PlotSettings>();
plots.addAll(cs.getPlots());
plots.addAll(cs.getGroupPlots());
for (PlotSettings ps : plots) {
String infile = ps.getInfile();
String column = ps.getDataColumn();
if (column == null) {
String[] top = ps.getRatioTop().split("\\+");
String[] bottom = ps.getRatioBottom().split("\\+");
ArrayList<String> cols = new ArrayList<String>();
cols.addAll(Arrays.asList(top));
cols.addAll(Arrays.asList(bottom));
for (String c : cols) {
DataColumn dc = new DataColumn(infile, c);
mUniqueDataColumns.add(dc);
}
} else {
DataColumn dc = new DataColumn(infile, column);
mUniqueDataColumns.add(dc);
}
}
for (GroupPlotSettings gps : cs.getGroupPlots()) {
String infile = gps.getInfile();
String column = gps.getGroupBy();
DataColumn dc = new DataColumn(infile, column);
mUniqueStringColumns.add(dc);
mStringSeries.put(dc, new StringSeries());
}
}
allSettings.addAll(settings);
}
validateChartSettings(allSettings);
// Figure out which column are being used in each infile.
for (DataColumn dc : mUniqueDataColumns) {
String infile = dc.getInfile();
String column = dc.getColumn();
Set<Pair<String, DataSeries>> cols = mColumnsByInfile.get(infile);
if (cols == null) {
cols = new HashSet<Pair<String, DataSeries>>();
mColumnsByInfile.put(infile, cols);
}
DataSeries series = new DataSeries();
mDataSeries.put(dc, series);
Pair<String, DataSeries> colSeries = new Pair<String, DataSeries>(column, series);
cols.add(colSeries);
}
return allSettings;
}
use of com.zimbra.common.util.Pair in project zm-mailbox by Zimbra.
the class ChartUtil method readCsvFiles.
private void readCsvFiles() throws Exception {
Date minDate = null;
Date maxDate = null;
// per-file--shouldn't happen much since this is only for groupplot
for (DataColumn c : mUniqueStringColumns) {
String inFilename = c.getInfile();
Reader reader = null;
StringSeries data = mStringSeries.get(c);
try {
reader = new MultipleDirsFileReader(inFilename, mSrcDirs);
} catch (FileNotFoundException e) {
System.out.printf("CSV file %s not found; Skipping...\n", inFilename);
continue;
}
CsvReader csv = null;
try {
csv = new CsvReader(reader);
int line = 1;
while (csv.hasNext()) {
line++;
String ctx = inFilename + ", line " + line;
Date ts = null;
try {
ts = readTS(csv, ctx);
} catch (ParseException e) {
if (e.getMessage().compareTo("Unparseable date: \"timestamp\"") != 0) {
// bug 54626, ignored the repeat timestamp header
System.out.println(ctx + ": " + e);
}
continue;
}
if (ts.before(mStartAt) || ts.after(mEndAt))
continue;
if (minDate == null) {
minDate = ts;
maxDate = ts;
} else {
if (ts.compareTo(minDate) < 0)
minDate = ts;
if (ts.compareTo(maxDate) > 0)
maxDate = ts;
}
String value = csv.getValue(c.getColumn());
data.AddEntry(ts, value);
}
} finally {
if (csv != null)
csv.close();
}
}
// Read CSVs and populate data series.
for (Iterator<Map.Entry<String, Set<Pair<String, DataSeries>>>> mapIter = mColumnsByInfile.entrySet().iterator(); mapIter.hasNext(); ) {
Map.Entry<String, Set<Pair<String, DataSeries>>> entry = mapIter.next();
String inFilename = entry.getKey();
Set<Pair<String, DataSeries>> columns = entry.getValue();
System.out.println("Reading CSV " + inFilename);
Reader reader = null;
try {
reader = new MultipleDirsFileReader(inFilename, mSrcDirs);
} catch (FileNotFoundException e) {
System.out.printf("CSV file %s not found; Skipping...\n", inFilename);
continue;
}
CsvReader csv = null;
try {
csv = new CsvReader(reader);
int line = 1;
while (csv.hasNext()) {
line++;
String context = inFilename + ", line " + line;
Date ts = null;
try {
ts = readTS(csv, context);
} catch (ParseException e) {
if (e.getMessage().compareTo("Unparseable date: \"timestamp\"") != 0) {
// bug 54626, ignored the repeat timestamp header
System.out.println(context + ": " + e);
}
continue;
}
if (ts.before(mStartAt) || ts.after(mEndAt))
continue;
// Set min/max date
if (minDate == null) {
minDate = ts;
maxDate = ts;
} else {
if (ts.compareTo(minDate) < 0) {
minDate = ts;
}
if (ts.compareTo(maxDate) > 0) {
maxDate = ts;
}
}
// Parse values
for (Iterator<Pair<String, DataSeries>> colIter = columns.iterator(); colIter.hasNext(); ) {
Pair<String, DataSeries> colSeries = colIter.next();
String column = colSeries.getFirst();
DataSeries series = colSeries.getSecond();
String val = csv.getValue(column);
if (!StringUtil.isNullOrEmpty(val)) {
try {
double d = Double.parseDouble(val);
try {
series.AddEntry(ts, d);
} catch (SeriesException e) {
System.out.printf("Can't add sample to series: timestamp=%s, value=%s\n", ts, val);
e.printStackTrace(System.out);
}
} catch (NumberFormatException e) {
System.out.println(String.format("%s: unable to parse value '%s' for %s: %s", context, val, column, e));
}
} else {
// default an entry to 0 if string is empty
series.AddEntry(ts, 0.0);
}
}
}
for (Iterator<Pair<String, DataSeries>> colIter = columns.iterator(); colIter.hasNext(); ) {
Pair<String, DataSeries> colSeries = colIter.next();
String column = colSeries.getFirst();
DataSeries series = colSeries.getSecond();
System.out.format("Adding %d %s points between %s and %s.\n\n", series.size(), column, minDate, maxDate);
}
} finally {
if (csv != null)
csv.close();
}
}
adustSampleRange(minDate, maxDate);
}
use of com.zimbra.common.util.Pair in project zm-mailbox by Zimbra.
the class LdapProvisioning method autoProvZMGProxyAccount.
@Override
public Pair<Account, Boolean> autoProvZMGProxyAccount(String emailAddr, String password) throws ServiceException {
Account acct = getAccountByForeignPrincipal(ZMG_PROXY_ACCT_FOREIGN_PRINCIPAL_PREFIX + emailAddr);
if (acct != null) {
return new Pair<>(acct, false);
}
String domainId = getConfig().getMobileGatewayDefaultProxyAccountDomainId();
if (domainId == null) {
return new Pair<>(null, false);
}
Domain domain = getDomainById(domainId);
if (domain == null) {
ZimbraLog.account.error("Domain corresponding to" + A_zimbraMobileGatewayDefaultProxyAccountDomainId + "not found ");
throw ServiceException.FAILURE("Missing server configuration", null);
}
String testDsId = "TestId";
Map<String, Object> testAttrs = getZMGProxyDataSourceAttrs(emailAddr, password, true, testDsId);
DataSource dsToTest = new DataSource(null, DataSourceType.imap, "Test", testDsId, testAttrs, this);
try {
DataSourceManager.test(dsToTest);
} catch (ServiceException e) {
ZimbraLog.account.debug("ZMG Proxy account auto provisioning failed", e);
throw AuthFailedServiceException.AUTH_FAILED(emailAddr, SystemUtil.getInnermostException(e).getMessage(), e);
}
Map<String, Object> acctAttrs = new HashMap<String, Object>();
acctAttrs.put(Provisioning.A_zimbraIsMobileGatewayProxyAccount, ProvisioningConstants.TRUE);
acctAttrs.put(Provisioning.A_zimbraForeignPrincipal, ZMG_PROXY_ACCT_FOREIGN_PRINCIPAL_PREFIX + emailAddr);
acctAttrs.put(Provisioning.A_zimbraHideInGal, ProvisioningConstants.TRUE);
acctAttrs.put(Provisioning.A_zimbraMailStatus, Provisioning.MailStatus.disabled.toString());
acctAttrs.put(Provisioning.A_zimbraMailHost, getLocalServer().getServiceHostname());
acct = createDummyAccount(acctAttrs, password, domain);
DataSource ds;
try {
Map<String, Object> dsAttrs = getZMGProxyDataSourceAttrs(emailAddr, password, false, null);
Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(acct);
dsAttrs.put(Provisioning.A_zimbraDataSourceFolderId, Integer.toString(mbox.createFolder(null, "/" + emailAddr, new Folder.FolderOptions()).getId()));
ds = createDataSource(acct, DataSourceType.imap, emailAddr, dsAttrs);
} catch (ServiceException e) {
try {
deleteAccount(acct.getId());
} catch (ServiceException e1) {
if (!AccountServiceException.NO_SUCH_ACCOUNT.equals(e1.getCode())) {
throw e1;
}
}
throw e;
}
DataSourceManager.asyncImportData(ds);
return new Pair<>(acct, true);
}
use of com.zimbra.common.util.Pair in project zm-mailbox by Zimbra.
the class RuleManager method getRuleByName.
/**
* Returns the portion of the Sieve script for the rule with the given name,
* or <tt>null</tt> if it doesn't exist.
*/
public static Pair<String, String> getRuleByName(String script, String ruleName) {
if (script == null) {
return null;
}
StringBuilder scriptBuf = new StringBuilder();
StringBuilder requireBuf = new StringBuilder();
boolean found = false;
BufferedReader reader = new BufferedReader(new StringReader(script));
String line;
try {
while ((line = reader.readLine()) != null) {
if (line.startsWith("require")) {
requireBuf.append(line).append("\r\n");
continue;
}
Matcher matcher = PAT_RULE_NAME.matcher(line);
if (matcher.matches()) {
String currentName = matcher.group(1);
if (currentName.equals(ruleName)) {
// First line of rule.
found = true;
} else if (found) {
// First line of next rule.
break;
}
}
if (found) {
scriptBuf.append(line).append("\r\n");
}
}
} catch (IOException e) {
ZimbraLog.filter.warn("Unable to get rule %s from script:\n%s.", ruleName, script, e);
}
Pair<String, String> requireScriptPair = new Pair<String, String>(requireBuf.toString(), scriptBuf.toString());
if (scriptBuf.length() > 0) {
return requireScriptPair;
} else {
return null;
}
}
Aggregations