List of usage examples for org.apache.commons.httpclient HttpURL getPort
public int getPort()
From source file:com.gist.twitter.TwitterClient.java
/** * Extracts the host and post from the baseurl and constructs an * appropriate AuthScope for them for use with HttpClient. */// w w w . j a va 2 s . com private AuthScope createAuthScope(String baseUrl) throws URIException { HttpURL url = new HttpURL(baseUrl); return new AuthScope(url.getHost(), url.getPort()); }
From source file:com.idega.slide.business.IWSlideServiceBean.java
@SuppressWarnings("deprecation") private HttpClient getHttpClient(HttpURL url, UsernamePasswordCredentials credentials) throws Exception { HttpSession currentSession = getCurrentSession(); HttpState state = new WebdavState(); AuthScope authScope = new AuthScope(url.getHost(), url.getPort()); state.setCredentials(authScope, credentials); if (currentSession != null) { IWTimestamp iwExpires = new IWTimestamp(System.currentTimeMillis()); iwExpires.setMinute(iwExpires.getMinute() + 30); Date expires = new Date(iwExpires.getTimestamp().getTime()); boolean secure = url instanceof HttpsURL; Cookie cookie = new Cookie(url.getHost(), CoreConstants.PARAMETER_SESSION_ID, currentSession.getId(), CoreConstants.SLASH, expires, secure); state.addCookie(cookie);/* w w w. ja v a2s .c om*/ } HttpClient client = new HttpClient(new MultiThreadedHttpConnectionManager()); client.setState(state); HostConfiguration hostConfig = client.getHostConfiguration(); hostConfig.setHost(url); Credentials hostCredentials = null; if (credentials == null) { String userName = url.getUser(); if (userName != null && userName.length() > 0) { hostCredentials = new UsernamePasswordCredentials(userName, url.getPassword()); } } if (hostCredentials != null) { HttpState clientState = client.getState(); clientState.setCredentials(null, url.getHost(), hostCredentials); clientState.setAuthenticationPreemptive(true); } return client; }
From source file:org.aksonov.mages.connection.ConnectionManager.java
/** * Gets the connection.//w ww. j a v a 2s .co m * * @param host the host * * @return the connection */ private static HttpConnection getConnection(String host) { try { synchronized (hosts) { if (!hosts.contains(host)) { HttpURL httpURL = new HttpURL(host); HostConfiguration hostConfig = new HostConfiguration(); hostConfig.setHost(httpURL.getHost(), httpURL.getPort()); hosts.put(host, hostConfig); } } HostConfiguration hostConfig = hosts.get(host); //Log.d("ConnectionManager", "Retrieving connection from the pool"); HttpConnection connection = connectionManager.getConnectionWithTimeout(hostConfig, TIMEOUT); return connection; } catch (Exception e) { Log.e("ConnectionManager.getConnection", e); return null; } }
From source file:org.apache.cocoon.components.webdav.impl.DefaultWebDAVEventFactory.java
public Event createEvent(HttpURL url) { Event event = null;//from w ww . j av a 2 s.co m try { String host = url.getHost(); int port = url.getPort(); String path = url.getEscapedPathQuery(); event = createEvent(host, port, path); if (getLogger().isDebugEnabled()) getLogger().debug("Created event for url: " + event.toString()); } catch (Exception e) { if (getLogger().isErrorEnabled()) getLogger().error("could not create Event!", e); } return event; }
From source file:org.apache.cocoon.transformation.DASLTransformer.java
protected void performSearchMethod(String query) throws SAXException { OptionsMethod optionsMethod = null;/*from w w w . ja va 2s .c o m*/ SearchMethod searchMethod = null; try { DOMStreamer propertyStreamer = new DOMStreamer(this.xmlConsumer); optionsMethod = new OptionsMethod(this.targetUrl); searchMethod = new SearchMethod(this.targetUrl, query); HttpURL url = new HttpURL(this.targetUrl); HttpState state = new HttpState(); state.setCredentials(null, new UsernamePasswordCredentials(url.getUser(), url.getPassword())); HttpConnection conn = new HttpConnection(url.getHost(), url.getPort()); // eventcaching stuff SourceValidity extraValidity = makeWebdavEventValidity(url); if (extraValidity != null && m_validity != null) m_validity.add(extraValidity); // end eventcaching stuff WebdavResource resource = new WebdavResource(new HttpURL(this.targetUrl)); if (!resource.exists()) { throw new SAXException("The WebDAV resource don't exist"); } optionsMethod.execute(state, conn); if (!optionsMethod.isAllowed("SEARCH")) { throw new SAXException("The server doesn't support the SEARCH method"); } int httpstatus = searchMethod.execute(state, conn); this.contentHandler.startElement(DASL_QUERY_NS, RESULT_ROOT_TAG, PREFIX + ":" + RESULT_ROOT_TAG, XMLUtils.EMPTY_ATTRIBUTES); // something might have gone wrong, report it // 207 = multistatus webdav response if (httpstatus != 207) { this.contentHandler.startElement(DASL_QUERY_NS, ERROR_ROOT_TAG, PREFIX + ":" + ERROR_ROOT_TAG, XMLUtils.EMPTY_ATTRIBUTES); // dump whatever the server said propertyStreamer.stream(searchMethod.getResponseDocument()); this.contentHandler.endElement(DASL_QUERY_NS, ERROR_ROOT_TAG, PREFIX + ":" + ERROR_ROOT_TAG); } else { // show results Enumeration enumeration = searchMethod.getAllResponseURLs(); while (enumeration.hasMoreElements()) { String path = (String) enumeration.nextElement(); Enumeration properties = searchMethod.getResponseProperties(path); AttributesImpl attr = new AttributesImpl(); attr.addAttribute(DASL_QUERY_NS, PATH_NODE_NAME, PREFIX + ":" + PATH_NODE_NAME, "CDATA", path); this.contentHandler.startElement(DASL_QUERY_NS, RESOURCE_NODE_NAME, PREFIX + ":" + RESOURCE_NODE_NAME, attr); while (properties.hasMoreElements()) { BaseProperty metadata = (BaseProperty) properties.nextElement(); Element propertyElement = metadata.getElement(); propertyStreamer.stream(propertyElement); } this.contentHandler.endElement(DASL_QUERY_NS, RESOURCE_NODE_NAME, PREFIX + ":" + RESOURCE_NODE_NAME); } } this.contentHandler.endElement(DASL_QUERY_NS, RESULT_ROOT_TAG, PREFIX + ":" + RESULT_ROOT_TAG); } catch (SAXException e) { throw new SAXException("Unable to fetch the query data:", e); } catch (HttpException e1) { this.getLogger().error("Unable to contact Webdav server", e1); throw new SAXException("Unable to connect with server: ", e1); } catch (IOException e2) { throw new SAXException("Unable to connect with server: ", e2); } catch (NullPointerException e) { throw new SAXException("Unable to fetch the query data:", e); } catch (Exception e) { throw new SAXException("Generic Error:", e); } finally { // cleanup if (searchMethod != null) searchMethod.releaseConnection(); if (optionsMethod != null) optionsMethod.releaseConnection(); } }
From source file:org.apache.webdav.lib.WebdavResource.java
/** * Compare to the WebdavResource object. * * @param another the other WebdavResource object * @return the value 0 if the argument is equal. *///from w ww. j ava 2 s . c om public int compareToWebdavResource(WebdavResource another) { try { HttpURL anotherUrl = another.getHttpURL(); String thisHost = httpURL.getHost(); String anotherHost = anotherUrl.getHost(); if (!thisHost.equalsIgnoreCase(anotherHost)) return thisHost.compareTo(anotherHost); int thisPort = httpURL.getPort(); int anotherPort = anotherUrl.getPort(); if (thisPort != anotherPort) return (thisPort < anotherPort) ? -1 : 1; boolean thisCollection = isCollection(); boolean anotherCollection = another.isCollection(); if (thisCollection && !anotherCollection) return -1; if (anotherCollection && !thisCollection) return 1; String thisPath = httpURL.getPathQuery(); String anotherPath = anotherUrl.getPathQuery(); return thisPath.compareTo(anotherPath); } catch (Exception e) { // FIXME: not to return 0. } return 0; }
From source file:org.fao.geonet.kernel.harvest.harvester.webdav.WebDavRetriever.java
private WebdavResource createResource(String url) throws Exception { if (log.isDebugEnabled()) log.debug("Creating WebdavResource"); HttpURL http = url.startsWith("https") ? new HttpsURL(url) : new HttpURL(url); if (params.useAccount) { if (log.isDebugEnabled()) log.debug("using account, username: " + params.username + " password: " + params.password); http.setUserinfo(params.username, params.password); } else {/* www . j a va 2s . co m*/ if (log.isDebugEnabled()) log.debug("not using account"); } //--- setup proxy, if the case GeonetContext gc = (GeonetContext) context.getHandlerContext(Geonet.CONTEXT_NAME); SettingManager sm = gc.getSettingManager(); boolean enabled = sm.getValueAsBool("system/proxy/use", false); String host = sm.getValue("system/proxy/host"); String port = sm.getValue("system/proxy/port"); if (!enabled) { if (log.isDebugEnabled()) { log.debug("local proxy not enabled"); log.debug("returning a new WebdavResource"); log.debug("using http port: " + http.getPort() + " http uri: " + http.getURI()); } return new WebdavResource(http, 1); } else { if (log.isDebugEnabled()) log.debug("local proxy enabled"); if (!Lib.type.isInteger(port)) { throw new Exception("Proxy port is not an integer : " + port); } if (log.isDebugEnabled()) { log.debug("returning a new WebdavResource"); log.debug( "using http proxy port: " + port + " proxy host: " + host + " http uri: " + http.getURI()); } return new WebdavResource(http, host, Integer.parseInt(port)); } }
From source file:org.kei.android.phone.cellhistory.towers.CellIdHelper.java
public static int tryToLocate(final Context context, final TowerInfo ti, int cfg_timeout, final String mode, final String apiKeyOpenCellID) { int timeout = cfg_timeout * 1000, ret = CellIdRequestEntity.OK; HttpConnectionManager connectionManager = new SimpleHttpConnectionManager(); connectionManager.getParams().setConnectionTimeout(timeout); connectionManager.getParams().setSoTimeout(timeout); // Create a connection to some 'hidden' Google-API String baseURL = null;/*from www .ja v a 2 s . c o m*/ if (mode.equals(OPEN_CELL_ID_API)) { ti.lock(); try { baseURL = "http://opencellid.org/cell/get?key=" + apiKeyOpenCellID + "&mcc=" + ti.getMCC() + "&mnc=" + ti.getMNC() + "&cellid=" + ti.getCellId() + "&lac=" + ti.getLac() + "&format=json"; } finally { ti.unlock(); } } else baseURL = "http://www.google.com/glm/mmap"; HttpConnection connection = null; ti.setCellLatitude(Double.NaN); ti.setCellLongitude(Double.NaN); try { // Setup the connection HttpURL httpURL = null; if (baseURL.startsWith("https")) httpURL = new HttpsURL(baseURL); else httpURL = new HttpURL(baseURL); final HostConfiguration host = new HostConfiguration(); host.setHost(httpURL.getHost(), httpURL.getPort()); connection = connectionManager.getConnection(host); // Open it connection.open(); if (mode.equals(OPEN_CELL_ID_API)) ret = new OpenCellIdRequestEntity(ti).decode(baseURL, connection, timeout); else ret = new GoogleHiddenRequestEntity(ti).decode(baseURL, connection, timeout); } catch (Exception e) { Log.e(CellIdHelper.class.getSimpleName(), "Exception: " + e.getMessage(), e); ret = CellIdRequestEntity.EXCEPTION; CellHistoryApp.addLog(context, "tryToLocate::Exception: " + e.getMessage()); } finally { connection.close(); } connectionManager.releaseConnection(connection); return ret; }