This project has retired. For details please refer to its Attic page.
ConnectionErrorDialog xref
View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.chemistry.opencmis.workbench;
20  
21  import java.awt.BorderLayout;
22  import java.awt.Component;
23  import java.awt.Dimension;
24  import java.awt.event.ActionEvent;
25  import java.awt.event.ActionListener;
26  import java.io.IOException;
27  import java.net.MalformedURLException;
28  import java.net.URISyntaxException;
29  import java.net.UnknownHostException;
30  import java.util.Locale;
31  
32  import javax.net.ssl.SSLException;
33  import javax.swing.BorderFactory;
34  import javax.swing.BoxLayout;
35  import javax.swing.JButton;
36  import javax.swing.JDialog;
37  import javax.swing.JEditorPane;
38  import javax.swing.JPanel;
39  import javax.swing.JScrollPane;
40  import javax.swing.ScrollPaneConstants;
41  import javax.swing.WindowConstants;
42  import javax.xml.stream.XMLStreamException;
43  
44  import org.apache.chemistry.opencmis.commons.exceptions.CmisConnectionException;
45  import org.apache.chemistry.opencmis.commons.exceptions.CmisNotSupportedException;
46  import org.apache.chemistry.opencmis.commons.exceptions.CmisObjectNotFoundException;
47  import org.apache.chemistry.opencmis.commons.exceptions.CmisPermissionDeniedException;
48  import org.apache.chemistry.opencmis.commons.exceptions.CmisProxyAuthenticationException;
49  import org.apache.chemistry.opencmis.commons.exceptions.CmisRuntimeException;
50  import org.apache.chemistry.opencmis.commons.exceptions.CmisTooManyRequestsException;
51  import org.apache.chemistry.opencmis.commons.exceptions.CmisUnauthorizedException;
52  import org.apache.chemistry.opencmis.commons.impl.json.parser.JSONParseException;
53  import org.xml.sax.SAXParseException;
54  
55  public class ConnectionErrorDialog extends JDialog {
56  
57      private static final long serialVersionUID = 1L;
58  
59      public static final String HTTP_PROXY_HOST = "http.proxyHost";
60      public static final String HTTP_PROXY_PORT = "http.proxyPort";
61      public static final String HTTPS_PROXY_HOST = "https.proxyHost";
62      public static final String HTTPS_PROXY_PORT = "https.proxyPort";
63      public static final String HTTP_NON_PROXY_HOSTS = "http.nonProxyHosts";
64  
65      private final Exception exception;
66  
67      public ConnectionErrorDialog(JDialog owner, Exception exception) {
68          super(owner, "Connection Error", true);
69          this.exception = exception;
70  
71          ClientHelper.logError(exception);
72  
73          createGUI();
74      }
75  
76      private void createGUI() {
77          setMinimumSize(new Dimension(WorkbenchScale.scaleInt(600), WorkbenchScale.scaleInt(400)));
78          setPreferredSize(new Dimension(WorkbenchScale.scaleInt(600), WorkbenchScale.scaleInt(450)));
79  
80          setLayout(new BorderLayout());
81  
82          StringBuilder hint = new StringBuilder(1024);
83          hint.append("<h2><font color=\"red\">Exception: <em>" + exception.getClass().getSimpleName()
84                  + "</em></font><br>");
85          ClientHelper.encodeHtml(hint, exception.getMessage());
86          hint.append("</h2>");
87          if (exception.getCause() != null) {
88              hint.append("<h3><font color=\"red\">Cause: <em>" + exception.getCause().getClass().getSimpleName()
89                      + "</em></font><br>");
90              ClientHelper.encodeHtml(hint, exception.getCause().getMessage());
91              hint.append("</h3>");
92          }
93          hint.append("<hr><br>");
94          hint.append(getHint());
95  
96          // hint area
97          JPanel hintsPanel = new JPanel();
98          hintsPanel.setLayout(new BoxLayout(hintsPanel, BoxLayout.PAGE_AXIS));
99          hintsPanel.setBorder(WorkbenchScale.scaleBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)));
100         add(hintsPanel, BorderLayout.CENTER);
101 
102         JEditorPane hints = new JEditorPane("text/html", hint.toString());
103         hints.setEditable(false);
104         hints.setCaretPosition(0);
105 
106         hintsPanel.add(new JScrollPane(hints, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
107                 ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER));
108 
109         // close button
110         JPanel buttonPanel = new JPanel();
111         buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.PAGE_AXIS));
112         buttonPanel.setBorder(WorkbenchScale.scaleBorder(BorderFactory.createEmptyBorder(0, 5, 5, 5)));
113         add(buttonPanel, BorderLayout.PAGE_END);
114 
115         JButton closeButton = new JButton("Close");
116         closeButton.setPreferredSize(new Dimension(Short.MAX_VALUE, WorkbenchScale.scaleInt(30)));
117         closeButton.setMaximumSize(new Dimension(Short.MAX_VALUE, Short.MAX_VALUE));
118         closeButton.setAlignmentX(Component.CENTER_ALIGNMENT);
119 
120         closeButton.addActionListener(new ActionListener() {
121             @Override
122             public void actionPerformed(ActionEvent e) {
123                 ConnectionErrorDialog.this.dispose();
124             }
125         });
126 
127         buttonPanel.add(closeButton);
128 
129         getRootPane().setDefaultButton(closeButton);
130 
131         ClientHelper.installEscapeBinding(this, getRootPane(), true);
132 
133         setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
134         pack();
135         setLocationRelativeTo(getOwner());
136 
137         setVisible(true);
138     }
139 
140     private String getHint() {
141         if (exception instanceof CmisObjectNotFoundException || exception instanceof CmisNotSupportedException) {
142             return "The CMIS Workbench could connect to the server but the provided URL is not a CMIS endpoint URL."
143                     + "<br>Check your URL and proxy settings." + getProxyConfig();
144         } else if (exception instanceof CmisUnauthorizedException) {
145             return "The provided credentials are invalid.<br>Check your credentials.";
146         } else if (exception instanceof CmisPermissionDeniedException) {
147             return "The provided credentials are invalid or the user has no permission to connect."
148                     + "<br>Check your credentials.";
149         } else if (exception instanceof CmisProxyAuthenticationException) {
150             return "The proxy server requires valid credentials.<br>Check the session parameters "
151                     + "'org.apache.chemistry.opencmis.binding.proxyuser' and "
152                     + "'org.apache.chemistry.opencmis.binding.proxypassword'." + getProxyConfig();
153         } else if (exception instanceof CmisTooManyRequestsException) {
154             return "The server indicated that you made too many request. Wait or contact the server administrator.";
155         } else if (exception instanceof CmisRuntimeException) {
156             return "Something fatal happend on the client or server side."
157                     + "<br>Check your URL, the binding, and your proxy settings."
158                     + "<br><br>Also see the CMIS Workbench log for more details." + getProxyConfig();
159         } else if (exception instanceof CmisConnectionException) {
160             Throwable cause = exception.getCause();
161             while (cause instanceof CmisConnectionException) {
162                 cause = cause.getCause();
163             }
164 
165             if (cause instanceof MalformedURLException || cause instanceof URISyntaxException) {
166                 return "The provided URL is not a valid URL.<br>Check your URL.";
167             } else if (cause instanceof UnknownHostException) {
168                 return "The CMIS Workbench could not connect to the server."
169                         + "<br>Check your URL and your network and proxy settings." + getProxyConfig();
170             } else if (cause instanceof SSLException) {
171                 return "The CMIS Workbench could not establish a SSL connection to the server."
172                         + "<br>Check your network and proxy settings."
173                         + "<br><br>If you want to connect to a server with a self-signed certificate, "
174                         + "add the parameter <code>-Dcmis.workbench.acceptSelfSignedCertificates=true</code> "
175                         + "to the JAVA_OPTS in the CMIS Workbench start script and restart."
176                         + "<br><b>WARNING:</b> It disables <em>all</em> SSL certificate checks!" + getProxyConfig();
177             } else if (cause instanceof JSONParseException) {
178                 return "The provided URL does not return a JSON response."
179                         + "<br>Check your URL, the binding, and your proxy settings."
180                         + "<br><br>Some servers return a HTML login page if the credentials are incorrect."
181                         + "<br>Check your credentials." + getProxyConfig();
182             } else if (cause instanceof XMLStreamException) {
183                 return "The provided URL does not return an AtomPub response."
184                         + "<br>Check your URL, the binding, and your proxy settings."
185                         + "<br><br>Some servers return a HTML login page if the credentials are incorrect."
186                         + "<br>Check your credentials." + getProxyConfig();
187             } else if (cause instanceof SAXParseException) {
188                 return "The provided URL does not return a WSDL."
189                         + "<br>Check your URL, the binding, and your proxy settings."
190                         + "<br><br>Some servers return a HTML login page if the credentials are incorrect."
191                         + "<br>Check your credentials." + getProxyConfig();
192             } else if (cause instanceof IOException) {
193                 return "A network problem occured.<br>Check your URL and your network and proxy settings."
194                         + getProxyConfig();
195             }
196 
197             if (exception.getMessage().toLowerCase(Locale.ENGLISH).startsWith("unexpected document")) {
198                 return "The provided URL does not return a AtomPub response."
199                         + "<br>Check your URL, the binding, and your proxy settings."
200                         + "<br><br>Some servers return a HTML login page if the credentials are incorrect."
201                         + "<br>Check your credentials." + getProxyConfig();
202             }
203 
204             return "Check the URL, the binding, and the credentials.";
205         }
206 
207         return exception.getMessage();
208     }
209 
210     private String getProxyConfig() {
211         StringBuilder sb = new StringBuilder(256);
212 
213         sb.append("<br><br><hr><br><em>Current proxy settings:</em><br><br>");
214 
215         if (System.getProperty(HTTP_PROXY_HOST) == null && System.getProperty(HTTPS_PROXY_HOST) == null) {
216             sb.append("<b>- no proxy settings -</b>");
217         } else {
218             sb.append("<table>");
219             if (System.getProperty(HTTP_PROXY_HOST) != null) {
220                 sb.append("<tr><td><b>HTTP proxy:</b></td><td>");
221                 sb.append(System.getProperty(HTTP_PROXY_HOST) + ":" + System.getProperty(HTTP_PROXY_PORT));
222                 sb.append("</td></tr>");
223             }
224 
225             if (System.getProperty(HTTPS_PROXY_HOST) != null) {
226                 sb.append("<tr><td><b>HTTPS proxy:</b></td><td>");
227                 sb.append(System.getProperty(HTTPS_PROXY_HOST) + ":" + System.getProperty(HTTPS_PROXY_PORT));
228                 sb.append("</td></tr>");
229             }
230 
231             if (System.getProperty(HTTP_NON_PROXY_HOSTS) != null) {
232                 sb.append("<tr><td><b>Non proxy hosts:</b></td><td>");
233                 sb.append(System.getProperty(HTTP_NON_PROXY_HOSTS));
234                 sb.append("</td></tr>");
235             }
236         }
237 
238         return sb.toString();
239     }
240 }