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

Axis 2 example source code file (ContextUtils.java)

This example Axis 2 source code file (ContextUtils.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

endpointdescription, endpointinterfacedescription, http, http_request_method, http_request_pathinfo, httpservletrequest, httpservletrequest, net, network, request, response, servlet, servlet, set, set, string, string, uri, url, url

The Axis 2 ContextUtils.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.context.utils;

import org.apache.axis2.context.ServiceContext;
import org.apache.axis2.jaxws.core.MessageContext;
import org.apache.axis2.jaxws.description.EndpointDescription;
import org.apache.axis2.jaxws.description.EndpointInterfaceDescription;
import org.apache.axis2.jaxws.description.OperationDescription;
import org.apache.axis2.jaxws.description.ServiceDescription;
import org.apache.axis2.jaxws.description.ServiceDescriptionWSDL;
import org.apache.axis2.transport.http.HTTPConstants;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.namespace.QName;
import javax.xml.ws.WebServiceContext;
import javax.xml.ws.handler.MessageContext.Scope;
import javax.xml.ws.handler.soap.SOAPMessageContext;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Map;


public class ContextUtils {
    private static final Log log = LogFactory.getLog(ContextUtils.class);
    private static final String WEBSERVICE_MESSAGE_CONTEXT = "javax.xml.ws.WebServiceContext";

    /**
     * Adds the appropriate properties to the MessageContext that the user will see
     *
     * @param soapMessageContext
     * @param jaxwsMessageContext
     */
    public static void addProperties(SOAPMessageContext soapMessageContext,
                                     MessageContext jaxwsMessageContext) {

        // Copy Axis2 MessageContext properties.  It's possible that some set of Axis2 handlers
        // have run and placed some properties in the context that need to be visible.
        soapMessageContext.putAll(jaxwsMessageContext.getProperties());

        EndpointDescription description = jaxwsMessageContext.getEndpointDescription();
        if (description !=null) {
            // Set the WSDL properties
            ServiceDescription sd =
                    description.getServiceDescription();
            if (sd != null) {
                URL wsdlLocation = ((ServiceDescriptionWSDL)sd).getWSDLLocation();
                if (wsdlLocation != null && !"".equals(wsdlLocation)) {
                    URI wsdlLocationURI = null;
                    try {
                        wsdlLocationURI = wsdlLocation.toURI();
                    }
                    catch (URISyntaxException ex) {
                        // TODO: NLS/RAS
                        log.warn("Unable to convert WSDL location URL to URI.  URL: " +
                                wsdlLocation.toString() + "; Service: " + sd.getServiceQName(), ex);
                    }
                    soapMessageContext
                            .put(javax.xml.ws.handler.MessageContext.WSDL_DESCRIPTION, wsdlLocationURI);
                    soapMessageContext.setScope(javax.xml.ws.handler.MessageContext.WSDL_DESCRIPTION,
                                                Scope.APPLICATION);
                }
    
                soapMessageContext
                        .put(javax.xml.ws.handler.MessageContext.WSDL_SERVICE, sd.getServiceQName());
                soapMessageContext
                        .setScope(javax.xml.ws.handler.MessageContext.WSDL_SERVICE, Scope.APPLICATION);
                if (log.isDebugEnabled()) {
                    log.debug("WSDL_SERVICE :" + sd.getServiceQName());
                }
            }
        }

        // If we are running within a servlet container, then JAX-WS requires that the
        // servlet related properties be set on the MessageContext
        soapMessageContext.put(javax.xml.ws.handler.MessageContext.SERVLET_CONTEXT,
                               jaxwsMessageContext.getProperty(HTTPConstants.MC_HTTP_SERVLETCONTEXT));
        soapMessageContext
                .setScope(javax.xml.ws.handler.MessageContext.SERVLET_CONTEXT, Scope.APPLICATION);

        if (log.isDebugEnabled()) {
            if (jaxwsMessageContext.getProperty(HTTPConstants.MC_HTTP_SERVLETCONTEXT) != null) {
                log.debug("Servlet Context Set");
            } else {
                log.debug("Servlet Context not found");
            }
        }

        HttpServletRequest req = (HttpServletRequest)jaxwsMessageContext
                .getProperty(HTTPConstants.MC_HTTP_SERVLETREQUEST);
        if (req == null) {
            if (log.isDebugEnabled()) {
                log.debug("HTTPServletRequest not found");
            }
        }
        if (req != null) {
            soapMessageContext.put(javax.xml.ws.handler.MessageContext.SERVLET_REQUEST, req);
            soapMessageContext.setScope(javax.xml.ws.handler.MessageContext.SERVLET_REQUEST,
                                        Scope.APPLICATION);
            if (log.isDebugEnabled()) {
                log.debug("SERVLET_REQUEST Set");
            }

            String pathInfo = req.getPathInfo();
            soapMessageContext.put(javax.xml.ws.handler.MessageContext.PATH_INFO, pathInfo);
            soapMessageContext
                    .setScope(javax.xml.ws.handler.MessageContext.PATH_INFO, Scope.APPLICATION);
            if (log.isDebugEnabled()) {
                if (pathInfo != null) {
                    log.debug("HTTP_REQUEST_PATHINFO Set");
                } else {
                    log.debug("HTTP_REQUEST_PATHINFO not found");
                }
            }
            String queryString = req.getQueryString();
            soapMessageContext.put(javax.xml.ws.handler.MessageContext.QUERY_STRING, queryString);
            soapMessageContext
                    .setScope(javax.xml.ws.handler.MessageContext.QUERY_STRING, Scope.APPLICATION);
            if (log.isDebugEnabled()) {
                if (queryString != null) {
                    log.debug("HTTP_REQUEST_QUERYSTRING Set");
                } else {
                    log.debug("HTTP_REQUEST_QUERYSTRING not found");
                }
            }
            String method = req.getMethod();
            soapMessageContext.put(javax.xml.ws.handler.MessageContext.HTTP_REQUEST_METHOD, method);
            soapMessageContext.setScope(javax.xml.ws.handler.MessageContext.HTTP_REQUEST_METHOD,
                                        Scope.APPLICATION);
            if (log.isDebugEnabled()) {
                if (method != null) {
                    log.debug("HTTP_REQUEST_METHOD Set");
                } else {
                    log.debug("HTTP_REQUEST_METHOD not found");
                }
            }

        }
        HttpServletResponse res = (HttpServletResponse)jaxwsMessageContext
                .getProperty(HTTPConstants.MC_HTTP_SERVLETRESPONSE);
        if (res == null) {
            if (log.isDebugEnabled()) {
                log.debug("Servlet Response not found");
            }
        }
        if (res != null) {
            soapMessageContext.put(javax.xml.ws.handler.MessageContext.SERVLET_RESPONSE, res);
            soapMessageContext.setScope(javax.xml.ws.handler.MessageContext.SERVLET_RESPONSE,
                                        Scope.APPLICATION);
            if (log.isDebugEnabled()) {
                log.debug("SERVLET_RESPONSE Set");
            }
        }
        
    }

    public static void addWSDLProperties(MessageContext jaxwsMessageContext) {
        org.apache.axis2.context.MessageContext msgContext =
                jaxwsMessageContext.getAxisMessageContext();
        ServiceContext serviceContext = msgContext.getServiceContext();
        SOAPMessageContext soapMessageContext = null;
        if (serviceContext != null) {
            WebServiceContext wsc =
                    (WebServiceContext)serviceContext.getProperty(WEBSERVICE_MESSAGE_CONTEXT);
            if (wsc != null) {
                soapMessageContext = (SOAPMessageContext)wsc.getMessageContext();
            }
        }
        OperationDescription op = jaxwsMessageContext.getOperationDescription();

        if (op != null && soapMessageContext != null) {
            soapMessageContext
                    .put(javax.xml.ws.handler.MessageContext.WSDL_OPERATION, op.getName());
            soapMessageContext.setScope(javax.xml.ws.handler.MessageContext.WSDL_OPERATION,
                                        Scope.APPLICATION);
            if (log.isDebugEnabled()) {
                log.debug("WSDL_OPERATION :" + op.getName());
            }

            EndpointInterfaceDescription eid = op.getEndpointInterfaceDescription();
            if (eid != null) {
                EndpointDescription ed = eid.getEndpointDescription();
                QName portType = eid.getPortType();
                if (portType == null || portType.getLocalPart() == "") {
                    if (log.isDebugEnabled()) {
                        log.debug(
                                "Did not get port type from EndpointInterfaceDescription, attempting to get PortType from EndpointDescription");
                    }
                }
                if (ed != null) {
                    soapMessageContext
                            .put(javax.xml.ws.handler.MessageContext.WSDL_PORT, ed.getPortQName());
                    soapMessageContext.setScope(javax.xml.ws.handler.MessageContext.WSDL_PORT,
                                                Scope.APPLICATION);
                    if (log.isDebugEnabled()) {
                        log.debug("WSDL_PORT :" + ed.getPortQName());
                    }
                }
                soapMessageContext
                        .put(javax.xml.ws.handler.MessageContext.WSDL_INTERFACE, portType);
                soapMessageContext.setScope(javax.xml.ws.handler.MessageContext.WSDL_INTERFACE,
                                            Scope.APPLICATION);
                if (log.isDebugEnabled()) {
                    log.debug("WSDL_INTERFACE :" + portType);
                }
            }
        } else {
            if (log.isDebugEnabled()) {
                log.debug("Unable to read WSDL operation, port and interface properties");
            }
        }
    }

}

Other Axis 2 examples (source code examples)

Here is a short list of links related to this Axis 2 ContextUtils.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.