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

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

Introduction

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

Prototype

public HttpConnectionManager getHttpConnectionManager()

Source Link

Usage

From source file:org.eclipsetrader.yahoo.internal.core.connector.SnapshotConnector.java

@Override
public void run() {
    try {/*  w  ww  .  j a va  2  s  .  c  o m*/
        HttpClient client = new HttpClient();
        client.getHttpConnectionManager().getParams().setConnectionTimeout(5000);
        Util.setupProxy(client, Util.snapshotFeedHost);

        synchronized (thread) {
            while (!isStopping()) {
                synchronized (symbolSubscriptions) {
                    if (symbolSubscriptions.size() != 0) {
                        String[] symbols = symbolSubscriptions.keySet()
                                .toArray(new String[symbolSubscriptions.size()]);
                        fetchLatestSnapshot(client, symbols, false);
                        setSubscriptionsChanged(false);
                    }
                }

                try {
                    thread.wait(5000);
                } catch (InterruptedException e) {
                    // Ignore exception, not important at this time
                }
            }
        }
    } catch (Exception e) {
        Status status = new Status(IStatus.ERROR, YahooActivator.PLUGIN_ID, 0, "Error reading data", e);
        YahooActivator.log(status);
    }
}

From source file:org.eclipsetrader.yahoo.internal.core.connector.StreamingConnector.java

@Override
public void run() {
    BufferedReader in = null;//  w  ww  . j  a v  a2s  .c o m
    char[] buffer = new char[512];

    try {
        HttpClient client = new HttpClient(new MultiThreadedHttpConnectionManager());
        client.getHttpConnectionManager().getParams().setConnectionTimeout(5000);
        Util.setupProxy(client, Util.streamingFeedHost);

        HttpMethod method = null;

        while (!isStopping()) {
            // Check if the connection was not yet initialized or there are changed in the subscriptions.
            if (in == null || isSubscriptionsChanged()) {
                try {
                    if (method != null) {
                        method.releaseConnection();
                    }
                    if (in != null) {
                        in.close();
                    }
                } catch (Exception e) {
                    // We can't do anything at this time, ignore
                }

                String[] symbols;
                synchronized (symbolSubscriptions) {
                    Set<String> s = new HashSet<String>(symbolSubscriptions.keySet());
                    s.add("MSFT");
                    symbols = s.toArray(new String[s.size()]);
                    setSubscriptionsChanged(false);
                    if (symbols.length == 0) {
                        break;
                    }
                }
                method = Util.getStreamingFeedMethod(symbols);

                client.executeMethod(method);

                in = new BufferedReader(new InputStreamReader(method.getResponseBodyAsStream()));

                line = new StringBuilder();
                script = new StringBuilder();
                inTag = false;
                inScript = false;

                fetchLatestSnapshot(client, symbols, false);
            }

            if (in.ready()) {
                int length = in.read(buffer);
                if (length == -1) {
                    in.close();
                    in = null;
                    continue;
                }
                processIncomingChars(buffer, length);
            } else {
                // Check stale data
                List<String> updateList = new ArrayList<String>();
                synchronized (symbolSubscriptions) {
                    long currentTime = System.currentTimeMillis();
                    for (FeedSubscription subscription : symbolSubscriptions.values()) {
                        long elapsedTime = currentTime - subscription.getIdentifierType().getLastUpdate();
                        if (elapsedTime >= 60000) {
                            updateList.add(subscription.getIdentifierType().getSymbol());
                            subscription.getIdentifierType().setLastUpdate(currentTime / 60000 * 60000);
                        }
                    }
                }
                if (updateList.size() != 0) {
                    fetchLatestSnapshot(client, updateList.toArray(new String[updateList.size()]), true);
                }
            }

            Thread.sleep(100);
        }
    } catch (Exception e) {
        Status status = new Status(IStatus.ERROR, YahooActivator.PLUGIN_ID, 0, "Error reading data", e);
        YahooActivator.log(status);
    } finally {
        try {
            if (in != null) {
                in.close();
            }
        } catch (Exception e) {
            // We can't do anything at this time, ignore
        }
    }
}

From source file:org.eclipsetrader.yahoo.internal.news.NewsProvider.java

protected IStatus jobRunner(IProgressMonitor monitor) {
    IPreferenceStore store = YahooActivator.getDefault().getPreferenceStore();

    Date limitDate = getLimitDate();

    Calendar today = Calendar.getInstance();
    int hoursAsRecent = YahooActivator.getDefault().getPreferenceStore()
            .getInt(YahooActivator.PREFS_HOURS_AS_RECENT);
    today.add(Calendar.HOUR_OF_DAY, -hoursAsRecent);
    Date recentLimitDate = today.getTime();

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

    try {//  w  ww .  j  a  v a2 s. co m
        JAXBContext jaxbContext = JAXBContext.newInstance(Category[].class);
        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();

        InputStream inputStream = FileLocator.openStream(YahooActivator.getDefault().getBundle(),
                new Path("data/news_feeds.xml"), false);
        JAXBElement<Category[]> element = unmarshaller.unmarshal(new StreamSource(inputStream),
                Category[].class);

        int total = 0;
        for (Category category : element.getValue()) {
            for (Page page : category.getPages()) {
                if (store.getBoolean(YahooActivator.PREFS_SUBSCRIBE_PREFIX + page.getId())) {
                    total++;
                }
            }
        }

        ISecurity[] securities = getRepositoryService().getSecurities();
        total += securities.length;

        monitor.beginTask("Fetching Yahoo! News", total);

        resetRecentFlag();

        final Set<HeadLine> added = new HashSet<HeadLine>();
        final Set<HeadLine> updated = new HashSet<HeadLine>();

        Category[] category = element.getValue();
        for (int i = 0; i < category.length && !monitor.isCanceled(); i++) {
            INewsHandler handler = new RSSNewsHandler();
            if (category[i].getHandler() != null) {
                try {
                    handler = (INewsHandler) Class.forName(category[i].getHandler()).newInstance();
                } catch (Exception e) {
                    Status status = new Status(IStatus.ERROR, YahooActivator.PLUGIN_ID, 0,
                            "Invalid news handler class", e);
                    YahooActivator.log(status);
                }
            }

            List<URL> l = new ArrayList<URL>();

            Page[] page = category[i].getPages();
            for (int ii = 0; ii < page.length && !monitor.isCanceled(); ii++) {
                if (store.getBoolean(YahooActivator.PREFS_SUBSCRIBE_PREFIX + page[ii].getId())) {
                    l.add(new URL(page[ii].getUrl()));
                }
            }

            HeadLine[] list = handler.parseNewsPages(l.toArray(new URL[l.size()]), monitor);
            for (HeadLine headLine : list) {
                if (headLine.getDate() == null || headLine.getDate().before(limitDate)) {
                    continue;
                }
                if (!oldItems.contains(headLine)) {
                    if (!headLine.getDate().before(recentLimitDate)) {
                        headLine.setRecent(true);
                    }
                    oldItems.add(headLine);
                    added.add(headLine);
                }
            }
        }

        if (store.getBoolean(YahooActivator.PREFS_UPDATE_SECURITIES_NEWS)) {
            for (int i = 0; i < securities.length && !monitor.isCanceled(); i++) {
                URL feedUrl = Util.getRSSNewsFeedForSecurity(securities[i]);
                if (feedUrl == null) {
                    continue;
                }

                monitor.subTask(feedUrl.toString());

                try {
                    if (YahooActivator.getDefault() != null) {
                        BundleContext context = YahooActivator.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()));
                                }
                            }
                            context.ungetService(reference);
                        }
                    }

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

                        String link = entry.getLink();
                        if (link == null && entry.getLinks().size() != 0) {
                            link = (String) entry.getLinks().get(0);
                        }
                        if (link != null) {
                            while (link.indexOf('*') != -1) {
                                link = link.substring(link.indexOf('*') + 1);
                            }
                            link = URLDecoder.decode(link, "UTF-8");
                        }
                        if (link == null) {
                            continue;
                        }

                        String source = null;

                        String title = entry.getTitle();
                        if (title.startsWith("[$$]")) {
                            title = title.substring(4, title.length());
                        }

                        if (title.endsWith(")")) {
                            int s = title.lastIndexOf('(');
                            if (s != -1) {
                                source = title.substring(s + 1, title.length() - 1);
                                if (source.startsWith("at ")) {
                                    source = source.substring(3);
                                }
                                title = title.substring(0, s - 1).trim();
                            }
                        }

                        HeadLine headLine = new HeadLine(entry.getPublishedDate(), source, title,
                                new ISecurity[] { securities[i] }, link);
                        int index = oldItems.indexOf(headLine);
                        if (index != -1) {
                            if (headLine.getDate().before(limitDate)) {
                                continue;
                            }
                            headLine = oldItems.get(index);
                            if (!headLine.contains(securities[i])) {
                                headLine.addMember(securities[i]);
                                updated.add(headLine);
                            }
                        } else {
                            if (!headLine.getDate().before(recentLimitDate)) {
                                headLine.setRecent(true);
                            }
                            oldItems.add(headLine);
                            added.add(headLine);
                        }
                    }
                } catch (Exception e) {
                    String msg = "Error fetching news from " + feedUrl.toString();
                    Status status = new Status(IStatus.ERROR, YahooActivator.PLUGIN_ID, 0, msg, e);
                    YahooActivator.log(status);
                }

                monitor.worked(1);
            }
        }

        if (added.size() != 0 || updated.size() != 0) {
            final INewsService service = getNewsService();
            service.runInService(new INewsServiceRunnable() {

                @Override
                public IStatus run(IProgressMonitor monitor) throws Exception {
                    service.addHeadLines(added.toArray(new IHeadLine[added.size()]));
                    service.updateHeadLines(updated.toArray(new IHeadLine[updated.size()]));
                    return Status.OK_STATUS;
                }
            }, null);
        }

        save();
    } catch (Exception e) {
        Status status = new Status(IStatus.ERROR, YahooActivator.PLUGIN_ID, 0, "Error fetching news", e);
        YahooActivator.log(status);
    } finally {
        monitor.done();
    }

    return Status.OK_STATUS;
}

From source file:org.eclipsetrader.yahoo.internal.news.RSSNewsHandler.java

@Override
public HeadLine[] parseNewsPages(URL[] url, IProgressMonitor monitor) {
    List<HeadLine> list = new ArrayList<HeadLine>();

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

    for (int i = 0; i < url.length && !monitor.isCanceled(); i++) {
        monitor.subTask(url[i].toString());

        try {//from  w  ww . j a v  a  2 s  .c  om
            Util.setupProxy(client, url[i].getHost());

            SyndFeed feed = fetcher.retrieveFeed(url[i], client);
            for (Iterator<?> iter = feed.getEntries().iterator(); iter.hasNext();) {
                SyndEntry entry = (SyndEntry) iter.next();

                String link = entry.getLink();
                if (link.lastIndexOf('*') != -1) {
                    link = link.substring(link.lastIndexOf('*') + 1);
                }
                link = URLDecoder.decode(link, "UTF-8");

                String source = null;

                String title = entry.getTitle();
                if (title.startsWith("[$$]")) {
                    title = title.substring(4, title.length());
                }

                if (title.endsWith(")")) {
                    int s = title.lastIndexOf('(');
                    if (s != -1) {
                        source = title.substring(s + 1, title.length() - 1);
                        if (source.startsWith("at ")) {
                            source = source.substring(3);
                        }
                        title = title.substring(0, s - 1).trim();
                    }
                }

                list.add(new HeadLine(entry.getPublishedDate(), source, title, null, link));
            }
        } catch (IllegalArgumentException e) {
            // Do nothing, could be an invalid URL
        } catch (Exception e) {
            e.printStackTrace();
        }

        monitor.worked(1);
    }

    return list.toArray(new HeadLine[list.size()]);
}

From source file:org.eclipsetrader.yahoojapan.internal.core.connector.SnapshotConnector.java

@Override
public void run() {
    try {/*  w  ww .  j a  va 2 s .  c om*/
        HttpClient client = new HttpClient();
        client.getHttpConnectionManager().getParams().setConnectionTimeout(5000);
        Util.setupProxy(client, Util.snapshotFeedHost);

        synchronized (thread) {
            while (!isStopping()) {
                synchronized (symbolSubscriptions) {
                    if (symbolSubscriptions.size() != 0) {
                        String[] symbols = symbolSubscriptions.keySet()
                                .toArray(new String[symbolSubscriptions.size()]);
                        if (symbolSubscriptions.size() == 1) {
                            fetchLatestSnapshot1(client, symbols, false);
                        } else {
                            fetchLatestSnapshot(client, symbols, false);
                        }
                        setSubscriptionsChanged(false);
                    }
                }

                try {
                    thread.wait(5000);
                } catch (InterruptedException e) {
                    // Ignore exception, not important at this time
                }
            }
        }
    } catch (Exception e) {
        Status status = new Status(IStatus.ERROR, YahooJapanActivator.PLUGIN_ID, 0, "Error reading data", e);
        YahooJapanActivator.log(status);
    }
}

From source file:org.eclipsetrader.yahoojapan.internal.news.NewsProvider.java

protected IStatus jobRunner(IProgressMonitor monitor) {
    IPreferenceStore store = YahooJapanActivator.getDefault().getPreferenceStore();

    Date limitDate = getLimitDate();

    Calendar today = Calendar.getInstance();
    int hoursAsRecent = YahooJapanActivator.getDefault().getPreferenceStore()
            .getInt(YahooJapanActivator.PREFS_HOURS_AS_RECENT);
    today.add(Calendar.HOUR_OF_DAY, -hoursAsRecent);
    Date recentLimitDate = today.getTime();

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

    try {/*  ww w  .j  a  va2  s . c  o m*/
        JAXBContext jaxbContext = JAXBContext.newInstance(Category[].class);
        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();

        InputStream inputStream = FileLocator.openStream(YahooJapanActivator.getDefault().getBundle(),
                new Path("data/news_feeds.xml"), false);
        JAXBElement<Category[]> element = unmarshaller.unmarshal(new StreamSource(inputStream),
                Category[].class);

        int total = 0;
        for (Category category : element.getValue()) {
            for (Page page : category.getPages()) {
                if (store.getBoolean(YahooJapanActivator.PREFS_SUBSCRIBE_PREFIX + page.getId())) {
                    total++;
                }
            }
        }

        ISecurity[] securities = getRepositoryService().getSecurities();
        total += securities.length;

        monitor.beginTask("Fetching Yahoo! News", total);

        resetRecentFlag();

        final Set<HeadLine> added = new HashSet<HeadLine>();
        final Set<HeadLine> updated = new HashSet<HeadLine>();

        Category[] category = element.getValue();
        for (int i = 0; i < category.length && !monitor.isCanceled(); i++) {
            INewsHandler handler = new RSSNewsHandler();
            if (category[i].getHandler() != null) {
                try {
                    handler = (INewsHandler) Class.forName(category[i].getHandler()).newInstance();
                } catch (Exception e) {
                    Status status = new Status(IStatus.ERROR, YahooJapanActivator.PLUGIN_ID, 0,
                            "Invalid news handler class", e);
                    YahooJapanActivator.log(status);
                }
            }

            List<URL> l = new ArrayList<URL>();

            Page[] page = category[i].getPages();
            for (int ii = 0; ii < page.length && !monitor.isCanceled(); ii++) {
                if (store.getBoolean(YahooJapanActivator.PREFS_SUBSCRIBE_PREFIX + page[ii].getId())) {
                    l.add(new URL(page[ii].getUrl()));
                }
            }

            HeadLine[] list = handler.parseNewsPages(l.toArray(new URL[l.size()]), monitor);
            for (HeadLine headLine : list) {
                if (headLine.getDate() == null || headLine.getDate().before(limitDate)) {
                    continue;
                }
                if (!oldItems.contains(headLine)) {
                    if (!headLine.getDate().before(recentLimitDate)) {
                        headLine.setRecent(true);
                    }
                    oldItems.add(headLine);
                    added.add(headLine);
                }
            }
        }

        if (store.getBoolean(YahooJapanActivator.PREFS_UPDATE_SECURITIES_NEWS)) {
            for (int i = 0; i < securities.length && !monitor.isCanceled(); i++) {
                URL feedUrl = Util.getRSSNewsFeedForSecurity(securities[i]);
                if (feedUrl == null) {
                    continue;
                }

                monitor.subTask(feedUrl.toString());

                try {
                    if (YahooJapanActivator.getDefault() != null) {
                        BundleContext context = YahooJapanActivator.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()));
                                }
                            }
                            context.ungetService(reference);
                        }
                    }

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

                        String link = entry.getLink();
                        if (link == null && entry.getLinks().size() != 0) {
                            link = (String) entry.getLinks().get(0);
                        }
                        if (link != null) {
                            while (link.indexOf('*') != -1) {
                                link = link.substring(link.indexOf('*') + 1);
                            }
                            link = URLDecoder.decode(link, "UTF-8");
                        }
                        if (link == null) {
                            continue;
                        }

                        String source = null;

                        String title = entry.getTitle();
                        if (title.startsWith("[$$]")) {
                            title = title.substring(4, title.length());
                        }

                        if (title.endsWith(")")) {
                            int s = title.lastIndexOf('(');
                            if (s != -1) {
                                source = title.substring(s + 1, title.length() - 1);
                                if (source.startsWith("at ")) {
                                    source = source.substring(3);
                                }
                                title = title.substring(0, s - 1).trim();
                            }
                        }

                        HeadLine headLine = new HeadLine(entry.getPublishedDate(), source, title,
                                new ISecurity[] { securities[i] }, link);
                        int index = oldItems.indexOf(headLine);
                        if (index != -1) {
                            if (headLine.getDate().before(limitDate)) {
                                continue;
                            }
                            headLine = oldItems.get(index);
                            if (!headLine.contains(securities[i])) {
                                headLine.addMember(securities[i]);
                                updated.add(headLine);
                            }
                        } else {
                            if (!headLine.getDate().before(recentLimitDate)) {
                                headLine.setRecent(true);
                            }
                            oldItems.add(headLine);
                            added.add(headLine);
                        }
                    }
                } catch (Exception e) {
                    String msg = "Error fetching news from " + feedUrl.toString();
                    Status status = new Status(IStatus.ERROR, YahooJapanActivator.PLUGIN_ID, 0, msg, e);
                    YahooJapanActivator.log(status);
                }

                monitor.worked(1);
            }
        }

        if (added.size() != 0 || updated.size() != 0) {
            final INewsService service = getNewsService();
            service.runInService(new INewsServiceRunnable() {

                @Override
                public IStatus run(IProgressMonitor monitor) throws Exception {
                    service.addHeadLines(added.toArray(new IHeadLine[added.size()]));
                    service.updateHeadLines(updated.toArray(new IHeadLine[updated.size()]));
                    return Status.OK_STATUS;
                }
            }, null);
        }

        save();
    } catch (Exception e) {
        Status status = new Status(IStatus.ERROR, YahooJapanActivator.PLUGIN_ID, 0, "Error fetching news", e);
        YahooJapanActivator.log(status);
    } finally {
        monitor.done();
    }

    return Status.OK_STATUS;
}

From source file:org.eclipsetrader.yahoojapanfx.internal.core.connector.SnapshotConnector.java

@Override
@SuppressWarnings({ "rawtypes", "unchecked" })
public void run() {
    try {//  w w w  . jav  a 2 s. c om
        HttpClient client = new HttpClient();
        client.getHttpConnectionManager().getParams().setConnectionTimeout(5000);
        Util.setupProxy(client, streamingServer);

        synchronized (thread) {
            while (!isStopping()) {
                synchronized (symbolSubscriptions) {
                    if (symbolSubscriptions.size() != 0) {
                        String[] symbols = symbolSubscriptions.keySet()
                                .toArray(new String[symbolSubscriptions.size()]);
                        fetchLatestSnapshot(client, symbols);
                        //                            subscriptionsChanged = false;
                    }
                }

                try {
                    thread.wait(5000);
                } catch (InterruptedException e) {
                    // Ignore exception, not important at this time
                }
            }
        }
    } catch (Exception e) {
        Status status = new Status(IStatus.ERROR, Activator.PLUGIN_ID, 0, "Error reading data", e);
        Activator.log(status);
    }

    if (!isStopping()) {
        thread = new Thread(this, name + " - Data Reader"); //$NON-NLS-1$
        try {
            Thread.sleep(2 * 1000);
        } catch (Exception e) {
            // Do nothing
        }
        thread.start();
    }
}

From source file:org.exoplatform.calendar.service.impl.RemoteCalendarServiceImpl.java

/**
 * Get the HttpClient object to prepare for the connection with remote server
 * @param remoteCalendar holds information about remote server
 * @return HttpClient object/* w w w.  j  a  va2s.c  o  m*/
 * @throws Exception
 */
public HttpClient getRemoteClient(RemoteCalendar remoteCalendar) throws Exception {
    HostConfiguration hostConfig = new HostConfiguration();
    String host = new URL(remoteCalendar.getRemoteUrl()).getHost();
    if (Utils.isEmpty(host))
        host = remoteCalendar.getRemoteUrl();
    hostConfig.setHost(host);
    HttpClient client = new HttpClient();
    client.setHostConfiguration(hostConfig);
    client.getHttpConnectionManager().getParams().setConnectionTimeout(10000);
    client.getHttpConnectionManager().getParams().setSoTimeout(10000);
    // basic authentication
    if (!Utils.isEmpty(remoteCalendar.getRemoteUser())) {
        Credentials credentials = new UsernamePasswordCredentials(remoteCalendar.getRemoteUser(),
                remoteCalendar.getRemotePassword());
        client.getState().setCredentials(new AuthScope(host, AuthScope.ANY_PORT, AuthScope.ANY_REALM),
                credentials);
    }
    return client;
}

From source file:org.geefive.salesforce.soqleditor.RESTfulQuery.java

public void executeObjectSearch() throws Exception {
    HttpClient restClient = new HttpClient();
    restClient.getParams().setCookiePolicy(CookiePolicy.RFC_2109);
    restClient.getHttpConnectionManager().getParams().setConnectionTimeout(30000);

    String restfulURLTarget = serverDomain + REST_LOGIN;
    System.out.println("RESTful URL Target: " + restfulURLTarget);
    GetMethod method = null;/*from w w  w . ja  v a 2s.  c  om*/
    try {
        method = new GetMethod(restfulURLTarget);
        System.out.println("Setting authorization header with SID [" + SID + "]");
        method.setRequestHeader("Authorization", "OAuth " + SID);

        //         NameValuePair[] params = new NameValuePair[1];
        //         params[0] = new NameValuePair("q","SELECT Name, Id, Phone, CreatedById from Account LIMIT 100");
        //         method.setQueryString(params);
        int httpResponseCode = restClient.executeMethod(method);
        System.out.println("HTTP_RESPONSE_CODE [" + httpResponseCode + "]");
        System.out.println("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%");
        System.out
                .println("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% EXECUTING QUERY %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%");
        if (httpResponseCode == HttpStatus.SC_OK) {
            JSONObject response = new JSONObject(
                    new JSONTokener(new InputStreamReader(method.getResponseBodyAsStream())));
            System.out.println("_____________________________________");
            System.out.println("RESPONSE [" + response + "]");

            JSONArray resultArray = response.getJSONArray("sobjects");
            //            StringBuffer soqlQueryResults = new StringBuffer();
            //            String token = "";
            JSONObject inner = null;
            inner = resultArray.getJSONObject(0);

            Iterator keys = inner.keys();
            HashMap columnNames = new HashMap();
            while (keys.hasNext()) {
                String column = (String) keys.next();
                if (!column.equalsIgnoreCase("attributes")) {
                    System.out.println("KEY>> " + column);
                    columnNames.put(column, "");
                }
            }
            System.out.println("____________________________________________________________________");

            JSONArray results = response.getJSONArray("sobjects");
            //               for (int i = 0; i < results.length(); i++) {
            for (int i = 0; i < 35; i++) {
                try {
                    if (results.getJSONObject(i).getString("searchable").equals("true")) {
                        String objectName = results.getJSONObject(i).getString("name");
                        if (objectName.equals("Asset")) {
                            parseObjectDetails(objectName);
                        }
                        //                        System.out.println("[" + results.getJSONObject(i).getString("custom") + "] " + results.getJSONObject(i).getString("name"));
                    }
                } catch (NullPointerException ex) {
                    //just looking for nulls
                    ex.printStackTrace();
                }
            } //end for
            System.out.println("____________________________________________________________________");

            /*
                            Iterator keyset = null;
                           JSONArray results = response.getJSONArray("records");
                            Vector<String> newRow = null;
                           for (int i = 0; i < results.length(); i++) {
                              keyset = columnNames.keySet().iterator();
                              newRow = new Vector<String>();
                              while(keyset.hasNext()){
             String columnName = (String)keyset.next();
             String columnValue = results.getJSONObject(i).getString(columnName);
             System.out.println(">> COLUMN_VALUE >> " + columnValue);
             newRow.addElement(columnValue);
                              }
                              soqlResults.addDataRow(newRow);
                           }//end for
            */

        }
    } finally {
        method.releaseConnection();
    }

}

From source file:org.geefive.salesforce.soqleditor.RESTfulQuery.java

private void parseObjectDetails(String object) throws Exception {
    System.out.println("xxxxxxxxxxxxxxxxxxxxx " + object + " xxxxxxxxxxxxxxxxxxxxx");
    HttpClient restClient = new HttpClient();
    restClient.getParams().setCookiePolicy(CookiePolicy.RFC_2109);
    restClient.getHttpConnectionManager().getParams().setConnectionTimeout(30000);
    //      Account/describe/
    String restfulURLTarget = serverDomain + REST_LOGIN + "/" + object + "/describe/";
    System.out.println("RESTful URL Target: " + restfulURLTarget);
    GetMethod method = null;// www . jav  a 2s. com
    try {
        method = new GetMethod(restfulURLTarget);
        method.setRequestHeader("Authorization", "OAuth " + SID);

        int httpResponseCode = restClient.executeMethod(method);
        System.out.println("HTTP_RESPONSE_CODE [" + httpResponseCode + "]");
        if (httpResponseCode == HttpStatus.SC_OK) {
            JSONObject response = new JSONObject(
                    new JSONTokener(new InputStreamReader(method.getResponseBodyAsStream())));
            System.out.println("RESPONSE [" + response + "]");

            //            Iterator rkeys = response.keys();      
            //            while(rkeys.hasNext()){
            //               String fieldName = (String)rkeys.next();
            //               System.out.println("KEY>> " + fieldName);
            //            }
            //            

            //            JSONArray fieldObject = response.getJSONArray("fields");
            //            Iterator<String> fieldIterator = fieldObject.getJSONObject(0).keys();
            //            HashMap<String,String> fieldNames = new HashMap<String,String> ();
            //            while(fieldIterator.hasNext()){
            //               String fieldName = fieldIterator.next();
            //               fieldNames.put(fieldName, "");
            //               System.out.println("FIELD KEY>> " + fieldName);
            //            }

            //            FIELD KEY>> byteLength
            //            FIELD KEY>> scale
            //            FIELD KEY>> unique
            //            FIELD KEY>> nillable
            //            FIELD KEY>> idLookup
            //            FIELD KEY>> sortable
            //            FIELD KEY>> inlineHelpText
            //            FIELD KEY>> relationshipName
            //            FIELD KEY>> soapType
            //            FIELD KEY>> type
            //            FIELD KEY>> externalId
            //            FIELD KEY>> calculated
            //            FIELD KEY>> calculatedFormula
            //            FIELD KEY>> htmlFormatted
            //            FIELD KEY>> createable
            //            FIELD KEY>> controllerName
            //            FIELD KEY>> picklistValues
            //            FIELD KEY>> writeRequiresMasterRead
            //            FIELD KEY>> name
            //            FIELD KEY>> nameField
            //            FIELD KEY>> length
            //            FIELD KEY>> digits
            //            FIELD KEY>> defaultValue
            //            FIELD KEY>> autoNumber
            //            FIELD KEY>> custom
            //            FIELD KEY>> referenceTo
            //            FIELD KEY>> precision
            //            FIELD KEY>> updateable
            //            FIELD KEY>> caseSensitive
            //            FIELD KEY>> relationshipOrder
            //            FIELD KEY>> label
            //            FIELD KEY>> dependentPicklist
            //            FIELD KEY>> deprecatedAndHidden
            //            FIELD KEY>> defaultValueFormula
            //            FIELD KEY>> filterable
            //            FIELD KEY>> namePointing
            //            FIELD KEY>> defaultedOnCreate
            //            FIELD KEY>> groupable
            //            FIELD KEY>> restrictedPicklist

            JSONArray fieldObject = response.getJSONArray("fields");
            for (int row = 0; row < fieldObject.length(); row++) {
                System.out.println("name [" + fieldObject.getJSONObject(row).getString("name") + "]");
                //               System.out.println("nameField [" + fieldObject.getJSONObject(row).getString("nameField") + "]");
                System.out.println("length [" + fieldObject.getJSONObject(row).getString("length") + "]");
                System.out.println("digits [" + fieldObject.getJSONObject(row).getString("digits") + "]");
                System.out.println(
                        "defaultValue [" + fieldObject.getJSONObject(row).getString("defaultValue") + "]");
                System.out
                        .println("autoNumber [" + fieldObject.getJSONObject(row).getString("autoNumber") + "]");
                System.out
                        .println("calculated [" + fieldObject.getJSONObject(row).getString("calculated") + "]");
                if (fieldObject.getJSONObject(row).getString("calculated").equals("true"))
                    System.out.println("calculatedFormula ["
                            + fieldObject.getJSONObject(row).getString("calculatedFormula") + "]");
                System.out.println("...............");
            }

            /*
                        JSONArray resultArray = response.getJSONArray("recordTypeInfos");
                        System.out.println("ARRAY_LENGTH: " + resultArray.length());
                                
                        JSONObject inner = null;
                        try{
                        inner = resultArray.getJSONObject(0);
                        }catch(Exception ex){
                           inner = resultArray.getJSONObject(1);
                        }
                                   
                        Iterator keys = inner.keys();
                        HashMap columnNames = new HashMap();
                        while(keys.hasNext()){
                           String column = (String)keys.next();
                           if(!column.equalsIgnoreCase("attributes")){
                              System.out.println("KEY>> " + column);
                              columnNames.put(column, "");
                           }
                        }
            */
        }
    } finally {
        method.releaseConnection();
    }

    System.out.println("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");

}