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

Axis 2 example source code file (RESTUtil.java)

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

axisbindingoperation, axisbindingoperation, axisendpoint, axisfault, axisfault, httplocationbaseddispatcher, io, ioexception, ioexception, outputstream, requesturibaseddispatcher, soapenvelope, string, string, xmlstreamexception

The Axis 2 RESTUtil.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.transport.http.util;

import org.apache.axiom.soap.SOAPEnvelope;
import org.apache.axis2.AxisFault;
import org.apache.axis2.Constants;
import org.apache.axis2.builder.BuilderUtil;
import org.apache.axis2.context.MessageContext;
import org.apache.axis2.description.AxisBindingOperation;
import org.apache.axis2.description.AxisEndpoint;
import org.apache.axis2.description.AxisOperation;
import org.apache.axis2.description.AxisService;
import org.apache.axis2.description.WSDL2Constants;
import org.apache.axis2.engine.AxisEngine;
import org.apache.axis2.dispatchers.HTTPLocationBasedDispatcher;
import org.apache.axis2.engine.Handler;
import org.apache.axis2.dispatchers.RequestURIBasedDispatcher;
import org.apache.axis2.dispatchers.RequestURIOperationDispatcher;
import org.apache.axis2.transport.TransportUtils;
import org.apache.axis2.transport.http.HTTPConstants;
import org.apache.axis2.transport.http.HTTPTransportUtils;

import javax.xml.stream.XMLStreamException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 *
 */
public class RESTUtil {

    public static Handler.InvocationResponse processXMLRequest(MessageContext msgContext,
                                                               InputStream in,
                                                               OutputStream out, String contentType)
            throws AxisFault {
        try {
            msgContext.setDoingREST(true);
            String charSetEncoding = BuilderUtil.getCharSetEncoding(contentType);
            msgContext.setProperty(Constants.Configuration.CHARACTER_SET_ENCODING, charSetEncoding);
            dispatchAndVerify(msgContext);
            in = HTTPTransportUtils.handleGZip(msgContext, in);
            SOAPEnvelope soapEnvelope = TransportUtils
                    .createSOAPMessage(msgContext, in, contentType);
            msgContext.setEnvelope(soapEnvelope);
            msgContext.setProperty(Constants.Configuration.CONTENT_TYPE,
                                   contentType);

            msgContext.setProperty(MessageContext.TRANSPORT_OUT, out);

        } catch (AxisFault axisFault) {
            throw axisFault;
        } catch (XMLStreamException e) {
            throw AxisFault.makeFault(e);
        } catch (IOException e) {
            throw AxisFault.makeFault(e);
        } finally {
            msgContext.setProperty(Constants.Configuration.MESSAGE_TYPE,
                                   HTTPConstants.MEDIA_TYPE_APPLICATION_XML);
        }
        return invokeAxisEngine(msgContext);
    }

    public static Handler.InvocationResponse processURLRequest(MessageContext msgContext,
                                                               OutputStream out, String contentType)
            throws AxisFault {
        // here, only the parameters in the URI are supported. Others will be discarded.
        try {

            if (contentType == null || "".equals(contentType)) {
                contentType = HTTPConstants.MEDIA_TYPE_X_WWW_FORM;
            }

            // set the required properties so that even if there is an error during the dispatch
            // phase the response message will be passed to the client well. 
            msgContext.setDoingREST(true);
            msgContext.setProperty(MessageContext.TRANSPORT_OUT, out);
            String charSetEncoding = BuilderUtil.getCharSetEncoding(contentType);
            msgContext.setProperty(Constants.Configuration.CHARACTER_SET_ENCODING, charSetEncoding);
            // 1. First dispatchAndVerify and find out the service and the operation.
            dispatchAndVerify(msgContext);
            SOAPEnvelope soapEnvelope;
            try {
                soapEnvelope = TransportUtils
                        .createSOAPMessage(msgContext, null, contentType);
            } catch (XMLStreamException e) {
                throw AxisFault.makeFault(e);
            }

            msgContext.setEnvelope(soapEnvelope);


        } catch (AxisFault axisFault) {
            throw axisFault;
        }
        catch (IOException e) {
            throw AxisFault.makeFault(e);
        } finally {
            msgContext.setProperty(Constants.Configuration.MESSAGE_TYPE,
                                   HTTPConstants.MEDIA_TYPE_APPLICATION_XML);
        }
        return invokeAxisEngine(msgContext);
    }

    private static Handler.InvocationResponse invokeAxisEngine(MessageContext messageContext)
            throws AxisFault {
        AxisEngine axisEngine = new AxisEngine(messageContext.getConfigurationContext());
        return axisEngine.receive(messageContext);

    }

    private static void dispatchAndVerify(MessageContext msgContext) throws AxisFault {
        RequestURIBasedDispatcher requestDispatcher = new RequestURIBasedDispatcher();
        requestDispatcher.invoke(msgContext);
        AxisService axisService = msgContext.getAxisService();
        if (axisService != null) {
            RequestURIOperationDispatcher requestURIOperationDispatcher =
                    new RequestURIOperationDispatcher();
            requestURIOperationDispatcher.invoke(msgContext);

            if (msgContext.getAxisOperation() == null) {
                HTTPLocationBasedDispatcher httpLocationBasedDispatcher =
                        new HTTPLocationBasedDispatcher();
                httpLocationBasedDispatcher.invoke(msgContext);
            }

            AxisOperation axisOperation;
            if ((axisOperation = msgContext.getAxisOperation()) != null) {
                AxisEndpoint axisEndpoint =
                        (AxisEndpoint) msgContext.getProperty(WSDL2Constants.ENDPOINT_LOCAL_NAME);
                if (axisEndpoint != null) {
                    AxisBindingOperation axisBindingOperation = (AxisBindingOperation) axisEndpoint
                            .getBinding().getChild(axisOperation.getName());
                    msgContext.setProperty(Constants.AXIS_BINDING_OPERATION, axisBindingOperation);
                }
                msgContext.setAxisOperation(axisOperation);
            }
        }
    }

    public static String getConstantFromHTTPLocation(String httpLocation) {
        if (httpLocation.charAt(0) != '?') {
            httpLocation = "/" + httpLocation;
        }
        int index = httpLocation.indexOf("{");
        if (index > -1) {
            httpLocation = httpLocation.substring(0, index);
        }
        return httpLocation;
    }

}


Other Axis 2 examples (source code examples)

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