Also thanks Julain/Bertand. I now have about the simplest hello world
program working. I suspect as I get further into the project, I will need to
look at the authentication system a lot more thoroughly, but for now I am
happy with just being able to POST and GET data!

public class PopulateRepository {
public static void main(String[] args) throws Exception {
String nodeName = "newNodea";
String userName = "admin";
String password = "admin";

DefaultHttpClient client = new DefaultHttpClient();
client.addRequestInterceptor(new PreemptiveAuthInterceptor(), 0);
client.getCredentialsProvider().setCredentials(new AuthScope("localhost",
8080), new UsernamePasswordCredentials(userName, password));
UrlEncodedFormEntity entity = new
UrlEncodedFormEntity(Arrays.<NameValuePair> asList(new
BasicNameValuePair("param1", "value11"), new BasicNameValuePair("param2",
"valueq")), "UTF-8");
HttpPost action = new HttpPost("http://localhost:8080/"; + nodeName);
action.setEntity(entity);
HttpResponse response = client.execute(action);

System.out.println("Status: " + response.getStatusLine().getStatusCode());
}

private static class PreemptiveAuthInterceptor implements
HttpRequestInterceptor {

@Override
public void process(HttpRequest request, HttpContext context) throws
HttpException, IOException {

AuthState authState = (AuthState)
context.getAttribute(ClientContext.TARGET_AUTH_STATE);
CredentialsProvider credsProvider = (CredentialsProvider)
context.getAttribute(ClientContext.CREDS_PROVIDER);
HttpHost targetHost = (HttpHost)
context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);

// If not auth scheme has been initialized yet
if (authState.getAuthScheme() == null) {
AuthScope authScope = new AuthScope(targetHost.getHostName(),
targetHost.getPort());

// Obtain credentials matching the target host
Credentials creds = credsProvider.getCredentials(authScope);

// If found, generate BasicScheme preemptively
if (creds != null) {
authState.setAuthScheme(new BasicScheme());
authState.setCredentials(creds);
}
}
}
}
}


On Fri, Jul 8, 2011 at 11:43 AM, Phil Rice <phil.rice.erud...@googlemail.com
> wrote:

> Thanks for the help Alex. Unfortunately that is one of the things that has
> changed in the new API. Its no longer a method available from the
> parameters. However the advice to follow the test framework was very good
> and the following now works. I can rip this apart and reduce to to the
> minimum working, which I will post for the benefit of anyone else trying to
> do this.
>
> DefaultHttpClient httpClient = new DefaultHttpClient();
> RequestExecutor executor = new RequestExecutor(httpClient);
>
> String nodeName = "slingNode1";
>
> List<NameValuePair> formParams = Arrays.<NameValuePair> asList(new
> BasicNameValuePair("param1", "value1"), new BasicNameValuePair("param2",
> "value2"));
>  UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formParams,
> "UTF-8");
> System.out.println(executor.execute(requestBuilder.buildPostRequest(nodeName).withEntity(entity).withCredentials("admin",
> "admin")).getContent());
>  System.out.println(executor.execute(requestBuilder.buildGetRequest(nodeName
> + ".json")).getContent());
>
> Once again thanks for all the help, and I look forward to seeing some of
> you in September at Berlin.
>
> Phil
>
> On Fri, Jul 8, 2011 at 10:19 AM, Alexander Klimetschek <aklim...@adobe.com
> > wrote:
>
>> On 08.07.11 10:39, "Phil Rice" <phil.rice.erud...@googlemail.com> wrote:
>> >DefaultHttpClient client = new DefaultHttpClient();
>> >client.getCredentialsProvider().setCredentials(new AuthScope("localhost",
>> >8080), new UsernamePasswordCredentials("admin", "admin"));
>>
>> I guess you need to use preemptive auth:
>>
>> client.getParams().setAuthenticationPreemptive(true);
>>
>> http://hc.apache.org/httpclient-3.x/authentication.html
>>
>>
>> Regards,
>> Alex
>>
>> --
>> Alexander Klimetschek
>> Developer // Adobe (Day) // Berlin - Basel
>>
>>
>>
>>
>>
>

Reply via email to