/**
 * 
 */
package com.fp.alfresco.auth;

import net.sf.json.JSONObject;
import net.sf.json.JSONSerializer;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.impl.client.HttpClients;

import com.fp.alfresco.util.ApiProperties;
import com.fp.alfresco.util.ApiResponseHandler;

/**
 * Autenticaci&oacute;n mediante un archivo de propiedades
 * @author bpt
 *
 */
public class AlfrescoFileAuth extends Authentication{

	
    @Override
    public boolean authenticate() {
	try{
	    super.user = ApiProperties.getProperty("api.user");
	    super.password = ApiProperties.getProperty("api.password");

	    HttpClient client = HttpClients.createDefault();
	    String authUrl = ApiProperties.getProperty("api.uri.base") +ApiProperties.getProperty("api.uri.login", super.user, super.password);
	    HttpRequestBase httpRequest = new HttpGet(authUrl);
	    ApiResponseHandler handler = new ApiResponseHandler();
	    HttpResponse response = client.execute(httpRequest);
	    String responseString =  (String) handler.handleResponse(response, false);

	    JSONObject json = (JSONObject) JSONSerializer.toJSON(responseString);
	    super.ticket = json.getJSONObject("data").getString("ticket");
	    if(super.ticket!=null){
	    	return true;
	    }
	}catch(Exception e){
	    e.printStackTrace();
	}
	return false;
    }

    public static void main(String[] args)
    {
	AlfrescoFileAuth auth = new AlfrescoFileAuth();
	boolean isauth = auth.authenticate();
	System.out.println(isauth);
    }
}
