JMecham
JMecham

Reputation: 291

How to convert from "javax.servlet.http.Cookie" to "org.apache.http.cookie.Cookie" in grails

I am trying to pull a cookie out of the request in a grails service like this:

def cookies = RequestContextHolder.currentRequestAttributes().getCurrentRequest().getCookies();

so that I can put it back into the request that I'm passing along to a web service. Unfortunately the cookie I get above is a javax.servlet.http.Cookie, and when I want to add the cookie to the RESTClient, like this:

for (int i=0; i<cookies.length; i++) {
    client.client.cookieStore.addCookie(cookies[i])
}

I found that addCookie is expecting a org.apache.http.cookie.Cookie, and I am hoping I don't have to do a complete conversion. Can anyone give me some advice as to the best way to handle this?

Thanks in advance.

Upvotes: 3

Views: 5911

Answers (1)

Jamie Counsell
Jamie Counsell

Reputation: 8143

Another answer linked to a URL that is now dead. The solution hosted at this URL was an Apache licensed script by Sandeep Gupta.

The solution basically involves copying over each property using public getters.

/**
 * Copyright (C) 2010, Sandeep Gupta
 * http://www.sangupta.com
 * 
 * The file is licensed under the the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * 
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package com.sangupta.util;

import java.util.Date;

import javax.servlet.http.Cookie;

/**
 * Utility class to help convert Cookie objects between Java Servlet Cookie's
 * and Apache HttpClient Cookie's. 
 * 
 * @author sangupta
 * @version 1.0
 * @since 30 Oct 2010
 */
public class ApacheCookieUtils {

 /**
  * Method to convert an Apache HttpClient cookie to a Java Servlet cookie.
  * 
  * @param apacheCookie the source apache cookie
  * @return a java servlet cookie
  */
 public static Cookie servletCookieFromApacheCookie(org.apache.commons.httpclient.Cookie apacheCookie) {
  if(apacheCookie == null) {
   return null;
  }

  String name = apacheCookie.getName();
  String value = apacheCookie.getValue();

  Cookie cookie = new Cookie(name, value);

  // set the domain
  value = apacheCookie.getDomain();
  if(value != null) {
   cookie.setDomain(value);
  }

  // path
  value = apacheCookie.getPath();
  if(value != null) {
   cookie.setPath(value);
  }

  // secure
  cookie.setSecure(apacheCookie.getSecure());

  // comment
  value = apacheCookie.getComment();
  if(value != null) {
   cookie.setComment(value);
  }

  // version
  cookie.setVersion(apacheCookie.getVersion());

  // From the Apache source code, maxAge is converted to expiry date using the following formula
  // if (maxAge >= 0) {
        //     setExpiryDate(new Date(System.currentTimeMillis() + maxAge * 1000L));
        // }
  // Reverse this to get the actual max age

  Date expiryDate = apacheCookie.getExpiryDate();
  if(expiryDate != null) {
   long maxAge = (expiryDate.getTime() - System.currentTimeMillis()) / 1000;
   // we have to lower down, no other option
   cookie.setMaxAge((int) maxAge);
  }

  // return the servlet cookie
  return cookie;
 }

 /**
  * Method to convert a Java Servlet cookie to an Apache HttpClient cookie.
  * 
  * @param cookie the Java servlet cookie to convert
  * @return the Apache HttpClient cookie
  */
 public static org.apache.commons.httpclient.Cookie apacheCookieFromServletCookie(Cookie cookie) {
  if(cookie == null) {
   return null;
  }

  org.apache.commons.httpclient.Cookie apacheCookie = null;

  // get all the relevant parameters
     String domain = cookie.getDomain();
     String name = cookie.getName();
     String value = cookie.getValue();
     String path = cookie.getPath();
     int maxAge = cookie.getMaxAge();
     boolean secure = cookie.getSecure();

     // create the apache cookie
     apacheCookie = new org.apache.commons.httpclient.Cookie(domain, name, value, path, maxAge, secure);

     // set additional parameters
     apacheCookie.setComment(cookie.getComment());
     apacheCookie.setVersion(cookie.getVersion());

     // return the apache cookie
     return apacheCookie;
 }

}

Upvotes: 1

Related Questions