List of usage examples for javax.servlet.http Cookie getPath
public String getPath()
From source file:com.anjz.util.CookieUtils.java
private static void getCookieHeaderValue(final Cookie cookie, final StringBuffer buf, final boolean httpOnly) { final int version = cookie.getVersion(); // this part is the same for all cookies String name = cookie.getName(); // Avoid NPE on malformed cookies if (name == null) { name = ""; }/*from ww w. java2s . c o m*/ String value = cookie.getValue(); if (value == null) { value = ""; } buf.append(name); buf.append("="); maybeQuote(version, buf, value); // add version 1 specific information if (version == 1) { // Version=1 ... required buf.append("; Version=1"); // Comment=comment if (cookie.getComment() != null) { buf.append("; Comment="); maybeQuote(version, buf, cookie.getComment()); } } // add domain information, if present if (cookie.getDomain() != null) { buf.append("; Domain="); maybeQuote(version, buf, cookie.getDomain()); } // Max-Age=secs/Discard ... or use old "Expires" format if (cookie.getMaxAge() >= 0) { if (version == 0) { buf.append("; Expires="); SimpleDateFormat dateFormat = new SimpleDateFormat(OLD_COOKIE_PATTERN, LOCALE_US); dateFormat.setTimeZone(TimeZone.getTimeZone("GMT")); //GMT? if (cookie.getMaxAge() == 0) { dateFormat.format(new Date(10000), buf, new FieldPosition(0)); } else { dateFormat.format(new Date(System.currentTimeMillis() + cookie.getMaxAge() * 1000L), buf, new FieldPosition(0)); } } else { buf.append("; Max-Age="); buf.append(cookie.getMaxAge()); } } else if (version == 1) { buf.append("; Discard"); } // Path=path if (cookie.getPath() != null) { buf.append("; Path="); maybeQuote(version, buf, cookie.getPath()); } // Secure if (cookie.getSecure()) { buf.append("; Secure"); } // HttpOnly if (httpOnly) { buf.append("; HttpOnly"); } }
From source file:com.liferay.portal.util.HttpImpl.java
protected org.apache.commons.httpclient.Cookie toCommonsCookie(Cookie cookie) { org.apache.commons.httpclient.Cookie commonsCookie = new org.apache.commons.httpclient.Cookie( cookie.getDomain(), cookie.getName(), cookie.getValue(), cookie.getPath(), cookie.getMaxAge(), cookie.getSecure());//from www .j a v a2s . co m commonsCookie.setVersion(cookie.getVersion()); return commonsCookie; }
From source file:io.restassured.module.mockmvc.internal.MockMvcRequestSenderImpl.java
private Cookies convertCookies(javax.servlet.http.Cookie[] servletCookies) { List<Cookie> cookies = new ArrayList<Cookie>(); for (javax.servlet.http.Cookie servletCookie : servletCookies) { Cookie.Builder cookieBuilder = new Cookie.Builder(servletCookie.getName(), servletCookie.getValue()); if (servletCookie.getComment() != null) { cookieBuilder.setComment(servletCookie.getComment()); }/* w ww . j a v a2 s . c o m*/ if (servletCookie.getDomain() != null) { cookieBuilder.setDomain(servletCookie.getDomain()); } if (servletCookie.getPath() != null) { cookieBuilder.setPath(servletCookie.getPath()); } cookieBuilder.setMaxAge(servletCookie.getMaxAge()); cookieBuilder.setVersion(servletCookie.getVersion()); cookieBuilder.setSecured(servletCookie.getSecure()); cookies.add(cookieBuilder.build()); } return new Cookies(cookies); }
From source file:ed.net.CookieJar.java
/** * Return <tt>true</tt> if the cookie should be submitted with a request * with given attributes, <tt>false</tt> otherwise. * @param destination the destination of the request * @param cookie {@link Cookie} to be matched * @return true if the cookie matches the criterium *//* ww w . java 2s. c o m*/ private boolean match(URL destination, final Cookie cookie) { String host = destination.getHost(); int port = destination.getPort(); String path = destination.getPath(); boolean secure = "https".equals(destination.getProtocol()); if (host == null) { throw new IllegalArgumentException("Host of origin may not be null"); } if (host.trim().equals("")) { throw new IllegalArgumentException("Host of origin may not be blank"); } if (port < 0) { port = 80; } if (path == null) { throw new IllegalArgumentException("Path of origin may not be null."); } if (cookie == null) { throw new IllegalArgumentException("Cookie may not be null"); } if (path.trim().equals("")) { path = "/"; } host = host.toLowerCase(); if (cookie.getDomain() == null) { return false; } if (cookie.getPath() == null) { return false; } return // only add the cookie if it hasn't yet expired !isExpired(cookie) // and the domain pattern matches && (domainMatch(host, cookie.getDomain())) // and the path is null or matching && (pathMatch(path, cookie.getPath())) // and if the secure flag is set, only if the request is // actually secure && (cookie.getSecure() ? secure : true); }
From source file:ddf.security.samlp.impl.LogoutMessageImpl.java
@Override public String sendSamlLogoutRequest(LogoutRequest request, String targetUri, boolean isSoap, @Nullable Cookie cookie) throws IOException, WSSecurityException { XMLObject xmlObject = isSoap ? SamlProtocol.createSoapMessage(request) : request; Element requestElement = getElementFromSaml(xmlObject); String requestMessage = DOM2Writer.nodeToString(requestElement); try (CloseableHttpClient httpClient = HttpClients.createDefault()) { HttpPost post = new HttpPost(targetUri); post.addHeader("Cache-Control", "no-cache, no-store"); post.addHeader("Pragma", "no-cache"); post.addHeader("SOAPAction", SAML_SOAP_ACTION); post.addHeader("Content-Type", "application/soap+xml"); post.setEntity(new StringEntity(requestMessage, "utf-8")); ResponseHandler<String> responseHandler = new BasicResponseHandler(); BasicHttpContext context = new BasicHttpContext(); if (cookie != null) { BasicClientCookie basicClientCookie = new BasicClientCookie(cookie.getName(), cookie.getValue()); basicClientCookie.setDomain(cookie.getDomain()); basicClientCookie.setPath(cookie.getPath()); BasicCookieStore cookieStore = new BasicCookieStore(); cookieStore.addCookie(basicClientCookie); context.setAttribute(HttpClientContext.COOKIE_STORE, cookieStore); }/*ww w .ja v a 2 s. c o m*/ return httpClient.execute(post, responseHandler, context); } }
From source file:com.google.gsa.valve.modules.noauth.HTTPNoAuthenticationProcess.java
/** * This method simulates the authentication process against a content * source, so that every document is consider here as public. * <p>/*from www. j a va 2s . c o m*/ * Creates the authentication cookie and always return 200, unless there is * any problem processing the request. * * @param request HTTP request * @param response HTTP response * @param authCookies vector that contains the authentication cookies * @param url the document url * @param creds an array of credentials for all external sources * @param id the default credential id to be retrieved from creds * @return the HTTP error code * @throws HttpException * @throws IOException */ public int authenticate(HttpServletRequest request, HttpServletResponse response, Vector<Cookie> authCookies, String url, Credentials creds, String id) throws HttpException, IOException { Cookie[] cookies = null; // Initialize status code int statusCode = HttpServletResponse.SC_UNAUTHORIZED; // Read cookies cookies = request.getCookies(); // Debug logger.debug("HTTP No authentication start"); // // Launch the authentication process // // Protection try { Cookie extAuthCookie = null; extAuthCookie = new Cookie("gsa_basic_noauth", ""); extAuthCookie.setValue("true"); String authCookieDomain = null; String authCookiePath = null; int authMaxAge = -1; // Cache cookie properties authCookieDomain = (request.getAttribute("authCookieDomain")).toString(); authCookiePath = (request.getAttribute("authCookiePath")).toString(); //authMaxAge try { authMaxAge = Integer.parseInt(valveConf.getAuthMaxAge()); } catch (NumberFormatException nfe) { logger.error( "Configuration error: chack the configuration file as the number set for authMaxAge is not OK:"); } // Set extra cookie parameters extAuthCookie.setDomain(authCookieDomain); extAuthCookie.setPath(authCookiePath); extAuthCookie.setMaxAge(authMaxAge); // Log info if (logger.isDebugEnabled()) logger.debug("Adding gsa_basic_noauth cookie: " + extAuthCookie.getName() + ":" + extAuthCookie.getValue() + ":" + extAuthCookie.getPath() + ":" + extAuthCookie.getDomain() + ":" + extAuthCookie.getSecure()); //add sendCookies support boolean isSessionEnabled = new Boolean(valveConf.getSessionConfig().isSessionEnabled()).booleanValue(); boolean sendCookies = false; if (isSessionEnabled) { sendCookies = new Boolean(valveConf.getSessionConfig().getSendCookies()).booleanValue(); } if ((!isSessionEnabled) || ((isSessionEnabled) && (sendCookies))) { response.addCookie(extAuthCookie); } //add cookie to the array authCookies.add(extAuthCookie); statusCode = HttpServletResponse.SC_OK; } catch (Exception e) { // Log error logger.error("HTTP Basic authentication failure: " + e.getMessage(), e); // Update status code statusCode = HttpServletResponse.SC_UNAUTHORIZED; } // End of the authentication process logger.debug("HTTP No Authentication completed (" + statusCode + ")"); // Return status code return statusCode; }
From source file:CookieServlet.java
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, java.io.IOException { Cookie cookie = null; Cookie[] cookies = request.getCookies(); boolean newCookie = false; if (cookies != null) { for (int i = 0; i < cookies.length; i++) { if (cookies[i].getName().equals("mycookie")) { cookie = cookies[i];/*from w w w . j a v a 2 s . co m*/ } } } if (cookie == null) { newCookie = true; int maxAge; try { maxAge = new Integer(getServletContext().getInitParameter("cookie-age")).intValue(); } catch (Exception e) { maxAge = -1; } cookie = new Cookie("mycookie", "" + getNextCookieValue()); cookie.setPath(request.getContextPath()); cookie.setMaxAge(maxAge); response.addCookie(cookie); } response.setContentType("text/html"); java.io.PrintWriter out = response.getWriter(); out.println("<html>"); out.println("<head>"); out.println("<title>Cookie info</title>"); out.println("</head>"); out.println("<body>"); out.println("<h2> Information about the cookie named \"mycookie\"</h2>"); out.println("Cookie value: " + cookie.getValue() + "<br>"); if (newCookie) { out.println("Cookie Max-Age: " + cookie.getMaxAge() + "<br>"); out.println("Cookie Path: " + cookie.getPath() + "<br>"); } out.println("</body>"); out.println("</html>"); out.close(); }
From source file:com.alfaariss.oa.util.web.CookieTool.java
/** * Set Cookie with optional extra context in application context * @param sCookie//from w ww.j a v a2 s. com * @param sValue * @param sExtraContext * @param oRequest * @return */ public Cookie createCookie(String sCookie, String sValue, String sExtraContext, HttpServletRequest oRequest) { assert sValue != null : "Supplied value == null"; assert oRequest != null : "Supplied request == null"; Cookie cookie = new Cookie(sCookie, sValue); if (_sCookieDomain != null) { cookie.setDomain(_sCookieDomain); _logger.debug("Created domain cookie on " + _sCookieDomain); } if (_iCookieVersion != -1) { cookie.setVersion(_iCookieVersion); _logger.debug("Setting cookie version: " + _iCookieVersion); } /* format sExtraContext */ if (sExtraContext == null) { sExtraContext = ""; } else { if (!sExtraContext.startsWith("/")) { sExtraContext = "/" + sExtraContext; } } String path = oRequest.getContextPath(); if (path != null && path.length() > 0) {//only set path if path not is empty (when hosted as server root, getContextPath() will return an empty string) cookie.setPath(path + sExtraContext);// /openaselect } else {//if no contextpath available then setting the cookie path on '/' instead of on the default path (which is for the sso cookie: /openaselect/sso) cookie.setPath("/" + sExtraContext); } cookie.setSecure(_bSecureCookie); StringBuffer sbDebug = new StringBuffer("Created '"); sbDebug.append(sCookie); sbDebug.append("' on path="); sbDebug.append(cookie.getPath()); _logger.debug(sbDebug.toString()); return cookie; }
From source file:de.micromata.genome.gwiki.page.GWikiContext.java
/** * set a cookie./*from w w w .j ava 2s. com*/ * * @param key the key * @param value the value */ @SuppressWarnings("deprecation") public void setCookie(String key, String value) { String cvalue = URLEncoder.encode(value); Cookie tsc = new Cookie(key, cvalue); tsc.setPath(getWikiWeb().getContextPath()); if (StringUtils.isEmpty(tsc.getPath()) == true) { tsc.setPath("/"); } tsc.setMaxAge((int) TimeInMillis.YEAR); response.addCookie(tsc); }
From source file:com.liferay.portal.util.HttpImpl.java
protected Cookie toServletCookie(org.apache.commons.httpclient.Cookie commonsCookie) { Cookie cookie = new Cookie(commonsCookie.getName(), commonsCookie.getValue()); String domain = commonsCookie.getDomain(); if (Validator.isNotNull(domain)) { cookie.setDomain(domain);//from www . j a v a 2 s . co m } Date expiryDate = commonsCookie.getExpiryDate(); if (expiryDate != null) { int maxAge = (int) (expiryDate.getTime() - System.currentTimeMillis()); maxAge = maxAge / 1000; if (maxAge > -1) { cookie.setMaxAge(maxAge); } } String path = commonsCookie.getPath(); if (Validator.isNotNull(path)) { cookie.setPath(path); } cookie.setSecure(commonsCookie.getSecure()); cookie.setVersion(commonsCookie.getVersion()); return cookie; }