Thank you all for you help and advice
I have to say that I am struggling with Apache Sling. I am an experienced
developer, and have put the time in: I think I am on day 5 now, and while I
am running launchpad, I am getting nowhere flat with using it from the Java
Code. To be honest am getting quite demoralized over it. I missed the email
about the developer version of CRX, so I will get that and play with it. I
am quite keen to stay in the open source world, although I take your point
that its a very good way to learn about the language.
So I have a specific pain at the moment. The code sample I gave in the first
post is still not working, although the HTML post is (thank you: that gave
me a feel good factor that this project actually works). The advise to use
Firebug was very good and caused me to add a couple of headers.
DefaultHttpClient client = new DefaultHttpClient();
client.getCredentialsProvider().setCredentials(new AuthScope("localhost",
8080), new UsernamePasswordCredentials("admin", "admin"));
String nodeName = "newNodeb8";
List<NameValuePair> formParams = Arrays.<NameValuePair> asList(new
BasicNameValuePair("param1", "value1"), new BasicNameValuePair("param2",
"value2"));
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formParams, "UTF-8");
HttpPost action = new HttpPost("http://localhost:8080/" + nodeName);
action.addHeader("Content-Type", "application/x-www-form-urlencoded");
action.addHeader("enctype", "multipart/form-data");
action.setEntity(entity);
HttpResponse response = client.execute(action);
System.out.println("Status: " + response.getStatusLine().getStatusCode());
System.out.println("Reason: " + response.getStatusLine().getReasonPhrase());
System.out.println("Reason: " + response.getStatusLine());
HttpEntity responseEntity = response.getEntity();
String page = responseEntity == null ? "<Empty Response>" :
EntityUtils.toString(responseEntity);
System.out.println("Response");
System.out.println(page);
The result is:
Status: 500
Reason: Internal Server Error
Reason: HTTP/1.1 500 Internal Server Error
Response
<html>
<head>
<title>Error while processing /newNodeb8</title>
</head>
<body>
<h1>Error while processing /newNodeb8</h1>
<table>
<tbody>
<tr>
<td>Status</td>
<td><div id="Status">500</div></td>
</tr>
<tr>
<td>Message</td>
<td><div id="Message">javax.jcr.AccessDeniedException:
/newNodeb8/param2: not allowed to add or modify item</div></td>
</tr>
<tr>
<td>Location</td>
<td><a href="/newNodeb8" id="Location">/newNodeb8</a></td>
</tr>
<tr>
<td>Parent Location</td>
<td><a href="/" id="ParentLocation">/</a></td>
</tr>
<tr>
<td>Path</td>
<td><div id="Path">/newNodeb8</div></td>
</tr>
<tr>
<td>Referer</td>
<td><a href="" id="Referer"></a></td>
</tr>
<tr>
<td>ChangeLog</td>
<td><div
id="ChangeLog"><pre>created("/newNodeb8");<br/>modified("/newNodeb8/param1");<br/>modified("/newNodeb8/param2");<br/></pre></div></td>
</tr>
</tbody>
</table>
<p><a href="">Go Back</a></p>
<p><a href="/newNodeb8">Modified Resource</a></p>
<p><a href="/">Parent of Modified Resource</a></p>
</body>
</html>
The response I get indicates that I have a security problem. I suspect that
there is an error in the credentials. Of course as luck would have it the
apache http client API has just had a radical rewrite and all the tutorials
and advise are for the old interface... Do any of you have a code snippet
like this that actually works?
When I get this working, I'll write it up as a tutorial about using it from
Java!
On Thu, Jul 7, 2011 at 2:32 PM, Mark Herman <[email protected]> wrote:
> "You could probably use their tools to create bundles then just deploy the
> bundles to your sling instance."
>
> Just need to say that using their tools in this manor may violate the
> developer edition's license agreement, I don't know. I'd still suggest
> looking into the product to help learn the language, but make sure you
> carefully read the license agreement before actually deploying bundles to
> sling.
>
> -----Original Message-----
> From: Mark Herman
> Sent: Thursday, July 07, 2011 9:04 AM
> To: [email protected]
> Subject: RE: Beginners question with Sling.
>
> In reference to ESP vs JSP:
>
> You may be able to use Day CRX (now Adobe) documentation as a resource.
>
> They have a series of blogs called "CRX Gems." Some are product specific
> but
> most are really just taking advantage of sling.
>
> http://dev.day.com/content/ddc/blog.html?_charset_=ISO-8859-1&query=crx+gems&
> main_entries_start=15&blog=search&y=0&x=0
> Page back into newer topics as well.
>
> Here are some links that I have stashed away that may be of interest to
> you:
> http://dev.day.com/content/ddc/blog/2010/07/learning_about_espv.html
> https://cwiki.apache.org/SLING/scripting-variables.html
>
> http://www.lucamasini.net/Home/sling-and-cq5/accessing-relational-data-as-sli
> ng-restful-urls
>
> I recently looked into whether to go with ESP or JSP and decided to go with
> JSP for the following reasons:
> - JSP is more standard, easier for others to jump on a project
> - As you have found documentation for ESP is scarce.
> - I wasn't able to find any advantages of using ESP besides that the
> common
> sling objects are provided for you. In JSP you just have to include a
> taglib
> and call the call sling:define objects.
> - It seems the people who write/use the product tend to use JSP more and
> are
> starting to shy away from ESP.
> - IDE support should be a bit better with JSP
>
> I don't think there is anything wrong with ESP, but it just seems like it
> doesn't have any major advantage make it a must have.
>
> A couple of discussions I have found:
> http://www.mail-archive.com/[email protected]/msg01105.html
> http://tech.groups.yahoo.com/group/Day-Communique/message/2464
>
>
> Sorry I can't help with your issues. As you probably have guessed most my
> experience is with Day/Adobe's product, so I suggest you may want to get a
> license for a developer edition of CRX. It's free (last i checked), and is
> basically a jackrabbit repository with sling on top plus their tools. They
> have an Eclipse ide with debugging, a node browser, and a web based ide.
> It's very easy to install, although you will need to register for a free
> license. Also note, eclipse ide is a separate download. You could probably
> use their tools to create bundles then just deploy the bundles to your
> sling
> instance.
>
> Good luck.
>
> -----Original Message-----
> From: Phil Rice [mailto:[email protected]]
> Sent: Thu 7/7/2011 3:56 AM
> To: [email protected]
> Subject: Re: Beginners question with Sling.
>
> Thanks for the swift reply
>
> - I'll raise a bug about "Sling in 15 minutes" as soon as I can make a
> unit test to capture it.
> - I'm hoping someone can point me to a tutorial or reference materials
> for ESP. Playing with javascript through reflection is hard work!
> - Thanks for the idea of running Sling in the same OSGI container. I
> suspect that won't work for crowd sourcing (you want one central
> repository), but I think it would be an excellent way to learn how
> everything ties together
> - I was puzzled that PUT was the wrong method. As I understand it PUT is
> the mechanism to "create or replace" in REST. However I replaced the call
> to
> a post, and I just get internal server error 500s with nothing appearing
> in
> the logs. I will chase that down when I have sorted out the next point:
> - Assuming that there is a problem in my understanding in what is going
> on, I reduced to the simplest thing that could wrong and produced the
> following HTML page
>
> <html>
> <head><title>Testing</title></head>
> <body>
> <form action="http://localhost:8080/newNode1" method="post">
> <label for="name1">Name1</label> <input type="text" id="name1"
> value="Value1" /><br />
> <label for="name2">Name3</label> <input type="text" id="name2"
> value="Value2" /><br />
> <label for="name3">Name4</label> <input type="text" id="name3"
> value="Value3" /><br />
> <label for="name4">Name5</label> <input type="text" id="name4"
> value="Value4" /><br />
> <button>Submit</button>
> </form>
> </body>
> </html>
> My hope is that when I click the button this would create newNode1 with
> attributes name1 = Value1 etc. And indeed (after logging in) it created
> newNode1. However when I examine the node created using the url
> http://localhost:8080/newNode1.json I find the following page:
>
> {"jcr:primaryType":"nt:unstructured"}
>
> However if I use
>
> curl -u admin:admin -F"name1=value1" -F"name2=value2"
> http://localhost:8080/newNodeQ1
>
> And surf to http://localhost:8080/newNodeQ1.json I find
>
> {"name2":"value2","jcr:primaryType":"nt:unstructured","name1":"value1"}
>
> I would appreciate some advice on how to get the HTML to work similar to
> curl
>
> Thanks again for the help
> ---------------
>
> My software stack is:
>
> - Windows 7 64 bit
> - JDK1.6.0_24
> - Eclipse Indigo Release build 20110615-0604
> - I am using jar "org.apache.sling.launchpad.base.jar", which I
> downloaded earlier this week.
> - I am running the jar using Eclipse using only defaults (no VM
> arguments, no program arguments). The main method is from
> class org.apache.sling.launchpad.app.Main
>
> The console output from sling is
>
> 07.07.2011 07:54:18.570 *INFO* [main] Setting sling.home=sling (default)
> 07.07.2011 07:54:18.572 *INFO* [main] Starting Sling in sling
> (C:\Users\Phil\workspace\Sling\sling)
> 07.07.2011 07:54:18.620 *INFO* [main] Checking launcher JAR in folder sling
> 07.07.2011 07:54:18.640 *INFO* [main] Existing launcher is up to date,
> using
> it: 2.3.0 (org.apache.sling.launchpad.base.jar)
> 07.07.2011 07:54:18.642 *INFO* [main] Loading launcher class
> org.apache.sling.launchpad.base.app.MainDelegate from
> org.apache.sling.launchpad.base.jar
> 07.07.2011 07:54:18.662 *INFO* [main] Starting launcher ...
> 07.07.2011 07:54:18.666 *INFO* [main] HTTP server port: 8080
> 07.07.2011 07:54:20.736 *INFO* [main] Startup completed
>
>
>
>
> On Wed, Jul 6, 2011 at 11:02 PM, Justin Edelson
> <[email protected]>wrote:
>
> >
> >
> > On Jul 6, 2011, at 5:16 PM, Phil Rice <[email protected]>
> > wrote:
> >
> > > Hello everyone.
> >
> > Hi
> >
> > >
> > > I have just downloaded Sling, and am very impressed with it. I am
> > planning
> > > on using it in conjunction with an Eclipse plugin to crowd source
> > > information about open source projects.
> >
> > Sounds interesting. Are you able to reuse code between the server side
> and
> > client side as both Sling and Eclipse are OSGi based?
> >
> > >
> > > I am having the usual teething problems that I get whenever I use an
> open
> > > source product, and I am hoping to get some assistance. I have spent
> > three
> > > days getting going, so I have tried quite a few combinations.
> > >
> > > Some general information for you "getting going with sling in 15
> minutes"
> > > was very helpful. I have a version of Sling built from the source code
> > using
> > > Maven, and a second which is the standalone Jar. On both of them the
> > > following curl command didn't work:
> > >
> > > curl -u admin:admin -F"sling:resourceType=foo/bar" -F"title=some
> > > title" http://localhost:8080/content/mynode
> > >
> > >
> > > I had to replace it with:
> > >
> > >
> > > curl -u admin:admin -F"sling:resourceType=foo/bar" -F"title=some
> > > title" http://localhost:8080/mynode
> >
> > This should have worked. Can you file a bug report?
> >
> > >
> > >
> > > At the moment my Sling-fu is weak and I am not sure if this is some
> > > configuration problem.
> > > ---------------
> > >
> > > I am now at the point at which I feel the master of Sling (pride comes
> > > before a fall) using Curl, so I have moved to Java. I have tried a
> number
> > of
> > > Java libraries include the Apache HttpClient, http-unit and
> Rest-assured.
> > I
> > > am now at the point at which I can get information out of Sling that I
> > put
> > > in with Curl, but I am unable to programatically add it. Specifically I
> > can
> > > create the node, but the attributes do not appear. Fairly obviously I
> am
> > > missing some critical configuration
> > >
> > > So I have the following questions:
> > >
> > > - ESP looks very nice. Where can I find information on how to use it.
> I
> > > have found some samples using it, but have not been very successful
> in
> > > finding tutorials / reference material.
> > > - What is the Java side client software that you recommend for use
> with
> > > Sling, and do you have any example code?
> >
> > The majority of the Sling integration test suite is written with Apache
> > HttpClient 3.x. That's a good point if reference.
> >
> > > - Can you advise why the following code does not set param1=value1,
> > > param2=value2?
> > >
> >
> > You're doing a PUT. I'm assuming you are using the default servlets and
> the
> > functionality which accepts form parameters and sets node properties from
> > them is in the default POST servlet. Of course, you're free to write your
> > own servlet which accepts PUTs.
> >
> > HTH,
> > Justin
> > >
> > > import org.apache.http.*;
> > > public class PopulateRepository {
> > > public static void main(String[] args) throws Exception {
> > > BasicCredentialsProvider credentialsProvider = new
> > > BasicCredentialsProvider();
> > > credentialsProvider.setCredentials(AuthScope.ANY, new
> > > UsernamePasswordCredentials("admin", "admin"));
> > > DefaultHttpClient client = new DefaultHttpClient();
> > > client.setCredentialsProvider(credentialsProvider);
> > >
> > > String nodeName = "newNodeName";
> > > List<NameValuePair> formParams = Arrays.<NameValuePair> asList(new
> > > BasicNameValuePair("param1", "value1"), new
> BasicNameValuePair("param2",
> > > "value2"));
> > > UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formParams,
> > "UTF-8");
> > > HttpPut action = new HttpPut("http://localhost:8080/" + nodeName);
> > > HttpResponse response = client.execute(action);
> > > }}
> > >
> > > Thanks for making such an interesting product
> >
> >
> >
>
>
>