All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.struts2.interceptor.CookieProviderInterceptor Maven / Gradle / Ivy

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under 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 org.apache.struts2.interceptor;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import com.opensymphony.xwork2.interceptor.PreResultListener;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
import org.apache.struts2.StrutsStatics;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.util.Set;

/**
 * 
 * Allows actions to send cookies to client, action must implement {@link CookieProvider}
 * You must reference this interceptor in your default stack or in action's stack, see example below.
 *
 * 
 *
 * 
 *
 * none
 *
 * 
 *
 * 
 *
 * 
    *
  • addCookiesToResponse - this method applies cookie created by action to response
  • *
* * * *
 * 
 *
 * <action ... >
 *   <interceptor-ref name="defaultStack"/>
 *   <interceptor-ref name="cookieProvider"/>
 *   ...
 * </action>
 *
 * 
 * 
*/ public class CookieProviderInterceptor extends AbstractInterceptor implements PreResultListener { private static final Logger LOG = LogManager.getLogger(CookieProviderInterceptor.class); public String intercept(ActionInvocation invocation) throws Exception { invocation.addPreResultListener(this); return invocation.invoke(); } /** * Do what name suggests * * @param action {@link CookieProvider} action * @param response current {@link HttpServletResponse} */ protected void addCookiesToResponse(CookieProvider action, HttpServletResponse response) { Set cookies = action.getCookies(); if (cookies != null) { for (Cookie cookie : cookies) { if (LOG.isDebugEnabled()) { LOG.debug("Sending cookie [{}] with value [{}] for domain [{}]", cookie.getName(), cookie.getValue(), (cookie.getDomain() != null ? cookie.getDomain() : "no domain")); } response.addCookie(cookie); } } } public void beforeResult(ActionInvocation invocation, String resultCode) { try { LOG.trace("beforeResult start"); ActionContext ac = invocation.getInvocationContext(); if (invocation.getAction() instanceof CookieProvider) { HttpServletResponse response = (HttpServletResponse) ac.get(StrutsStatics.HTTP_RESPONSE); addCookiesToResponse((CookieProvider) invocation.getAction(), response); } LOG.trace("beforeResult end"); } catch (Exception ex) { LOG.error("Unable to setup cookies", ex); } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy