001/*
002 *  Copyright 2018 Anyware Services
003 *
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package org.ametys.plugins.messagingconnector.dynamic;
017
018import org.apache.avalon.framework.service.ServiceException;
019import org.apache.avalon.framework.service.ServiceManager;
020import org.apache.cocoon.environment.ObjectModelHelper;
021import org.apache.cocoon.environment.Request;
022import org.apache.cocoon.xml.AttributesImpl;
023import org.apache.cocoon.xml.XMLUtils;
024import org.xml.sax.SAXException;
025
026import org.ametys.plugins.linkdirectory.dynamic.AbstractInternalDynamicInformationGenerator;
027import org.ametys.plugins.linkdirectory.dynamic.DynamicInformationException;
028import org.ametys.plugins.linkdirectory.dynamic.DynamicInformationException.ExceptionType;
029import org.ametys.plugins.messagingconnector.MessagingConnector;
030import org.ametys.runtime.i18n.I18nizableText;
031import org.ametys.web.transformation.xslt.AmetysXSLTHelper;
032
033/**
034 * Abstract generator for saxing dynamic information for agenda/mail service
035 */
036public abstract class AbstractMessagingConnectorDynamicInformationGenerator extends AbstractInternalDynamicInformationGenerator
037{
038    /** The messagingConnector component */
039    protected MessagingConnector _messagingConnector;
040    
041    @Override
042    public void service(ServiceManager serviceManager) throws ServiceException
043    {
044        super.service(serviceManager);
045        _messagingConnector = (MessagingConnector) manager.lookup(MessagingConnector.ROLE);
046    }
047    
048    @Override
049    protected void saxError(DynamicInformationException e) throws SAXException
050    {
051        if (e.getType() == ExceptionType.UNAUTHORIZED && _messagingConnector.supportUserCredential(getCurrentUser()))
052        {
053            AttributesImpl attr = new AttributesImpl();
054            attr.addCDATAAttribute(ERROR_ATTRIBUTE_TYPE, e.getType().name());
055            XMLUtils.startElement(contentHandler, ERROR, attr);
056            
057            // Error message
058            I18nizableText errorMessage = new I18nizableText("plugin.link-directory", "PLUGINS_LINKDIRECTORY_DYNAMIC_INFO_PROVIDER_ERROR_" + ExceptionType.UNAUTHORIZED.name());
059            errorMessage.toSAX(contentHandler);
060            
061            String spanId = getSpanId();
062            
063            // span element where to insert change password link
064            attr.clear();
065            attr.addCDATAAttribute("id", spanId);
066            attr.addCDATAAttribute("class", "link-change-password-link");
067            XMLUtils.createElement(contentHandler, "span", attr);
068            
069            // JS script to change password
070            attr.clear();
071            attr.addCDATAAttribute("type", "text/javascript");
072            XMLUtils.startElement(contentHandler, "script", attr);
073            XMLUtils.data(contentHandler, _scriptToUpdatePassword(spanId));
074            XMLUtils.endElement(contentHandler, "script");
075            
076            XMLUtils.endElement(contentHandler, ERROR);
077        }
078        else
079        {
080            super.saxError(e);
081        }
082    }
083    
084    /**
085     * Get the id of span element where to insert the link to set/update password
086     * @return id of span element
087     */
088    protected abstract String getSpanId();
089
090    private String _scriptToUpdatePassword(String spanId)
091    {
092        Request request = ObjectModelHelper.getRequest(objectModel);
093        
094        String uriPrefix = AmetysXSLTHelper.uriPrefix();
095        String siteUriPrefix = AmetysXSLTHelper.siteUriPrefix();
096        String lang = request.getParameter("lang");
097        String uniqueId = request.getParameter("uniqueId");
098        
099        return "if (!window.MessagingConnector || !MessagingConnector.insertChangePasswordLink)\n"
100                    + "{\n"
101                    + String.format("\tMessagingConnector = {SITE_URI_PREFIX: '%s', LANG: '%s'}\n", siteUriPrefix, lang)
102                    + String.format("\t$j.getScript(\"%s/plugins/messaging-connector/resources/js/messaging-connector-password.js\")", uriPrefix, lang)
103                    + String.format(".done(function(){MessagingConnector.insertChangePasswordLink('%s', updateDynamicInformation_%s)})", spanId, uniqueId)
104                    + ".fail(function(jqxhr, settings, exception) { console.error(\"An error occurred while loading js files\", exception); })\n"
105                    + "}\nelse{\n"
106                    + String.format("\tMessagingConnector.insertChangePasswordLink('%s', updateDynamicInformation_%s)\n", spanId, uniqueId)
107                    + "}\n";
108                                        
109    }
110
111}