Skip to content

SimpleCookieCache is disabled by default cloud request #3

@ghost

Description

When detecting a device using this method of the API

public AbstractDevice getDeviceFromRequest(HttpServletRequest request, HttpServletResponse response, String... search_capabilities) 

The CloudClientManager is wrapping the request passed in in a DefaultCloudRequest:

CloudClient cc = new CloudClient(new DefaultCloudRequest(request), response, config, (search_capabilities != null && search_capabilities.length > 0) ? search_capabilities : parsedCapabilities, credentials, cache, this, proxy);

But the getCookies method of DefaultCloudRequest is not delegating correctly, it's just throwing an exception:

public Cookie[] getCookies() {
        throw new IllegalStateException("ERROR: Trying to get cookies from a CloudRequest which does not support cookie storage.");
    }

This means that it is impossible for a client to provide a request that will store the cookie to cache the requests to the WURFL service.

This has caused a massive spike in our usage, taking us over our licensed number of requests.

I can propose a patch that allows this to happen while maintaining the functionilty provided by the DefaultCloudRequest:

/**
 * Copyright (c) 2015 ScientiaMobile Inc.
 *
 * The WURFL Cloud Client is intended to be used in both open-source and
 * commercial environments. To allow its use in as many situations as possible,
 * the WURFL Cloud Client is dual-licensed. You may choose to use the WURFL
 * Cloud Client under either the GNU GENERAL PUBLIC LICENSE, Version 2.0, or
 * the MIT License.
 *
 * Refer to the COPYING.txt file distributed with this package.
 */
package com.scientiamobile.wurflcloud;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Vector;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

import com.scientiamobile.wurflcloud.utils.Constants;

/**
 * CloudRequest default implementation.
 *
 */
public final class DefaultCloudRequest implements CloudRequest {

    private final Map<String, String> headers = new HashMap<String, String>();

  private Cookie[] cookies = new Cookie[0];

  public DefaultCloudRequest(HttpServletRequest servletRequest) throws IllegalArgumentException {

    if (servletRequest == null) {
      throw new IllegalArgumentException("Error: Servlet request cannot be null.");
    }

    String userAgentLC = servletRequest.getHeader(Constants.USER_AGENT_LC);
    String userAgentUC = servletRequest.getHeader("User-Agent");
    cookies = servletRequest.getCookies();

    this.headers.put("user-agent", userAgentUC != null ? userAgentUC : userAgentLC);
  }

    public DefaultCloudRequest(String userAgent) throws IllegalArgumentException {
        this.headers.put(Constants.USER_AGENT_LC, userAgent);
    }

    /**
     * {@inheritDoc}
     */
    public Enumeration<String> getHeaderNames() {
        return new Vector<String>(headers.keySet()).elements();
    }

    /**
     * {@inheritDoc}
     */
    public String getHeader(String name) {
        return headers.get(name);
    }

    /**
     * {@inheritDoc}
     */
    public String getRemoteAddr() {
        return null;
    }

    /**
     * {@inheritDoc}
     */
    public Cookie[] getCookies() {
      if (cookies == null) {
        throw new IllegalStateException("ERROR: Trying to get cookies from a CloudRequest which does not support cookie storage.");
      }
      return cookies;
    }

}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions