04-04-2014 06:48 AM
04-04-2014 07:47 AM
04-04-2014 10:26 AM
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.DeleteMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.json.JSONObject;
public class LoginAddLogoutTest {
public String login() {
String ticket = null;
HttpClient client = new HttpClient();
String apiurl = "http://localhost:8080/alfresco/service/api/login";
PostMethod post = new PostMethod(apiurl);
try {
JSONObject login = new JSONObject();
login.put("username", "admin");
login.put("password", "admin");
post.setDoAuthentication(true);
post.setRequestHeader("Content-Type", "application/json");
post.setRequestEntity(new StringRequestEntity(login.toString(),
"application/json", "UTF-8"));
int status = client.executeMethod(post);
if (status != HttpStatus.SC_OK) {
System.err.println("Method failed: " + post.getStatusLine());
}
String responseData = post.getResponseBodyAsString();
System.out.println(responseData);
JSONObject response = new JSONObject(responseData);
ticket = response.getJSONObject("data").getString("ticket");
} catch (Exception e) {
e.printStackTrace();
} finally {
post.releaseConnection();
}
return ticket;
}
public void logout(String ticket, String tologoutTicket) {
HttpClient client = new HttpClient();
String apiurl = "http://localhost:8080/alfresco/service/api/login/ticket/"
+ tologoutTicket + "?alf_ticket=" + ticket;
DeleteMethod post = new DeleteMethod(apiurl);
try {
post.setDoAuthentication(true);
post.setRequestHeader("Content-Type", "application/xml");
int status = client.executeMethod(post);
if (status != HttpStatus.SC_OK) {
System.err.println("Method failed: " + post.getStatusLine());
}
String resultString = post.getResponseBodyAsString();
System.out.println(resultString);
} catch (Exception e) {
e.printStackTrace();
} finally {
post.releaseConnection();
}
}
public static void main(String[] args) {
LoginAddLogoutTest t = new LoginAddLogoutTest();
String ticket = t.login();
t.logout(ticket, ticket); // in my case I logout admin using admin
// authentication
}
}
04-04-2014 12:52 PM
Tags
Find what you came for
We want to make your experience in Hyland Connect as valuable as possible, so we put together some helpful links.