Search in sources :

Example 31 with VelocityContext

use of org.apache.velocity.VelocityContext in project chilo-producer by cccties.

the class Process method createTemplatePage.

private void createTemplatePage(Content content, String templateFileName, Path outFilePath) throws IOException {
    // template name
    content.put("template", Config.getTemplate());
    Properties p = new Properties();
    p.setProperty("input.encoding", "UTF-8");
    p.setProperty("output.encoding", "UTF-8");
    p.setProperty("file.resource.loader.path", Config.getTemplateBaseDir());
    Velocity.init(p);
    VelocityContext context = content.getVelocityContext();
    org.apache.velocity.Template template = Velocity.getTemplate(Config.getTemplateFile(lang, templateFileName).toString(), "UTF-8");
    BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFilePath.toFile()), "UTF-8"));
    template.merge(context, bw);
    bw.close();
}
Also used : VelocityContext(org.apache.velocity.VelocityContext) FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) Properties(java.util.Properties) BufferedWriter(java.io.BufferedWriter)

Example 32 with VelocityContext

use of org.apache.velocity.VelocityContext in project Gargoyle by callakrsos.

the class MailUtil method toVelocityContext.

/**
	 * Map객체를 VelocityContext 객체로 변환.
	 *
	 * @Date 2015. 9. 13.
	 * @param mailContent
	 * @return
	 * @User KYJ
	 */
private static VelocityContext toVelocityContext(Map<String, Object> mailContent) {
    Iterator<String> iterator = mailContent.keySet().iterator();
    VelocityContext velocityContext = new VelocityContext();
    while (iterator.hasNext()) {
        String key = iterator.next();
        Object value = mailContent.get(key);
        velocityContext.put(key, value);
    }
    return velocityContext;
}
Also used : VelocityContext(org.apache.velocity.VelocityContext)

Example 33 with VelocityContext

use of org.apache.velocity.VelocityContext in project Gargoyle by callakrsos.

the class MailUtil method sendMail.

/**
	 * 메일전송
	 *
	 * @Date 2015. 9. 13.
	 * @param mail
	 *            메일을 보내기 위한 정보가 담긴 객체
	 * @param mailContent
	 *            메일템플릿안에 들어갈 내용이 담기는 정보.
	 * @throws Exception
	 * @User KYJ
	 */
public static boolean sendMail(List<Mail> mails, Map<String, Object> mailContent) throws Exception {
    try {
        Mailer bean = BeanUtil.getBean("mailer", Mailer.class);
        VelocityContext merge = toVelocityContext(mailContent);
        SenderMailInfo sender = BeanUtil.getBean("mailSenderInfo", SenderMailInfo.class);
        bean.sendMail(sender, mails, merge);
    } catch (Exception e) {
        LOGGER.error(ValueUtil.toString(e));
        return false;
    }
    return true;
}
Also used : VelocityContext(org.apache.velocity.VelocityContext) Mailer(com.kyj.fx.voeditor.visual.framework.mail.Mailer) SenderMailInfo(com.kyj.fx.voeditor.visual.framework.mail.SenderMailInfo)

Example 34 with VelocityContext

use of org.apache.velocity.VelocityContext in project oxTrust by GluuFederation.

the class UpdateTrustRelationshipAction method downloadConfiguration.

public String downloadConfiguration() {
    ByteArrayOutputStream bos = new ByteArrayOutputStream(16384);
    ZipOutputStream zos = ResponseHelper.createZipStream(bos, "Shibboleth v3 configuration files");
    try {
        zos.setMethod(ZipOutputStream.DEFLATED);
        zos.setLevel(Deflater.DEFAULT_COMPRESSION);
        // Add files
        String idpMetadataFilePath = shibboleth3ConfService.getIdpMetadataFilePath();
        if (!ResponseHelper.addFileToZip(idpMetadataFilePath, zos, Shibboleth3ConfService.SHIB3_IDP_IDP_METADATA_FILE)) {
            log.error("Failed to add " + idpMetadataFilePath + " to zip");
            return OxTrustConstants.RESULT_FAILURE;
        }
        if (this.trustRelationship.getSpMetaDataFN() == null) {
            log.error("SpMetaDataFN is not set.");
            return OxTrustConstants.RESULT_FAILURE;
        }
        String spMetadataFilePath = shibboleth3ConfService.getSpMetadataFilePath(this.trustRelationship.getSpMetaDataFN());
        if (!ResponseHelper.addFileToZip(spMetadataFilePath, zos, Shibboleth3ConfService.SHIB3_IDP_SP_METADATA_FILE)) {
            log.error("Failed to add " + spMetadataFilePath + " to zip");
            return OxTrustConstants.RESULT_FAILURE;
        }
        String sslDirFN = appConfiguration.getShibboleth3IdpRootDir() + File.separator + TrustService.GENERATED_SSL_ARTIFACTS_DIR + File.separator;
        String spKeyFilePath = sslDirFN + shibboleth3ConfService.getSpNewMetadataFileName(this.trustRelationship).replaceFirst("\\.xml$", ".key");
        if (!ResponseHelper.addFileToZip(spKeyFilePath, zos, Shibboleth3ConfService.SHIB3_IDP_SP_KEY_FILE)) {
            log.error("Failed to add " + spKeyFilePath + " to zip");
        //				return OxTrustConstants.RESULT_FAILURE;
        }
        String spCertFilePath = sslDirFN + shibboleth3ConfService.getSpNewMetadataFileName(this.trustRelationship).replaceFirst("\\.xml$", ".crt");
        if (!ResponseHelper.addFileToZip(spCertFilePath, zos, Shibboleth3ConfService.SHIB3_IDP_SP_CERT_FILE)) {
            log.error("Failed to add " + spCertFilePath + " to zip");
        //				return OxTrustConstants.RESULT_FAILURE;
        }
        String spAttributeMap = shibboleth3ConfService.generateSpAttributeMapFile(this.trustRelationship);
        if (spAttributeMap == null) {
            log.error("spAttributeMap is not set.");
            return OxTrustConstants.RESULT_FAILURE;
        }
        if (!ResponseHelper.addFileContentToZip(spAttributeMap, zos, Shibboleth3ConfService.SHIB3_SP_ATTRIBUTE_MAP_FILE)) {
            log.error("Failed to add " + spAttributeMap + " to zip");
            return OxTrustConstants.RESULT_FAILURE;
        }
        VelocityContext context = new VelocityContext();
        context.put("spUrl", trustRelationship.getUrl());
        String gluuSPEntityId = trustRelationship.getEntityId();
        context.put("gluuSPEntityId", gluuSPEntityId);
        String spHost = trustRelationship.getUrl().replaceAll(":[0-9]*$", "").replaceAll("^.*?//", "");
        context.put("spHost", spHost);
        String idpUrl = appConfiguration.getIdpUrl();
        context.put("idpUrl", idpUrl);
        String idpHost = idpUrl.replaceAll(":[0-9]*$", "").replaceAll("^.*?//", "");
        context.put("idpHost", idpHost);
        context.put("orgInum", StringHelper.removePunctuation(organizationService.getOrganizationInum()));
        context.put("orgSupportEmail", appConfiguration.getOrgSupportEmail());
        String spShibboleth3FilePath = shibboleth3ConfService.getSpShibboleth3FilePath();
        String shibConfig = templateService.generateConfFile(Shibboleth3ConfService.SHIB3_SP_SHIBBOLETH2_FILE, context);
        if (!ResponseHelper.addFileContentToZip(shibConfig, zos, Shibboleth3ConfService.SHIB3_SP_SHIBBOLETH2_FILE)) {
            log.error("Failed to add " + spShibboleth3FilePath + " to zip");
            return OxTrustConstants.RESULT_FAILURE;
        }
        String spReadMeResourceName = shibboleth3ConfService.getSpReadMeResourceName();
        String fileName = (new File(spReadMeResourceName)).getName();
        // InputStream is = resourceLoader.getResourceAsStream(spReadMeResourceName);
        //InputStream is = this.getClass().getClassLoader().getResourceAsStream(spReadMeResourceName);
        InputStream is = getClass().getResourceAsStream(spReadMeResourceName);
        if (!ResponseHelper.addResourceToZip(is, fileName, zos)) {
            log.error("Failed to add " + spReadMeResourceName + " to zip");
            return OxTrustConstants.RESULT_FAILURE;
        }
        String spReadMeWindowsResourceName = shibboleth3ConfService.getSpReadMeWindowsResourceName();
        fileName = (new File(spReadMeWindowsResourceName)).getName();
        // is = resourceLoader.getResourceAsStream(spReadMeWindowsResourceName);
        is = getClass().getResourceAsStream(spReadMeWindowsResourceName);
        if (!ResponseHelper.addResourceToZip(is, fileName, zos)) {
            log.error("Failed to add " + spReadMeWindowsResourceName + " to zip");
            return OxTrustConstants.RESULT_FAILURE;
        }
    } finally {
        IOUtils.closeQuietly(zos);
        IOUtils.closeQuietly(bos);
    }
    boolean result = ResponseHelper.downloadFile("shibboleth3-configuration.zip", OxTrustConstants.CONTENT_TYPE_APPLICATION_ZIP, bos.toByteArray(), FacesContext.getCurrentInstance());
    return result ? OxTrustConstants.RESULT_SUCCESS : OxTrustConstants.RESULT_FAILURE;
}
Also used : ZipOutputStream(java.util.zip.ZipOutputStream) VelocityContext(org.apache.velocity.VelocityContext) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) File(java.io.File)

Example 35 with VelocityContext

use of org.apache.velocity.VelocityContext in project oxTrust by GluuFederation.

the class Shibboleth3ConfService method generateSpMetadataFileContent.

public String generateSpMetadataFileContent(GluuSAMLTrustRelationship trustRelationship, String certificate) {
    VelocityContext context = new VelocityContext();
    context.put("certificate", certificate);
    context.put("trustRelationship", trustRelationship);
    context.put("entityId", Util.encodeString(StringHelper.removePunctuation(trustRelationship.getInum())));
    context.put("spHost", trustRelationship.getUrl().replaceFirst("/$", ""));
    // Generate sp-metadata.xml meta-data file
    String spMetadataFileContent = templateService.generateConfFile(SHIB3_IDP_SP_METADATA_FILE, context);
    return spMetadataFileContent;
}
Also used : VelocityContext(org.apache.velocity.VelocityContext)

Aggregations

VelocityContext (org.apache.velocity.VelocityContext)146 StringWriter (java.io.StringWriter)43 Template (org.apache.velocity.Template)41 VelocityEngine (org.apache.velocity.app.VelocityEngine)23 IOException (java.io.IOException)19 Test (org.junit.Test)15 HashMap (java.util.HashMap)13 Map (java.util.Map)12 ObjEntity (org.apache.cayenne.map.ObjEntity)12 File (java.io.File)11 ArrayList (java.util.ArrayList)10 ResourceNotFoundException (org.apache.velocity.exception.ResourceNotFoundException)10 Writer (java.io.Writer)8 ExecuteResult (com.ctrip.platform.dal.daogen.entity.ExecuteResult)7 Progress (com.ctrip.platform.dal.daogen.entity.Progress)7 Callable (java.util.concurrent.Callable)7 ParseErrorException (org.apache.velocity.exception.ParseErrorException)7 JavaCodeGenContext (com.ctrip.platform.dal.daogen.generator.java.JavaCodeGenContext)6 OutputStreamWriter (java.io.OutputStreamWriter)6 PrintWriter (java.io.PrintWriter)6