Example usage for org.apache.commons.httpclient HttpClient getState

List of usage examples for org.apache.commons.httpclient HttpClient getState

Introduction

In this page you can find the example usage for org.apache.commons.httpclient HttpClient getState.

Prototype

public HttpState getState()

Source Link

Usage

From source file:com.inbravo.scribe.rest.service.crm.zd.ZDV2RESTCRMService.java

/**
 * //w w w.  j av  a2s.  c  om
 * @param cADCommandObject
 * @param query
 * @param select
 * @param order
 * @return
 * @throws Exception
 */
private final ScribeCommandObject searchAllTypeOfObjects(final ScribeCommandObject cADCommandObject,
        final String query, final String select, final String order) throws Exception {

    if (logger.isDebugEnabled()) {
        logger.debug("----Inside searchAllTypeOfObjects query: " + query + " & select: " + select + " & order: "
                + order);
    }
    GetMethod getMethod = null;
    try {

        String serviceURL = null;
        String serviceProtocol = null;
        String userId = null;
        String password = null;
        String sessionId = null;
        String crmPort = "80";

        /* Get agent from session manager */
        final ScribeCacheObject cacheObject = zDCRMSessionManager
                .getCrmUserIdWithCRMSessionInformation(cADCommandObject.getCrmUserId());

        /* Get CRM information from agent */
        serviceURL = cacheObject.getScribeMetaObject().getCrmServiceURL();
        serviceProtocol = cacheObject.getScribeMetaObject().getCrmServiceProtocol();
        userId = cacheObject.getScribeMetaObject().getCrmUserId();
        password = cacheObject.getScribeMetaObject().getCrmPassword();
        crmPort = cacheObject.getScribeMetaObject().getCrmPort();

        /* Create Zen desk URL */
        final String zenDeskURL = serviceProtocol + "://" + serviceURL + zdAPISubPath + "search.json";

        if (logger.isDebugEnabled()) {
            logger.debug("----Inside searchAllTypeOfObjects zenDeskURL: " + zenDeskURL + " & sessionId: "
                    + sessionId);
        }

        /* Instantiate get method */
        getMethod = new GetMethod(zenDeskURL);

        /* Set request content type */
        getMethod.addRequestHeader("Content-Type", "application/json");

        /* Cookie is required to be set in case of search */
        getMethod.addRequestHeader("Cookie", sessionId);

        final HttpClient httpclient = new HttpClient();

        /* Set credentials */
        httpclient.getState().setCredentials(new AuthScope(serviceURL, this.validateCrmPort(crmPort)),
                new UsernamePasswordCredentials(userId, password));

        /* Check if user has not provided a valid query */
        if (ZDCRMMessageFormatUtils.validateQuery(query)) {
            getMethod.setQueryString(new NameValuePair[] {
                    new NameValuePair("query", ZDCRMMessageFormatUtils.createZDQuery(query)) });
        }

        /* Execute method */
        int result = httpclient.executeMethod(getMethod);

        if (logger.isDebugEnabled()) {
            logger.debug("----Inside searchAllTypeOfObjects response code: " + result + " & body: "
                    + getMethod.getResponseBodyAsString());
        }

        if (result == HttpStatus.SC_OK) {

            /* Create JSON object from response */
            final JSONObject jObj = new JSONObject(getMethod.getResponseBodyAsString());

            /* Create new Scribe object list */
            final List<ScribeObject> cADbjectList = new ArrayList<ScribeObject>();

            /* Get select field list */
            List<String> selectFieldList = null;

            /* Check if user is asking for all fields */
            if (select != null && !select.equalsIgnoreCase(HTTPConstants.allCRMObjectFields)) {

                /* Create select field list */
                selectFieldList = ZDCRMMessageFormatUtils.createSelectFieldList(select);
            }

            /* Get all keys */
            @SuppressWarnings("rawtypes")
            final Iterator itr = jObj.keys();

            /* Iterate over user json object list */
            while (itr.hasNext()) {

                /* Get key name */
                final String key = (String) itr.next();

                /* Get array value */
                if (jObj.get(key).getClass().isAssignableFrom(JSONArray.class)) {

                    /* Get array object */
                    final JSONArray arrObj = (JSONArray) jObj.get(key);

                    /* Run over all JSON objects */
                    for (int i = 0; i < arrObj.length(); i++) {

                        /* Get individual object */
                        final JSONObject subObj = (JSONObject) arrObj.get(i);

                        /* Create list of elements */
                        final List<Element> elementList = new ArrayList<Element>();

                        /* Create new Scribe object */
                        final ScribeObject cADbject = new ScribeObject();

                        /* Get all keys */
                        @SuppressWarnings("rawtypes")
                        final Iterator subItr = subObj.keys();

                        /* Loop over each user in list */
                        while (subItr.hasNext()) {

                            /* Get key name */
                            final String subKey = (String) subItr.next();

                            /* Get value */
                            final Object value = subObj.get(subKey);

                            if (selectFieldList != null) {

                                /* Add only user requested fields */
                                if (selectFieldList.contains(subKey.trim().toUpperCase())) {

                                    /* Add element in list */
                                    elementList
                                            .add(ZDCRMMessageFormatUtils.createMessageElement(subKey, value));
                                }
                            } else {

                                /* Add element in list */
                                elementList.add(ZDCRMMessageFormatUtils.createMessageElement(subKey, value));
                            }

                            /* Set Scribe object type */
                            if (subKey.equalsIgnoreCase("result_type")) {

                                cADbject.setObjectType("" + value);
                            }

                            if (logger.isDebugEnabled()) {
                                logger.debug("----Inside searchAllTypeOfObjects key : " + subKey + " & value: "
                                        + value);
                            }
                        }

                        /* Add all CRM fields */
                        cADbject.setXmlContent(elementList);

                        /* Add Scribe object in list */
                        cADbjectList.add(cADbject);
                    }
                } else {
                    if (logger.isDebugEnabled()) {
                        logger.debug(
                                "----Inside searchAllTypeOfObjects unexpected JSON object type in response : "
                                        + jObj);
                    }
                }
            }

            /* Check if no record found */
            if (cADbjectList.size() == 0) {

                /* Throw user error */
                throw new ScribeException(ScribeResponseCodes._1004 + "No records found at Zendesk");
            }

            /* Set the final object in command object */
            cADCommandObject.setObject(cADbjectList.toArray(new ScribeObject[cADbjectList.size()]));

        } else if (result == HttpStatus.SC_BAD_REQUEST || result == HttpStatus.SC_METHOD_NOT_ALLOWED
                || result == HttpStatus.SC_NOT_ACCEPTABLE || result == HttpStatus.SC_UNPROCESSABLE_ENTITY) {

            /* Throw user error */
            throw new ScribeException(ScribeResponseCodes._1003 + "Invalid request : "
                    + ZDCRMMessageFormatUtils.getErrorFromResponse(getMethod.getResponseBodyAsString()));
        } else if (result == HttpStatus.SC_UNAUTHORIZED) {

            /* Throw user error */
            throw new ScribeException(ScribeResponseCodes._1012 + "Anauthorized by Zendesk CRM");
        } else if (result == HttpStatus.SC_NOT_FOUND) {

            /* Throw user error */
            throw new ScribeException(ScribeResponseCodes._1004 + "Requested record not found at Zendesk CRM");
        } else if (result == HttpStatus.SC_MOVED_TEMPORARILY) {

            /* Throw user error */
            throw new ScribeException(ScribeResponseCodes._1004
                    + "Requested data not found at Zendesk CRM : Seems like Zendesk Service URL/Protocol is not correct");
        }
    } catch (final ScribeException exception) {
        throw exception;
    } catch (final JSONException e) {

        /* Throw user error */
        throw new ScribeException(ScribeResponseCodes._1020 + "Recieved an invalid response from Zendesk CRM",
                e);
    } catch (final ParserConfigurationException exception) {

        /* Throw user error */
        throw new ScribeException(ScribeResponseCodes._1020 + "Recieved an invalid response from Zendesk CRM",
                exception);
    } catch (final SAXException exception) {

        /* Throw user error */
        throw new ScribeException(ScribeResponseCodes._1020 + "Recieved an invalid response from Zendesk CRM",
                exception);
    } catch (final IOException exception) {

        /* Throw user error */
        throw new ScribeException(
                ScribeResponseCodes._1020 + "Communication error while communicating with Zendesk CRM",
                exception);
    } catch (final Exception e) {
        throw new ScribeException(ScribeResponseCodes._1000 + "Problem while communicating with Zendesk CRM",
                e);
    } finally {

        /* Release connection socket */
        if (getMethod != null) {
            getMethod.releaseConnection();
        }
    }
    return cADCommandObject;
}

From source file:it.unibz.instasearch.jobs.CheckUpdatesJob.java

/**
 * @param httpClient//from www.j  a v  a2  s .c  o  m
 * @param versionCheckUrl
 * @throws URISyntaxException 
 */
private void configureProxy(HttpClient httpClient, String versionCheckUrl) throws URISyntaxException {
    IProxyService proxyService = InstaSearchPlugin.getDefault().getProxyService();

    if (proxyService != null && proxyService.isProxiesEnabled()) {
        URI uri = new URI(versionCheckUrl);
        final IProxyData[] proxiesData = proxyService.select(uri);

        IProxyData proxy = null;
        for (IProxyData proxyData : proxiesData) {
            if (proxyData.getType().equals(IProxyData.HTTP_PROXY_TYPE)) {
                proxy = proxyData;
                break;
            }
        }

        if (proxy == null)
            return;

        HostConfiguration config = httpClient.getHostConfiguration();
        config.setProxy(proxy.getHost(), proxy.getPort());

        if (proxy.isRequiresAuthentication()) {
            Credentials credentials = new UsernamePasswordCredentials(proxy.getUserId(), proxy.getPassword());
            AuthScope authScope = new AuthScope(proxy.getHost(), proxy.getPort());
            httpClient.getState().setProxyCredentials(authScope, credentials);
        }
    }
}

From source file:net.sourceforge.eclipsetrader.core.CurrencyConverter.java

private Double downloadQuote(String symbol) {
    Double result = null;/*from  ww w.j a  va 2  s.  co  m*/

    String host = "quote.yahoo.com"; //$NON-NLS-1$
    StringBuffer url = new StringBuffer("http://" + host + "/download/javasoft.beans?symbols="); //$NON-NLS-1$ //$NON-NLS-2$
    url = url.append(symbol + "=X"); //$NON-NLS-1$
    url.append("&format=sl1d1t1c1ohgvbap"); //$NON-NLS-1$

    String line = ""; //$NON-NLS-1$
    try {
        HttpClient client = new HttpClient();
        client.getHttpConnectionManager().getParams().setConnectionTimeout(5000);

        BundleContext context = CorePlugin.getDefault().getBundle().getBundleContext();
        ServiceReference reference = context.getServiceReference(IProxyService.class.getName());
        if (reference != null) {
            IProxyService proxy = (IProxyService) context.getService(reference);
            IProxyData data = proxy.getProxyDataForHost(host, IProxyData.HTTP_PROXY_TYPE);
            if (data != null) {
                if (data.getHost() != null)
                    client.getHostConfiguration().setProxy(data.getHost(), data.getPort());
                if (data.isRequiresAuthentication())
                    client.getState().setProxyCredentials(AuthScope.ANY,
                            new UsernamePasswordCredentials(data.getUserId(), data.getPassword()));
            }
        }

        HttpMethod method = new GetMethod(url.toString());
        method.setFollowRedirects(true);
        client.executeMethod(method);

        BufferedReader in = new BufferedReader(new InputStreamReader(method.getResponseBodyAsStream()));
        while ((line = in.readLine()) != null) {
            String[] item = line.split(","); //$NON-NLS-1$
            if (line.indexOf(";") != -1) //$NON-NLS-1$
                item = line.split(";"); //$NON-NLS-1$

            // 1 = Last price or N/A
            if (item[1].equalsIgnoreCase(Messages.CurrencyConverter_NA) == false)
                result = new Double(numberFormat.parse(item[1]).doubleValue());
            // 2 = Date
            // 3 = Time
            // 4 = Change
            // 5 = Open
            // 6 = Maximum
            // 7 = Minimum
            // 8 = Volume
            // 9 = Bid Price
            // 10 = Ask Price
            // 11 = Close Price

            // 0 = Code
        }
        in.close();
    } catch (Exception e) {
        logger.error(e, e);
    }

    return result;
}

From source file:com.ning.http.client.providers.apache.ApacheAsyncHttpProvider.java

public <T> ListenableFuture<T> execute(Request request, AsyncHandler<T> handler) throws IOException {
    if (isClose.get()) {
        throw new IOException("Closed");
    }/*from  w w w  .  ja  v  a 2s  .  c  o  m*/

    if (ResumableAsyncHandler.class.isAssignableFrom(handler.getClass())) {
        request = ResumableAsyncHandler.class.cast(handler).adjustRequestRange(request);
    }

    if (config.getMaxTotalConnections() > -1 && (maxConnections.get() + 1) > config.getMaxTotalConnections()) {
        throw new IOException(String.format("Too many connections %s", config.getMaxTotalConnections()));
    }

    if (idleConnectionTimeoutThread != null) {
        idleConnectionTimeoutThread.shutdown();
        idleConnectionTimeoutThread = null;
    }

    int requestTimeout = requestTimeout(config, request.getPerRequestConfig());
    if (config.getIdleConnectionTimeoutInMs() > 0 && requestTimeout != -1
            && requestTimeout < config.getIdleConnectionTimeoutInMs()) {
        idleConnectionTimeoutThread = new IdleConnectionTimeoutThread();
        idleConnectionTimeoutThread.setConnectionTimeout(config.getIdleConnectionTimeoutInMs());
        idleConnectionTimeoutThread.addConnectionManager(connectionManager);
        idleConnectionTimeoutThread.start();
    }

    HttpClient httpClient = new HttpClient(params, connectionManager);

    Realm realm = request.getRealm() != null ? request.getRealm() : config.getRealm();
    if (realm != null) {
        httpClient.getParams().setAuthenticationPreemptive(realm.getUsePreemptiveAuth());
        Credentials defaultcreds = new UsernamePasswordCredentials(realm.getPrincipal(), realm.getPassword());
        httpClient.getState().setCredentials(new AuthScope(null, -1, AuthScope.ANY_REALM), defaultcreds);
    }

    HttpMethodBase method = createMethod(httpClient, request);
    ApacheResponseFuture f = new ApacheResponseFuture<T>(handler, requestTimeout, request, method);
    f.touch();

    f.setInnerFuture(
            config.executorService().submit(new ApacheClientRunnable(request, handler, method, f, httpClient)));
    maxConnections.incrementAndGet();
    return f;
}

From source file:net.sourceforge.eclipsetrader.yahoo.ItalianNewsProvider.java

private void update(URL feedUrl, Security security) {
    Calendar limit = Calendar.getInstance();
    limit.add(Calendar.DATE,/*from  w  ww.  j a  v  a  2s  .  c o  m*/
            -CorePlugin.getDefault().getPreferenceStore().getInt(CorePlugin.PREFS_NEWS_DATE_RANGE));

    boolean subscribersOnly = YahooPlugin.getDefault().getPreferenceStore()
            .getBoolean(YahooPlugin.PREFS_SHOW_SUBSCRIBERS_ONLY);
    log.debug(feedUrl);

    try {
        HttpClient client = new HttpClient();
        client.getHttpConnectionManager().getParams().setConnectionTimeout(5000);

        BundleContext context = YahooPlugin.getDefault().getBundle().getBundleContext();
        ServiceReference reference = context.getServiceReference(IProxyService.class.getName());
        if (reference != null) {
            IProxyService proxy = (IProxyService) context.getService(reference);
            IProxyData data = proxy.getProxyDataForHost(feedUrl.getHost(), IProxyData.HTTP_PROXY_TYPE);
            if (data != null) {
                if (data.getHost() != null)
                    client.getHostConfiguration().setProxy(data.getHost(), data.getPort());
                if (data.isRequiresAuthentication())
                    client.getState().setProxyCredentials(AuthScope.ANY,
                            new UsernamePasswordCredentials(data.getUserId(), data.getPassword()));
            }
        }

        SyndFeed feed = fetcher.retrieveFeed(feedUrl, client);
        for (Iterator iter = feed.getEntries().iterator(); iter.hasNext();) {
            SyndEntry entry = (SyndEntry) iter.next();

            if (!subscribersOnly && entry.getTitle().indexOf("[$$]") != -1) //$NON-NLS-1$
                continue;

            NewsItem news = new NewsItem();
            news.setRecent(true);

            String title = entry.getTitle();
            if (title.endsWith(")")) //$NON-NLS-1$
            {
                int s = title.lastIndexOf('(');
                if (s != -1) {
                    news.setSource(title.substring(s + 1, title.length() - 1));
                    title = title.substring(0, s - 1).trim();
                }
            }
            news.setTitle(decode(title));

            news.setUrl(entry.getLink());

            Date entryDate = entry.getPublishedDate();
            if (entry.getUpdatedDate() != null)
                entryDate = entry.getUpdatedDate();
            if (entryDate != null) {
                Calendar date = Calendar.getInstance();
                date.setTime(entryDate);
                date.set(Calendar.SECOND, 0);
                date.set(Calendar.MILLISECOND, 0);
                news.setDate(date.getTime());
            }

            if (security != null)
                news.addSecurity(security);

            if (!news.getDate().before(limit.getTime()) && !isDuplicated(news)) {
                log.trace(news.getTitle() + " (" + news.getSource() + ")"); //$NON-NLS-1$ //$NON-NLS-2$
                CorePlugin.getRepository().save(news);
                oldItems.add(news);
            }
        }
    } catch (Exception e) {
        CorePlugin.logException(e);
    }
}

From source file:de.innovationgate.webgate.api.rss2.SimpleRSS.java

private InputStream retrievePage(String url, NativeQueryOptions nativeOptions) throws WGQueryException {

    try {//ww  w . j  a  va 2  s. c om

        // Retrieve from web
        HttpClient client = WGFactory.getHttpClientFactory().createHttpClient();
        client.setConnectionTimeout(10000);
        if (_useProxy) {
            client.getHostConfiguration().setProxy(_proxyHost, _proxyPort);
            if (_proxyCredentials != null) {
                Credentials credentials;
                if (_proxyDomain != null) {
                    List elements = WGUtils.deserializeCollection(_proxyCredentials, ":");
                    credentials = new NTCredentials((String) elements.get(0), (String) elements.get(1),
                            _proxyHost, _proxyDomain);
                } else {
                    credentials = new UsernamePasswordCredentials(_proxyCredentials);

                }
                client.getState().setProxyCredentials(null, _proxyHost, credentials);

            }
        }
        HttpMethod method = new GetMethod(url);
        method.setFollowRedirects(true);
        method.setStrictMode(false);

        if (nativeOptions.containsKey(QUERYOPTION_USER) && nativeOptions.containsKey(QUERYOPTION_PWD)) {
            method.setDoAuthentication(true);
            client.getParams().setAuthenticationPreemptive(true);
            client.getState().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(
                    nativeOptions.get(QUERYOPTION_USER), nativeOptions.get(QUERYOPTION_PWD)));
        }

        client.executeMethod(method);

        // Read response. Wrap content decoder if necessary.
        InputStream inStream = method.getResponseBodyAsStream();

        // Return InputStream from given URL
        return inStream;
    } catch (MalformedURLException e) {
        throw new WGQueryException("Malformed feed URL", url, e);
    } catch (IOException e) {
        throw new WGQueryException("IO Exception retrieving feed", url, e);
    }
}

From source file:greenfoot.export.mygame.MyGameClient.java

/**
 * Get a http client, configured to use the proxy if specified in Greenfoot config
 *//*from   www . j  a  va  2s .com*/
protected HttpClient getHttpClient() {
    HttpClient httpClient = new HttpClient();

    String proxyHost = Config.getPropString("proxy.host", null);
    String proxyPortStr = Config.getPropString("proxy.port", null);
    if (proxyHost != null && proxyHost.length() != 0 && proxyPortStr != null) {
        HostConfiguration hostConfig = httpClient.getHostConfiguration();

        int proxyPort = 80;
        try {
            proxyPort = Integer.parseInt(proxyPortStr);
        } catch (NumberFormatException nfe) {
        }

        hostConfig.setProxy(proxyHost, proxyPort);
        String proxyUser = Config.getPropString("proxy.user", null);
        String proxyPass = Config.getPropString("proxy.password", null);
        if (proxyUser != null) {
            AuthScope authScope = new AuthScope(proxyHost, proxyPort);
            Credentials proxyCreds = new UsernamePasswordCredentials(proxyUser, proxyPass);
            httpClient.getState().setProxyCredentials(authScope, proxyCreds);
        }
    }

    return httpClient;
}

From source file:com.cyberway.issue.crawler.datamodel.credential.Rfc2617Credential.java

public boolean populate(CrawlURI curi, HttpClient http, HttpMethod method, String payload) {
    boolean result = false;
    String authRealm = payload;//from  w  w  w  .j av a  2  s. c  om
    if (authRealm == null) {
        logger.severe("No authscheme though creds: " + curi);
        return result;
    }

    // Always add the credential to HttpState. Doing this because no way of
    // removing the credential once added AND there is a bug in the
    // credentials management system in that it always sets URI root to
    // null: it means the key used to find a credential is NOT realm + root
    // URI but just the realm. Unless I set it everytime, there is
    // possibility that as this thread progresses, it might come across a
    // realm already loaded but the login and password are from another
    // server. We'll get a failed authentication that'd be difficult to
    // explain.
    //
    // Have to make a UsernamePasswordCredentials. The httpclient auth code
    // does an instanceof down in its guts.
    UsernamePasswordCredentials upc = null;
    try {
        upc = new UsernamePasswordCredentials(getLogin(curi), getPassword(curi));
        http.getState().setCredentials(
                new AuthScope(curi.getUURI().getHost(), curi.getUURI().getPort(), authRealm), upc);
        logger.fine("Credentials for realm " + authRealm + " for CrawlURI " + curi.toString()
                + " added to request: " + result);
        result = true;
    } catch (AttributeNotFoundException e1) {
        logger.severe("Failed to get login and password for " + curi + " and " + authRealm);
    } catch (URIException e) {
        logger.severe("Failed to parse host from " + curi + ": " + e.getMessage());
    }

    return result;
}

From source file:com.liferay.portal.plugin.PluginPackageUtil.java

private RemotePluginPackageRepository _loadRepository(String repositoryURL)
        throws PluginPackageException, PortalException {

    RemotePluginPackageRepository repository = null;

    StringBundler sb = new StringBundler(8);

    if (!repositoryURL.startsWith(Http.HTTP_WITH_SLASH) && !repositoryURL.startsWith(Http.HTTPS_WITH_SLASH)) {

        sb.append(Http.HTTP_WITH_SLASH);
    }//from ww  w.ja v  a  2 s . c o m

    sb.append(repositoryURL);
    sb.append(StringPool.SLASH);
    sb.append(REPOSITORY_XML_FILENAME_PREFIX);
    sb.append(StringPool.DASH);
    sb.append(ReleaseInfo.getVersion());
    sb.append(StringPool.PERIOD);
    sb.append(REPOSITORY_XML_FILENAME_EXTENSION);

    String pluginsXmlURL = sb.toString();

    try {
        HttpImpl httpImpl = (HttpImpl) HttpUtil.getHttp();

        HostConfiguration hostConfiguration = httpImpl.getHostConfiguration(pluginsXmlURL);

        HttpClient httpClient = httpImpl.getClient(hostConfiguration);

        httpImpl.proxifyState(httpClient.getState(), hostConfiguration);

        GetMethod getFileMethod = new GetMethod(pluginsXmlURL);

        byte[] bytes = null;

        try {
            int responseCode = httpClient.executeMethod(hostConfiguration, getFileMethod);

            if (responseCode != HttpServletResponse.SC_OK) {
                if (_log.isDebugEnabled()) {
                    _log.debug("A repository for version " + ReleaseInfo.getVersion() + " was not found. "
                            + "Checking general repository");
                }

                sb.setIndex(0);

                sb.append(repositoryURL);
                sb.append(StringPool.SLASH);
                sb.append(REPOSITORY_XML_FILENAME_PREFIX);
                sb.append(StringPool.PERIOD);
                sb.append(REPOSITORY_XML_FILENAME_EXTENSION);

                pluginsXmlURL = sb.toString();

                getFileMethod.releaseConnection();

                getFileMethod = new GetMethod(pluginsXmlURL);

                responseCode = httpClient.executeMethod(hostConfiguration, getFileMethod);

                if (responseCode != HttpServletResponse.SC_OK) {
                    throw new PluginPackageException("Unable to download file " + pluginsXmlURL
                            + " because of response code " + responseCode);
                }
            }

            bytes = getFileMethod.getResponseBody();
        } finally {
            getFileMethod.releaseConnection();
        }

        if ((bytes != null) && (bytes.length > 0)) {
            repository = _parseRepositoryXml(new String(bytes), repositoryURL);

            _repositoryCache.put(repositoryURL, repository);
            _availableTagsCache.addAll(repository.getTags());
            _lastUpdateDate = new Date();
            _updateAvailable = null;

            return repository;
        } else {
            _lastUpdateDate = new Date();

            throw new PluginPackageException("Download returned 0 bytes");
        }
    } catch (MalformedURLException mue) {
        _repositoryCache.remove(repositoryURL);

        throw new PluginPackageException("Invalid URL " + pluginsXmlURL, mue);
    } catch (IOException ioe) {
        _repositoryCache.remove(repositoryURL);

        throw new PluginPackageException("Unable to communicate with repository " + repositoryURL, ioe);
    } catch (DocumentException de) {
        _repositoryCache.remove(repositoryURL);

        throw new PluginPackageException("Unable to parse plugin list for repository " + repositoryURL, de);
    }
}

From source file:com.xpn.xwiki.plugin.feed.XWikiFeedFetcher.java

/**
 * @see com.sun.syndication.fetcher.FeedFetcher#retrieveFeed(java.net.URL)
 *//* w w  w  . ja v  a2s . c  o  m*/
public SyndFeed retrieveFeed(URL feedUrl, int timeout)
        throws IllegalArgumentException, IOException, FeedException, FetcherException {
    if (feedUrl == null) {
        throw new IllegalArgumentException("null is not a valid URL");
    }
    HttpClient client = new HttpClient();
    if (timeout != 0) {
        client.getParams().setSoTimeout(timeout);
        client.getParams().setParameter("http.connection.timeout", new Integer(timeout));
    }

    System.setProperty("http.useragent", getUserAgent());
    client.getParams().setParameter("httpclient.useragent", getUserAgent());

    String proxyHost = System.getProperty("http.proxyHost");
    String proxyPort = System.getProperty("http.proxyPort");
    if ((proxyHost != null) && (!proxyHost.equals(""))) {
        int port = 3128;
        if ((proxyPort != null) && (!proxyPort.equals(""))) {
            port = Integer.parseInt(proxyPort);
        }
        client.getHostConfiguration().setProxy(proxyHost, port);
    }

    String proxyUser = System.getProperty("http.proxyUser");
    if ((proxyUser != null) && (!proxyUser.equals(""))) {
        String proxyPassword = System.getProperty("http.proxyPassword");
        Credentials defaultcreds = new UsernamePasswordCredentials(proxyUser, proxyPassword);
        client.getState().setProxyCredentials(AuthScope.ANY, defaultcreds);
    }

    String urlStr = feedUrl.toString();
    FeedFetcherCache cache = getFeedInfoCache();
    if (cache != null) {
        // retrieve feed
        HttpMethod method = new GetMethod(urlStr);
        method.addRequestHeader("Accept-Encoding", "gzip");
        try {
            if (isUsingDeltaEncoding()) {
                method.setRequestHeader("A-IM", "feed");
            }

            // get the feed info from the cache
            // Note that syndFeedInfo will be null if it is not in the cache
            SyndFeedInfo syndFeedInfo = cache.getFeedInfo(feedUrl);
            if (syndFeedInfo != null) {
                method.setRequestHeader("If-None-Match", syndFeedInfo.getETag());

                if (syndFeedInfo.getLastModified() instanceof String) {
                    method.setRequestHeader("If-Modified-Since", (String) syndFeedInfo.getLastModified());
                }
            }

            method.setFollowRedirects(true);

            int statusCode = client.executeMethod(method);
            fireEvent(FetcherEvent.EVENT_TYPE_FEED_POLLED, urlStr);
            handleErrorCodes(statusCode);

            SyndFeed feed = getFeed(syndFeedInfo, urlStr, method, statusCode);

            syndFeedInfo = buildSyndFeedInfo(feedUrl, urlStr, method, feed, statusCode);

            cache.setFeedInfo(new URL(urlStr), syndFeedInfo);

            // the feed may have been modified to pick up cached values
            // (eg - for delta encoding)
            feed = syndFeedInfo.getSyndFeed();

            return feed;
        } finally {
            method.releaseConnection();
        }
    } else {
        // cache is not in use
        HttpMethod method = new GetMethod(urlStr);
        try {
            method.setFollowRedirects(true);

            int statusCode = client.executeMethod(method);
            fireEvent(FetcherEvent.EVENT_TYPE_FEED_POLLED, urlStr);
            handleErrorCodes(statusCode);

            return getFeed(null, urlStr, method, statusCode);
        } finally {
            method.releaseConnection();
        }
    }
}