List of usage examples for org.apache.commons.httpclient HttpConnection open
public void open() throws IOException
From source file:it.intecs.pisa.toolbox.util.URLReader.java
public static Object getURLContent(String host, String url, int port) throws Exception { HttpMethod method;/* w w w . jav a 2s . c o m*/ try { method = new GetMethod(url); HttpConnection conn = new HttpConnection(host, port); String proxyHost; String proxyPort; if ((proxyHost = System.getProperty("http.proxyHost")) != null && (proxyPort = System.getProperty("http.proxyPort")) != null) { conn.setProxyHost(proxyHost); conn.setProxyPort(Integer.parseInt(proxyPort)); } conn.open(); method.execute(new HttpState(), conn); String inpTmp = method.getResponseBodyAsString(); Reader in = new InputStreamReader(method.getResponseBodyAsStream()); StringBuffer out = new StringBuffer(); char[] buffer = new char[1024]; for (int count = in.read(buffer); count >= 0; count = in.read(buffer)) { out.append(buffer, 0, count); } return out.toString(); } catch (Exception e) { e.printStackTrace(System.out); return null; } }
From source file:es.gva.cit.catalog.DiscoveryServiceClient.java
/** * It tries if the server is ready//from ww w . j a v a2 s . c o m * * @return boolean true --> server is ready false --> server is not ready */ public boolean serverIsReady() throws ServerIsNotReadyException { Properties systemSettings = System.getProperties(); Object isProxyEnabled = systemSettings.get("http.proxySet"); if ((isProxyEnabled == null) || (isProxyEnabled.equals("false"))) { Socket sock; try { sock = new Socket(getUri().getHost(), getUri().getPort()); } catch (UnknownHostException e) { throw new ServerIsNotReadyException(e); } catch (IOException e) { throw new ServerIsNotReadyException(e); } return (sock != null); } else { Object host = systemSettings.get("http.proxyHost"); Object port = systemSettings.get("http.proxyPort"); Object user = systemSettings.get("http.proxyUserName"); Object password = systemSettings.get("http.proxyPassword"); if ((host != null) && (port != null)) { int iPort = 80; try { iPort = Integer.parseInt((String) port); } catch (Exception e) { // Use 80 } HttpConnection connection = new HttpConnection(getUri().getHost(), getUri().getPort()); connection.setProxyHost((String) host); connection.setProxyPort(iPort); Authenticator.setDefault(new SimpleAuthenticator(user, password)); try { connection.open(); connection.close(); } catch (IOException e) { throw new ServerIsNotReadyException(e); } } } return true; }
From source file:edu.internet2.middleware.shibboleth.idp.profile.saml2.SLOProfileHandler.java
/** * Issues back channel logout request to session participant. * * @param sloContext/*from w w w.j av a2s . co m*/ * @param serviceLogoutInfo * @throws ProfileException */ private void initiateBackChannelLogout(SingleLogoutContext sloContext, LogoutInformation serviceLogoutInfo) throws ProfileException { if (!serviceLogoutInfo.isLoggedIn()) { log.info("Logout status for entity is '{}', not attempting logout", serviceLogoutInfo.getLogoutStatus().toString()); return; } String spEntityID = serviceLogoutInfo.getEntityID(); Endpoint endpoint = getEndpointForBinding(spEntityID, SAMLConstants.SAML2_SOAP11_BINDING_URI); if (endpoint == null) { log.info("No SAML2 LogoutRequest SOAP endpoint found for entity '{}'", spEntityID); serviceLogoutInfo.setLogoutUnsupported(); return; } serviceLogoutInfo.setLogoutAttempted(); LogoutRequestContext requestCtx = createLogoutRequestContext(sloContext, serviceLogoutInfo, endpoint); if (requestCtx == null) { log.info("Cannot create LogoutRequest Context for entity '{}'", spEntityID); serviceLogoutInfo.setLogoutFailed(); return; } HttpConnection httpConn = null; try { //prepare http message exchange for soap log.debug("Preparing HTTP transport for SOAP request"); httpConn = createHttpConnection(serviceLogoutInfo, endpoint); if (httpConn == null) { log.warn("Unable to acquire usable http connection from the pool"); serviceLogoutInfo.setLogoutFailed(); return; } log.debug("Opening HTTP connection to '{}'", endpoint.getLocation()); httpConn.open(); if (!httpConn.isOpen()) { log.warn("HTTP connection could not be opened"); serviceLogoutInfo.setLogoutFailed(); return; } log.debug("Preparing transports and encoders/decoders"); prepareSOAPTransport(requestCtx, httpConn, endpoint); SAMLMessageEncoder encoder = new HTTPSOAP11Encoder(); SAMLMessageDecoder decoder = new HTTPSOAP11Decoder(getParserPool()); //encode and sign saml request encoder.encode(requestCtx); //TODO: audit log is still missing log.info("Issuing back-channel logout request to SP '{}'", spEntityID); //execute SOAP/HTTP call log.debug("Executing HTTP POST"); if (!requestCtx.execute(httpConn)) { log.warn("Logout execution failed on SP '{}', HTTP status is '{}'", spEntityID, requestCtx.getHttpStatus()); serviceLogoutInfo.setLogoutFailed(); return; } //decode saml response decoder.decode(requestCtx); LogoutResponse spResponse = requestCtx.getInboundSAMLMessage(); StatusCode statusCode = spResponse.getStatus().getStatusCode(); if (statusCode.getValue().equals(StatusCode.SUCCESS_URI)) { log.info("Logout was successful on SP '{}'.", spEntityID); serviceLogoutInfo.setLogoutSucceeded(); } else { log.warn("Logout failed on SP '{}', logout status code is '{}'.", spEntityID, statusCode.getValue()); StatusCode secondaryCode = statusCode.getStatusCode(); if (secondaryCode != null) { log.warn("Additional status code: '{}'", secondaryCode.getValue()); } serviceLogoutInfo.setLogoutFailed(); } } catch (SocketTimeoutException e) { //socket connect or read timeout log.info("Socket timeout while sending SOAP request to SP '{}'", serviceLogoutInfo.getEntityID()); serviceLogoutInfo.setLogoutFailed(); } catch (IOException e) { //other networking error log.info("IOException caught while sending SOAP request", e); serviceLogoutInfo.setLogoutFailed(); } catch (Throwable t) { //unexpected log.error("Unexpected exception caught while sending SAML Logout request", t); serviceLogoutInfo.setLogoutFailed(); } finally { // requestCtx.releaseConnection(); if (httpConn != null && httpConn.isOpen()) { log.debug("Closing HTTP connection"); try { httpConn.close(); } catch (Throwable t) { log.warn("Caught exception while closing HTTP Connection", t); } } } }
From source file:org.aksonov.mages.connection.ConnectionManager.java
/** * Read post.// www .j a v a 2s . co m * * @param host the host * @param servlet the servlet * @param formData the form data * @param entity the entity * @param reader the reader * * @return the object * * @throws Exception the exception */ public static Object readPost(String host, String servlet, String formData, RequestEntity entity, DataReader reader) throws Exception { HttpConnection connection = getConnection(host); if (connection == null) { throw new IllegalArgumentException("Null connection"); } synchronized (connection) { PostMethod postMethod = null; try { //Log.d("ConnectionManager", "Open connection"); connection.open(); //Log.d("ConnectionManager", "Set timeout"); connection.setSocketTimeout(TIMEOUT); postMethod = new PostMethod(host + "/" + servlet + "?" + formData); postMethod.setRequestEntity(entity); //postMethod.setRequestHeader("Transfer-encoding", "base64"); //postMethod.setRequestHeader("Content-type", "application/octet-stream"); postMethod.execute(new HttpState(), connection); InputStream response = postMethod.getResponseBodyAsStream(); DataInputStream stream = new DataInputStream(response); Object data = reader.read(stream); return data; } catch (Exception e) { Log.e("ConnectionManager", e); throw e; } finally { if (postMethod != null) postMethod.releaseConnection(); connection.close(); connectionManager.releaseConnection(connection); } } }
From source file:org.aksonov.mages.connection.ConnectionManager.java
/** * Read get.// ww w. j a v a 2s . c om * * @param host the host * @param servlet the servlet * @param formData the form data * @param reader the reader * * @return the object * * @throws Exception the exception */ public static Object readGet(String host, String servlet, String formData, DataReader reader) throws Exception { HttpConnection connection = getConnection(host); if (connection == null) { throw new IllegalArgumentException("Null connection"); } synchronized (connection) { GetMethod getMethod = null; try { Log.d("ConnectionManager", "Open connection"); connection.open(); connection.setSocketTimeout(TIMEOUT); String url = host + "/" + servlet + "?" + formData; Log.d("ConnectionManager", "GET " + url); getMethod = new GetMethod(url); //getMethod.setRequestHeader("Transfer-encoding", "base64"); //getMethod.setRequestHeader("Content-type", "application/octet-stream"); getMethod.execute(new HttpState(), connection); InputStream response = getMethod.getResponseBodyAsStream(); DataInputStream stream = new DataInputStream(response); Object data = reader.read(stream); return data; } catch (Exception e) { Log.e("ConnectionManager", e); throw e; } finally { if (getMethod != null) { getMethod.releaseConnection(); } connection.close(); connectionManager.releaseConnection(connection); } } }
From source file:org.cauldron.tasks.HttpCall.java
/** * Running an HttpTask retrieves the path contents according to the task * attributes. POST body comes from the input. *///from ww w . ja v a2 s. com public Object run(Context context, Object input) throws TaskException { // For POST, body must be available as input. String body = null; if (!isGet) { body = (String) context.convert(input, String.class); if (body == null) throw new TaskException("HTTP POST input must be convertible to String"); } // Prepare request parameters. NameValuePair[] nvp = null; if (params != null && params.size() > 0) { nvp = new NameValuePair[params.size()]; int count = 0; for (Iterator entries = params.entrySet().iterator(); entries.hasNext();) { Map.Entry entry = (Map.Entry) entries.next(); String key = (String) entry.getKey(); String value = (String) entry.getValue(); nvp[count++] = new NameValuePair(key, value); } } // Create the retrieval method and set parameters. // HttpMethod method; if (isGet) { GetMethod get = new GetMethod(); if (nvp != null) get.setQueryString(nvp); method = get; } else { PostMethod post = new PostMethod(); post.setRequestBody(body); if (nvp != null) post.addParameters(nvp); method = post; } // Make the call. method.setPath(path); HttpConnection connection = connectionManager.getConnection(config); try { connection.open(); method.execute(new HttpState(), connection); return method.getResponseBodyAsString(); } catch (HttpException e) { throw new TaskException(e); } catch (IOException e) { throw new TaskException(e); } finally { connection.close(); } }
From source file:org.globus.axis.transport.commons.tests.CommonsHttpConnectionManagerTest.java
public void testConnectionReuseWithoutParams() throws Exception { CommonsHttpConnectionManager manager = new CommonsHttpConnectionManager(null); HostConfiguration h1 = new HostConfiguration(); h1.setHost(address, server1.getLocalPort()); HttpConnection c1 = manager.getConnection(h1); // new connection assertTrue(!c1.isOpen());// ww w. j ava 2 s . c o m c1.open(); c1.releaseConnection(); HostConfiguration h2 = new HostConfiguration(); h2.setHost(address, server1.getLocalPort()); HttpConnection c2 = manager.getConnection(h2); // connection should have been released // so c2 is c1 assertTrue(h2.equals(h1)); assertTrue(c2 == c1); assertTrue(c2.isOpen()); HttpConnection c3 = manager.getConnection(h2); // connection c2 was not released so new connection // c2 != c3 assertTrue(!c3.isOpen()); assertTrue(c3 != c2); assertTrue(c3 != c1); c2.releaseConnection(); c3.releaseConnection(); Server server2 = new Server(); // it's a new port HostConfiguration h4 = new HostConfiguration(); h4.setHost(address, server2.getLocalPort()); HttpConnection c4 = manager.getConnection(h4); assertTrue(!c4.isOpen()); assertTrue(c4 != c1); assertTrue(c4 != c2); assertTrue(c4 != c3); server2.close(); }
From source file:org.globus.axis.transport.commons.tests.CommonsHttpConnectionManagerTest.java
public void testConnectionReuseWithParams() throws Exception { CommonsHttpConnectionManager manager = new CommonsHttpConnectionManager(PARAMS); HostConfiguration h1 = new HostConfiguration(); h1.setHost(address, server1.getLocalPort()); h1.getParams().setParameter("A", "foo"); h1.getParams().setParameter("B", "bar"); h1.getParams().setParameter("C", "fff"); HttpConnection c1 = manager.getConnection(h1); assertTrue(!c1.isOpen());/*from ww w . j av a2s . c om*/ c1.open(); c1.releaseConnection(); HostConfiguration h2 = new HostConfiguration(); h2.setHost(address, server1.getLocalPort()); h2.getParams().setParameter("A", "foo"); h2.getParams().setParameter("B", "bar"); // still should be reused since C is not checked param h2.getParams().setParameter("C", "ggg"); HttpConnection c2 = manager.getConnection(h2); // connection should have been released // so c2 is c1 assertTrue(h2.equals(h1)); assertTrue(c2.isOpen()); assertTrue(c2 == c1); HttpConnection c3 = manager.getConnection(h2); // new connection becuase it wasn't released assertTrue(c3 != c1); assertTrue(c3 != c2); assertTrue(!c3.isOpen()); c2.releaseConnection(); c3.releaseConnection(); // this one does not have params HostConfiguration h4 = new HostConfiguration(); h4.setHost(address, server1.getLocalPort()); HttpConnection c4 = manager.getConnection(h4); // new connection assertTrue(c4 != c1); assertTrue(c4 != c2); assertTrue(c4 != c3); assertTrue(!c4.isOpen()); c4.open(); c4.releaseConnection(); // this one only has B parameter HostConfiguration h5 = new HostConfiguration(); h5.setHost(address, server1.getLocalPort()); h5.getParams().setParameter("B", "bar"); HttpConnection c5 = manager.getConnection(h5); // also a new connection assertTrue(c5 != c1); assertTrue(c5 != c2); assertTrue(c5 != c3); assertTrue(c5 != c4); assertTrue(!c5.isOpen()); c5.open(); c5.releaseConnection(); // this one only has different B parameter HostConfiguration h6 = new HostConfiguration(); h6.setHost(address, server1.getLocalPort()); h6.getParams().setParameter("A", "fooo"); h6.getParams().setParameter("B", "bar"); HttpConnection c6 = manager.getConnection(h6); assertTrue(c6 != c1); assertTrue(c6 != c2); assertTrue(c6 != c3); assertTrue(c6 != c4); assertTrue(c6 != c5); assertTrue(!c6.isOpen()); c6.open(); c6.releaseConnection(); }
From source file:org.globus.axis.transport.commons.tests.CommonsHttpConnectionManagerTest.java
public void testIdleConnectionSweeper() throws Exception { CommonsHttpConnectionManager manager = new CommonsHttpConnectionManager(null); manager.setConnectionIdleTime(1000 * 2); HostConfiguration h1 = new HostConfiguration(); h1.setHost(address, server1.getLocalPort()); HttpConnection c1 = manager.getConnection(h1); // new connection assertTrue(!c1.isOpen());/* w ww. j a va2 s.c o m*/ c1.open(); Thread.sleep(1000); c1.releaseConnection(); assertTrue(c1 == manager.getConnection(h1)); assertTrue(c1.isOpen()); c1.releaseConnection(); Thread.sleep(1000 * 4); HttpConnection c2 = manager.getConnection(h1); assertTrue(c1 != c2); }
From source file:org.globus.axis.transport.commons.tests.CommonsHttpConnectionManagerTest.java
public void testMultipleConnectionRelease() throws Exception { CommonsHttpConnectionManager manager = new CommonsHttpConnectionManager(null); HostConfiguration h1 = new HostConfiguration(); h1.setHost(address, server1.getLocalPort()); HttpConnection c1 = manager.getConnection(h1); assertTrue(!c1.isOpen());//from ww w.ja va2 s . co m c1.open(); c1.releaseConnection(); c1.releaseConnection(); HttpConnection c2 = manager.getConnection(h1); assertTrue(c1 == c2); HttpConnection c3 = manager.getConnection(h1); assertTrue(c3 != c2); }