Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SimpleCookieCache is disabled by default cloud request #3

Open
ghost opened this issue Nov 18, 2015 · 5 comments
Open

SimpleCookieCache is disabled by default cloud request #3

ghost opened this issue Nov 18, 2015 · 5 comments

Comments

@ghost
Copy link

ghost commented Nov 18, 2015

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;
    }

}
@elliotfehr
Copy link
Contributor

@jason-rgl Thank you for letting us know. Are you able to submit this change in the form of a pull request so that we can review the changes being made?

@ghost
Copy link
Author

ghost commented Nov 19, 2015

Hi Elliot,

I’ve tried to create a pull request but it’s telling me I do not have permission to push my changes to this repository.

[cid:[email protected]]

Regards,
Jason

From: Elliot Fehr [mailto:[email protected]]
Sent: Thursday, November 19, 2015 1:50 AM
To: WURFL/wurfl-cloud-client-java [email protected]
Cc: Jason Hopkins [email protected]
Subject: Re: [wurfl-cloud-client-java] SimpleCookieCache is disabled by default cloud request (#3)

@jason-rglhttps://github.com/jason-rgl Thank you for letting us know. Are you able to submit this change in the form of a pull request so that we can review the changes being made?


Reply to this email directly or view it on GitHubhttps://github.com//issues/3#issuecomment-157921969.

@elliotfehr
Copy link
Contributor

@jason-rgl To submit a pull request on Github, you will want to fork the repository and make the changes to your forked copy. Once ready, you can request the changes be merged upstream by submitting a pull request of your forked repository. A good tutorial can be found here: https://help.github.com/articles/fork-a-repo/

@ghost
Copy link
Author

ghost commented Nov 19, 2015

Thanks Elliot, I’m completely new to github… you may have guessed.

From: Elliot Fehr [mailto:[email protected]]
Sent: Thursday, November 19, 2015 2:18 PM
To: WURFL/wurfl-cloud-client-java [email protected]
Cc: Jason Hopkins [email protected]
Subject: Re: [wurfl-cloud-client-java] SimpleCookieCache is disabled by default cloud request (#3)

@jason-rglhttps://github.com/jason-rgl To submit a pull request on Github, you will want to fork the repository and make the changes to your forked copy. Once ready, you can request the changes be merged upstream by submitting a pull request of your forked repository. A good tutorial can be found here: https://help.github.com/articles/fork-a-repo/


Reply to this email directly or view it on GitHubhttps://github.com//issues/3#issuecomment-158070162.

@ghost
Copy link
Author

ghost commented Nov 20, 2015

Hi Elliot,

Pull request created, but it doesn’t have my change…. Let me try again.

Regards,
Jason

From: Elliot Fehr [mailto:[email protected]]
Sent: Thursday, November 19, 2015 2:18 PM
To: WURFL/wurfl-cloud-client-java [email protected]
Cc: Jason Hopkins [email protected]
Subject: Re: [wurfl-cloud-client-java] SimpleCookieCache is disabled by default cloud request (#3)

@jason-rglhttps://github.com/jason-rgl To submit a pull request on Github, you will want to fork the repository and make the changes to your forked copy. Once ready, you can request the changes be merged upstream by submitting a pull request of your forked repository. A good tutorial can be found here: https://help.github.com/articles/fork-a-repo/


Reply to this email directly or view it on GitHubhttps://github.com//issues/3#issuecomment-158070162.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant