Failed to read cookies from Android client client

I have a web application that authenticates and then checks for a cookie in the request in order to allow subsequent calls from the same client. I wrote a code snippet that works fine when I run it from eclipse, but does not work when launched from the Android emulator. Here is what i did

A class to create a webresource for a URI and bind a filter to it to attach cookies to the request

public class TolvenRestClient {
        private static Client client;
        private static final ThreadLocal<String> instance = new ThreadLocal<String>();

    static {
        ClientConfig config = new DefaultClientConfig();
        client = Client.create(config);
        client.setFollowRedirects(true);
    }

    private Client getClient() {
        return client;
    }

    public WebResource getResource(String path){
        WebResource webResource = getClient().resource(path);
        webResource.addFilter(new ClientFilter() {
            @Override
            public ClientResponse handle(ClientRequest clientRequest) throws ClientHandlerException {
                if (instance.get() != null) {
                    clientRequest.getHeaders().putSingle("Cookie", "SSOCookie=" + instance.get());
                }
                ClientResponse clientResponse = getNext().handle(clientRequest);
                String cookieName = "SSOCookie";
                System.out.println("cookies "+clientResponse.getCookies());
                for (NewCookie newCookie : clientResponse.getCookies()) {
                    if (cookieName.equals(newCookie.getName())) {
                        String extendedSessionId = newCookie.getValue();                            
                        instance.set(extendedSessionId);
                        break;
                    }
                }
                return clientResponse;
            }
        }); 
        return webResource;
    }

and then sample code to run webservice calls

RestClient client = new RestClient();

    MultivaluedMap<String, String> formData = new MultivaluedMapImpl();
    formData.add("username", "demo");
    formData.add("password", "1234qwer");
    formData.add("realm", "tolven");
    WebResource webResource = client.getResource("https://dev.example.com:8443/gatekeeper/rs/authenticate/login");
    ClientResponse response = webResource.type(MediaType.APPLICATION_FORM_URLENCODED).post(ClientResponse.class,formData);
    //ClientResponse response = webResource.accept("text/html").get(ClientResponse.class);
    System.out.println("Here ....." + response.toString());     
    webResource = client.getResource("https://dev.example.com:8443/api/vestibule/accountList");
    response = webResource.get(ClientResponse.class);
    Accounts accounts = response.getEntity(Accounts.class);

This code works great when run from eclipse. But when run inside Android emulator, it cannot read cookies from client response

I get the following exception

09-20 19:55:31.067: W/System.err(1172): java.lang.NullPointerException
09-20 19:55:31.077: W/System.err(1172):     at javax.ws.rs.core.NewCookie.valueOf(NewCookie.java:126)
09-20 19:55:31.077: W/System.err(1172):     at com.sun.jersey.api.client.ClientResponse.getCookies(ClientResponse.java:714)
09-20 19:55:31.077: W/System.err(1172):     at com.userexample.LoginActivityTolven$1$1.handle(LoginActivityTolven.java:71)
09-20 19:55:31.088: W/System.err(1172):     at com.sun.jersey.api.client.WebResource.handle(WebResource.java:680)
09-20 19:55:31.088: W/System.err(1172):     at com.sun.jersey.api.client.WebResource.access$200(WebResource.java:74)
09-20 19:55:31.097: W/System.err(1172):     at com.sun.jersey.api.client.WebResource$Builder.post(WebResource.java:568)
09-20 19:55:31.097: W/System.err(1172):     at com.userexample.LoginActivityTolven$1.onClick(LoginActivityTolven.java:86)
09-20 19:55:31.097: W/System.err(1172):     at android.view.View.performClick(View.java:4240)
09-20 19:55:31.097: W/System.err(1172):     at android.view.View$PerformClick.run(View.java:17721)
09-20 19:55:31.107: W/System.err(1172):     at android.os.Handler.handleCallback(Handler.java:730)
09-20 19:55:31.107: W/System.err(1172):     at android.os.Handler.dispatchMessage(Handler.java:92)
09-20 19:55:31.107: W/System.err(1172):     at android.os.Looper.loop(Looper.java:137)
09-20 19:55:31.107: W/System.err(1172):     at android.app.ActivityThread.main(ActivityThread.java:5103)
09-20 19:55:31.107: W/System.err(1172):     at java.lang.reflect.Method.invokeNative(Native Method)
09-20 19:55:31.117: W/System.err(1172):     at java.lang.reflect.Method.invoke(Method.java:525)
09-20 19:55:31.117: W/System.err(1172):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
09-20 19:55:31.117: W/System.err(1172):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-20 19:55:31.117: W/System.err(1172):     at dalvik.system.NativeStart.main(Native Method)

Has anyone tried using the jersey client on android before and seen this problem?

+4
1

, Jersey 1.x, JAX-RS null NewCookie.

, META-INF/services . Android , META-INF/services, .

- , ServiceIteratorProvider.

:

:

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

import com.sun.jersey.spi.service.ServiceFinder.ServiceIteratorProvider;

public class YourOwnServiceIteratorProvider<T> extends ServiceIteratorProvider<T>
{
    private static final HashMap<String, String[]> SERVICES = new HashMap<String, String[]>();

    private static final String[] com_sun_jersey_spi_HeaderDelegateProvider = new String[] {
            "com.sun.jersey.core.impl.provider.header.LocaleProvider",
            "com.sun.jersey.core.impl.provider.header.EntityTagProvider",
            "com.sun.jersey.core.impl.provider.header.MediaTypeProvider",
            "com.sun.jersey.core.impl.provider.header.CacheControlProvider",
            "com.sun.jersey.core.impl.provider.header.NewCookieProvider",
            "com.sun.jersey.core.impl.provider.header.CookieProvider",
            "com.sun.jersey.core.impl.provider.header.URIProvider",
            "com.sun.jersey.core.impl.provider.header.DateProvider",
            "com.sun.jersey.core.impl.provider.header.StringProvider"

    };
    private static final String[] com_sun_jersey_spi_inject_InjectableProvider = new String[] {
            "com.sun.jersey.core.impl.provider.xml.SAXParserContextProvider",
            "com.sun.jersey.core.impl.provider.xml.XMLStreamReaderContextProvider",
            "com.sun.jersey.core.impl.provider.xml.DocumentBuilderFactoryProvider",
            "com.sun.jersey.core.impl.provider.xml.TransformerFactoryProvider"
    };
    private static final String[] javax_ws_rs_ext_MessageBodyReader = new String[] {
            "com.sun.jersey.core.impl.provider.entity.StringProvider",
            "com.sun.jersey.core.impl.provider.entity.ByteArrayProvider",
            "com.sun.jersey.core.impl.provider.entity.FileProvider",
            "com.sun.jersey.core.impl.provider.entity.InputStreamProvider",
            "com.sun.jersey.core.impl.provider.entity.DataSourceProvider",
            "com.sun.jersey.core.impl.provider.entity.RenderedImageProvider",
            "com.sun.jersey.core.impl.provider.entity.MimeMultipartProvider",
            "com.sun.jersey.core.impl.provider.entity.FormProvider",
            "com.sun.jersey.core.impl.provider.entity.FormMultivaluedMapProvider",
            "com.sun.jersey.core.impl.provider.entity.XMLRootElementProvider$App",
            "com.sun.jersey.core.impl.provider.entity.XMLRootElementProvider$Text",
            "com.sun.jersey.core.impl.provider.entity.XMLRootElementProvider$General",
            "com.sun.jersey.core.impl.provider.entity.XMLJAXBElementProvider$App",
            "com.sun.jersey.core.impl.provider.entity.XMLJAXBElementProvider$Text",
            "com.sun.jersey.core.impl.provider.entity.XMLJAXBElementProvider$General",
            "com.sun.jersey.core.impl.provider.entity.XMLListElementProvider$App",
            "com.sun.jersey.core.impl.provider.entity.XMLListElementProvider$Text",
            "com.sun.jersey.core.impl.provider.entity.XMLListElementProvider$General",
            "com.sun.jersey.core.impl.provider.entity.ReaderProvider",
            "com.sun.jersey.core.impl.provider.entity.DocumentProvider",
            "com.sun.jersey.core.impl.provider.entity.SourceProvider$StreamSourceReader",
            "com.sun.jersey.core.impl.provider.entity.SourceProvider$SAXSourceReader",
            "com.sun.jersey.core.impl.provider.entity.SourceProvider$DOMSourceReader",
            "com.sun.jersey.core.impl.provider.entity.XMLRootObjectProvider$App",
            "com.sun.jersey.core.impl.provider.entity.XMLRootObjectProvider$Text",
            "com.sun.jersey.core.impl.provider.entity.XMLRootObjectProvider$General",
            "com.sun.jersey.core.impl.provider.entity.EntityHolderReader"
    };
    private static final String[] javax_ws_rs_ext_MessageBodyWriter = new String[] {
            "com.sun.jersey.core.impl.provider.entity.StringProvider",
            "com.sun.jersey.core.impl.provider.entity.ByteArrayProvider",
            "com.sun.jersey.core.impl.provider.entity.FileProvider",
            "com.sun.jersey.core.impl.provider.entity.InputStreamProvider",
            "com.sun.jersey.core.impl.provider.entity.DataSourceProvider",
            "com.sun.jersey.core.impl.provider.entity.RenderedImageProvider",
            "com.sun.jersey.core.impl.provider.entity.MimeMultipartProvider",
            "com.sun.jersey.core.impl.provider.entity.FormProvider",
            "com.sun.jersey.core.impl.provider.entity.FormMultivaluedMapProvider",
            "com.sun.jersey.core.impl.provider.entity.XMLRootElementProvider$App",
            "com.sun.jersey.core.impl.provider.entity.XMLRootElementProvider$Text",
            "com.sun.jersey.core.impl.provider.entity.XMLRootElementProvider$General",
            "com.sun.jersey.core.impl.provider.entity.XMLJAXBElementProvider$App",
            "com.sun.jersey.core.impl.provider.entity.XMLJAXBElementProvider$Text",
            "com.sun.jersey.core.impl.provider.entity.XMLJAXBElementProvider$General",
            "com.sun.jersey.core.impl.provider.entity.XMLListElementProvider$App",
            "com.sun.jersey.core.impl.provider.entity.XMLListElementProvider$Text",
            "com.sun.jersey.core.impl.provider.entity.XMLListElementProvider$General",
            "com.sun.jersey.core.impl.provider.entity.ReaderProvider",
            "com.sun.jersey.core.impl.provider.entity.DocumentProvider",
            "com.sun.jersey.core.impl.provider.entity.StreamingOutputProvider",
            "com.sun.jersey.core.impl.provider.entity.SourceProvider$SourceWriter"
    };

    static
    {
        SERVICES.put("com.sun.jersey.spi.HeaderDelegateProvider",
            com_sun_jersey_spi_HeaderDelegateProvider);
        SERVICES.put("com.sun.jersey.spi.inject.InjectableProvider",
            com_sun_jersey_spi_inject_InjectableProvider);
        SERVICES.put("javax.ws.rs.ext.MessageBodyReader", javax_ws_rs_ext_MessageBodyReader);
        SERVICES.put("javax.ws.rs.ext.MessageBodyWriter", javax_ws_rs_ext_MessageBodyWriter);
    }

    @SuppressWarnings("unchecked")
    @Override
    public Iterator<Class<T>> createClassIterator(Class<T> service, String serviceName,
        ClassLoader loader, boolean ignoreOnClassNotFound)
    {
        String[] classesNames = SERVICES.get(serviceName);
        int length = classesNames.length;
        ArrayList<Class<T>> classes = new ArrayList<Class<T>>(length);
        for (int i = 0; i < length; i++)
        {
            try
            {
                classes.add((Class<T>) Class.forName(classesNames[i]));
            } catch (ClassNotFoundException e)
            {
                e.printStackTrace();
            }
        }
        return null;
    }

    @Override
    public Iterator<T> createIterator(Class<T> service, String serviceName, ClassLoader loader,
        boolean ignoreOnClassNotFound)
    {
        String[] classesNames = SERVICES.get(serviceName);
        int length = classesNames.length;
        ArrayList<T> classes = new ArrayList<T>(length);
        for (int i = 0; i < length; i++)
        {
            try
            {
                classes.add(service.cast(Class.forName(classesNames[i]).newInstance()));
            } catch (IllegalAccessException e)
            {
                e.printStackTrace();
            } catch (InstantiationException e)
            {
                e.printStackTrace();
            } catch (ClassNotFoundException e)
            {
                e.printStackTrace();
            }
        }
        return classes.iterator();
    }
}

:

// ...
Client client = Client.create();
// ...
ServiceFinder.setIteratorProvider(new YourOwnServiceIteratorProvider());
// ...

+1

All Articles