Android client - OpenKM 5.1

From OpenKM Documentation
(Redirected from Android client)
Jump to: navigation, search

Contents

Although Android platform programming language is Java, it has some limitations respect the Oracle JDK. For example, the WeServices stuff. So, we need to use an external library to make SOAP request. This library is called ksoap and can be located at http://code.google.com/p/ksoap2-android/.

Authentication

package com.openkm.ws.test;
 
import java.io.IOException;
 
import org.ksoap2.SoapEnvelope;
import org.ksoap2.SoapFault;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;
 
import android.util.Log;
 
public class AuthClient {
    private static final String NAMESPACE = "http://endpoint.ws.openkm.com/";
    private static final String URL = "http://10.0.2.2:8080/OpenKM/OKMAuth";
    private static final String SOAP_ACTION = "\"\"";
    private static final String TAG = "OKM_AUTH_CLIENT";
 
    /**
     * Auth user login
     */
    public static String login(String user, String password) throws IOException {	
        try {
            SoapObject request = new SoapObject(NAMESPACE, "login");
            request.addProperty("user", user);
            request.addProperty("password", password);
 
            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
            envelope.setOutputSoapObject(request);
 
            HttpTransportSE transport = new HttpTransportSE(URL);
            transport.setXmlVersionTag("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
            transport.call(SOAP_ACTION, envelope);
 
            SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
            Log.i(TAG, "SOAP Result... " + response);
            String result = (String) response.toString();
            return result;
        } catch (SoapFault e) {
            Log.e(TAG, "Code: " + e.faultcode + ", String: " + e.faultstring);
            throw new IOException(e.faultstring);
        } catch (XmlPullParserException e) {
            throw new IOException(e.getMessage());
        }
    }
 
    /**
     * Auth user logout
     */
    public static void logout(String token) throws IOException {
        try {
            SoapObject request = new SoapObject(NAMESPACE, "logout");
            request.addProperty("token", token);
 
            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
            envelope.setOutputSoapObject(request);
 
            HttpTransportSE transport = new HttpTransportSE(URL);
            transport.call(SOAP_ACTION, envelope);
 
            SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
            Log.i(TAG, "SOAP Result... " + response);
        } catch (SoapFault e) {
            Log.e(TAG, "Code: " + e.faultcode + ", String: " + e.faultstring);
            throw new IOException(e.faultstring);
        } catch (XmlPullParserException e) {
            throw new IOException(e.getMessage());
        }
    }
}

Create document

package com.openkm.ws.test;
 
import java.io.IOException;
 
import org.kobjects.base64.Base64;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.SoapFault;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;
 
import android.util.Log;
 
public class DocumentClient {
    private static final String NAMESPACE = "http://endpoint.ws.openkm.com/";
    private static final String URL = "http://10.0.2.2:8080/OpenKM/OKMDocument";
    private static final String SOAP_ACTION = "\"\"";
    private static final String TAG = "OKM_DOC_CLIENT";
 
    /**
     * Create document
     */
    public static void create(String token, String docPath, byte[] content) throws IOException {
        HttpTransportSE transport = new HttpTransportSE(URL);
 
        try {
            SoapObject request = new SoapObject(NAMESPACE, "createSimple");
            request.addProperty("token", token);
            request.addProperty("docPath", docPath);
            request.addProperty("content", new SoapPrimitive(SoapEnvelope.ENC, "base64", Base64.encode(content)));
 
            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
            envelope.setOutputSoapObject(request);	
            transport.setXmlVersionTag("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
            transport.call(SOAP_ACTION, envelope);
 
            SoapObject response = (SoapObject) envelope.getResponse();
            Log.i(TAG, "SOAP Result... " + response);
        } catch (SoapFault e) {
            Log.e(TAG, "Code: " + e.faultcode + ", String: " + e.faultstring);
            throw new IOException(e.faultstring);
        } catch (XmlPullParserException e) {
            throw new IOException(e.getMessage());
        }
    }
}

Parsing of SoapFault detail

package com.openkm.ws.test;
 
import org.kxml2.kdom.Element;
import org.kxml2.kdom.Node;
 
public class Utility {
 
    /**
     * Parse ksoap faul detail.
     */
    public static String parse(Node node) {
        StringBuilder sb = new StringBuilder();
 
        for (int i=0; i<node.getChildCount(); i++) {
            Element detail = (Element) node.getChild(i);
 
            for (int j=0; j<detail.getChildCount(); j++) {
                Element exception = (Element) detail.getChild(j);
                sb.append(exception.getName());
 
                for (int k=0; k<exception.getChildCount(); k++) {
                    Element message = (Element) exception.getChild(k);
 
                    for (int l=0; l<message.getChildCount(); l++) {
                        String text = (String) message.getChild(l);
                        sb.append(": ");
                        sb.append(text);
                    }
                }
            }
        }
 
        return sb.toString();
    }
}