Example usage for org.apache.commons.lang.text StrSubstitutor replace

List of usage examples for org.apache.commons.lang.text StrSubstitutor replace

Introduction

In this page you can find the example usage for org.apache.commons.lang.text StrSubstitutor replace.

Prototype

public String replace(Object source) 

Source Link

Document

Replaces all the occurrences of variables in the given source object with their matching values from the resolver.

Usage

From source file:sos.scheduler.misc.ParameterSubstitutor.java

public String replace(final String source) {
    StrSubstitutor strSubstitutor = new StrSubstitutor(keylist);
    return strSubstitutor.replace(source);
}

From source file:stg.utils.SystemPropertySettings.java

public void load(String pstrFile) throws IOException {
    this.source = pstrFile;

    FileInputStream fis = null;/*  w ww.j  a  v  a 2 s  .com*/
    Properties props = new Properties();
    try {
        fis = new FileInputStream(new File(pstrFile));
        props.load(fis);
    } finally {
        if (fis != null) {
            fis.close();
        }
    }
    Map<Object, Object> map = new HashMap<Object, Object>();
    map.putAll(System.getProperties());
    StrSubstitutor sub = new StrSubstitutor(map);
    for (Entry<Object, Object> entry : props.entrySet()) {
        String key = (String) entry.getKey();
        String value = (String) entry.getValue();
        System.setProperty(key, sub.replace(value));
    }
}

From source file:uk.ac.ebi.arrayexpress.app.Application.java

public void sendEmail(String originator, String[] recipients, String subject, String message) {
    try {/*from  w  w w  . j  a v a 2s . c  om*/
        Thread currentThread = Thread.currentThread();
        String hostName = "unknown";
        try {
            InetAddress localMachine = InetAddress.getLocalHost();
            hostName = localMachine.getHostName();
        } catch (Exception xx) {
            logger.debug("Caught an exception:", xx);
        }

        if (null == recipients || 0 == recipients.length) {
            recipients = getPreferences().getStringArray("app.reports.recipients");
        }

        if (null == originator || "".equals(originator)) {
            originator = getPreferences().getString("app.reports.originator");
        }

        Map<String, String> params = new HashMap<String, String>();
        params.put("variable.appname", getName());
        params.put("variable.thread", String.valueOf(currentThread));
        params.put("variable.hostname", hostName);
        StrSubstitutor sub = new StrSubstitutor(params);

        emailer.send(recipients, getPreferences().getStringArray("app.reports.hidden-recipients"), subject,
                sub.replace(message), originator);

    } catch (Throwable x) {
        logger.error("[SEVERE] Cannot even send an email without an exception:", x);
    }
}

From source file:uk.ac.ebi.arrayexpress.servlets.GenomeSpaceAuthServlet.java

private void displayResult(HttpServletResponse response, String returnURL, String username, String token,
        String message) throws ServletException, IOException {
    setCookie(response, GS_TOKEN_COOKIE, token);
    setCookie(response, GS_USERNAME_COOKIE, username);
    setCookie(response, GS_AUTH_MESSAGE_COOKIE, message);
    setCookie(response, GS_RETURN_URL_COOKIE, null);

    if (null != returnURL) {
        response.sendRedirect(returnURL);
    } else {//from  www  .j a  v  a 2s  . c  o  m
        try (PrintWriter out = response.getWriter()) {
            URL resource = Application.getInstance()
                    .getResource("/WEB-INF/server-assets/templates/gs-auth-result.txt");
            String template = StringTools.streamToString(resource.openStream(), "ISO-8859-1");
            Map<String, String> params = new HashMap<>();
            params.put("gs.token", null != token ? token : "<null>");
            params.put("gs.username", null != username ? username : "<null>");
            params.put("gs.message", null != message ? message : "<null>");
            StrSubstitutor sub = new StrSubstitutor(params);
            out.print(sub.replace(template));
        } catch (Exception x) {
            logger.error("Caught an exception:", x);
        }
    }
}

From source file:uk.ac.ebi.arrayexpress.servlets.QueryServlet.java

private void reportQueryError(PrintWriter out, String templateName, String query) {
    try {/*  w ww .  ja va2  s  .  c o  m*/
        URL resource = Application.getInstance()
                .getResource("/WEB-INF/server-assets/templates/" + templateName);
        String template = StringTools.streamToString(resource.openStream(), "ISO-8859-1");
        Map<String, String> params = new HashMap<String, String>();
        params.put("variable.query", query);
        StrSubstitutor sub = new StrSubstitutor(params);
        out.print(sub.replace(template));
    } catch (Exception x) {
        logger.error("Caught an exception:", x);
    }
}

From source file:uk.ac.ebi.fg.annotare2.integration.OtrsMessengerService.java

private void sendOtrsMessage(String ticketNumber, Message message, boolean isNewTicket) throws Exception {
    OtrsConnector otrs = getOtrsConnector();

    Object ticketId = messageParser.toObject(otrs.dispatchCall("TicketObject", "TicketCheckNumber",
            ImmutableMap.of("Tn", (Object) ticketNumber)), Object.class);
    if (null != ticketId) {
        boolean isInternalSender = message.getFrom().matches(".*annotare[@]ebi[.]ac[.]uk.*");
        boolean isInternalRecipient = message.getTo().matches(".*annotare[@]ebi[.]ac[.]uk.*");
        Map<String, String> templateParams = ImmutableMap.of("original.subject", message.getSubject(),
                "original.body", message.getBody(), "ticket.number", ticketNumber);
        StrSubstitutor sub = new StrSubstitutor(templateParams);

        for (int i = 0; i < 3; ++i) {
            Object articleId = messageParser.toObject(otrs.dispatchCall("TicketObject",
                    isInternalRecipient ? "ArticleCreate" : "ArticleSend",
                    new ImmutableMap.Builder<String, Object>().put("TicketID", ticketId)
                            .put("ArticleType", isInternalSender ? "email-internal" : "email-external")
                            .put("SenderType", isInternalSender ? "agent" : "customer")
                            .put("HistoryType", isNewTicket ? "EmailCustomer" : "FollowUp")
                            .put("HistoryComment", "Sent from Annotare").put("From", message.getFrom())
                            .put("To", message.getTo())
                            .put("Subject",
                                    isInternalRecipient ? message.getSubject()
                                            : sub.replace(properties.getOtrsIntegrationSubjectTemplate()))
                            .put("Type", "text/plain").put("Charset", EMAIL_ENCODING_UTF_8)
                            .put("Body",
                                    isInternalRecipient ? message.getBody()
                                            : sub.replace(properties.getOtrsIntegrationBodyTemplate()))
                            .put("UserID", 1).build()),
                    Object.class);
            if (null != articleId) {
                return;
            }/*w ww.  j a v  a  2s  .  co m*/
            Thread.sleep(500);
        }
        throw new Exception("Unable to create article for ticket " + String.valueOf(ticketId));
    } else {
        throw new Exception("Unable to get ticket ID for ticket #" + ticketNumber);
    }
}

From source file:uk.ac.ebi.fg.annotare2.web.server.services.MessengerImpl.java

private void send(String template, Map<String, String> parameters, User user, Submission submission,
        boolean isDirectSend) {
    StrSubstitutor sub = new StrSubstitutor(parameters);

    String from = sub.replace(properties.getEmailFromAddress(template));
    String to = sub.replace(properties.getEmailToAddress(template));
    String subject = sub.replace(properties.getEmailSubject(template));
    String body = sub.replace(properties.getEmailTemplate(template));

    if (null != from && null != to && null != subject && null != body) {
        if (isDirectSend) {
            try {
                messengerService.directEmail(from, to, subject, body);
            } catch (Throwable x) {
                log.error("Unable to send message directly", x);
            }//from  w w  w.  j a  v a2s.  c o  m
        } else {
            boolean hadOpenSession = sessionFactory.hasOpenSession();
            Session session = hadOpenSession ? sessionFactory.getCurrentSession()
                    : sessionFactory.openSession();
            try {
                queueMessage(from, to, subject, body, user, submission);
            } catch (Throwable x) {
                log.error("Unable to queue a message", x);
                throw new RuntimeException(x);
            } finally {
                if (!hadOpenSession) {
                    session.close();
                }
            }
        }
    } else {
        throw new RuntimeException("Unable to queue a message as there are null parameters");
    }
}