001: /*
002: * $Id: Token.java 497654 2007-01-19 00:21:57Z rgielen $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021: package org.apache.struts2.components;
022:
023: import java.util.Map;
024:
025: import javax.servlet.http.HttpServletRequest;
026: import javax.servlet.http.HttpServletResponse;
027:
028: import org.apache.struts2.views.annotations.StrutsTag;
029: import org.apache.struts2.util.TokenHelper;
030:
031: import com.opensymphony.xwork2.util.ValueStack;
032:
033: /**
034: * <!-- START SNIPPET: javadoc -->
035: * Stop double-submission of forms.</p>
036: *
037: * The token tag is used to help with the "double click" submission problem. It is needed if you are using the
038: * TokenInterceptor or the TokenSessionInterceptor. The s:token tag merely places a hidden element that contains
039: * the unique token.</p>
040: * <!-- END SNIPPET: javadoc -->
041: *
042: * <p/> <b>Examples</b>
043: *
044: * <pre>
045: * <!-- START SNIPPET: example -->
046: * <s:token />
047: * <!-- END SNIPPET: example -->
048: * </pre>
049: *
050: * @see org.apache.struts2.interceptor.TokenInterceptor
051: * @see org.apache.struts2.interceptor.TokenSessionStoreInterceptor
052: *
053: */
054: @StrutsTag(name="token",tldTagClass="org.apache.struts2.views.jsp.ui.TokenTag",description="Stop double-submission of forms")
055: public class Token extends UIBean {
056:
057: public static final String TEMPLATE = "token";
058:
059: public Token(ValueStack stack, HttpServletRequest request,
060: HttpServletResponse response) {
061: super (stack, request, response);
062: }
063:
064: protected String getDefaultTemplate() {
065: return TEMPLATE;
066: }
067:
068: /**
069: * First looks for the token in the PageContext using the supplied name (or {@link org.apache.struts2.util.TokenHelper#DEFAULT_TOKEN_NAME}
070: * if no name is provided) so that the same token can be re-used for the scope of a request for the same name. If
071: * the token is not in the PageContext, a new Token is created and set into the Session and the PageContext with
072: * the name.
073: */
074: protected void evaluateExtraParams() {
075: super .evaluateExtraParams();
076:
077: String tokenName;
078: Map parameters = getParameters();
079:
080: if (parameters.containsKey("name")) {
081: tokenName = (String) parameters.get("name");
082: } else {
083: if (name == null) {
084: tokenName = TokenHelper.DEFAULT_TOKEN_NAME;
085: } else {
086: tokenName = findString(name);
087:
088: if (tokenName == null) {
089: tokenName = name;
090: }
091: }
092:
093: addParameter("name", tokenName);
094: }
095:
096: String token = buildToken(tokenName);
097: addParameter("token", token);
098: addParameter("tokenNameField", TokenHelper.TOKEN_NAME_FIELD);
099: }
100:
101: /**
102: * This will be removed in a future version of Struts.
103: * @deprecated Templates should use $parameters from now on, not $tag.
104: */
105: public String getTokenNameField() {
106: return TokenHelper.TOKEN_NAME_FIELD;
107: }
108:
109: private String buildToken(String name) {
110: Map context = stack.getContext();
111: Object myToken = context.get(name);
112:
113: if (myToken == null) {
114: myToken = TokenHelper.setToken(name);
115: context.put(name, myToken);
116: }
117:
118: return myToken.toString();
119: }
120: }
|