001/* 002 * (C) Copyright 2006-2007 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * Nuxeo - initial API and implementation 018 * 019 * $Id: JOOoConvertPluginImpl.java 18651 2007-05-13 20:28:53Z sfermigier $ 020 */ 021 022package org.nuxeo.ecm.webapp.helpers; 023 024import static org.jboss.seam.ScopeType.SESSION; 025 026import java.io.Serializable; 027import java.util.Map; 028 029import javax.faces.context.FacesContext; 030 031import org.jboss.seam.annotations.In; 032import org.jboss.seam.annotations.Name; 033import org.jboss.seam.annotations.Scope; 034import org.jboss.seam.annotations.Startup; 035import org.jboss.seam.core.Manager; 036 037@Startup 038@Name("conversationIdGenerator") 039@Scope(SESSION) 040public class ConversationIdGenerator implements Serializable { 041 042 private static final long serialVersionUID = 15643987456876L; 043 044 private static final String MAIN_CONVERSATION_PREFIX = "0NXMAIN"; 045 046 private static final String CREATE_CONVERSATION_PREFIX = "0NXCREATE"; 047 048 private int mainConversationCounter = 0; 049 050 private int createConversationCounter = 0; 051 052 @In(value = "org.jboss.seam.core.manager") 053 public transient Manager conversationManager; 054 055 public String getNextMainConversationId() { 056 String newMainConversationId; 057 if (mainConversationCounter == 0) { 058 newMainConversationId = MAIN_CONVERSATION_PREFIX; 059 } else { 060 newMainConversationId = MAIN_CONVERSATION_PREFIX + mainConversationCounter; 061 } 062 mainConversationCounter += 1; 063 return newMainConversationId; 064 } 065 066 public String getNextCreateConversationId() { 067 String newCreateConversationId; 068 if (createConversationCounter == 0) { 069 newCreateConversationId = CREATE_CONVERSATION_PREFIX; 070 } else { 071 newCreateConversationId = CREATE_CONVERSATION_PREFIX + createConversationCounter; 072 } 073 createConversationCounter += 1; 074 return newCreateConversationId; 075 } 076 077 public String getCurrentOrNewMainConversationId() { 078 079 // this case can happend if user logged in from a bookmarked URL that 080 // contains conversation ID 081 String existingConversationId = getConversationIdInURL(); 082 if (existingConversationId != null && existingConversationId.startsWith(MAIN_CONVERSATION_PREFIX)) { 083 return existingConversationId; 084 } 085 086 if (conversationManager.isReallyLongRunningConversation()) { 087 existingConversationId = conversationManager.getCurrentConversationId(); 088 if (existingConversationId.startsWith(MAIN_CONVERSATION_PREFIX)) { 089 return existingConversationId; 090 } else { 091 return getNextMainConversationId(); 092 } 093 } else { 094 return getNextMainConversationId(); 095 } 096 } 097 098 protected String getConversationIdInURL() { 099 final FacesContext facesContext = FacesContext.getCurrentInstance(); 100 if (facesContext == null) { 101 return null; 102 } 103 Map<String, Object> rMap = facesContext.getExternalContext().getRequestMap(); 104 String conversationIdInUrl; 105 if (rMap.containsKey(conversationManager.getConversationIdParameter())) { 106 conversationIdInUrl = (String) rMap.get(conversationManager.getConversationIdParameter()); 107 } else { 108 Map<String, String> pMap = facesContext.getExternalContext().getRequestParameterMap(); 109 if (pMap.containsKey(conversationManager.getConversationIdParameter())) { 110 conversationIdInUrl = pMap.get(conversationManager.getConversationIdParameter()); 111 } else { 112 return null; 113 } 114 } 115 return conversationIdInUrl; 116 } 117 118}