List of usage examples for java.lang Exception equals
public boolean equals(Object obj)
From source file:com.android.tools.idea.sdk.remote.internal.UrlOpener.java
/** * Opens a URL. It can be a simple URL or one which requires basic * authentication.//w w w . j a v a 2 s. co m * <p/> * Tries to access the given URL. If http response is either * {@code HttpStatus.SC_UNAUTHORIZED} or * {@code HttpStatus.SC_PROXY_AUTHENTICATION_REQUIRED}, asks for * login/password and tries to authenticate into proxy server and/or URL. * <p/> * This implementation relies on the Apache Http Client due to its * capabilities of proxy/http authentication. <br/> * Proxy configuration is determined by {@link ProxySelectorRoutePlanner} using the JVM proxy * settings by default. * <p/> * For more information see: <br/> * - {@code http://hc.apache.org/httpcomponents-client-ga/} <br/> * - {@code http://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/impl/conn/ProxySelectorRoutePlanner.html} * <p/> * There's a very simple realm cache implementation. * Login/Password for each realm are stored in a static {@link Map}. * Before asking the user the method verifies if the information is already * available in the memory cache. * * @param url the URL string to be opened. * @param needsMarkResetSupport Indicates the caller <em>must</em> have an input stream that * supports the mark/reset operations (as indicated by {@link InputStream#markSupported()}. * Implementation detail: If the original stream does not, it will be fetched and wrapped * into a {@link ByteArrayInputStream}. This can only work sanely if the resource is a * small file that can fit in memory. It also means the caller has no chance of showing * a meaningful download progress. If unsure, callers should set this to false. * @param monitor {@link ITaskMonitor} to output status. * @param headers An optional array of HTTP headers to use in the GET request. * @return Returns a {@link Pair} with {@code first} holding an {@link InputStream} * and {@code second} holding an {@link HttpResponse}. * The returned pair is never null and contains * at least a code; for http requests that provide them the response * also contains locale, headers and an status line. * The input stream can be null, especially in case of error. * The caller must only accept the stream if the response code is 200 or similar. * @throws IOException Exception thrown when there are problems retrieving * the URL or its content. * @throws CanceledByUserException Exception thrown if the user cancels the * authentication dialog. */ @NonNull static Pair<InputStream, HttpResponse> openUrl(@NonNull String url, boolean needsMarkResetSupport, @NonNull ITaskMonitor monitor, @Nullable Header[] headers) throws IOException, CanceledByUserException { Exception fallbackOnJavaUrlConnect = null; Pair<InputStream, HttpResponse> result = null; try { result = openWithHttpClient(url, monitor, headers); } catch (UnknownHostException e) { // Host in unknown. No need to even retry with the Url object, // if it's broken, it's broken. It's already an IOException but // it could use a better message. throw new IOException("Unknown Host " + e.getMessage(), e); } catch (ClientProtocolException e) { // We get this when HttpClient fails to accept the current protocol, // e.g. when processing file:// URLs. fallbackOnJavaUrlConnect = e; } catch (IOException e) { throw e; } catch (CanceledByUserException e) { // HTTP Basic Auth or NTLM login was canceled by user. throw e; } catch (Exception e) { if (DEBUG) { System.out.printf("[HttpClient Error] %s : %s\n", url, e.toString()); } fallbackOnJavaUrlConnect = e; } if (fallbackOnJavaUrlConnect != null) { // If the protocol is not supported by HttpClient (e.g. file:///), // revert to the standard java.net.Url.open. try { result = openWithUrl(url, headers); } catch (IOException e) { throw e; } catch (Exception e) { if (DEBUG && !fallbackOnJavaUrlConnect.equals(e)) { System.out.printf("[Url Error] %s : %s\n", url, e.toString()); } } } // If the caller requires an InputStream that supports mark/reset, let's // make sure we have such a stream. if (result != null && needsMarkResetSupport) { InputStream is = result.getFirst(); if (is != null) { if (!is.markSupported()) { try { // Consume the whole input stream and offer a byte array stream instead. // This can only work sanely if the resource is a small file that can // fit in memory. It also means the caller has no chance of showing // a meaningful download progress. InputStream is2 = toByteArrayInputStream(is); if (is2 != null) { result = Pair.of(is2, result.getSecond()); try { is.close(); } catch (Exception ignore) { } } } catch (Exception e3) { // Ignore. If this can't work, caller will fail later. } } } } if (result == null) { // Make up an error code if we don't have one already. HttpResponse outResponse = new BasicHttpResponse(new ProtocolVersion("HTTP", 1, 0), //$NON-NLS-1$ HttpStatus.SC_METHOD_FAILURE, ""); //$NON-NLS-1$; // 420=Method Failure result = Pair.of(null, outResponse); } return result; }
From source file:org.encuestame.core.exception.EnMeMappingExceptionResolver.java
@Override protected ModelAndView doResolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) { ex.printStackTrace();/*from w w w . ja v a 2 s . co m*/ String errorView = "redirect:/error"; if (ex.equals(DataAccessResourceFailureException.class)) { errorView = "redirect:/error"; } ModelAndView modelAndView = new ModelAndView(errorView); if (logger.isDebugEnabled()) { ex.printStackTrace(); } //set get parameters; //modelAndView.getModel().put(EXCEPTION_TYPE, ex.getClass()); //modelAndView.getModel().put("error", "fail"); StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); ex.printStackTrace(pw); //System.out.println(sw.toString()); RequestSessionMap.setErrorMessage(ex.getMessage(), sw.toString()); return modelAndView; }
From source file:org.jclouds.http.functions.ParseURIFromListOrLocationHeaderIf20xTest.java
@Test public void testExceptionWhenIOExceptionOn200() throws ExecutionException, InterruptedException, TimeoutException, IOException { Function<HttpResponse, URI> function = new ParseURIFromListOrLocationHeaderIf20x(); HttpResponse response = createMock(HttpResponse.class); expect(response.getStatusCode()).andReturn(200).atLeastOnce(); expect(response.getFirstHeaderOrNull(HttpHeaders.CONTENT_TYPE)).andReturn("text/uri-list"); RuntimeException exception = new RuntimeException("bad"); expect(response.getContent()).andThrow(exception); replay(response);// ww w. ja v a 2 s. c o m try { function.apply(response); } catch (Exception e) { assert e.equals(exception); } verify(response); }
From source file:org.jclouds.http.functions.ReturnStringIf200Test.java
@Test public void testExceptionWhenIOExceptionOn200() throws ExecutionException, InterruptedException, TimeoutException, IOException { Function<HttpResponse, String> function = new ReturnStringIf200(); HttpResponse response = createMock(HttpResponse.class); expect(response.getStatusCode()).andReturn(200).atLeastOnce(); RuntimeException exception = new RuntimeException("bad"); expect(response.getContent()).andThrow(exception); replay(response);//from www. jav a 2 s . c om try { function.apply(response); } catch (Exception e) { assert e.equals(exception); } verify(response); }