<commit_before>/*
 * Copyright 2006 Wyona
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.wyona.org/licenses/APACHE-LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package org.wyona.yanel.impl.resources;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.StringReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
import javax.mail.MessagingException;
import javax.mail.internet.AddressException;
import javax.servlet.http.HttpServletRequest;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.sax.SAXResult;
import javax.xml.transform.sax.SAXTransformerFactory;
import javax.xml.transform.sax.TransformerHandler;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

import org.apache.log4j.Category;
import org.apache.xml.resolver.tools.CatalogResolver;
import org.apache.xml.serializer.Serializer;
import org.wyona.yanel.core.Path;
import org.wyona.yanel.core.Resource;
import org.wyona.yanel.core.api.attributes.ViewableV1;
import org.wyona.yanel.core.api.attributes.CreatableV2;
import org.wyona.yanel.core.attributes.viewable.View;
import org.wyona.yanel.core.attributes.viewable.ViewDescriptor;
import org.wyona.yarep.core.NoSuchNodeException;
import org.wyona.yarep.core.Repository;
import org.wyona.yarep.core.RepositoryException;
import org.wyona.yarep.core.RepositoryFactory;
import org.wyona.yanel.core.serialization.SerializerFactory;
import org.wyona.yanel.core.source.ResourceResolver;
import org.wyona.yanel.core.transformation.I18nTransformer;
import org.wyona.yanel.core.transformation.I18nTransformer2;
import org.wyona.yanel.core.transformation.XIncludeTransformer;
import org.wyona.yarep.util.RepoPath;
import org.wyona.yarep.util.YarepUtil;
import org.wyona.yanel.core.util.HttpServletRequestHelper;import org.wyona.yanel.core.util.PathUtil;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;
;

/**
 * 
 */
public class ContactResource extends Resource implements ViewableV1, CreatableV2 {

    private static final String SMTP_HOST = "smtpHost:";
    private static final String SMTP_PORT = "smtpPort:";
    private static final String TO = "to:";
    private static final String SUBJECT = "subject:";
    private static Category log = Category.getInstance(ContactResource.class);
    private String smtpHost = "";
    private int smtpPort = 25;
    private String to = "";
    private String from = "";
    private String subject = "YANEL FEEDBACK";
    private String content = "";
    private ContactBean contact = null;
    private String defaultLanguage = "en";
    private String messageBundle = "contact-form";
    
    private HashMap properties = new HashMap();
    private Repository repository  = null;
    private RepoPath rp = null;
    private Path path = null;
    private String language = null;
    
    /**
     * 
     */
    public ContactResource() {
	properties.put("smtpHost", smtpHost);
	properties.put("to", to);
	properties.put("smtpPort", String.valueOf(smtpPort));
	properties.put("subject", subject);
	
    }

    /**
     * 
     */
    public ViewDescriptor[] getViewDescriptors() {
        return null;
    }
    
    /**
     * 
     */
    public View getView(Path path, String viewId) {
        return null;
    }

    /**
     * 
     */
    public View getView(HttpServletRequest request, String viewId)
            throws Exception {
        path = new Path(request.getServletPath());
        rp = new org.wyona.yarep.util.YarepUtil().getRepositoryPath(new org.wyona.yarep.core.Path(path.toString()), getRepositoryFactory());
        repository = rp.getRepo();
        try {
            language = request.getParameter("yanel.meta.language");
        } catch(Exception e) {
            log.debug("language param is not found will use default : " + language);
            language = defaultLanguage;
        }
        if(language == null || ("").equals(language)) {
            log.debug("language param is empty or null : " + language);
            language = defaultLanguage;
        }
        File xslFile = org.wyona.commons.io.FileUtil.file(rtd.getConfigFile().getParentFile().getAbsolutePath(), "xslt" + File.separator + "contact-form.xsl");
        File xmlFile = org.wyona.commons.io.FileUtil.file(rtd.getConfigFile().getParentFile().getAbsolutePath(), "xml" + File.separator + "contact-form.xml");        
        try {
            
            // create reader:
            XMLReader xmlReader = XMLReaderFactory.createXMLReader();
            CatalogResolver catalogResolver = new CatalogResolver();
            xmlReader.setEntityResolver(catalogResolver);
            
            // create first xslt transformer:
            SAXTransformerFactory tf = (SAXTransformerFactory)TransformerFactory.newInstance();

            //transformer = TransformerFactory.newInstance().newTransformer(new StreamSource(xslFile));
            TransformerHandler xsltHandler1 = tf.newTransformerHandler(new StreamSource(xslFile));
            Transformer transformer = xsltHandler1.getTransformer();
            
            boolean submit = false;
            Enumeration enumeration = request.getParameterNames();
            while(enumeration.hasMoreElements()){
                if(enumeration.nextElement().toString().equals("submit")) 
                    submit = true;
            }
            if(submit) {
                sendMail(request, transformer);
                transformer.setParameter("company", HttpServletRequestHelper.getParameter(request, "company"));
                transformer.setParameter("firstName", HttpServletRequestHelper.getParameter(request, "firstName"));
                transformer.setParameter("lastName", HttpServletRequestHelper.getParameter(request, "lastName"));
                transformer.setParameter("email", HttpServletRequestHelper.getParameter(request, "email"));
                transformer.setParameter("address", HttpServletRequestHelper.getParameter(request, "address"));
                transformer.setParameter("zipCity", HttpServletRequestHelper.getParameter(request, "zipCity"));
                transformer.setParameter("message", HttpServletRequestHelper.getParameter(request, "message"));
            }
            
            // create first i18n transformer:
            I18nTransformer2 i18nTransformer1 = new I18nTransformer2(messageBundle, language, getRealm().getDefaultLanguage());
            i18nTransformer1.setEntityResolver(catalogResolver);
            
            // create second xslt transformer:
            TransformerHandler xsltHandler2 = tf.newTransformerHandler(getXSLTStreamSource(path, repository));
            transformer = xsltHandler2.getTransformer();
            transformer.setParameter("yanel.path.name", path.getName());
            transformer.setParameter("yanel.path", path.toString());
            transformer.setParameter("yanel.back2context", PathUtil.backToContext(realm, getPath()));
            transformer.setParameter("yarep.back2realm", PathUtil.backToRealm(getPath()));

            // create xinclude transformer:<commit_msg>fixed menu links on the contact page by passing the correct path<commit_after>/*
 * Copyright 2006 Wyona
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.wyona.org/licenses/APACHE-LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package org.wyona.yanel.impl.resources;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.StringReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
import javax.mail.MessagingException;
import javax.mail.internet.AddressException;
import javax.servlet.http.HttpServletRequest;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.sax.SAXResult;
import javax.xml.transform.sax.SAXTransformerFactory;
import javax.xml.transform.sax.TransformerHandler;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

import org.apache.log4j.Category;
import org.apache.xml.resolver.tools.CatalogResolver;
import org.apache.xml.serializer.Serializer;
import org.wyona.yanel.core.Path;
import org.wyona.yanel.core.Resource;
import org.wyona.yanel.core.api.attributes.ViewableV1;
import org.wyona.yanel.core.api.attributes.CreatableV2;
import org.wyona.yanel.core.attributes.viewable.View;
import org.wyona.yanel.core.attributes.viewable.ViewDescriptor;
import org.wyona.yarep.core.NoSuchNodeException;
import org.wyona.yarep.core.Repository;
import org.wyona.yarep.core.RepositoryException;
import org.wyona.yarep.core.RepositoryFactory;
import org.wyona.yanel.core.serialization.SerializerFactory;
import org.wyona.yanel.core.source.ResourceResolver;
import org.wyona.yanel.core.transformation.I18nTransformer;
import org.wyona.yanel.core.transformation.I18nTransformer2;
import org.wyona.yanel.core.transformation.XIncludeTransformer;
import org.wyona.yarep.util.RepoPath;
import org.wyona.yarep.util.YarepUtil;
import org.wyona.yanel.core.util.HttpServletRequestHelper;import org.wyona.yanel.core.util.PathUtil;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;
;

/**
 * 
 */
public class ContactResource extends Resource implements ViewableV1, CreatableV2 {

    private static final String SMTP_HOST = "smtpHost:";
    private static final String SMTP_PORT = "smtpPort:";
    private static final String TO = "to:";
    private static final String SUBJECT = "subject:";
    private static Category log = Category.getInstance(ContactResource.class);
    private String smtpHost = "";
    private int smtpPort = 25;
    private String to = "";
    private String from = "";
    private String subject = "YANEL FEEDBACK";
    private String content = "";
    private ContactBean contact = null;
    private String defaultLanguage = "en";
    private String messageBundle = "contact-form";
    
    private HashMap properties = new HashMap();
    private Repository repository  = null;
    private RepoPath rp = null;
    private Path path = null;
    private String language = null;
    
    /**
     * 
     */
    public ContactResource() {
	properties.put("smtpHost", smtpHost);
	properties.put("to", to);
	properties.put("smtpPort", String.valueOf(smtpPort));
	properties.put("subject", subject);
	
    }

    /**
     * 
     */
    public ViewDescriptor[] getViewDescriptors() {
        return null;
    }
    
    /**
     * 
     */
    public View getView(Path path, String viewId) {
        return null;
    }

    /**
     * 
     */
    public View getView(HttpServletRequest request, String viewId)
            throws Exception {
        path = new Path(request.getServletPath());
        rp = new org.wyona.yarep.util.YarepUtil().getRepositoryPath(new org.wyona.yarep.core.Path(path.toString()), getRepositoryFactory());
        repository = rp.getRepo();
        try {
            language = request.getParameter("yanel.meta.language");
        } catch(Exception e) {
            log.debug("language param is not found will use default : " + language);
            language = defaultLanguage;
        }
        if(language == null || ("").equals(language)) {
            log.debug("language param is empty or null : " + language);
            language = defaultLanguage;
        }
        File xslFile = org.wyona.commons.io.FileUtil.file(rtd.getConfigFile().getParentFile().getAbsolutePath(), "xslt" + File.separator + "contact-form.xsl");
        File xmlFile = org.wyona.commons.io.FileUtil.file(rtd.getConfigFile().getParentFile().getAbsolutePath(), "xml" + File.separator + "contact-form.xml");        
        try {
            
            // create reader:
            XMLReader xmlReader = XMLReaderFactory.createXMLReader();
            CatalogResolver catalogResolver = new CatalogResolver();
            xmlReader.setEntityResolver(catalogResolver);
            
            // create first xslt transformer:
            SAXTransformerFactory tf = (SAXTransformerFactory)TransformerFactory.newInstance();

            //transformer = TransformerFactory.newInstance().newTransformer(new StreamSource(xslFile));
            TransformerHandler xsltHandler1 = tf.newTransformerHandler(new StreamSource(xslFile));
            Transformer transformer = xsltHandler1.getTransformer();
            
            boolean submit = false;
            Enumeration enumeration = request.getParameterNames();
            while(enumeration.hasMoreElements()){
                if(enumeration.nextElement().toString().equals("submit")) 
                    submit = true;
            }
            if(submit) {
                sendMail(request, transformer);
                transformer.setParameter("company", HttpServletRequestHelper.getParameter(request, "company"));
                transformer.setParameter("firstName", HttpServletRequestHelper.getParameter(request, "firstName"));
                transformer.setParameter("lastName", HttpServletRequestHelper.getParameter(request, "lastName"));
                transformer.setParameter("email", HttpServletRequestHelper.getParameter(request, "email"));
                transformer.setParameter("address", HttpServletRequestHelper.getParameter(request, "address"));
                transformer.setParameter("zipCity", HttpServletRequestHelper.getParameter(request, "zipCity"));
                transformer.setParameter("message", HttpServletRequestHelper.getParameter(request, "message"));
            }
            
            // create first i18n transformer:
            I18nTransformer2 i18nTransformer1 = new I18nTransformer2(messageBundle, language, getRealm().getDefaultLanguage());
            i18nTransformer1.setEntityResolver(catalogResolver);
            
            // create second xslt transformer:
            TransformerHandler xsltHandler2 = tf.newTransformerHandler(getXSLTStreamSource(path, repository));
            transformer = xsltHandler2.getTransformer();
            transformer.setParameter("yanel.path.name", PathUtil.getName(getPath()));
            transformer.setParameter("yanel.path", getPath());
            transformer.setParameter("yanel.back2context", PathUtil.backToContext(realm, getPath()));
            transformer.setParameter("yarep.back2realm", PathUtil.backToRealm(getPath()));

            // create xinclude transformer:
{'fraction_non_alphanumeric': 0.10460429897410845, 'fraction_numerical': 0.004885197850512946, 'mean_word_length': 3.4466467553624764, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 44}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6966577', 'n_tokens_mistral': 4537, 'n_tokens_neox': 4442, 'n_words': 922}