/* * Copyright 2012 Tallinn University Centre for Educational Technology * * Licensed 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 ee.htk.dippler.app.service; import org.ksoap2.SoapEnvelope; import org.ksoap2.serialization.SoapObject; import org.ksoap2.serialization.SoapPrimitive; import org.ksoap2.serialization.SoapSerializationEnvelope; import org.ksoap2.transport.HttpTransportSE; import android.content.Context; import android.content.SharedPreferences; import android.preference.PreferenceManager; import android.sax.EndTextElementListener; import android.sax.RootElement; import android.util.Xml; public class Connector { private static final String SOAP_ACTION = "http://backoffice.dippler.htk.tlu.ee/newUserSession"; private static final String METHOD_NAME = "newUserSession"; private static final String NAMESPACE = "http://backoffice.dippler.htk.tlu.ee/"; private Context app_context = null; private SessionObject session; public Connector(Context context) { app_context = context; } private Context getLocalContext() { return app_context; } public String connect() { SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getLocalContext()); SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); request.addProperty("email", prefs.getString("dippler-username", "")); request.addProperty("password", prefs.getString("dippler-password", "")); SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); envelope.setOutputSoapObject(request); //If preference not set, fallback to default URL HttpTransportSE ht = new HttpTransportSE(prefs.getString("dippler-bos-wsdl", "http://htk.tlu.ee/backoffice/BackOfficeService?wsdl")); try { ht.call(SOAP_ACTION, envelope); SoapPrimitive response = (SoapPrimitive) envelope.getResponse(); session = getSession(response.toString()); return session.getId(); } catch (Exception e) { e.printStackTrace(); } return null; } public String getSessionId() { if ( session != null ) { return session.getId(); } return null; } public SessionObject getSession() { return this.session; } private SessionObject getSession(String raw_xml) { final SessionObject session = new SessionObject(); try { final RootElement root = new RootElement("response"); root.getChild("status").setEndTextElementListener( new EndTextElementListener() { public void end(String body) { session.setStatus(body); } }); root.getChild("sessionId").setEndTextElementListener( new EndTextElementListener() { public void end(String body) { session.setId(body); } }); Xml.parse(raw_xml, root.getContentHandler()); } catch (Exception e) { e.printStackTrace(); } return session; } }