/*
 * 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 java.io.IOException;

import org.omg.CORBA.SystemException;
import org.omg.CORBA.CompletionStatus;

import com.sun.corba.se.impl.core.Response;
import com.sun.corba.se.impl.core.ClientResponse;
import com.sun.corba.se.impl.corba.IOR;
import com.sun.corba.se.impl.core.ORB;
import com.sun.corba.se.impl.core.ServiceContext;
import com.sun.corba.se.impl.core.ServiceContexts;
import com.sun.corba.se.impl.protocol.giopmsgheaders.Message;
import com.sun.corba.se.impl.protocol.giopmsgheaders.ReplyMessage;
import com.sun.corba.se.impl.orbutil.MinorCodes;

class LocalClientResponseImpl extends IIOPInputStream implements ClientResponse
{
    LocalClientResponseImpl(ORB orb, byte[] buf, ReplyMessage header)
    {
	super(orb, buf, header.getSize(), header.isLittleEndian(), header, null);

        this.reply = header;

        // NOTE (Ram J) (06/02/2000) if we set result.setIndex(bodyBegin)
        // in LocalServerResponse.getClientResponse(), then we do not need
        // to read the headers (done below) 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) This has been moved from
        // LocalServerResponse.getClientResponse()
        // 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.
        this.setIndex(Message.GIOPMessageHeaderLength);

        // BUGFIX(Ram Jeyaraman) For local invocations, the reply mesg fields
        // needs to be set, by reading the response buffer contents
        // to correctly set the exception type and other info.
        this.reply.read(this);
    }

    LocalClientResponseImpl(SystemException ex)
    {
	this.systemException = ex;
    }

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

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

    public boolean isLocationForward() {
        if ( reply != null ) {
            return ( (reply.getReplyStatus() == ReplyMessage.LOCATION_FORWARD) ||
                     (reply.getReplyStatus() == ReplyMessage.LOCATION_FORWARD_PERM) );
            //return reply.getReplyStatus() == ReplyMessage.LOCATION_FORWARD;
        } else {
            return false;
        }
    }
    
    public boolean isDifferentAddrDispositionRequested() {
        if (reply != null) {
            return reply.getReplyStatus() == ReplyMessage.NEEDS_ADDRESSING_MODE;
        }
    
        return false;
    }
        
    public short getAddrDisposition() {
        if (reply != null) {
            return reply.getAddrDisposition();
        }
        
        throw new org.omg.CORBA.INTERNAL(
            "Null reply in getAddrDisposition",
            MinorCodes.NULL_REPLY_IN_GET_ADDR_DISPOSITION,        
            CompletionStatus.COMPLETED_MAYBE);
    }
        
    public IOR getForwardedIOR() {
	if ( reply != null )
	    return reply.getIOR();
	else
	    return null;
    }

    public int getRequestId() {
	if ( reply != null )
	    return reply.getRequestId();
	else
	    throw new org.omg.CORBA.INTERNAL("Error in getRequestId");
    }

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

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

    public java.lang.String peekUserExceptionId() {
        mark(Integer.MAX_VALUE);
        String result = read_string();
        reset();
        return result;
    }

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

    private ReplyMessage reply;
    private SystemException systemException;
}
