You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@wink.apache.org by "Harris, Christopher P" <ch...@baxter.com> on 2015/04/17 00:40:09 UTC

How to properly consume response content using Apache HttpClient and Atom

Hi.

How can I obtain an AtomFeed from a ClientResponse object?

I'm successfully using Apache HttpClient v4.0.1 within Apache Wink v1.1.1 to successfully call Sharepoint RESTful web services that return JSON.  My target platform is WAS v8.0.x, which is why I'm using such old versions of both Apache products.  I'm using the Apache HttpClient, because I can configure it to use NTLM authentication, which allows me to authenticate with Sharepoint.

Recently, I discovered that I need to execute GET requests to a new Sharepoint RESTful web service that returns the Atom media type.  Once I am able to successfully read the Atom content, I'll convert it to JAXB, my chosen implementation being MOXy.

The few examples that I've found on the web demonstrate making a GET or POST request that convert the response to an AtomEntry or AtomFeed:
https://wink.apache.org/1.0/html/6.1%20Getting%20Started%20with%20Apache%20Wink%20Client.html
https://wink.apache.org/1.0/html/Appendix%20A%20-%20Feeds%20Support.html

The only issue with this approach is that I need to eventually call:
response.consumeContent();
...to ensure that I consume the content and close the HttpClient connection.  Without calling this line of code, I've discovered in the past that I'll eventually be greeted with an error stating something along the lines of that I've used up the allocated number connections within the connection pool to that Sharepoint resource.

The only other somewhat-useful example that I've found is this:
http://apache-wink-users.3471013.n2.nabble.com/InputStream-amp-async-examples-td7572690.html

However, that guy's implementation (on GitHub) seems a bit more complicated than what I think Wink probably already allows me to do.  Plus, he's trying to use asynchronous http clients, which I am not trying to use.

My thoughts were that I could simply get an AtomFeed entity from a ClientResponse object.  However, the AtomFeed entity doesn't seem to contain any data when I attempt this idea.  I am receiving a 200 response status code.

Here's some code to demonstrate what I'm trying to do:

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.HashSet;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.ws.rs.core.Application;
import javax.ws.rs.core.MediaType;

import org.apache.http.auth.AuthScope;
import org.apache.http.auth.NTCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.wink.client.ClientResponse;
import org.apache.wink.client.Resource;
import org.apache.wink.client.RestClient;
import org.apache.wink.common.model.atom.AtomEntry;
import org.apache.wink.common.model.atom.AtomFeed;

public int sharepointGET() {
           ClientResponse response = null;
           try {
               String spUri = "http://<url_base>/_api/sp.userprofiles.peoplemanager/getpropertiesfor(@v)";

               DefaultHttpClient httpClient = new DefaultHttpClient();
               httpClient.getAuthSchemes().register("ntlm",new JCIFSNTLMSchemeFactory());
               CredentialsProvider credsProvider = new BasicCredentialsProvider();
               NTCredentials ntcred = new NTCredentials(username, password, InetAddress.getLocalHost().getHostName(), domain);
               credsProvider.setCredentials(new AuthScope(host, 443, AuthScope.ANY_REALM, "NTLM"), ntcred);
               httpClient.setCredentialsProvider(credsProvider);

               org.apache.wink.client.ClientConfig httpClientConfig = new org.apache.wink.client.ApacheHttpClientConfig(httpClient);
               Application app = new Application();
               httpClientConfig.applications(app);
               RestClient client = new RestClient(httpClientConfig);
               Resource resource = client.resource(spUri);
               response = resource.queryParam("@v", "'<some_domain>\\<some_user_name>'").accept(MediaType.APPLICATION_ATOM_XML).get()<file:///\\%3csome_user_name%3e'%22).accept(MediaType.APPLICATION_ATOM_XML).get()>;

               AtomFeed feed = response.getEntity(AtomFeed.class);        //<------ What I would like to do

               System.out.println(feed.getTotalResults());
               for (AtomEntry entry : feed.getEntries()) {
                   System.out.println("\t" + entry.getTitle().getValue());
               }

               System.out.println("The response is " + response.getStatusCode());

               response.consumeContent();                                 //<------ What I need to do
           } catch (UnknownHostException ex) {
               Logger.getLogger(WinkClientWrapper.class.getName()).log(Level.SEVERE, null, ex);
           }

           return response.getStatusCode();
       }



-        Chris Harris