Android Open Source - meets-android Soap Api Method






From Project

Back to project page meets-android.

License

The source code is released under:

MIT License

If you think the Android project meets-android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.theagilemonkeys.meets.magento;
/*ww w.  java2  s. c o m*/
import android.util.Log;

import com.theagilemonkeys.meets.ApiMethod;
import com.theagilemonkeys.meets.utils.soap.SoapParser;

import org.jdeferred.Deferred;
import org.ksoap2.HeaderProperty;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.SoapFault;
import org.ksoap2.serialization.KvmSerializable;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Android Meets SDK
 * Original work Copyright (c) 2014 [TheAgileMonkeys]
 *
 * @author ??lvaro Lpez Espinosa
 */
public class SoapApiMethod<RESULT> extends ApiMethod<RESULT> {
    private static final String HTTPS_PREFIX = "https://";
    private static String apiSessionId;
    private static final Object sessionIdLock = new Object();

    public static final String API_SESSION_EXPIRED_FAULTCODE = "5";

    //These properties need to be configured on app init
    public static String baseUrl;
    public static String soapApiUser;
    public static String soapApiPass;
    public static String soapNamespace;

    private SoapSerializationEnvelope soapEnvelope;
    private HttpTransportSE httpTransport;

    public SoapApiMethod(Class magentoModelClass) {
        super(magentoModelClass);
    }
    @Override
    public Deferred run(Map<String, Object> params, List<String> urlExtraSegments) {
        httpTransport = new HttpTransportSE(baseUrl);
        soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        soapEnvelope.implicitTypes = true;
        soapEnvelope.dotNet = false;
        soapEnvelope.xsd = SoapSerializationEnvelope.XSD;
        soapEnvelope.enc = SoapSerializationEnvelope.ENC;
        return super.run(params, urlExtraSegments);
    }

    @Override
    protected String getBaseUrl() {
        return baseUrl;
    }

    protected void parseResponse(Object response, RESULT model) throws Exception {
        SoapParser.parse((SoapObject) response, model);
    }

    @Override
    public RESULT loadDataFromNetwork() throws Exception {
        ensureApiLogin();

        Object res = send(getMethodName(), params);

        if ( SoapParser.isPrimitiveOrInmutable(modelClass) ){
            return (RESULT) res;
        }
        else{
            RESULT model = (RESULT) modelClass.newInstance();
            parseResponse(res, model);
            return model;
        }
    }

    private Object send(String method, Map<String, Object> params) throws IOException, XmlPullParserException {
        SoapObject request = new SoapObject(soapNamespace, method);

        if ( params == null)
            params = new HashMap<String, Object>();

        if (apiSessionIsValid())
            params.put("sessionId", getApiSessionId());

        for (Map.Entry<String,Object> entry : params.entrySet()){
            String key = entry.getKey();
            Object val = entry.getValue();
            if ( val instanceof KvmSerializable ){
                request.addProperty(buildPropertyInfo(key, (KvmSerializable) val));
            }
            else{
                request.addProperty(key, val);
            }
        }

        soapEnvelope.setOutputSoapObject(request);

        List<HeaderProperty> headerList = new ArrayList<HeaderProperty>();
        String basicAuthName = getBasicAuthName();
        String basicAuthPass = getBasicAuthPass();
        if (basicAuthName != null && basicAuthPass != null) {
            byte[] token = (basicAuthName + ":" + basicAuthPass).getBytes();
            headerList.add(new HeaderProperty("Authorization", "Basic " + org.kobjects.base64.Base64.encode(token)));
        }
        // This is due to a ksoap error. Sometimes we need to close connection and restart.
        headerList.add(new HeaderProperty("Connection", "Close"));


        httpTransport.call("", soapEnvelope, headerList);

        try {
            return soapEnvelope.getResponse();
        }
        catch (SoapFault e) {
            if (API_SESSION_EXPIRED_FAULTCODE.equals(e.faultcode)) {
                // Session expired. Set apiSessionId to null and let it reconnect on retry
                setApiSessionId(null);
                Log.d(getClass().getSimpleName(), "Soap api session expired. Trying to reconnect");
            }
            throw e;
        }
    }

    private Object send(String method, String... params) throws IOException, XmlPullParserException {
        Map<String, Object> finalParams = new HashMap<String, Object>();

        if ( params != null )
            for(int i = 0; i < params.length; i += 2)
                finalParams.put(params[i], params[i+1]);

        return send(method, finalParams);
    }

    private PropertyInfo buildPropertyInfo(String name, KvmSerializable val) {
        PropertyInfo propertyInfo = new PropertyInfo();
        propertyInfo.setName(name);
        propertyInfo.setValue(val);
        propertyInfo.setType(val.getClass());
        return propertyInfo;
    }

    private void ensureApiLogin() throws IOException, XmlPullParserException {
        synchronized (sessionIdLock) {
            if ( getApiSessionId() == null )
                setApiSessionId((String) send("login", "username", soapApiUser, "apiKey", soapApiPass));
        }

    }

    private static void setApiSessionId(String id) {
        synchronized (sessionIdLock) {
            apiSessionId = id;
        }
    }

    private static String getApiSessionId() {
        synchronized (sessionIdLock) {
            return apiSessionId;
        }
    }


    private static boolean apiSessionIsValid(){
        synchronized (sessionIdLock) {
            return apiSessionId != null;
        }
    }
}




Java Source Code List

com.theagilemonkeys.meets.ApiMethodModelHelperInterface.java
com.theagilemonkeys.meets.ApiMethodModelHelper.java
com.theagilemonkeys.meets.ApiMethod.java
com.theagilemonkeys.meets.MeetsSpiceService.java
com.theagilemonkeys.meets.Meets.java
com.theagilemonkeys.meets.magento.RestApiMethod.java
com.theagilemonkeys.meets.magento.SoapApiMethod.java
com.theagilemonkeys.meets.magento.methods.CatalogCategoryInfo.java
com.theagilemonkeys.meets.magento.methods.CatalogCategoryLevel.java
com.theagilemonkeys.meets.magento.methods.CatalogCategoryTree.java
com.theagilemonkeys.meets.magento.methods.CatalogInventoryStockItemList.java
com.theagilemonkeys.meets.magento.methods.CatalogProductAttributeOptions.java
com.theagilemonkeys.meets.magento.methods.CatalogProductInfo.java
com.theagilemonkeys.meets.magento.methods.CatalogProductList.java
com.theagilemonkeys.meets.magento.methods.CustomerAddressCreate.java
com.theagilemonkeys.meets.magento.methods.CustomerAddressDelete.java
com.theagilemonkeys.meets.magento.methods.CustomerAddressInfo.java
com.theagilemonkeys.meets.magento.methods.CustomerAddressList.java
com.theagilemonkeys.meets.magento.methods.CustomerAddressUpdate.java
com.theagilemonkeys.meets.magento.methods.CustomerCustomerCreate.java
com.theagilemonkeys.meets.magento.methods.CustomerCustomerInfo.java
com.theagilemonkeys.meets.magento.methods.CustomerCustomerList.java
com.theagilemonkeys.meets.magento.methods.CustomerCustomerUpdate.java
com.theagilemonkeys.meets.magento.methods.Products.java
com.theagilemonkeys.meets.magento.methods.ShoppingCartCreate.java
com.theagilemonkeys.meets.magento.methods.ShoppingCartCustomerAddresses.java
com.theagilemonkeys.meets.magento.methods.ShoppingCartCustomerSet.java
com.theagilemonkeys.meets.magento.methods.ShoppingCartInfo.java
com.theagilemonkeys.meets.magento.methods.ShoppingCartOrder.java
com.theagilemonkeys.meets.magento.methods.ShoppingCartPaymentList.java
com.theagilemonkeys.meets.magento.methods.ShoppingCartPaymentMethod.java
com.theagilemonkeys.meets.magento.methods.ShoppingCartProductAdd.java
com.theagilemonkeys.meets.magento.methods.ShoppingCartProductRemove.java
com.theagilemonkeys.meets.magento.methods.ShoppingCartShippingList.java
com.theagilemonkeys.meets.magento.methods.ShoppingCartShippingMethod.java
com.theagilemonkeys.meets.magento.models.MageMeetsAddress.java
com.theagilemonkeys.meets.magento.models.MageMeetsCartItem.java
com.theagilemonkeys.meets.magento.models.MageMeetsCartPayment.java
com.theagilemonkeys.meets.magento.models.MageMeetsCartShipping.java
com.theagilemonkeys.meets.magento.models.MageMeetsCart.java
com.theagilemonkeys.meets.magento.models.MageMeetsCategory.java
com.theagilemonkeys.meets.magento.models.MageMeetsCustomer.java
com.theagilemonkeys.meets.magento.models.MageMeetsProduct.java
com.theagilemonkeys.meets.magento.models.MageStockInfoList.java
com.theagilemonkeys.meets.magento.models.MageStockItem.java
com.theagilemonkeys.meets.magento.models.base.MageMeetsCollectionPojos.java
com.theagilemonkeys.meets.magento.models.base.MageMeetsCollection.java
com.theagilemonkeys.meets.magento.models.base.MageMeetsFactory.java
com.theagilemonkeys.meets.magento.models.base.MageMeetsModel.java
com.theagilemonkeys.meets.models.MeetsAddress.java
com.theagilemonkeys.meets.models.MeetsCart.java
com.theagilemonkeys.meets.models.MeetsCategory.java
com.theagilemonkeys.meets.models.MeetsCustomer.java
com.theagilemonkeys.meets.models.MeetsProduct.java
com.theagilemonkeys.meets.models.MeetsStock.java
com.theagilemonkeys.meets.models.base.MeetsCollectionPojos.java
com.theagilemonkeys.meets.models.base.MeetsCollection.java
com.theagilemonkeys.meets.models.base.MeetsFactory.java
com.theagilemonkeys.meets.models.base.MeetsListener.java
com.theagilemonkeys.meets.models.base.MeetsModel.java
com.theagilemonkeys.meets.utils.Copier.java
com.theagilemonkeys.meets.utils.StringUtils.java
com.theagilemonkeys.meets.utils.soap.Serializable.java
com.theagilemonkeys.meets.utils.soap.SoapParser.java