001/* 002 * Copyright 2016 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.about; 017 018import java.io.BufferedReader; 019import java.io.File; 020import java.io.FileNotFoundException; 021import java.io.FileReader; 022import java.io.IOException; 023import java.util.ArrayList; 024import java.util.Iterator; 025import java.util.LinkedHashMap; 026import java.util.List; 027import java.util.Map; 028 029import org.apache.avalon.framework.component.Component; 030import org.apache.avalon.framework.context.Context; 031import org.apache.avalon.framework.context.ContextException; 032import org.apache.avalon.framework.context.Contextualizable; 033import org.apache.avalon.framework.service.ServiceException; 034import org.apache.avalon.framework.service.ServiceManager; 035import org.apache.avalon.framework.service.Serviceable; 036import org.apache.cocoon.Constants; 037 038import org.ametys.core.ui.Callable; 039import org.ametys.core.util.DateUtils; 040import org.ametys.core.util.I18nUtils; 041import org.ametys.core.version.Version; 042import org.ametys.core.version.VersionsHandler; 043import org.ametys.runtime.i18n.I18nizableText; 044import org.ametys.runtime.plugin.component.AbstractLogEnabled; 045 046/** 047 * Helper providing information (versions, licenses) on running application for "About Ametys" feature. 048 */ 049public class AboutInfoProvider extends AbstractLogEnabled implements Serviceable, Component, Contextualizable 050{ 051 /** The Avalon role */ 052 public static final String ROLE = AboutInfoProvider.class.getName(); 053 054 /** The name of the license text file */ 055 private static final String NOTICE_FILE_PATH = "/NOTICE.txt"; 056 /** The path of the Application logo */ 057 private static final String LOGO_FILE_PATH = "/app_logo.png"; 058 059 /** The versions handler */ 060 private VersionsHandler _versionsHandler; 061 /** The cocoon context */ 062 private org.apache.cocoon.environment.Context _cocoonContext; 063 064 @Override 065 public void service(ServiceManager manager) throws ServiceException 066 { 067 _versionsHandler = (VersionsHandler) manager.lookup(VersionsHandler.ROLE); 068 } 069 070 @Override 071 public void contextualize(Context context) throws ContextException 072 { 073 _cocoonContext = (org.apache.cocoon.environment.Context) context.get(Constants.CONTEXT_ENVIRONMENT_CONTEXT); 074 } 075 076 /** 077 * Gets the information for "About Ametys" feature 078 * @param lang the lang 079 * @return A map of information needed for "About Ametys" feature. 080 * Contains the application name, the versions of the application and the license text. 081 * @throws IOException if there are issue while reading the license file 082 */ 083 @Callable 084 public Map<String, Object> getInfo(String lang) throws IOException 085 { 086 Map<String, Object> result = new LinkedHashMap<>(); 087 088 result.put("applicationLogo", getApplicationLogo()); 089 result.put("applicationName", getApplicationName(lang)); 090 result.put("versions", getVersions()); 091 result.put("licenseText", getLicenseText()); 092 093 return result; 094 } 095 096 /** 097 * Get the path of the application logo 098 * @return The path to the Ametys application logo. 099 */ 100 @Callable 101 public String getApplicationLogo () 102 { 103 String path = _cocoonContext.getRealPath(LOGO_FILE_PATH); 104 File imgFile = new File(path); 105 106 if (imgFile.exists() && imgFile.isFile()) 107 { 108 return _cocoonContext.getRealPath(LOGO_FILE_PATH); 109 } 110 return null; 111 } 112 113 /** 114 * Gets the application name 115 * @param lang The lang 116 * @return The name of the application. 117 */ 118 @Callable 119 public String getApplicationName(String lang) 120 { 121 return I18nUtils.getInstance().translate(new I18nizableText("application", "APPLICATION_PRODUCT_LABEL"), lang); 122 } 123 124 /** 125 * Gets the available versions of the application. 126 * @return The list of versions 127 */ 128 @Callable 129 public List<Map<String, Object>> getVersions() 130 { 131 List<Map<String, Object>> versions = new ArrayList<>(); 132 133 Iterator<Version> it = _versionsHandler.getVersions().iterator(); 134 while (it.hasNext()) 135 { 136 Version version = it.next(); 137 138 Map<String, Object> versionInfos = new LinkedHashMap<>(); 139 versionInfos.put("name", version.getName()); 140 versionInfos.put("version", version.getVersion()); 141 versionInfos.put("date", version.getDate() != null ? DateUtils.dateToString(version.getDate()) : null); 142 143 versions.add(versionInfos); 144 } 145 146 return versions; 147 } 148 149 /** 150 * Gets the content of the license text. 151 * @return The content of the license text. 152 * @throws IOException If the license file cannot be read 153 */ 154 @Callable 155 public String getLicenseText() throws IOException 156 { 157 String path = _cocoonContext.getRealPath(NOTICE_FILE_PATH); 158 File licenseFile = new File(path); 159 160 if (licenseFile.exists() && licenseFile.isFile()) 161 { 162 StringBuffer sb = new StringBuffer(); 163 try (BufferedReader reader = new BufferedReader(new FileReader(licenseFile))) 164 { 165 String line; 166 while ((line = reader.readLine()) != null) 167 { 168 sb.append(line); 169 sb.append("<br/>"); 170 } 171 } 172 catch (FileNotFoundException e) 173 { 174 getLogger().warn("License file {} is not present at {}", NOTICE_FILE_PATH, path); 175 return ""; 176 } 177 178 return sb.toString(); 179 } 180 else 181 { 182 getLogger().warn("License file {} is not present at {}", NOTICE_FILE_PATH, path); 183 return ""; 184 } 185 } 186}