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