Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Apache License 

import java.net.HttpCookie;

import java.util.Date;

public class Main {
    @Deprecated
    // Deprecated because this uses org.apache.http, which is itself deprecated
    public static HttpCookie servletCookieFromApacheCookie(org.apache.http.cookie.Cookie apacheCookie) {
        if (apacheCookie == null) {
            return null;
        }

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

        HttpCookie cookie = new HttpCookie(name, value);

        value = apacheCookie.getDomain();
        if (value != null) {
            cookie.setDomain(value);
        }
        value = apacheCookie.getPath();
        if (value != null) {
            cookie.setPath(value);
        }
        cookie.setSecure(apacheCookie.isSecure());

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