Search in sources :

Example 31 with LineIterator

use of org.apache.commons.io.LineIterator in project pratilipi by Pratilipi.

the class I18n method getStrings.

@SuppressWarnings("unchecked")
public static Map<String, String> getStrings(Language language) {
    Map<String, String> strings = LANG_STRINGS_MAP.get(language);
    if (strings == null) {
        strings = language == Language.ENGLISH ? new HashMap<String, String>() : (Map<String, String>) ((HashMap<String, String>) getStrings(Language.ENGLISH)).clone();
        try {
            File langFile = new File(I18n.class.getResource("language." + language.getCode()).toURI());
            LineIterator it = FileUtils.lineIterator(langFile, "UTF-8");
            while (it.hasNext()) {
                String line = it.nextLine();
                if (line.indexOf('=') != -1) {
                    String key = line.substring(0, line.indexOf('=')).trim();
                    String value = line.substring(line.indexOf('=') + 1).trim();
                    if (language == Language.ENGLISH || strings.get(key) != null)
                        strings.put(key, value);
                }
            }
            LineIterator.closeQuietly(it);
            LANG_STRINGS_MAP.put(language, strings);
        } catch (NullPointerException | URISyntaxException | IOException e) {
            logger.log(Level.SEVERE, "Exception while reading from " + language + " language file.", e);
        }
    }
    return strings;
}
Also used : HashMap(java.util.HashMap) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) Map(java.util.Map) HashMap(java.util.HashMap) File(java.io.File) LineIterator(org.apache.commons.io.LineIterator)

Example 32 with LineIterator

use of org.apache.commons.io.LineIterator in project pratilipi by Pratilipi.

the class PratilipiSite method isEligibleForPWA.

private boolean isEligibleForPWA(String email, Language language) {
    if (email == null || language == null)
        return false;
    List<String> lines = new ArrayList<>();
    String file = "data/pwa-user-list." + language.getCode();
    try {
        InputStream inputStream = getClass().getResource(file).openStream();
        LineIterator it = IOUtils.lineIterator(inputStream, "UTF-8");
        while (it.hasNext()) lines.add(it.next().trim().toLowerCase());
        LineIterator.closeQuietly(it);
    } catch (NullPointerException | IOException e) {
        logger.log(Level.SEVERE, "Exception in reading file: " + file, e);
    }
    for (String line : lines) {
        if (line.isEmpty())
            continue;
        if (email.equals(line))
            return true;
    }
    return false;
}
Also used : InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) IOException(java.io.IOException) LineIterator(org.apache.commons.io.LineIterator)

Example 33 with LineIterator

use of org.apache.commons.io.LineIterator in project pratilipi by Pratilipi.

the class EmailUtil method sendMail.

@Deprecated
public static void sendMail(String name, String email, String templateName, Language language, Map<String, String> dataModel) throws UnexpectedServerException {
    dataModel.put("user_display_name", name);
    String senderName = null;
    Pattern senderNamePattern = Pattern.compile("<#-- SENDER_NAME:(.+?)-->");
    String senderEmail = null;
    Pattern senderEmailPattern = Pattern.compile("<#-- SENDER_EMAIL:(.+?)-->");
    String subject = null;
    Pattern subjectPattern = Pattern.compile("<#-- SUBJECT:(.+?)-->");
    String body = FreeMarkerUtil.processTemplate(dataModel, filePath + templateName + "." + language.getCode() + ".ftl");
    try {
        File file = new File(EmailUtil.class.getResource("template/" + templateName + "." + language.getCode() + ".ftl").toURI());
        LineIterator it = FileUtils.lineIterator(file, "UTF-8");
        Matcher m = null;
        String line = null;
        while (it.hasNext()) {
            line = it.nextLine().trim();
            if (line.isEmpty())
                continue;
            else if (senderName == null && (m = senderNamePattern.matcher(line)).find())
                senderName = m.group(1).trim();
            else if (senderEmail == null && (m = senderEmailPattern.matcher(line)).find())
                senderEmail = m.group(1).trim();
            else if (subject == null && (m = subjectPattern.matcher(line)).find())
                subject = m.group(1).trim();
            else if (senderName != null && senderEmail != null && subject != null)
                break;
        }
        sendMail(senderName, senderEmail, name, email, subject, body);
    } catch (IOException | URISyntaxException e1) {
        logger.log(Level.SEVERE, "Failed to process \"" + templateName + "." + language.getCode() + "\" email template.", e1);
        throw new UnexpectedServerException();
    }
}
Also used : Pattern(java.util.regex.Pattern) UnexpectedServerException(com.pratilipi.common.exception.UnexpectedServerException) Matcher(java.util.regex.Matcher) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) File(java.io.File) LineIterator(org.apache.commons.io.LineIterator)

Example 34 with LineIterator

use of org.apache.commons.io.LineIterator in project pratilipi by Pratilipi.

the class PratilipiSite method getListTitle.

private String getListTitle(String listName, Language lang) {
    String listTitle = null;
    try {
        String fileName = "list." + lang.getCode() + "." + listName;
        InputStream inputStream = DataAccessor.class.getResource("curated/" + fileName).openStream();
        LineIterator it = IOUtils.lineIterator(inputStream, "UTF-8");
        listTitle = it.nextLine().trim();
        LineIterator.closeQuietly(it);
    } catch (NullPointerException | IOException e) {
        logger.log(Level.SEVERE, "Exception while reading from data file.", e);
    }
    return listTitle;
}
Also used : InputStream(java.io.InputStream) DataAccessor(com.pratilipi.data.DataAccessor) IOException(java.io.IOException) LineIterator(org.apache.commons.io.LineIterator)

Example 35 with LineIterator

use of org.apache.commons.io.LineIterator in project pratilipi by Pratilipi.

the class PratilipiSite method createDataModelForStaticPage.

public Map<String, Object> createDataModelForStaticPage(String pageName, Language lang) throws UnexpectedServerException {
    StringBuilder content = new StringBuilder();
    String staticTitle = null;
    try {
        String fileName = "static." + (lang == null ? "" : lang.getCode() + ".") + pageName;
        File file = new File(getClass().getResource(dataFilePrefix + fileName).toURI());
        LineIterator it = FileUtils.lineIterator(file, "UTF-8");
        if (it.hasNext())
            staticTitle = it.nextLine().trim();
        while (it.hasNext()) content.append(it.nextLine() + "<br/>");
        LineIterator.closeQuietly(it);
    } catch (NullPointerException e) {
        return null;
    } catch (URISyntaxException | IOException e) {
        logger.log(Level.SEVERE, "Exception while reading from data file.", e);
        throw new UnexpectedServerException();
    }
    Map<String, Object> dataModel = new HashMap<String, Object>();
    dataModel.put("title", SEOTitleUtil.getStaticPageTitle(pageName, lang));
    dataModel.put("staticTitle", staticTitle);
    dataModel.put("content", content.toString());
    return dataModel;
}
Also used : UnexpectedServerException(com.pratilipi.common.exception.UnexpectedServerException) HashMap(java.util.HashMap) JsonObject(com.google.gson.JsonObject) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) File(java.io.File) LineIterator(org.apache.commons.io.LineIterator)

Aggregations

LineIterator (org.apache.commons.io.LineIterator)42 IOException (java.io.IOException)24 File (java.io.File)13 InputStream (java.io.InputStream)12 ArrayList (java.util.ArrayList)9 HashMap (java.util.HashMap)8 StringReader (java.io.StringReader)7 FileIteratingFirehose (io.druid.data.input.impl.FileIteratingFirehose)5 BufferedReader (java.io.BufferedReader)5 InputStreamReader (java.io.InputStreamReader)5 Matcher (java.util.regex.Matcher)5 Pattern (java.util.regex.Pattern)5 UnexpectedServerException (com.pratilipi.common.exception.UnexpectedServerException)4 FileNotFoundException (java.io.FileNotFoundException)4 FileWriter (java.io.FileWriter)3 Reader (java.io.Reader)3 URISyntaxException (java.net.URISyntaxException)3 DataAccessor (com.pratilipi.data.DataAccessor)2 BufferedWriter (java.io.BufferedWriter)2 FileReader (java.io.FileReader)2