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.core.ui.log;
017
018import java.io.File;
019import java.io.IOException;
020import java.util.HashMap;
021import java.util.List;
022import java.util.Map;
023
024import org.apache.avalon.framework.service.ServiceException;
025import org.apache.avalon.framework.service.ServiceManager;
026import org.apache.commons.lang3.StringUtils;
027
028import org.ametys.core.ui.Callable;
029import org.ametys.core.ui.StaticClientSideElement;
030import org.ametys.core.util.JSONUtils;
031import org.ametys.plugins.core.ui.log.parser.LogFileParser;
032import org.ametys.runtime.util.AmetysHomeHelper;
033
034/**
035 * Client side element to display archived logs.
036 */
037public class ArchivedLogClientSideElement extends StaticClientSideElement
038{
039    /** JSON Utils */
040    protected JSONUtils _jsonUtils;
041    
042    @Override
043    public void service(ServiceManager smanager) throws ServiceException
044    {
045        super.service(smanager);
046        _jsonUtils = (JSONUtils) smanager.lookup(JSONUtils.ROLE);
047    }
048    
049    /**
050     * Get the log lines of the given log file.
051     * @param logFileName The log file name.
052     * @param filtersJson The filters (received as a JSON object)
053     * @param limit Limit of displayed events (can be 0 or -1 for no limit)
054     * @return A {@link Map}, in "lines" there are the log lines, in "error" there is the error message if an error occurs.
055     */
056    @Callable
057    public Map<String, Object> getLogLines(String logFileName, String filtersJson, Integer limit)
058    {
059        Map<String, Object> result = new HashMap<>();
060        File logFile = new File(_getLogsDirectory(), logFileName);  
061        if (logFile.exists())
062        {
063            try
064            {
065                result.put("lines", LogFileParser.parseFile(logFile, _convertFilters(filtersJson), limit, getLogger()));
066            }
067            catch (IOException e)
068            {
069                String errorMessage = String.format("An error occurs while parsing the log file '%s'.", logFileName);
070                result.put("error", errorMessage);
071                getLogger().error(errorMessage, e);
072            }
073        }
074        else
075        {
076            result.put("error", String.format("The file '%s' doesn't exists in the logs directory.", logFileName));
077        }
078        return result;
079    }
080    
081    /**
082     * Get the logs directory.
083     * @return The directory where the logs are stored
084     */
085    private File _getLogsDirectory()
086    {
087        return new File(AmetysHomeHelper.getAmetysHome(), "logs");
088    }
089    
090    @SuppressWarnings("unchecked")
091    private Map<String, Object> _convertFilters(String filtersJson)
092    {
093        Map<String, Object> filtersAsMap = new HashMap<>();
094        
095        if (StringUtils.isNotBlank(filtersJson))
096        {
097            List<Object> filtersAsList = _jsonUtils.convertJsonToList(filtersJson);
098            for (Object filterObject : filtersAsList)
099            {
100                Map<String, Object> filterMap = (Map<String, Object>) filterObject;
101                filtersAsMap.put(filterMap.get("property").toString(), filterMap.get("value"));
102            }
103        }
104        
105        return filtersAsMap;
106    }
107}