詳細知識を表示詳細知識を非表示Example Code: Application accessing the SOAP administration
The following code is a simple Java application to access the SOAP server. Please note that this program will not run instantly on your system. You have to change the constants for the ILIAS server path, the user with administrative rights and the ILIAS client ID.
It may be as well necessary to change some of the ID's hardcoded in the example to make this program work.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 | package de.ilias; import java.io.IOException; import org.ksoap2.SoapEnvelope; import org.ksoap2.serialization.SoapObject; import org.ksoap2.serialization.SoapSerializationEnvelope; import org.ksoap2.transport.HttpTransportSE; import org.xmlpull.v1.XmlPullParserException; public class SOAPExample { public static final String PATH_TO_YOUR_ILIAS_INSTALLATION = "http://localhost/ilias3"; public static final String NAME_OF_THE_ILIAS_CLIENT = "ilias3"; public static final String USERNAME_OF_USER_WITH_SUFFICIENT_PERMISSION = "root"; public static final String PASSWORD_OF_USER_WITH_SUFFICIENT_PERMISSION = "homer"; public ilUserData getUser(String sid, int user_id) { // retrieve a user from ILIAS using a mapping to a complex soap object SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); SoapObject soapObject = new SoapObject("urn:ilUserAdministration", "getUser"); soapObject.addProperty("sid", sid); soapObject.addProperty("user_id", new Integer(user_id)); envelope.setOutputSoapObject(soapObject); // map the response to the ilUserData object (if you don't map the response, the response will by of type String) envelope.addMapping("urn:ilUserAdministration", "ilUserData", new ilUserData().getClass()); HttpTransportSE transport = new HttpTransportSE(PATH_TO_YOUR_ILIAS_INSTALLATION + "/webservice/soap/server.php"); try { transport.call("", envelope); ilUserData ud = (ilUserData)envelope.getResponse(); return ud; } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (XmlPullParserException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } public int addUser(String sid) { // add a user using the minimum set of parameters. you must at least set these userdata fields to run the addUser method successfully SoapObject soapObject = new SoapObject("urn:ilUserAdministration", "addUser"); soapObject.addProperty("sid", sid); ilUserData userdata = new ilUserData(); userdata.setEmail("fakeemail@fakemail.org"); userdata.setFirstname("Fake"); userdata.setLastname("User"); userdata.setGender("m"); userdata.setLogin("fakeuser"); userdata.setPasswd("fakepasswd"); // time_limit_owner must be the reference id of the administration user folder. Usually it is 7 but you have to check this!!!! userdata.setTimeLimitOwner(7); userdata.setTimeLimitUnlimited(1); userdata.setUserLanguage("en"); soapObject.addProperty("user_data", userdata); SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); // you have to retrieve a valid role id here. For the example I am taking 2 which is the default Administrator role // but you have to use SOAP to get your roles: call getRoles soapObject.addProperty("global_role_id", new Integer(2)); envelope.setOutputSoapObject(soapObject); userdata.register(envelope); HttpTransportSE transport = new HttpTransportSE(PATH_TO_YOUR_ILIAS_INSTALLATION + "/webservice/soap/server.php"); try { transport.call("addUser", envelope); Integer newUserId = (Integer) envelope.getResponse(); return newUserId.intValue(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (XmlPullParserException e) { // TODO Auto-generated catch block e.printStackTrace(); System.out.println(transport.requestDump); } return 0; } public String login() { String session = null; // first log in as a user with administrative rights and retrieve your session id SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); SoapObject soapObject = new SoapObject("urn:ilUserAdministration", "login"); soapObject.addProperty("client", NAME_OF_THE_ILIAS_CLIENT); soapObject.addProperty("username", USERNAME_OF_USER_WITH_SUFFICIENT_PERMISSION); soapObject.addProperty("password", PASSWORD_OF_USER_WITH_SUFFICIENT_PERMISSION); envelope.setOutputSoapObject(soapObject); HttpTransportSE transport = new HttpTransportSE(PATH_TO_YOUR_ILIAS_INSTALLATION + "/webservice/soap/server.php"); transport.debug = true; try { transport.call("login", envelope); session = envelope.getResponse().toString(); return session; } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (XmlPullParserException e) { // TODO Auto-generated catch block e.printStackTrace(); } if (session == null) { System.out.println("Your login wasn't successful"); } return null; } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub SOAPExample se = new SOAPExample(); String sid = se.login(); ilUserData userdata = se.getUser(sid, 11085); System.out.println(userdata.toString()); int newUserId = se.addUser(sid); System.out.println("A new user with id = " + newUserId + " was created"); } } |
|