You are right, there should be a way to use RestTemplate, haven't had
time to explore about this but I will check once I get my hand free.
Udai
On Wed, Nov 16, 2011 at 7:57 PM, Satish <www.s...@gmail.com> wrote:
> Hi Udai,
> Thank you for the response. I did follow you mifos-rest.py example and it
> was helpful. I was using Spring's RestTemplate and was having problem in
> saving session for subsequent rest calls. However, I have managed to
> implement it using HttpClient. Solution is working but i feel it cannot be
> so crude and there must exist a better solution.
>
> This is what i have done:
> //This method prepare the connections and return the session with
> authentication.
> //class: RESTClient
>
> public HttpClient prepareConnection() throws HttpException, IOException{
>
>
>
> logger.debug("Establishing http session with mifos");
>
> httpConn = new HttpClient();
>
> Credentials credentials = new
> UsernamePasswordCredentials(username,password);
>
> httpConn.getParams().setAuthenticationPreemptive(true);
>
> httpConn.getState().setCredentials(AuthScope.ANY, credentials);
>
> HttpConnectionManagerParams conpar = new HttpConnectionManagerParams();
>
> conpar.setConnectionTimeout(5 * 60000); // 5 minutes
>
> conpar.setSoTimeout(5 * 60000); // 5 minutes
>
> httpConn.getHttpConnectionManager().setParams(conpar);
>
> GetMethod get = new GetMethod(URL);
>
> get.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
>
> get.setDoAuthentication(true);
>
> httpConn.executeMethod(get);
>
> String responseBody = get.getResponseBodyAsString();
>
> if (responseBody.contains("j_spring_security_check")) {
>
> String loginUrl = URL +
> "/j_spring_security_check?j_username="+username+"&j_password="+password;
>
> PostMethod authpost = new PostMethod(loginUrl);
>
> // Release Get Connection
>
> get.releaseConnection();
>
> httpRes = httpConn.executeMethod(authpost);
>
> responseBody = authpost.getResponseBodyAsString();
>
> if (httpRes == 301 || httpRes == 302 || httpRes == 307) {
>
> // redirected, get content page
>
> get = new GetMethod(authpost.getResponseHeader("Location")
>
> .getValue());
>
> System.out.println(authpost.getResponseHeader("Location").getValue());
>
> get.setRequestHeader("Content-Type","text/plain; charset=UTF-8");
>
> authpost.releaseConnection();
>
> httpRes = httpConn.executeMethod(get);
>
> responseBody = get.getResponseBodyAsString();
>
> get.releaseConnection();
>
> }
>
> } else {
>
> get.releaseConnection();
>
> }
>
> return httpConn;
>
>
>
> }
>
> After i get this authenticated session, I create RestTemplate and invoke
> REST services.
>
> RESTClient rs = new RESTClient("mifos", "testmifos");
>
> HttpClient httpClient = rs.prepareConnection();
>
> CommonsClientHttpRequestFactory cf = new
> CommonsClientHttpRequestFactory(httpClient);
>
> RestTemplate restTemplate = new RestTemplate(cf);
>
> String url = "http://localhost:9081/mifos/personnel/id-current.json";
>
> HttpHeaders headers = new HttpHeaders();
>
> headers.setContentType(MediaType.APPLICATION_JSON);
>
> String result = restTemplate.getForObject(url, String.class);
>
> Could you please advise if there is a better way to achieve this? Thanks.
> Regards,
> Satish
>
>
> On 16 November 2011 21:51, Udai Gupta <mail...@gmail.com> wrote:
>>
>> I have an example how the authentication works using python.
>> https://github.com/mifos/head/blob/master/rest/scripts/mifos-rest.py#L47
>>
>> url = base_url + '/j_spring_security_check'
>> data = urllib.urlencode({'j_username' : username, 'j_password' :
>> password, 'spring-security-redirect' : '/status.json'})
>> headers = {'User-agent' : 'Mifos REST Client',
>> 'Content-Type':'application/x-www-form-urlencoded'}
>> req = Request(url, data, headers)
>>
>> You have to submit these parameters.
>> j_usename = mifos
>> j_password = testmifos
>> spring-security-redirect: /status.json
>>
>> to URL like http://localhost:8080/mifos/j_spring_security_check
>> with
>> 'Content-Type':'application/x-www-form-urlencoded'
>>
>> The response on successful login will contain cookie with jsessionid
>> for authentication.
>> and a JSON response { status : 'Success' }
>>
>> If you are looking into a specific client/language let me know I will
>> try to create an example code for authentication and successful call.
>>
>> Udai
>> On Sun, Nov 13, 2011 at 6:00 PM, Satish <www.s...@gmail.com> wrote:
>> > Hi Udai/Binny,
>> > I have been following up recent development on the REST front in Mifos
>> > and
>> > trying to build a external client which will consume the web services.
>> > I have been trying this from a spring app ( I did try to follow up
>> > Udai's
>> > python client too) but couldnt successfully authenticate mifos. As I
>> > understand, i will have to authenticate and then maintain cookie data
>> > for
>> > following request, however It is just not working out. May I ask you to
>> > help me with a little code snippet in Java which can let me authenticate
>> > with mifos and save cookie to use for subsequest request?
>> > I tried various ways to create a http request and send it to mifos, but
>> > either it fails with "invalid username/password" (which is misleading)
>> > or
>> > does nothing.
>> > Also, I see in REST API for authentication at
>> >
>> > https://docs.google.com/document/d/1dZWg7Dw6kGJbpowjHJRCgdX3r9EI3p2K2yCGcC0s6XM/edit?hl=en_US
>> > 1)Authentication
>> > Content-Type:application/json
>> > url:http://localhost:8080/mifoslive/authenticate
>> > method:post
>> > Body:
>> > {
>> > PersonnelDetails: {
>> > userName: "support",
>> > password: "password"
>> > }
>> > }
>> > Is this really there in place? I couldnt find it. Could any one of you
>> > help
>> > me sort this authentication with mifos please?
>> > Sorry about sending a direct email, My group access is not working yet.
>> > I
>> > will appreciate your help. thanks.
>> > Regards,
>> > Satish
>
>
------------------------------------------------------------------------------
RSA(R) Conference 2012
Save $700 by Nov 18
Register now
http://p.sf.net/sfu/rsa-sfdev2dev1
Mifos-developer mailing list
mifos-d...@lists.sourceforge.net
Unsubscribe or change settings at:
https://lists.sourceforge.net/lists/listinfo/mifos-developer
------------------------------------------------------------------------------