View Javadoc
1   /**
2    * This file Copyright (c) 2015 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.cache.browser.app;
35  
36  import info.magnolia.cache.browser.rest.CacheServiceFactory;
37  import info.magnolia.cache.browser.rest.client.CacheService;
38  import info.magnolia.i18nsystem.I18nizer;
39  import info.magnolia.i18nsystem.SimpleTranslator;
40  import info.magnolia.objectfactory.ComponentProvider;
41  import info.magnolia.ui.api.availability.AvailabilityChecker;
42  import info.magnolia.ui.api.context.UiContext;
43  import info.magnolia.ui.dialog.actionarea.DialogActionExecutor;
44  import info.magnolia.ui.dialog.formdialog.FormBuilder;
45  import info.magnolia.ui.dialog.formdialog.FormDialogPresenterImpl;
46  import info.magnolia.ui.dialog.formdialog.FormView;
47  import info.magnolia.ui.dialog.registry.DialogDefinitionRegistry;
48  import info.magnolia.ui.vaadin.integration.contentconnector.ContentConnector;
49  import info.magnolia.ui.vaadin.overlay.MessageStyleTypeEnum;
50  
51  import java.util.ArrayList;
52  import java.util.List;
53  import java.util.Map;
54  
55  import javax.inject.Inject;
56  import javax.ws.rs.ClientErrorException;
57  import javax.ws.rs.ProcessingException;
58  
59  import org.apache.commons.lang.StringUtils;
60  import org.slf4j.Logger;
61  import org.slf4j.LoggerFactory;
62  
63  import com.vaadin.data.Item;
64  
65  /**
66   * Presenter with custom validation of reachability of subscribers.
67   */
68  public class CacheFormDialogPresenter extends FormDialogPresenterImpl {
69  
70      private static final Logger log = LoggerFactory.getLogger(CacheFormDialogPresenter.class);
71  
72      private final SimpleTranslator i18n;
73      private final CacheServiceFactory cacheServiceFactory;
74      private final UiContext uiContext;
75  
76      @Inject
77      public CacheFormDialogPresenter(DialogDefinitionRegistry dialogDefinitionRegistry, FormBuilder formBuilder, ComponentProvider componentProvider, DialogActionExecutor executor, FormView view, I18nizer i18nizer, SimpleTranslator i18n, AvailabilityChecker checker, ContentConnector contentConnector, CacheServiceFactory cacheServiceFactory, UiContext uiContext) {
78          super(dialogDefinitionRegistry, formBuilder, componentProvider, executor, view, i18nizer, i18n, checker, contentConnector);
79          this.i18n = i18n;
80          this.cacheServiceFactory = cacheServiceFactory;
81          this.uiContext = uiContext;
82      }
83  
84      @Override
85      public boolean isValid() {
86          boolean valid = super.isValid();
87          showValidation(true);
88          if (!valid) {
89              return false;
90          }
91          Item item = getView().getItemDataSource();
92          String username = (String) item.getItemProperty("username").getValue();
93          String password = (String) item.getItemProperty("password").getValue();
94          Map<String, CacheService> cacheServices = cacheServiceFactory.createCacheServices(new String[]{username, password});
95          List<String> unreachableSubscribers = new ArrayList<>();
96          for (Map.Entry<String, CacheService> entry : cacheServices.entrySet()) {
97              String subscriberName = entry.getKey();
98              CacheService cacheService = entry.getValue();
99              try {
100                 cacheService.ping();
101             } catch (ClientErrorException | ProcessingException e) {
102                 log.warn("Subscriber {} rest cache endpoint is not reachable: {}", subscriberName, e.getMessage());
103                 log.debug(e.getMessage(), e);
104                 unreachableSubscribers.add(subscriberName);
105                 valid = false;
106             }
107         }
108         if (!unreachableSubscribers.isEmpty()) {
109             uiContext.openNotification(MessageStyleTypeEnum.ERROR, true, i18n.translate("cache-browser-app.restLogin.validation.error", StringUtils.join(unreachableSubscribers.iterator(), ", ")));
110         }
111         return valid;
112     }
113 }