[JBoss Portal] - Re: additional fields on the login page
by Antoine_h
Should not be easy in a proper way.
1) Look at the Tomcat Authentification process.
look how the UserName and Password from the form are passed to the Jaas login module.
and then add a parameter and get it in the jaas login module, then up to the portlet (see 2).
but you may have to modify Tomcat... which is not recommended.
may be all the parameters from the form are passed to the login module. Then do as in 2) to retrieve them.
2) add the parameter to the username (concatenation with a special separator), then create your own jaas login module (similar to the one provided), and parse the username to retrieve the name and parameter.
use javascript to concatenate the name with the extra parameter.
In the jaas login module : make your own Principal (inherited from java Principal), with the added parameters. In the porlet, retrieve the parameter from your Principal.
3) in the html form : add some javascript to write a cookie with the parameter. Then, in the javascript, send the form httprequest.
in the portlet render method, retrieve the cookie (it should be in the list of cookies in the httprequest). use the helper in this post :
http://jboss.org/index.html?module=bb&op=viewtopic&t=104250
just curious : parameter what for ?
View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4028770#4028770
Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4028770
19Â years, 1Â month
[JBoss Portal] - HttpServletRequest, HttpServletResponse, Session, Cookie fro
by Antoine_h
Here some code to get and use the HttpServletRequest, HttpServletResponse, HttpSession, read and write Cookies from a PortletRequest (in processAction method or doView).
This is not compliant with JSR-168. And is "as is", and may not work.
| import java.util.logging.Logger;
|
| import javax.portlet.PortletRequest;
| import javax.servlet.http.Cookie;
| import javax.servlet.http.HttpServletRequest;
| import javax.servlet.http.HttpServletResponse;
|
| import org.jboss.portal.portlet.aspects.portlet.ContextDispatcherInterceptor;
| import org.jboss.portal.portlet.impl.spi.AbstractInvocationContext;
| import org.jboss.portal.portlet.invocation.PortletInvocation;
| import org.jboss.portal.portlet.spi.InvocationContext;
|
| /**
| * A class to help manage the HttpServletRequest and Session of the portal.
| *
| */
| public class HttpServletRequestHelper {
| private static final Logger log = Logger
| .getLogger(HttpServletRequestHelper.class.getName());
|
|
| /***************************************************************************
| * For getting and using the HttpServletRequest and HttpServletResponse.
| **************************************************************************/
| /**
| * This method retieve the {@link HttpServletRequest} from the
| * {@link PortletRequest}. It call first the
| * {@link #getAbstractInvocationContext(PortletRequest, String)} to get the
| * {@link AbstractInvocationContext}, then retrive the HttpServletRequest
| * from it.
| *
| * <p>
| * See the code for details of how it retrieves it.
| * </p>
| *
| * @param request
| * The PortletRequest.
| * @param msgLogPrefix
| * The message prefix for all log, to tell from where this
| * helping method was called.
| * @return
| */
| public static final HttpServletRequest getHttpServletRequest(
| PortletRequest request, String msgLogPrefix) {
| // Try to cast the context of invocation to an AbstractInvocationContext
| AbstractInvocationContext abstractInvCtxt = getAbstractInvocationContext(
| request, msgLogPrefix);
| HttpServletRequest httpServletRequest = null;
| if (abstractInvCtxt != null) {
| // The AbstractInvocationContext exists : try to retrieve the client
| // httpServletRequest.
| httpServletRequest = abstractInvCtxt.getClientRequest();
| if (httpServletRequest == null) {
| log.warning(msgLogPrefix + " The HttpServletRequest is null !");
| }
| } else {
| log.warning(msgLogPrefix
| + " The AbstractInvocationContext is null !");
| }
| return httpServletRequest;
| }
|
| /**
| * This method retieve the {@link HttpServletRequest} from the
| * {@link PortletRequest}. It call first the
| * {@link #getAbstractInvocationContext(PortletRequest, String)} to get the
| * {@link AbstractInvocationContext}, then retrive the HttpServletResponse
| * from it.
| *
| * <p>
| * See the code for details of how it retrieves it.
| * </p>
| *
| * @param request
| * The PortletRequest.
| * @param msgLogPrefix
| * The message prefix for all log, to tell from where this
| * helping method was called.
| * @return
| */
| public static final HttpServletResponse getHttpServletResponse(
| PortletRequest request, String msgLogPrefix) {
| // Try to cast the context of invocation to an AbstractInvocationContext
| AbstractInvocationContext abstractInvCtxt = getAbstractInvocationContext(
| request, msgLogPrefix);
| HttpServletResponse httpServletResponse = null;
| if (abstractInvCtxt != null) {
| // The AbstractInvocationContext exists : try to retrieve the client
| // httpServletRequest.
| httpServletResponse = abstractInvCtxt.getClientResponse();
| if (httpServletResponse == null) {
| log
| .warning(msgLogPrefix
| + " The HttpServletResponse is null !");
| }
| } else {
| log.warning(msgLogPrefix
| + " The AbstractInvocationContext is null !");
| }
| return httpServletResponse;
| }
|
| /**
| * This method retieve the {@link AbstractInvocationContext} from the
| * {@link PortletRequest}. The {@link AbstractInvocationContext} is used to
| * get the ClientRequest etc...
| * <p>
| * See the code for details of how it retrieves it.
| * </p>
| *
| * @param request
| * The PortletRequest.
| * @param msgLogPrefix
| * The message prefix for all log, to tell from where this
| * helping method was called.
| * @return
| */
| public static final AbstractInvocationContext getAbstractInvocationContext(
| PortletRequest request, String msgLogPrefix) {
| PortletInvocation portletInvocation = (PortletInvocation) request
| .getAttribute(ContextDispatcherInterceptor.REQ_ATT_COMPONENT_INVOCATION);
| // return portletInvocation.getDispatchedRequest();
|
| // Get the context of the invocation
| InvocationContext invocationContext = portletInvocation.getContext();
|
| // Try to cast the context of invocation to an AbstractInvocationContext
| AbstractInvocationContext abstractInvCtxt = null;
| if (invocationContext != null) {
| if (invocationContext instanceof AbstractInvocationContext) {
| abstractInvCtxt = (AbstractInvocationContext) invocationContext;
| } else {
| log
| .warning(msgLogPrefix
| + " The InvocationContext is not of instance of AbstractInvocationContext !");
| log.warning(msgLogPrefix + " The InvocationContext is : "
| + invocationContext + " // "
| + invocationContext.getClass().getName() + " // "
| + invocationContext.toString());
| }
|
| } else {
| log.warning(msgLogPrefix + " The invocationContext is null !");
| }
| return abstractInvCtxt;
| }
| /***************************************************************************
| * End of For getting and using the HttpServletRequest and
| * HttpServletResponse.
| **************************************************************************/
| /***************************************************************************
| * Set and retrieve a Cookie
| **************************************************************************/
| /**
| * Return the cookie with name cookieName, from the
| * {@link HttpServletRequest}, or null if not found. Does not check for the path
| *
| * @param httpRequest
| * @param cookieName
| * The name of the cookie to look for
| * @param msgLogPrefix
| * The message prefix for all log, to tell from where this
| * helping method was called.
| * @return The cookie, or null if not found.
| */
| public static Cookie getCookie(HttpServletRequest httpRequest,
| String cookieName, String cookiePath, String msgLogPrefix) {
| Cookie cookieRet = null;
| Cookie[] cookieTab = httpRequest.getCookies();
|
| // log.warning(msgLogPrefix + " The list of cookies :"
| // + msgCookieListAsString(cookieTab));
|
| if (cookieTab != null) {
| for (int i = 0; i < cookieTab.length; i++) {
| if (cookieTab.getName().compareTo(cookieName) == 0) {
| // log.warning(msgLogPrefix + " i = " + i
| // + " same name cookieTab.getName()="
| // + cookieTab.getName());
| // log.warning(msgLogPrefix + " i = " + i
| // + " PATH cookieTab.getPath()="
| // + cookieTab.getPath());
|
| cookieRet = cookieTab;
| break;
| }
| }
| // } else {
| // log
| // .warning(msgLogPrefix
| // + " The List of cookies is null. From HttpServletRequest :
| // LocalAddr="
| // + httpRequest.getLocalAddr() + ", and LocalName="
| // + httpRequest.getLocalName() + ", and user="
| // + httpRequest.getRemoteUser());
| }
| return cookieRet;
| }
|
| /**
| * Store a cookie.
| *
| * @param httpResponse
| * @param cookieName
| * @param cookiePath
| * @param cookieValue
| * @param maxAgeSeconds
| * The cookie will expire after that many seconds have passed.
| * See {@link Cookie#setMaxAge(int)}. One year = 60*60*24*365 =
| * 31536000 sec.
| * @param msgLogPrefix
| */
| public static void storeCookie(HttpServletResponse httpResponse,
| String cookieName, String cookiePath, String cookieValue,
| int maxAgeSeconds, String msgLogPrefix) {
| Cookie cookie = new Cookie(cookieName, cookieValue);
| cookie.setMaxAge(maxAgeSeconds); // one year = 60*60*24*365
| // log.warning(msgLogPrefix + " cookie.setMaxAge = " +
| // cookie.getMaxAge()
| // + ", and LocalName=" + "");
|
| cookie.setPath(cookiePath);
|
| httpResponse.addCookie(cookie);
| }
|
| /**
| * Return the log message of a cookie : name, path, value, maxAge.
| *
| * @param cookie
| * @return
| */
| public static String msgCookieAsString(Cookie cookie) {
| String msgRet = "Cookie[" + cookie.getName() + "], path=["
| + cookie.getPath() + "]" + ", value=" + cookie.getValue()
| + ", maxAge=" + cookie.getMaxAge() + ", domain="
| + cookie.getDomain() + "";
| return msgRet;
| }
|
| /**
| * Return the log message of the list of cookie.
| *
| * @param cookieTab
| * @return
| */
| public static String msgCookieListAsString(Cookie[] cookieTab) {
| String msgRet = null;
| if (cookieTab == null) {
| msgRet = "Cookie list is null !";
| return msgRet;
| } else {
| msgRet = "Cookie list size=" + cookieTab.length;
| }
| for (int i = 0; i < cookieTab.length; i++) {
| msgRet = msgRet + "\ni=" + i + ", "
| + msgCookieAsString(cookieTab);
| }
| return msgRet;
| }
|
| /***************************************************************************
| * End of Set and retrieve a Cookie
| **************************************************************************/
|
| }
|
View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4028767#4028767
Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4028767
19Â years, 1Â month