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