/** * Returns the HTTP encoded string required for web service authentication. * * <p>Order of authentication methods: token then username/password, if token not initialised or * null if both methods not initialised. * * @return String containing HTTP encoded string or null. * @throws UnsupportedEncodingException */ public static String getAuth() throws UnsupportedEncodingException { StringBuilder data = new StringBuilder(); if (MoodleCallRestWebService.getToken() != null) { data.append(URLEncoder.encode("wstoken", "UTF-8")) .append("=") .append(URLEncoder.encode(MoodleCallRestWebService.getToken(), "UTF-8")); } else { if (MoodleCallRestWebService.getUsername() != null && MoodleCallRestWebService.getPassword() != null) { data.append(URLEncoder.encode("wsusername", "UTF-8")) .append("=") .append(URLEncoder.encode(MoodleCallRestWebService.getUsername(), "UTF-8")); data.append("&"); // Fix by César Martínez data.append(URLEncoder.encode("wspassword", "UTF-8")) .append("=") .append(URLEncoder.encode(MoodleCallRestWebService.getPassword(), "UTF-8")); } else return null; } return data.toString(); }