Search in sources :

Example 26 with GetMethod

use of org.apache.commons.httpclient.methods.GetMethod in project Openfire by igniterealtime.

the class CrowdManager method getGroup.

/**
	 * Get the description of a group from crowd
	 * @param groupName
	 * @return a Group object
	 * @throws RemoteException
	 */
public Group getGroup(String groupName) throws RemoteException {
    if (LOG.isDebugEnabled())
        LOG.debug("Get group:" + groupName + " from crowd");
    GetMethod get = createGetMethodXmlResponse(crowdServer.resolve("group?groupname=" + urlEncode(groupName)));
    Group group = null;
    try {
        int httpCode = client.executeMethod(get);
        if (httpCode != 200) {
            handleHTTPError(get);
        }
        group = JAXB.unmarshal(get.getResponseBodyAsStream(), Group.class);
    } catch (IOException ioe) {
        handleError(ioe);
    } finally {
        get.releaseConnection();
    }
    return group;
}
Also used : Group(org.jivesoftware.openfire.crowd.jaxb.Group) GetMethod(org.apache.commons.httpclient.methods.GetMethod) IOException(java.io.IOException)

Example 27 with GetMethod

use of org.apache.commons.httpclient.methods.GetMethod in project Openfire by igniterealtime.

the class FaviconServlet method getImage.

private byte[] getImage(String url) {
    try {
        // Try to get the fiveicon from the url using an HTTP connection from the pool
        // that also allows to configure timeout values (e.g. connect and get data)
        GetMethod get = new GetMethod(url);
        get.setFollowRedirects(true);
        int response = client.executeMethod(get);
        if (response < 400) {
            // Check that the response was successful. Should we also filter 30* code?
            return get.getResponseBody();
        } else {
            // Remote server returned an error so return null
            return null;
        }
    } catch (IllegalStateException e) {
        // Something failed (probably a method not supported) so try the old stye now
        try {
            URLConnection urlConnection = new URL(url).openConnection();
            urlConnection.setReadTimeout(1000);
            urlConnection.connect();
            try (DataInputStream di = new DataInputStream(urlConnection.getInputStream())) {
                ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
                DataOutputStream out = new DataOutputStream(byteStream);
                int len;
                byte[] b = new byte[1024];
                while ((len = di.read(b)) != -1) {
                    out.write(b, 0, len);
                }
                out.flush();
                return byteStream.toByteArray();
            }
        } catch (IOException ioe) {
            // We failed again so return null
            return null;
        }
    } catch (IOException ioe) {
        // We failed so return null
        return null;
    }
}
Also used : GetMethod(org.apache.commons.httpclient.methods.GetMethod) URL(java.net.URL) URLConnection(java.net.URLConnection)

Example 28 with GetMethod

use of org.apache.commons.httpclient.methods.GetMethod in project Openfire by igniterealtime.

the class HttpClientWithTimeoutFeedFetcher method retrieveFeed.

/**
	 * @see com.sun.syndication.fetcher.FeedFetcher#retrieveFeed(java.net.URL)
	 */
@Override
public SyndFeed retrieveFeed(URL feedUrl) throws IllegalArgumentException, IOException, FeedException, FetcherException {
    if (feedUrl == null) {
        throw new IllegalArgumentException("null is not a valid URL");
    }
    // TODO Fix this
    //System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.SimpleLog");
    HttpClient client = new HttpClient();
    HttpConnectionManager conManager = client.getHttpConnectionManager();
    conManager.getParams().setParameter("http.connection.timeout", 3000);
    conManager.getParams().setParameter("http.socket.timeout", 3000);
    if (getCredentialSupplier() != null) {
        client.getState().setAuthenticationPreemptive(true);
        // TODO what should realm be here?
        Credentials credentials = getCredentialSupplier().getCredentials(null, feedUrl.getHost());
        if (credentials != null) {
            client.getState().setCredentials(null, feedUrl.getHost(), credentials);
        }
    }
    System.setProperty("httpclient.useragent", "Openfire Admin Console: v" + XMPPServer.getInstance().getServerInfo().getVersion().getVersionString());
    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();
        }
    }
}
Also used : SyndFeed(com.sun.syndication.feed.synd.SyndFeed) GetMethod(org.apache.commons.httpclient.methods.GetMethod) SyndFeedInfo(com.sun.syndication.fetcher.impl.SyndFeedInfo) FeedFetcherCache(com.sun.syndication.fetcher.impl.FeedFetcherCache) URL(java.net.URL)

Example 29 with GetMethod

use of org.apache.commons.httpclient.methods.GetMethod in project Openfire by igniterealtime.

the class UpdateManager method downloadPlugin.

/**
     * Download and install latest version of plugin.
     *
     * @param url the URL of the latest version of the plugin.
     * @return true if the plugin was successfully downloaded and installed.
     */
public boolean downloadPlugin(String url) {
    boolean installed = false;
    // Download and install new version of plugin
    HttpClient httpClient = new HttpClient();
    // Check if a proxy should be used
    if (isUsingProxy()) {
        HostConfiguration hc = new HostConfiguration();
        hc.setProxy(getProxyHost(), getProxyPort());
        httpClient.setHostConfiguration(hc);
    }
    GetMethod getMethod = new GetMethod(url);
    //execute the method
    try {
        int statusCode = httpClient.executeMethod(getMethod);
        if (statusCode == 200) {
            //get the resonse as an InputStream
            try (InputStream in = getMethod.getResponseBodyAsStream()) {
                String pluginFilename = url.substring(url.lastIndexOf("/") + 1);
                installed = XMPPServer.getInstance().getPluginManager().installPlugin(in, pluginFilename);
            }
            if (installed) {
                // Remove the plugin from the list of plugins to update
                for (Update update : pluginUpdates) {
                    if (update.getURL().equals(url)) {
                        update.setDownloaded(true);
                    }
                }
                // Save response in a file for later retrieval
                saveLatestServerInfo();
            }
        }
    } catch (IOException e) {
        Log.warn("Error downloading new plugin version", e);
    }
    return installed;
}
Also used : HostConfiguration(org.apache.commons.httpclient.HostConfiguration) InputStream(java.io.InputStream) HttpClient(org.apache.commons.httpclient.HttpClient) GetMethod(org.apache.commons.httpclient.methods.GetMethod) IOException(java.io.IOException)

Example 30 with GetMethod

use of org.apache.commons.httpclient.methods.GetMethod in project pinot by linkedin.

the class ChangeTableState method execute.

@Override
public boolean execute() throws Exception {
    if (_controllerHost == null) {
        _controllerHost = NetUtil.getHostAddress();
    }
    String stateValue = _state.toLowerCase();
    if (!stateValue.equals("enable") && !stateValue.equals("disable") && !stateValue.equals("drop")) {
        throw new IllegalArgumentException("Invalid value for state: " + _state + "\n Value must be one of enable|disable|drop");
    }
    HttpClient httpClient = new HttpClient();
    HttpURL url = new HttpURL(_controllerHost, Integer.parseInt(_controllerPort), URI_TABLES_PATH + _tableName);
    url.setQuery("state", stateValue);
    GetMethod httpGet = new GetMethod(url.getEscapedURI());
    int status = httpClient.executeMethod(httpGet);
    if (status != 200) {
        throw new RuntimeException("Failed to change table state, error: " + httpGet.getResponseBodyAsString());
    }
    return true;
}
Also used : HttpClient(org.apache.commons.httpclient.HttpClient) GetMethod(org.apache.commons.httpclient.methods.GetMethod) HttpURL(org.apache.commons.httpclient.HttpURL)

Aggregations

GetMethod (org.apache.commons.httpclient.methods.GetMethod)357 HttpClient (org.apache.commons.httpclient.HttpClient)216 HttpMethod (org.apache.commons.httpclient.HttpMethod)93 IOException (java.io.IOException)82 Test (org.junit.Test)70 InputStream (java.io.InputStream)65 HttpException (org.apache.commons.httpclient.HttpException)41 Map (java.util.Map)39 ArrayList (java.util.ArrayList)37 HashMap (java.util.HashMap)36 JSONParser (org.json.simple.parser.JSONParser)34 JSONObject (org.json.simple.JSONObject)31 Header (org.apache.commons.httpclient.Header)22 Element (org.w3c.dom.Element)22 PostMethod (org.apache.commons.httpclient.methods.PostMethod)21 TypeToken (com.google.gson.reflect.TypeToken)20 List (java.util.List)19 HttpState (org.apache.commons.httpclient.HttpState)18 URI (java.net.URI)17 JSONArray (org.json.simple.JSONArray)17