grails - 如何在 grails 中将 "javax.servlet.http.Cookie"转换为 "org.apache.http.cookie.Cookie"

标签 grails cookies groovy

我正在尝试从 grails 服务中的请求中提取一个 cookie,如下所示:

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

这样我就可以将它放回我传递给 Web 服务的请求中。不幸的是,我上面得到的 cookie 是 javax.servlet.http.Cookie,当我想将 cookie 添加到 RESTClient 时,如下所示:
for (int i=0; i<cookies.length; i++) {
    client.client.cookieStore.addCookie(cookies[i])
}

我发现 addCookie 需要一个 org.apache.http.cookie.Cookie,我希望我不必进行完整的转换。谁能给我一些关于处理这个问题的最佳方法的建议?

提前致谢。

最佳答案

链接到现在已死的 URL 的另一个答案。此 URL 上托管的解决方案是 Sandeep Gupta 的 Apache 许可脚本。

该解决方案基本上涉及使用公共(public) setter/getter 复制每个属性。

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

}

关于grails - 如何在 grails 中将 "javax.servlet.http.Cookie"转换为 "org.apache.http.cookie.Cookie",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13774340/

相关文章:

Groovy:在这些情况下如何替换 leftShift 运算符?

grails - Grails的KahaDb属性

grails - 在Gradle中禁用缩小

Grails 3/GORM 从结果中剔除单个记录

适用于不同环境的 Grails DB 迁移

javascript - 使用 Google 网页优化器进行 A/B 测试; cookie 告诉我访客获得 A 或 B 的内容

Javascript 添加数字,就好像它们是字符串一样

c++ - 使用 C++ 删除 Internet Explorer 8 历史记录、Cookie、缓存和密码

java - Groovy/Java - 复制路径中带有括号的文件时出现问题

ajax - 通过 View 下拉菜单过滤 Controller 中的数据