/*
 * Copyright (c) 1999, 2003, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Oracle designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Oracle in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

package com.sun.corba.se.impl.iiop;

import org.omg.CORBA.SystemException;

import com.sun.corba.se.impl.core.ServerResponse;
import com.sun.corba.se.impl.core.ORB;
import com.sun.corba.se.impl.corba.IOR;
import com.sun.corba.se.impl.core.ServiceContext;
import com.sun.corba.se.impl.core.ServiceContexts;
import com.sun.corba.se.impl.core.ClientResponse;
import com.sun.corba.se.impl.protocol.giopmsgheaders.MessageBase;
import com.sun.corba.se.impl.protocol.giopmsgheaders.ReplyMessage;

class LocalServerResponseImpl
    extends     IIOPOutputStream
    implements  ServerResponse
{
    LocalServerResponseImpl(LocalServerRequestImpl request, ServiceContexts svc)
    {
        this(request,
            MessageBase.createReply(
                            (ORB)request.orb(),
                            request.getGIOPVersion(),
                            request.getRequestId(), ReplyMessage.NO_EXCEPTION,
                            svc, null),
            null);
    }

    LocalServerResponseImpl(LocalServerRequestImpl request, ServiceContexts svc,
            boolean user)
    {
        this(request,
            MessageBase.createReply(
                            (ORB)request.orb(),
                            request.getGIOPVersion(), request.getRequestId(),
                            user ? ReplyMessage.USER_EXCEPTION :
                                   ReplyMessage.SYSTEM_EXCEPTION,
                            svc, null),
            null);
    }

    LocalServerResponseImpl( LocalServerRequestImpl request, ReplyMessage reply,
			     IOR ior)
    {
        super(request.getGIOPVersion(),
              (ORB)request.orb(), 
              null, 
              BufferManagerFactory.newBufferManagerWrite(BufferManagerFactory.GROW),
              request.getStreamFormatVersionForReply());

        setMessage(reply);

	ORB orb = (ORB)request.orb();

	ServerResponseImpl.runServantPostInvoke(orb, request);

	if( request.executePIInResponseConstructor() ) {
	    // Invoke server request ending interception points (send_*):
	    // Note: this may end up with a SystemException or an internal
	    // Runtime ForwardRequest.
	    orb.getPIHandler().invokeServerPIEndingPoint( reply );

	    // Note this will be executed even if a ForwardRequest or
	    // SystemException is thrown by a Portable Interceptors ending 
	    // point since we end up in this constructor again anyway.
	    orb.getPIHandler().cleanupServerPIRequest();

	    // See (Local)ServerRequestImpl.createSystemExceptionResponse
	    // for why this is necesary.
	    request.setExecutePIInResponseConstructor(false);
	}

	// Once you get here then the final reply is available (i.e.,
	// postinvoke and interceptors have completed.
	if (request.executeRemoveThreadInfoInResponseConstructor()) {
	    ServerResponseImpl.removeThreadInfo(orb, request);
	}
        
	reply.write(this);
	if (reply.getIOR() != null)
	    reply.getIOR().write(this);

        this.reply = reply;
        this.ior = reply.getIOR();
    }

    public boolean isSystemException() {
        if (reply != null)
            return reply.getReplyStatus() == ReplyMessage.SYSTEM_EXCEPTION;
        return false;
    }

    public boolean isUserException() {
        if (reply != null)
            return reply.getReplyStatus() == ReplyMessage.USER_EXCEPTION;
        return false;
    }

    public boolean isLocationForward() {
        if (ior != null)
            return true;
        return false;
    }

    public IOR getForwardedIOR() {
    	return ior;
    }

    public int getRequestId() {
        if (reply != null)
            return reply.getRequestId();
        return -1;
    }

    public ServiceContexts getServiceContexts() {
        if (reply != null)
            return reply.getServiceContexts();
        return null;
    }

    public SystemException getSystemException() {
        if (reply != null)
            return reply.getSystemException();
        return null;
    }

    public ReplyMessage getReply()
    {
    	return reply ;
    }

    public ClientResponse getClientResponse()
    {
        // set the size of the marshalled data in the message header
        getMessage().setSize(getByteBuffer(), getSize());

        // Construct a new ClientResponse out of the buffer in this ClientRequest
        LocalClientResponseImpl result =
            new LocalClientResponseImpl( (ORB)orb(), toByteArray(), reply);

        // NOTE (Ram J) (06/02/2000) if we set result.setIndex(bodyBegin) here
        // then the LocalClientResponse does not need to read the headers anymore.
        // This will be an optimisation which is can be done to speed up the
        // local invocation by avoiding reading the headers in the local cases.

        // BUGFIX(Ram Jeyaraman) result.setOffset is now done in
        // LocalClientResponseImpl constructor.
        /*
          // Skip over all of the GIOP header information.  This positions
          // the offset in the buffer so that the skeleton can correctly read
          // the marshalled arguments.
          result.setOffset( bodyBegin ) ;
        */

        return result ;
    }

    /**
     * Check to see if the response is local.
     */
    public boolean isLocal(){
        return true;
    }

    private ReplyMessage reply;
    private IOR ior; // forwarded IOR
}
