001/* 002 * This library is part of OpenCms - 003 * the Open Source Content Management System 004 * 005 * Copyright (c) Alkacon Software GmbH & Co. KG (https://www.alkacon.com) 006 * 007 * This library is free software; you can redistribute it and/or 008 * modify it under the terms of the GNU Lesser General Public 009 * License as published by the Free Software Foundation; either 010 * version 2.1 of the License, or (at your option) any later version. 011 * 012 * This library is distributed in the hope that it will be useful, 013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 015 * Lesser General Public License for more details. 016 * 017 * For further information about Alkacon Software, please see the 018 * company website: https://www.alkacon.com 019 * 020 * For further information about OpenCms, please see the 021 * project website: https://www.opencms.org 022 * 023 * You should have received a copy of the GNU Lesser General Public 024 * License along with this library; if not, write to the Free Software 025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 026 */ 027 028package org.opencms.gwt.client.ui; 029 030import org.opencms.gwt.client.CmsCoreProvider; 031import org.opencms.gwt.shared.CmsListInfoBean; 032import org.opencms.gwt.shared.I_CmsUploadConstants; 033 034import java.util.ArrayList; 035import java.util.HashMap; 036import java.util.List; 037import java.util.Map; 038 039import com.google.common.base.Joiner; 040import com.google.gwt.core.client.GWT; 041import com.google.gwt.dom.client.Style.Unit; 042import com.google.gwt.json.client.JSONArray; 043import com.google.gwt.json.client.JSONObject; 044import com.google.gwt.json.client.JSONString; 045import com.google.gwt.json.client.JSONValue; 046import com.google.gwt.uibinder.client.UiBinder; 047import com.google.gwt.uibinder.client.UiField; 048import com.google.gwt.user.client.ui.Composite; 049import com.google.gwt.user.client.ui.Widget; 050 051/** 052 * Shows which viruses were found in files the user tried to upload. 053 */ 054public class CmsVirusReport extends Composite { 055 056 /** The UiBinder interface for this widget. */ 057 protected interface I_CmsVirusReportUiBinder extends UiBinder<Widget, CmsVirusReport> { 058 // empty 059 } 060 061 /** The UIBinder for this class. */ 062 static final I_CmsVirusReportUiBinder UI_BINDER = GWT.create(I_CmsVirusReportUiBinder.class); 063 064 /** Displays the warning message to the user. */ 065 @UiField 066 protected CmsMessageWidget m_message; 067 068 /** Contains the resource boxes with the virus information. */ 069 @UiField 070 protected CmsFieldSet m_fieldset; 071 072 /** 073 * Creates a new virus report widget. 074 * 075 * @param viruses a map from file names to lists of virus names found in the corresponding files 076 */ 077 public CmsVirusReport(Map<String, List<String>> viruses) { 078 079 Widget content = UI_BINDER.createAndBindUi(this); 080 initWidget(content); 081 CmsScrollPanel scroll = new CmsScrollPanel(); 082 scroll.getElement().getStyle().setProperty("maxHeight", 500, Unit.PX); 083 CmsList<?> list = new CmsList<>(); 084 m_fieldset.add(scroll); 085 scroll.add(list); 086 m_message.setMessageText( 087 org.opencms.gwt.client.Messages.get().key( 088 org.opencms.gwt.client.Messages.GUI_UPLOAD_VIRUSES_FOUND_WARNING_0)); 089 090 for (Map.Entry<String, List<String>> entry : viruses.entrySet()) { 091 String commaSeparatedViruses = Joiner.on(", ").join(entry.getValue()); 092 CmsListInfoBean infoBean = new CmsListInfoBean(entry.getKey(), commaSeparatedViruses, null); 093 CmsListItemWidget listItemWidget = new CmsListItemWidget(infoBean); 094 listItemWidget.setIcon(CmsCoreProvider.get().getResourceTypeIcon(entry.getKey())); 095 list.add(new CmsListItem(listItemWidget)); 096 } 097 } 098 099 /** 100 * Creates a popup containing a virus report. 101 * 102 * @param viruses a map from file names to lists of viruses found in the corresponding files 103 * @param callback the callback to executing after closing the dialog 104 * @return 105 */ 106 public static CmsPopup createPopup(Map<String, List<String>> viruses, Runnable callback) { 107 108 CmsPopup popup = new CmsPopup(); 109 popup.setModal(true); 110 popup.setGlassEnabled(true); 111 popup.setMainContent(new CmsVirusReport(viruses)); 112 popup.setCaption( 113 org.opencms.gwt.client.Messages.get().key( 114 org.opencms.gwt.client.Messages.GUI_UPLOAD_VIRUSES_FOUND_TITLE_0)); 115 CmsPushButton ok = new CmsPushButton(); 116 ok.setText(org.opencms.gwt.client.Messages.get().key(org.opencms.gwt.client.Messages.GUI_OK_0)); 117 ok.addClickHandler(event -> { 118 popup.hide(); 119 callback.run(); 120 }); 121 popup.addButton(ok); 122 return popup; 123 } 124 125 /** 126 * Extracts virus warnings from a JSON object. 127 * 128 * @param jsonObject the JSON object 129 * @return the map of virus warnings, which maps file names to lists of viruses found in the corresponding file 130 */ 131 public static Map<String, List<String>> getVirusWarnings(JSONObject jsonObject) { 132 133 Map<String, List<String>> viruses = new HashMap<>(); 134 JSONValue virusWarnings = jsonObject.get(I_CmsUploadConstants.ATTR_VIRUS_WARNINGS); 135 if (virusWarnings != null) { 136 JSONObject virusWarningsObj = (JSONObject)virusWarnings; 137 for (String key : virusWarningsObj.keySet()) { 138 List<String> virusList = new ArrayList<>(); 139 viruses.put(key, virusList); 140 JSONArray virusesForFile = (JSONArray)virusWarningsObj.get(key); 141 for (int i = 0; i < virusesForFile.size(); i++) { 142 JSONString jsv = (JSONString)virusesForFile.get(i); 143 virusList.add(jsv.stringValue()); 144 } 145 } 146 } 147 return viruses; 148 } 149 150}