001/*
002 * CDDL HEADER START
003 *
004 * The contents of this file are subject to the terms of the
005 * Common Development and Distribution License, Version 1.0 only
006 * (the "License").  You may not use this file except in compliance
007 * with the License.
008 *
009 * You can obtain a copy of the license at legal-notices/CDDLv1_0.txt
010 * or http://forgerock.org/license/CDDLv1.0.html.
011 * See the License for the specific language governing permissions
012 * and limitations under the License.
013 *
014 * When distributing Covered Code, include this CDDL HEADER in each
015 * file and include the License file at legal-notices/CDDLv1_0.txt.
016 * If applicable, add the following below this CDDL HEADER, with the
017 * fields enclosed by brackets "[]" replaced with your own identifying
018 * information:
019 *      Portions Copyright [yyyy] [name of copyright owner]
020 *
021 * CDDL HEADER END
022 *
023 *
024 *      Copyright 2008-2010 Sun Microsystems, Inc.
025 *      Portions Copyright 2014 ForgeRock AS
026 */
027
028package org.opends.guitools.controlpanel.ui;
029
030import java.awt.Component;
031import java.awt.GridBagConstraints;
032import java.awt.Insets;
033
034import javax.naming.NameNotFoundException;
035import javax.naming.NamingException;
036
037import org.forgerock.i18n.LocalizableMessage;
038import org.forgerock.i18n.LocalizableMessageBuilder;
039import org.opends.guitools.controlpanel.browser.BasicNodeError;
040import org.opends.guitools.controlpanel.browser.ReferralLimitExceededException;
041import org.opends.guitools.controlpanel.event.ConfigurationChangeEvent;
042import org.opends.quicksetup.util.Utils;
043import org.opends.server.types.LDAPURL;
044import org.opends.server.types.OpenDsException;
045
046import static com.forgerock.opendj.cli.Utils.*;
047
048import static org.opends.messages.AdminToolMessages.*;
049
050/**
051 * The panel that is displayed when there is an error searching an entry.
052 */
053public class ErrorSearchingEntryPanel extends StatusGenericPanel
054{
055  private static final long serialVersionUID = -8460172599072631973L;
056
057  /** Default constructor. */
058  public ErrorSearchingEntryPanel()
059  {
060    super();
061    GridBagConstraints gbc = new GridBagConstraints();
062    gbc.gridx = 0;
063    gbc.gridy = 0;
064    gbc.gridwidth = 1;
065    gbc.gridheight = 1;
066    gbc.weightx = 1.0;
067    gbc.anchor = GridBagConstraints.CENTER;
068    gbc.fill = GridBagConstraints.BOTH;
069    gbc.insets = new Insets(20, 20, 0, 20);
070    createErrorPane();
071    add(errorPane, gbc);
072    errorPane.setVisible(true);
073  }
074
075  /** {@inheritDoc} */
076  @Override
077  public Component getPreferredFocusComponent()
078  {
079    return errorPane;
080  }
081
082  /** {@inheritDoc} */
083  @Override
084  public void okClicked()
085  {
086  }
087
088  /** {@inheritDoc} */
089  @Override
090  public LocalizableMessage getTitle()
091  {
092    return INFO_CTRL_PANEL_ERROR_SEARCHING_ENTRY_TITLE.get();
093  }
094
095  /** {@inheritDoc} */
096  @Override
097  public void configurationChanged(ConfigurationChangeEvent ev)
098  {
099  }
100
101  /**
102   * Sets the error to be displayed in the panel.
103   * @param dn the DN of the entry that caused a problem.
104   * @param t the Throwable that occurred when searching the entry.
105   */
106  public void setError(String dn, Throwable t)
107  {
108    LocalizableMessage title = INFO_CTRL_PANEL_ERROR_SEARCHING_ENTRY_TITLE.get();
109    LocalizableMessage details;
110    if (t instanceof OpenDsException)
111    {
112      details = ERR_CTRL_PANEL_ERROR_SEARCHING_ENTRY.get(dn,
113      ((OpenDsException)t).getMessageObject());
114    }
115    else
116    {
117      details = ERR_CTRL_PANEL_ERROR_SEARCHING_ENTRY.get(dn, t);
118    }
119    updateErrorPane(errorPane, title, ColorAndFontConstants.errorTitleFont,
120        details, ColorAndFontConstants.defaultFont);
121  }
122
123  /**
124   * Sets the error to be displayed in the panel.
125   * @param dn the DN of the local entry.
126   * @param referrals the list of referrals defined in the entry.
127   * @param error the error that occurred resolving the referral.
128   */
129  public void setReferralError(String dn, String[] referrals,
130      BasicNodeError error)
131  {
132    LocalizableMessage title = INFO_CTRL_PANEL_ERROR_RESOLVING_REFERRAL_TITLE.get();
133    LocalizableMessageBuilder details = new LocalizableMessageBuilder();
134    StringBuilder sb = new StringBuilder();
135    for (String ref: referrals)
136    {
137      if (sb.length() > 0)
138      {
139        sb.append("<br>");
140      }
141      sb.append("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;").append(ref);
142    }
143    details.append(INFO_CTRL_PANEL_ERROR_RESOLVING_REFERRAL_MSG.get(dn, sb));
144    Exception ex = error.getException();
145    if (ex instanceof NamingException)
146    {
147      Object arg = error.getArg();
148      LocalizableMessage msg = null;
149      if (arg != null)
150      {
151        // Maybe is the LDAPURL
152        try
153        {
154          LDAPURL url = LDAPURL.decode(arg.toString(), false);
155          if (url.getHost() != null)
156          {
157            String hostPort = url.getHost()+":"+url.getPort();
158            if (ex instanceof ReferralLimitExceededException)
159            {
160              msg = LocalizableMessage.raw(ex.getLocalizedMessage());
161            }
162            else if (ex instanceof NameNotFoundException)
163            {
164              msg = ERR_CTRL_PANEL_COULD_NOT_FIND_PROVIDED_ENTRY_IN_REFERRAL.get(arg, hostPort);
165            }
166            else
167            {
168              msg = getMessageForException((NamingException) ex, hostPort);
169            }
170          }
171          else if (ex instanceof ReferralLimitExceededException)
172          {
173            msg = LocalizableMessage.raw(ex.getLocalizedMessage());
174          }
175          else if (ex instanceof NameNotFoundException)
176          {
177            msg = ERR_CTRL_PANEL_COULD_NOT_FIND_PROVIDED_ENTRY_IN_REFERRAL_NO_HOST.get(arg);
178          }
179          else
180          {
181            msg = Utils.getMessageForException((NamingException)ex);
182          }
183        }
184        catch (Throwable t)
185        {
186        }
187      }
188
189      if (msg == null)
190      {
191        if (ex instanceof ReferralLimitExceededException)
192        {
193          msg = LocalizableMessage.raw(ex.getLocalizedMessage());
194        }
195        else
196        {
197          msg = Utils.getMessageForException((NamingException)ex);
198        }
199      }
200      if (arg != null)
201      {
202        details.append("<br><br>").append(ERR_CTRL_PANEL_RESOLVING_REFERRAL_DETAILS.get(arg, msg));
203      }
204      else
205      {
206        details.append("<br><br>").append(INFO_CTRL_PANEL_DETAILS_THROWABLE.get(msg));
207      }
208    }
209    else if (ex != null)
210    {
211      String msg = ex.getLocalizedMessage();
212      if (msg == null)
213      {
214        msg = ex.toString();
215      }
216      details.append("<br><br>").append(INFO_CTRL_PANEL_DETAILS_THROWABLE.get(msg));
217    }
218    details.append("<br><br>").append(INFO_CTRL_PANEL_HOW_TO_EDIT_REFERRALS.get());
219    updateErrorPane(errorPane, title, ColorAndFontConstants.errorTitleFont,
220        details.toMessage(), ColorAndFontConstants.defaultFont);
221  }
222}