001: /* ConversationUtil.java
002: {{IS_NOTE
003: Purpose:
004:
005: Description:
006:
007: History:
008: Jul 25, 2007 10:03:38 AM , Created by Dennis Chen
009: }}IS_NOTE
010:
011: Copyright (C) 2007 Potix Corporation. All Rights Reserved.
012:
013: {{IS_RIGHT
014: This program is distributed under GPL Version 2.0 in the hope that
015: it will be useful, but WITHOUT ANY WARRANTY.
016: }}IS_RIGHT
017: */
018: package org.zkoss.seam;
019:
020: import java.io.UnsupportedEncodingException;
021: import java.net.URLEncoder;
022: import java.util.Map;
023:
024: import org.jboss.seam.core.Manager;
025:
026: /**
027: * This class helps developers to do thing with Seam's Conversation
028: * @author Dennis.Chen
029: */
030: public class ConversationUtil {
031:
032: /**
033: * Append current Long Running Conversation's id to url.
034: * @param url
035: * @return if exist a long running conversation the new url which contains a long running conversation id will be return.
036: * other wish return url directly.
037: */
038: static public String appendLongRunningConversation(String url) {
039: if (!Manager.instance().isLongRunningConversation())
040: return url;
041: String parmname = Manager.instance()
042: .getConversationIdParameter();
043: String conversationId = Manager.instance()
044: .getCurrentConversationId();
045: if (containsParameter(url, parmname))
046: return url;
047: int loc = url.indexOf('?');
048: if (loc > 0) {
049: try {
050: url += "&" + parmname + "="
051: + URLEncoder.encode(conversationId, "UTF-8");
052: } catch (UnsupportedEncodingException e) {
053: throw new RuntimeException(e.getMessage(), e);
054: }
055: } else {
056: try {
057: url += "?" + parmname + "="
058: + URLEncoder.encode(conversationId, "UTF-8");
059: } catch (UnsupportedEncodingException e) {
060: throw new RuntimeException(e.getMessage(), e);
061: }
062: }
063: return url;
064: }
065:
066: /**package**/
067: static String encodeParameters(String url,
068: Map<String, Object> parameters) {
069: if (parameters.isEmpty())
070: return url;
071:
072: StringBuilder builder = new StringBuilder(url);
073: for (Map.Entry<String, Object> param : parameters.entrySet()) {
074: String parameterName = param.getKey();
075: if (!containsParameter(url, parameterName)) {
076: Object parameterValue = param.getValue();
077: if (parameterValue instanceof Iterable) {
078: for (Object value : (Iterable) parameterValue) {
079: builder.append('&').append(parameterName)
080: .append('=');
081: if (value != null) {
082: builder.append(encode(value));
083: }
084: }
085: } else {
086: builder.append('&').append(parameterName).append(
087: '=');
088: if (parameterValue != null) {
089: builder.append(encode(parameterValue));
090: }
091: }
092: }
093: }
094: if (url.indexOf('?') < 0) {
095: builder.setCharAt(url.length(), '?');
096: }
097: return builder.toString();
098: }
099:
100: /**package**/
101: static boolean containsParameter(String url, String parameterName) {
102: return url.indexOf('?' + parameterName + '=') > 0
103: || url.indexOf('&' + parameterName + '=') > 0;
104: }
105:
106: /**package**/
107: static String encode(Object value) {
108: try {
109: return URLEncoder.encode(String.valueOf(value), "UTF-8");
110: } catch (UnsupportedEncodingException iee) {
111: throw new RuntimeException(iee);
112: }
113: }
114: }
|