You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@sling.apache.org by André Basse <an...@gmail.com> on 2012/07/11 02:15:09 UTC

500, Denied Access denied - for post from JAVA

Hi all,

I have some trouble with my Java post request. Sling issues a 500
error when I try to post my JPG to /images.

I can find following line in the log file in Sling:
11.07.2012 10:09:54.604 *ERROR* [127.0.0.1 [1341965394583] POST
/images/tubu2.jpg HTTP/1.1]
org.apache.sling.servlets.post.impl.operations.ModifyOperation Access
Denied Access denied.

Below is my Java code. Any help is much appreciated.


public static void main(String[] args) throws HttpException, IOException {
	
	   Credentials credentials = new UsernamePasswordCredentials("admin", "admin");
	   HttpClient httpClient = new HttpClient();
	   httpClient.getState().setCredentials(AuthScope.ANY, credentials);

	   String url = "http://localhost:8080/images/tubu2.jpg";
	   String fieldName = "./";
	   File localFile = new File("C:/temp/test.jpg");
	   String typeHint = "nt:file";
	
	   final Part[] parts = new Part[typeHint == null ? 1 : 2];
       parts[0] = new FilePart(fieldName, localFile);
       if (typeHint != null) {
           parts[1] = new StringPart(fieldName + "@TypeHint", typeHint);
       }
  	
       final PostMethod post = new PostMethod(url);
       post.setFollowRedirects(false);
       post.setRequestEntity(new MultipartRequestEntity(parts,
post.getParams()));
       final int status = httpClient.executeMethod(post);
	
       System.out.println(Integer.toString(status));
    	
    }

}



Thanks,

Andre

Re: 500, Denied Access denied - for post from JAVA

Posted by Chetan Mehrotra <ch...@gmail.com>.
The 500 error is coming because HttpClient does not send the username/pwd
by default. Use preemptive authentication by adding following line to your
code

httpClient.getParams().setAuthenticationPreemptive(true);

This would send u/p by default in first call itself and you should get 201
response

Chetan Mehrotra


On Wed, Jul 11, 2012 at 5:45 AM, André Basse <an...@gmail.com> wrote:

> Hi all,
>
> I have some trouble with my Java post request. Sling issues a 500
> error when I try to post my JPG to /images.
>
> I can find following line in the log file in Sling:
> 11.07.2012 10:09:54.604 *ERROR* [127.0.0.1 [1341965394583] POST
> /images/tubu2.jpg HTTP/1.1]
> org.apache.sling.servlets.post.impl.operations.ModifyOperation Access
> Denied Access denied.
>
> Below is my Java code. Any help is much appreciated.
>
>
> public static void main(String[] args) throws HttpException, IOException {
>
>            Credentials credentials = new
> UsernamePasswordCredentials("admin", "admin");
>            HttpClient httpClient = new HttpClient();
>            httpClient.getState().setCredentials(AuthScope.ANY,
> credentials);
>
>            String url = "http://localhost:8080/images/tubu2.jpg";
>            String fieldName = "./";
>            File localFile = new File("C:/temp/test.jpg");
>            String typeHint = "nt:file";
>
>            final Part[] parts = new Part[typeHint == null ? 1 : 2];
>        parts[0] = new FilePart(fieldName, localFile);
>        if (typeHint != null) {
>            parts[1] = new StringPart(fieldName + "@TypeHint", typeHint);
>        }
>
>        final PostMethod post = new PostMethod(url);
>        post.setFollowRedirects(false);
>        post.setRequestEntity(new MultipartRequestEntity(parts,
> post.getParams()));
>        final int status = httpClient.executeMethod(post);
>
>        System.out.println(Integer.toString(status));
>
>     }
>
> }
>
>
>
> Thanks,
>
> Andre
>