alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Axis 2 example source code file (MessageContext.java)

This example Axis 2 source code file (MessageContext.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Axis 2 tags/keywords

axisfault, axisfault, boolean, delegate_to_axismc, delegate_to_axismc, hashmap, hashmap, iterator, mepcontext, object, object, operationdescription, string, throwable, util, xml

The Axis 2 MessageContext.java source code

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.apache.axis2.jaxws.core;

import org.apache.axis2.AxisFault;
import org.apache.axis2.description.AxisService;
import org.apache.axis2.jaxws.description.EndpointDescription;
import org.apache.axis2.jaxws.description.OperationDescription;
import org.apache.axis2.jaxws.handler.MEPContext;
import org.apache.axis2.jaxws.message.Message;
import org.apache.axis2.jaxws.message.util.MessageUtils;

import javax.xml.namespace.QName;
import javax.xml.ws.BindingProvider;
import javax.xml.ws.Service.Mode;
import javax.xml.ws.WebServiceException;
import java.util.HashMap;
import java.util.Map;
import java.util.AbstractMap;
import java.util.Collection;
import java.util.Iterator;
import java.util.Set;

/**
 * The <code>org.apache.axis2.jaxws.core.MessageContext is an interface that extends the
 * JAX-WS 2.0 <code>javax.xml.ws.handler.MessageContext defined in the spec.  This
 * encapsulates all of the functionality needed of the MessageContext for the other JAX-WS spec
 * pieces (the handlers for example) and also provides the needed bits of contextual information for
 * the rest of the JAX-WS implementation.
 * <p/>
 * Specifically, this is responsible for providing APIs so that the client and server implementation
 * portions can get to the Message, defined by the Message Model format and also any metadata that
 * is available.
 */
public class MessageContext {

    private InvocationContext invocationCtx;
    private org.apache.axis2.context.MessageContext axisMsgCtx;
    private Map<String, Object> properties;
    private EndpointDescription endpointDesc;
    private OperationDescription operationDesc;
    private QName operationName;    //FIXME: This should become the OperationDescription
    private Message message;
    private Mode mode;
    private boolean isOutbound;  // Outbound or inbound message context
    
    // TODO:  flag to set whether we delegate property setting up to the
    // axis2 message context object or keep it local
    private boolean DELEGATE_TO_AXISMC = true;
    
    /*
     * JAXWS runtime uses a request and response mc, but we need to know the pair.
     * We will use this mepCtx as a wrapper to the request and response message contexts
     * where the requestMC and responseMC have the same parent MEPContext to
     * preserve the relationship.
     */
    private MEPContext mepCtx;

    // If a local exception is thrown, the exception is placed on the message context.
    // It is not converted into a Message.
    private Throwable localException = null;
    private AxisFault causedByException = null;

    /**
     * Construct a MessageContext without a prior Axis2 MessageContext
     * (usage outbound dispatch/proxy)
     */
    public MessageContext() {
        axisMsgCtx = new org.apache.axis2.context.MessageContext();
        isOutbound = true;
        if (!DELEGATE_TO_AXISMC) {
            properties = new HashMap<String, Object>();
        }
           
    }
    
    /**
     * Construct a MessageContext with a prior MessageContext
     * (usage inbound client/server or outbound server)
     * @param mc
     * @throws WebServiceException
     */
    public MessageContext(org.apache.axis2.context.MessageContext mc) throws WebServiceException {
        if (!DELEGATE_TO_AXISMC) {
            properties = new HashMap<String, Object>();
        }
        // Assume inbound (caller must setOutbound)
        isOutbound = false;

        /*
         * Instead of creating a member MEPContext object every time, we will
         * rely on users of this MessageContext class to create a new
         * MEPContext and call setMEPContext(MEPContext)
         */
        
        if (mc != null) {
            axisMsgCtx = mc;
            message = MessageUtils.getMessageFromMessageContext(mc);
            if (message != null) {
                message.setMessageContext(this);
            }
        } else {
            axisMsgCtx = new org.apache.axis2.context.MessageContext();
        }
    }

    public InvocationContext getInvocationContext() {
        return invocationCtx;
    }

    public void setInvocationContext(InvocationContext ic) {
        invocationCtx = ic;
    }

    public Map<String, Object> getProperties() {
        if (DELEGATE_TO_AXISMC) {
            // only use properties that are local to the axis2 MC,
            // not the options bag.  See org.apache.axis2.context.AbstractContext
            Iterator names = axisMsgCtx.getPropertyNames();
            HashMap tempProps = new HashMap<String, Object>();
            for (; names.hasNext();) {
                String name = (String)names.next();
                tempProps.put(name, axisMsgCtx.getProperty(name));
            }
            //return new ReadOnlyProperties(tempProps);
            return tempProps;
        }
        return properties;
    }
    
    public void setProperties(Map<String, Object> _properties) {
        if (DELEGATE_TO_AXISMC) {
            // make sure copy is made, not just reference:
            _properties.put(org.apache.axis2.context.MessageContext.COPY_PROPERTIES, true);
            axisMsgCtx.setProperties(_properties);
        } else {
            getProperties().putAll(_properties);
        }
    }
    
    public Object getProperty(String key) {
        if (DELEGATE_TO_AXISMC) {
            // only use properties that are local to the axis2 MC,
            // not the options bag.  See org.apache.axis2.context.AbstractContext
            Iterator names = axisMsgCtx.getPropertyNames();
            for (; names.hasNext();) {
                String name = (String)names.next();
                if (name.equals(key)) {
                    return axisMsgCtx.getProperty(key);
                }
            }
            return null;
        }
        return getProperties().get(key);
    }
    
    // acts like Map.put(key, value)
    public Object setProperty(String key, Object value) {
        if (DELEGATE_TO_AXISMC) {
            // only use properties that are local to the axis2 MC,
            // not the options bag.  See org.apache.axis2.context.AbstractContext
            Object retval = null;
            Iterator names = axisMsgCtx.getPropertyNames();
            for (; names.hasNext();) {
                String name = (String)names.next();
                if (name.equals(key)) {
                    retval = axisMsgCtx.getProperty(key);
                }
            }
            axisMsgCtx.setProperty(key, value);
            return retval;
        } else {
            return getProperties().put(key, value);
        }
    }

    public EndpointDescription getEndpointDescription() {
        return endpointDesc;
    }

    public void setEndpointDescription(EndpointDescription ed) {
        endpointDesc = ed;
    }

    public OperationDescription getOperationDescription() {
        return operationDesc;
    }

    public void setOperationDescription(OperationDescription od) {
        operationDesc = od;
    }

    public Mode getMode() {
        return mode;
    }

    public void setMode(Mode m) {
        mode = m;
    }

    //FIXME: This should become the OperationDescription
    public QName getOperationName() {
        return operationName;
    }

    //FIXME: This should become the OperationDescription
    public void setOperationName(QName op) {
        operationName = op;
    }

    public void setMessage(Message msg) {
        message = msg;
        msg.setMessageContext(this);
    }

    public Message getMessage() {
        return message;
    }

    public org.apache.axis2.context.MessageContext getAxisMessageContext() {
        return axisMsgCtx;
    }

    public ClassLoader getClassLoader() {
        AxisService svc = axisMsgCtx.getAxisService();
        if (svc != null)
            return svc.getClassLoader();
        else
            return null;
    }

    /**
     * Used to determine whether or not session state has been enabled.
     *
     * @return
     */
    public boolean isMaintainSession() {
        boolean maintainSession = false;

        Boolean value = (Boolean) getProperty(BindingProvider.SESSION_MAINTAIN_PROPERTY);
        if (value != null && value.booleanValue()) {
            maintainSession = true;
        }

        return maintainSession;
    }

    /**
     * The local exception is the Throwable object held on the Message from a problem that occurred
     * due to something other than the server.  In other words, no message ever travelled across
     * the wire.
     *
     * @return the Throwable object or null
     */
    public Throwable getLocalException() {
        return localException;
    }

    /**
     * The local exception is the Throwable object held on the Message from a problem that occurred
     * due to something other than the server.  In other words, no message ever travelled across the
     * wire.
     *
     * @param t
     * @see Throwable
     */
    public void setLocalException(Throwable t) {
        localException = t;
    }
    
    /**
     * @param t
     */
    public void setCausedByException (AxisFault t){
        this.causedByException = t;
    }
    
    /**
     * @return
     */
    public AxisFault getCausedByException(){
        return this.causedByException;
    }
    

    /**
     * Set the wrapper MEPContext.  Internally, this method also sets
     * the MEPContext's children so the pointer is bi-directional; you can
     * get the MEPContext from the MessageContext and vice-versa.
     * 
     * @param mepCtx
     */
    public void setMEPContext(MEPContext mepCtx) {
        if (this.mepCtx == null) {
            this.mepCtx = mepCtx;
            // and set parent's child pointer
            this.mepCtx.setResponseMessageContext(this);
        }
    }

    public MEPContext getMEPContext() {
        if (mepCtx == null) {
            setMEPContext(new MEPContext(this));
        }
        return mepCtx;
    }
    
    /**
     * @return if outbound MessageContext
     */
    public boolean isOutbound() {
        return isOutbound;
    }

    /**
     * @param isOutbound true if outbound MessageContext
     */
    public void setOutbound(boolean isOutbound) {
        this.isOutbound = isOutbound;
    }

}

Other Axis 2 examples (source code examples)

Here is a short list of links related to this Axis 2 MessageContext.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.