List of usage examples for java.net ConnectException toString
public String toString()
From source file:com.sun.socialsite.util.ImageUtil.java
/** * Grabs an image from a URI (and scales it to some reasonable size). * * @param uriString the URI from which to grab the image. *///from w w w . jav a 2 s .c o m public BufferedImage getImage(final String uriString) { // TODO: better approach to Exceptions if ((uriString == null) || ("".equals(uriString))) { return null; } URI uri = null; GetMethod method = null; try { uri = new URI(uriString, false); HttpClient httpClient = new HttpClient(httpConnectionManager); method = new GetMethod(uri.getEscapedURI()); httpClient.executeMethod(method); BufferedImage origImage = ImageIO.read(method.getResponseBodyAsStream()); log.debug(String.format("Got origImage for %s", uriString)); return getScaledImage(origImage, maxWidth, maxHeight); } catch (ConnectException e) { log.warn(String.format("Failed to retrieve image: %s [%s]", uriString, e.toString())); return null; } catch (ConnectTimeoutException e) { log.warn(String.format("Failed to retrieve image: %s [%s]", uriString, e.toString())); return null; } catch (SocketTimeoutException e) { log.warn(String.format("Failed to retrieve image: %s [%s]", uriString, e.toString())); return null; } catch (UnknownHostException e) { log.warn(String.format("Failed to retrieve image: %s [%s]", uriString, e.toString())); return null; } catch (IllegalArgumentException e) { log.warn(String.format("Failed to retrieve image: %s [%s]", uriString, e.toString())); return null; } catch (IllegalStateException e) { log.warn(String.format("Failed to retrieve image: %s [%s]", uriString, e.toString())); return null; } catch (IIOException e) { log.warn(String.format("Failed to process image: %s [%s]", uriString, e.toString())); return null; } catch (Throwable t) { log.error(String.format("Failed to retrieve image: %s", uriString), t); return null; } finally { if (method != null) { method.releaseConnection(); } } }
From source file:net.fenyo.gnetwatch.actions.ActionHTTP.java
/** * Establishes the connections to the server. * @param idx number of connections to establish. * @param querier http/ftp parameters./*from ww w . j ava 2s . co m*/ * @param connections array of connections established. * @param streams streams associated to the connections. * @param sizes data sizes ready to be read on the connections. * @param url url to connect to. * @param proxy proxy to use. * @return number of bytes received. * @throws IOException IO exception. */ private int connect(final int idx, final IPQuerier querier, final URLConnection[] connections, final InputStream[] streams, final int[] sizes, final URL url, final Proxy proxy) throws IOException { error_string = ""; try { connections[idx] = querier.getUseProxy() ? url.openConnection(proxy) : url.openConnection(); connections[idx].setUseCaches(false); connections[idx].connect(); streams[idx] = connections[idx].getInputStream(); sizes[idx] = connections[idx].getContentLength(); } catch (final IOException ex) { streams[idx] = null; sizes[idx] = 0; int response_code = 0; try { response_code = ((HttpURLConnection) connections[idx]).getResponseCode(); } catch (final ConnectException ex2) { getGUI().appendConsole(ex2.toString() + "<BR/>"); try { Thread.sleep(1000); } catch (final InterruptedException ex3) { } throw ex2; } error_string = "(http error " + response_code + ")"; final InputStream error_stream = ((HttpURLConnection) connections[idx]).getErrorStream(); if (error_stream == null) return 0; int nread, nread_tot = 0; String error_str = ""; final byte[] error_buf = new byte[65536]; while ((nread = error_stream.read(error_buf)) > 0) { // log.debug("error: " + new String(error_buf).substring(0, nread - 1)); error_str += new String(error_buf); nread_tot += nread; } error_stream.close(); return nread_tot; } return 0; }
From source file:edu.ku.brc.helpers.HTTPGetter.java
/** * Performs a "generic" HTTP request and fill member variable with results * use "getDigirResultsetStr" to get the results as a String * * @param url URL to be executed/*from ww w .j av a2s . c om*/ * @param fileCache the file to place the results * @return returns an error code */ public byte[] doHTTPRequest(final String url, final File fileCache) { byte[] bytes = null; Exception excp = null; status = ErrorCode.NoError; // Create an HttpClient with the MultiThreadedHttpConnectionManager. // This connection manager must be used if more than one thread will // be using the HttpClient. httpClient = new HttpClient(new MultiThreadedHttpConnectionManager()); GetMethod method = null; try { method = new GetMethod(url); //log.debug("getting " + method.getURI()); //$NON-NLS-1$ httpClient.executeMethod(method); // get the response body as an array of bytes long bytesRead = 0; if (fileCache == null) { bytes = method.getResponseBody(); bytesRead = bytes.length; } else { BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(fileCache)); bytes = new byte[4096]; InputStream ins = method.getResponseBodyAsStream(); BufferedInputStream bis = new BufferedInputStream(ins); while (bis.available() > 0) { int numBytes = bis.read(bytes); if (numBytes > 0) { bos.write(bytes, 0, numBytes); bytesRead += numBytes; } } bos.flush(); bos.close(); bytes = null; } log.debug(bytesRead + " bytes read"); //$NON-NLS-1$ } catch (ConnectException ce) { excp = ce; log.error(String.format("Could not make HTTP connection. (%s)", ce.toString())); //$NON-NLS-1$ status = ErrorCode.HttpError; } catch (HttpException he) { excp = he; log.error(String.format("Http problem making request. (%s)", he.toString())); //$NON-NLS-1$ status = ErrorCode.HttpError; } catch (IOException ioe) { excp = ioe; log.error(String.format("IO problem making request. (%s)", ioe.toString())); //$NON-NLS-1$ status = ErrorCode.IOError; } catch (java.lang.IllegalArgumentException ioe) { excp = ioe; log.error(String.format("IO problem making request. (%s)", ioe.toString())); //$NON-NLS-1$ status = ErrorCode.IOError; } catch (Exception e) { excp = e; log.error("Error: " + e); //$NON-NLS-1$ status = ErrorCode.Error; } finally { // always release the connection after we're done if (isThrowingErrors && status != ErrorCode.NoError) { edu.ku.brc.af.core.UsageTracker.incrHandledUsageCount(); edu.ku.brc.exceptions.ExceptionTracker.getInstance().capture(HTTPGetter.class, excp); } if (method != null) method.releaseConnection(); //log.debug("Connection released"); //$NON-NLS-1$ } if (listener != null) { if (status == ErrorCode.NoError) { listener.completed(this); } else { listener.completedWithError(this, status); } } return bytes; }
From source file:net.timewalker.ffmq4.transport.tcp.io.TcpPacketTransport.java
/** * Connect the transport to its remote endpoint *//*from w w w. j av a2 s. c o m*/ private Socket connect(URI transportURI) throws PacketTransportException { String protocol = transportURI.getScheme(); String host = transportURI.getHost(); int port = transportURI.getPort(); int connectTimeout = settings.getIntProperty(FFMQClientSettings.TRANSPORT_TCP_CONNECT_TIMEOUT, 30); try { Socket socket = SocketUtils.setupSocket(createSocket(protocol), socketSendBufferSize, socketRecvBufferSize); log.debug("#" + id + " opening a TCP connection to " + host + ":" + port); socket.connect(new InetSocketAddress(host, port), connectTimeout * 1000); return socket; } catch (ConnectException e) { log.error("#" + id + " could not connect to " + host + ":" + port + " (timeout=" + connectTimeout + "s) : " + e.getMessage()); throw new PacketTransportException("Could not connect to " + host + ":" + port + " : " + e.toString()); } catch (Exception e) { log.error( "#" + id + " could not connect to " + host + ":" + port + " (timeout=" + connectTimeout + "s)", e); throw new PacketTransportException("Could not connect to " + host + ":" + port + " : " + e.toString()); } }
From source file:org.jzkit.z3950.util.ZEndpoint.java
public void run() { log.debug("Bringing assoc up........Active Z Thread counter = " + (++active_thread_counter)); log.debug("My thread priority : " + this.getPriority()); log.debug("My isDaemon: " + this.isDaemon()); try {//from www. j a va 2 s.c om assoc_status = ASSOC_STATUS_CONNECTING; connect(); assoc_status = ASSOC_STATUS_CONNECTED; log.debug("Connect completed OK, Listening for incoming PDUs"); } catch (ConnectException ce) { log.info(ce.toString()); assoc_status = ASSOC_STATUS_PERM_FAILURE; sendDummyFailInitResponse(ce.toString()); running = false; } catch (IOException ioe) { log.warn("ZEndpoint thread encountered an exception", ioe); assoc_status = ASSOC_STATUS_PERM_FAILURE; sendDummyFailInitResponse(ioe.toString()); running = false; } while (running) { try { log.debug("Waiting for data on input stream....."); BERInputStream bds = new BERInputStream(incoming_data, charset_encoding, DEFAULT_BUFF_SIZE, reg); PDU_type pdu = null; pdu = (PDU_type) codec.serialize(bds, pdu, false, "PDU"); log.debug("Notifiy observers"); if (pdu.which == PDU_type.close_CID) { log.debug("Just got a close APDU"); close_notified = true; } decOpCount(); notifyAPDUEvent(pdu); // If the target does not support concurrent operations then it's possible that // outbound APDU's have stacked up whilst we wait for the response handled here. // Therefore, here we check the outgoing apdu queue and send any messages that // have been queued if (!close_notified) sendPending(); log.debug("Yield to other threads...."); yield(); } catch (InterruptedIOException iioe) { log.debug("Processing java.io.InterruptedIOException, shut down association" + " - hostname=" + target_hostname); log.info(iioe.toString()); running = false; } catch (SocketException se) { // Most likely socket closed. log.info("SocketException"); log.info(se.toString() + " - hostname=" + target_hostname); running = false; } catch (Exception e) { log.warn("ZEndpoint Unknown error", e); log.info(e.toString() + " - hostname=" + target_hostname); running = false; } finally { } } synchronized (op_counter_lock) { op_counter_lock.notifyAll(); } // We might need to notify any listening objects that the assoc has been // shut down if the target did not send a close before snapping the assoc // (Or in case there was a network problem etc) if (!close_notified) { notifyClose(); } // End of association, clear out all listeners log.debug("End of ZEndpoint listening thread for host " + target_hostname + " active z thread counter=" + (--active_thread_counter)); pdu_announcer.deleteObservers(); pdu_announcer = null; try { incoming_data = null; outgoing_data = null; if (z_assoc != null) z_assoc.close(); } catch (Exception e) { // catches the socket close execption... } assoc_status = ASSOC_STATUS_IDLE; z_assoc = null; }
From source file:org.openhab.binding.ebus.internal.connection.EBusTCPConnector.java
@Override protected boolean connect() throws IOException { try {/*from ww w. j a v a2 s . co m*/ socket = new Socket(hostname, port); socket.setSoTimeout(20000); socket.setKeepAlive(true); socket.setTcpNoDelay(true); socket.setTrafficClass((byte) 0x10); // Useful? We try it // socket.setReceiveBufferSize(1); socket.setSendBufferSize(1); inputStream = socket.getInputStream(); outputStream = socket.getOutputStream(); return super.connect(); } catch (ConnectException e) { logger.error(e.toString()); } catch (Exception e) { logger.error(e.toString(), e); } return false; }
From source file:se.simonsoft.cms.testing.svn.SvnTestSetup.java
private boolean isHttpUrlSvnParent(String httpUrl) { RestURL restUrl = new RestURL(httpUrl); RestAuthenticationClientCert auth = null; try {// w ww .j a va2s. c o m auth = new RestAuthenticationClientCert(getIgnoringTrustManager(), null, "testuser", "testpassword"); } catch (KeyManagementException e1) { //Should not happen, so won't handle the error. e1.printStackTrace(); } RestClient restClientJavaNet = new RestClientJavaNet(restUrl.r(), auth); ResponseHeaders head; try { head = restClientJavaNet.head(restUrl.p()); } catch (java.net.ConnectException ec) { logger.debug("Rejecting URL", restUrl, "due to connection error:", ec.toString()); return false; } catch (IOException e) { throw new RuntimeException("Svn test setup failed", e); } if (head.getStatus() != 200 && head.getStatus() != 401) { logger.debug("Rejecting URL", restUrl, "due to status", head.getStatus()); return false; } // TODO check for "Colleciton of repositories" logger.debug("URL", restUrl, "ok with content type", head.getContentType()); return true; }