public static RMSBean setupNewClientSequence()

in modules/core/src/main/java/org/apache/sandesha2/util/SequenceManager.java [170:316]


	public static RMSBean setupNewClientSequence(MessageContext firstAplicationMsgCtx,
			String internalSequenceId, StorageManager storageManager) throws SandeshaException {
		if (log.isDebugEnabled())
			log.debug("Enter: SequenceManager::setupNewClientSequence");
		
		RMSBean rmsBean = new RMSBean();
		rmsBean.setInternalSequenceID(internalSequenceId);

		// If we are server-side, we use the details from the inbound sequence to help set
		// up the reply sequence.
		String inboundSequence = null;
		RMDBean inboundBean = null;
		if(firstAplicationMsgCtx.isServerSide()) {
			inboundSequence = (String) firstAplicationMsgCtx.getProperty(Sandesha2Constants.MessageContextProperties.INBOUND_SEQUENCE_ID);
			if(inboundSequence != null) {
				inboundBean = SandeshaUtil.getRMDBeanFromSequenceId(storageManager, inboundSequence);
				if (log.isDebugEnabled())
					log.debug("SequenceManager:: server side app msg: inboundBean=" + inboundBean);
			}
		}
		
		// Finding the spec version
		String specVersion = getSpecVersion(firstAplicationMsgCtx, storageManager);
		rmsBean.setRMVersion(specVersion);

		// Set up the To EPR
		EndpointReference toEPR = firstAplicationMsgCtx.getTo();

		if (toEPR == null) {
			String message = SandeshaMessageHelper.getMessage(SandeshaMessageKeys.toEPRNotValid, null);
			log.debug(message);
			throw new SandeshaException(message);
		}

		rmsBean.setToEndpointReference(toEPR);

		// Discover the correct acksTo and replyTo EPR for this RMSBean
		EndpointReference acksToEPR = null;
		EndpointReference replyToEPR = null;

		if (firstAplicationMsgCtx.isServerSide()) {
			// Server side, we want the replyTo and AcksTo EPRs to point into this server.
			// We can work that out by looking at the RMD bean that pulled the message in,
			// and copying its 'ReplyTo' address.
			EndpointReference strippedReplyToEpr = stripAddress(inboundBean.getReplyToEndpointReference());
			if(inboundBean != null && strippedReplyToEpr != null) {
				acksToEPR = strippedReplyToEpr;
				replyToEPR = strippedReplyToEpr;
		} else {
				String beanInfo = (inboundBean == null) ? "null" : inboundBean.toString();
				String message = SandeshaMessageHelper.getMessage(
						SandeshaMessageKeys.cannotChooseAcksTo, inboundSequence, beanInfo);
				SandeshaException e = new SandeshaException(message);
				if(log.isDebugEnabled()) log.debug("Throwing", e);
				throw e;
			}

		} else {
			replyToEPR = firstAplicationMsgCtx.getReplyTo();
			
			// If the replyTo is the none URI, we need to rewrite it as the
			// anon replyTo. Setting the value to null should have that effect.
			if(replyToEPR != null && replyToEPR.hasNoneAddress()) {
				replyToEPR = null;
			}

            // if this is an in only service invocation and if the user has set the
            // use seperate listner option then it should work as an dual channel model.
            if ((replyToEPR == null) && firstAplicationMsgCtx.getOptions().isUseSeparateListener() &&
                    firstAplicationMsgCtx.getAxisOperation().getMessageExchangePattern().equals(WSDL2Constants.MEP_URI_OUT_ONLY)){

                // first check whether the transport in is set or not
                TransportInDescription transportIn = firstAplicationMsgCtx.getTransportIn();
                if (transportIn == null) {
                    transportIn = firstAplicationMsgCtx.getOptions().getTransportIn();
                }

                //If use seperate listner is false then we have to use the annonymous end point.
                if ((transportIn == null) && firstAplicationMsgCtx.getOptions().isUseSeparateListener()) {
                    try {
                        transportIn = ClientUtils.inferInTransport(
                                firstAplicationMsgCtx.getConfigurationContext().getAxisConfiguration(),
                                firstAplicationMsgCtx.getOptions(),
                                firstAplicationMsgCtx);
                        replyToEPR = firstAplicationMsgCtx.getConfigurationContext().getListenerManager().getEPRforService(
                            firstAplicationMsgCtx.getAxisService().getName(),
                            firstAplicationMsgCtx.getAxisOperation().getName().getLocalPart(),
                            transportIn.getName());
                    } catch (AxisFault axisFault) {
                        throw new SandeshaException("Can not infer replyToEPR from the first message context ", axisFault);
                    }
                }
            }

            // For client-side sequences there are 3 options:
			// 1) An explict AcksTo, set via the client API
			// 2) The replyTo from the app message
			// 3) The anonymous URI (for which we can leave a null EPR)
			String acksTo = (String) firstAplicationMsgCtx.getProperty(SandeshaClientConstants.AcksTo);
			if (acksTo != null) {
				if (log.isDebugEnabled())
					log.debug("Using explicit AcksTo, addr=" + acksTo);
				acksToEPR = new EndpointReference(acksTo);
			} else if(replyToEPR != null) {
				if (log.isDebugEnabled())
					log.debug("Using replyTo EPR as AcksTo, addr=" + replyToEPR.getAddress());
				acksToEPR = replyToEPR;
			}
		}
		// In case either of the replyTo or AcksTo is anonymous, rewrite them using the AnonURI template
		//(this should be done only for RM 1.1)
		ConfigurationContext config = firstAplicationMsgCtx.getConfigurationContext();
		
		if (Sandesha2Constants.SPEC_VERSIONS.v1_1.equals(specVersion)) {
			replyToEPR = SandeshaUtil.rewriteEPR(rmsBean, replyToEPR, config);
			acksToEPR = SandeshaUtil.rewriteEPR(rmsBean, acksToEPR, config);
		}
		
		// Store both the acksTo and replyTo 
		if(replyToEPR != null) rmsBean.setReplyToEndpointReference(replyToEPR);
		if(acksToEPR  != null) rmsBean.setAcksToEndpointReference(acksToEPR);
		
		// New up the client completed message ranges list
		rmsBean.setClientCompletedMessages(new RangeString());

		// saving transportTo value;
		String transportTo = (String) firstAplicationMsgCtx.getProperty(Constants.Configuration.TRANSPORT_URL);
		if (transportTo != null) {
			rmsBean.setTransportTo(transportTo);
		}

		// Set the soap version use by this client
		rmsBean.setSoapVersion(SandeshaUtil.getSOAPVersion(firstAplicationMsgCtx.getEnvelope()));

		//setting the autoTermination property for the client side.
		if (!firstAplicationMsgCtx.isServerSide()) {
			Object avoidAutoTermination = firstAplicationMsgCtx.getProperty(SandeshaClientConstants.AVOID_AUTO_TERMINATION);
			if (avoidAutoTermination!=null && JavaUtils.isTrueExplicitly(avoidAutoTermination))
				rmsBean.setAvoidAutoTermination(true);
		}
		// updating the last activated time.
		rmsBean.setLastActivatedTime(System.currentTimeMillis());
		
		if (log.isDebugEnabled())
			log.debug("Exit: SequenceManager::setupNewClientSequence " + rmsBean);
		return rmsBean;
	}