You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@sling.apache.org by Ben Short <be...@benshort.co.uk> on 2010/01/29 12:25:40 UTC
Storing anonymous user data
Hi,
I want to store data for anonymous users, such as the contents of a shopping
cart.
When the user comes to the site a script will look for a path contained in a
cookie. If this cookie present then any 'add to basket' forms will post to
it and any requests to show the basket will pull content from this path.
If the cookie is not present then a new unique path will be created for this
user and the cookie will be returned in the response.
Another way I can see it working is the basket contents is
stored entirely on the client side in a cookie.
Can anyone give me any pointers on best practices of how to
store anonymous user data?
Regards
Ben Short
Re: Storing anonymous user data
Posted by Felix Meschberger <fm...@gmail.com>.
Hi,
If you are using the @scr.* tags you have to use the maven-scr-plugin to
generate the SCR descriptor file. Otherwise the component will not be
known and registered....
This is what I interpret from the log actually.
Regards
Felix
On 29.01.2010 15:08, Ben Short wrote:
> Seems to deploy fine..
>
> 29.01.2010 14:06:52.747 *INFO* [FelixDispatchQueue]
> com.daisytechnologies.spike.website-spike BundleEvent STOPPED
> 29.01.2010 14:06:52.762 *INFO* [FelixDispatchQueue]
> com.daisytechnologies.spike.website-spike BundleEvent UNRESOLVED
> 29.01.2010 14:06:52.762 *INFO* [FelixDispatchQueue]
> com.daisytechnologies.spike.website-spike BundleEvent UPDATED
> 29.01.2010 14:06:52.762 *INFO* [FelixDispatchQueue]
> org.apache.felix.framework FrameworkEvent PACKAGES REFRESHED
> 29.01.2010 14:06:52.762 *INFO* [FelixDispatchQueue]
> com.daisytechnologies.spike.website-spike BundleEvent RESOLVED
> 29.01.2010 14:06:52.856 *INFO* [Background Update
> com.daisytechnologies.spike.website-spike (52)]
> org.apache.sling.jcr.contentloader.internal.ContentLoaderService createFile:
> Cannot find content type
> for GET.jsp, using application/octet-stream
> 29.01.2010 14:06:52.856 *INFO* [Background Update
> com.daisytechnologies.spike.website-spike (52)]
> org.apache.sling.jcr.contentloader.internal.ContentLoaderService createFile:
> Cannot find content type
> for xml.jsp, using application/octet-stream
> 29.01.2010 14:06:52.856 *INFO* [Background Update
> com.daisytechnologies.spike.website-spike (52)]
> org.apache.sling.jcr.contentloader.internal.ContentLoaderService createFile:
> Cannot find content type
> for GET.jsp, using application/octet-stream
> 29.01.2010 14:06:53.012 *INFO* [FelixDispatchQueue]
> com.daisytechnologies.spike.website-spike BundleEvent STARTED
>
> And the logging when I perform the post
>
> 29.01.2010 14:08:16.419 *INFO* [127.0.0.1 [1264774096419] POST
> /mollycupcakes/baskets HTTP/1.1] logs/request.log 29/Jan/2010:14:08:16 +0000
> [102] -> POST /mollycupcakes/baskets HTTP/1.1
> 29.01.2010 14:08:16.434 *INFO* [127.0.0.1 [1264774096419] POST
> /mollycupcakes/baskets HTTP/1.1] logs/request.log 29/Jan/2010:14:08:16 +0000
> [102] <- 500 text/html 15ms
> 29.01.2010 14:08:16.434 *INFO* [127.0.0.1 [1264774096419] POST
> /mollycupcakes/baskets HTTP/1.1] logs/access.log 127.0.0.1 - anonymous
> 29/Jan/2010:14:08:16 +0000 "POST /mollycupcakes/baskets HTTP/1.1"
> 500 1992 "-" "curl/7.19.5 (i586-pc-mingw32msvc) libcurl/7.19.5
> OpenSSL/0.9.8j zlib/1.2.3"
>
> 2010/1/29 Vidar Ramdal <vi...@idium.no>
>
>> On Fri, Jan 29, 2010 at 2:51 PM, Ben Short <be...@benshort.co.uk> wrote:
>>> Thank you for the response.
>>>
>>> I have the following rough class.
>>>
>>> package com.daisytechnologies.basket;
>>>
>>> import org.apache.sling.api.SlingHttpServletRequest;
>>> import org.apache.sling.api.servlets.HtmlResponse;
>>> import org.apache.sling.servlets.post.SlingPostOperation;
>>> import org.apache.sling.servlets.post.SlingPostProcessor;
>>>
>>> import javax.jcr.Node;
>>> import javax.jcr.RepositoryException;
>>> import javax.jcr.Session;
>>> import javax.jcr.SimpleCredentials;
>>>
>>> /**
>>> *
>>> * @scr.component metatype="no" immediate="true"
>>> * @scr.service
>>> interface="org.apache.sling.servlets.post.SlingPostOperation"
>>> * @scr.property name="sling.post.operation" value="createBasket"
>>> */
>>> public class CreateBasketPostOperation implements SlingPostOperation {
>>> public void run(SlingHttpServletRequest request, HtmlResponse
>>> htmlResponse, SlingPostProcessor[] slingPostProcessors) {
>>>
>>> Session session =
>>> request.getResourceResolver().adaptTo(Session.class);
>>>
>>> try {
>>> Session superSession = session.getRepository().login(new
>>> SimpleCredentials("admin", "admin".toCharArray()));
>>>
>>> final String path = request.getResource().getPath();
>>>
>>> Node basketsNode = (Node)superSession.getItem(path);
>>>
>>> Node basketNode =
>>> basketsNode.addNode(Long.toString(System.currentTimeMillis()));
>>>
>>> htmlResponse.onCreated(basketNode.getPath());
>>>
>>> superSession.save();
>>> superSession.logout();
>>> } catch (RepositoryException e) {
>>> e.printStackTrace();
>>> }
>>>
>>> }
>>> }
>>>
>>> And the following in my pom
>>>
>>> <plugin>
>>> <groupId>org.apache.felix</groupId>
>>> <artifactId>maven-bundle-plugin</artifactId>
>>> <extensions>true</extensions>
>>> <configuration>
>>> <instructions>
>>>
>>>
>> <Sling-Initial-Content>initial-content;overwrite:=true;uninstall=true</Sling-Initial-Content>
>>> <Private-Package>>
>>> com.daisytechnologies.basket
>>> </Private-Package>>
>>> </instructions>
>>> </configuration>
>>> </plugin>
>>>
>>> the bundle deploys ok but when I execute the following command
>>>
>>> curl -F":operation=addBasket"
>> http://127.0.0.1:8080/mollycupcakes/baskets
>>>
>>> I get the following error..
>>>
>>> Status
>>> 500
>>> Message
>>> Invalid operation specified for POST request
>>> Location <http://127.0.0.1:8080/mollycupcakes/baskets>Parent
>>> Location<http://127.0.0.1:8080/mollycupcakes/baskets>
>>> Path
>>> Refererhttp://127.0.0.1:8080/mollycupcakesChangeLog
>>>
>>>
>>> any ideas where im going wrong?
>>
>> Check the Felix web console
>> (http://localhost:8080/system/console/bundles) and see that your
>> bundle is listed there, and is "active". If it is not, click to expand
>> it and check if any of the imports are listed in red.
>> You can also tail your sling.log, and see what is output when you
>> start your bundle.
>>
>>> Also how do i inject the SlingRepository into the post operation?
>>
>> You just declare it with a /** @scr.reference */ annotation:
>>
>> /** @scr.reference */
>> private SlingRepository repository;
>>
>> --
>> Vidar S. Ramdal <vi...@idium.no> - http://www.idium.no
>> Sommerrogata 13-15, N-0255 Oslo, Norway
>> + 47 22 00 84 00 / +47 21 531941, ext 2070
>>
>
Re: Storing anonymous user data
Posted by Ben Short <be...@benshort.co.uk>.
Seems to deploy fine..
29.01.2010 14:06:52.747 *INFO* [FelixDispatchQueue]
com.daisytechnologies.spike.website-spike BundleEvent STOPPED
29.01.2010 14:06:52.762 *INFO* [FelixDispatchQueue]
com.daisytechnologies.spike.website-spike BundleEvent UNRESOLVED
29.01.2010 14:06:52.762 *INFO* [FelixDispatchQueue]
com.daisytechnologies.spike.website-spike BundleEvent UPDATED
29.01.2010 14:06:52.762 *INFO* [FelixDispatchQueue]
org.apache.felix.framework FrameworkEvent PACKAGES REFRESHED
29.01.2010 14:06:52.762 *INFO* [FelixDispatchQueue]
com.daisytechnologies.spike.website-spike BundleEvent RESOLVED
29.01.2010 14:06:52.856 *INFO* [Background Update
com.daisytechnologies.spike.website-spike (52)]
org.apache.sling.jcr.contentloader.internal.ContentLoaderService createFile:
Cannot find content type
for GET.jsp, using application/octet-stream
29.01.2010 14:06:52.856 *INFO* [Background Update
com.daisytechnologies.spike.website-spike (52)]
org.apache.sling.jcr.contentloader.internal.ContentLoaderService createFile:
Cannot find content type
for xml.jsp, using application/octet-stream
29.01.2010 14:06:52.856 *INFO* [Background Update
com.daisytechnologies.spike.website-spike (52)]
org.apache.sling.jcr.contentloader.internal.ContentLoaderService createFile:
Cannot find content type
for GET.jsp, using application/octet-stream
29.01.2010 14:06:53.012 *INFO* [FelixDispatchQueue]
com.daisytechnologies.spike.website-spike BundleEvent STARTED
And the logging when I perform the post
29.01.2010 14:08:16.419 *INFO* [127.0.0.1 [1264774096419] POST
/mollycupcakes/baskets HTTP/1.1] logs/request.log 29/Jan/2010:14:08:16 +0000
[102] -> POST /mollycupcakes/baskets HTTP/1.1
29.01.2010 14:08:16.434 *INFO* [127.0.0.1 [1264774096419] POST
/mollycupcakes/baskets HTTP/1.1] logs/request.log 29/Jan/2010:14:08:16 +0000
[102] <- 500 text/html 15ms
29.01.2010 14:08:16.434 *INFO* [127.0.0.1 [1264774096419] POST
/mollycupcakes/baskets HTTP/1.1] logs/access.log 127.0.0.1 - anonymous
29/Jan/2010:14:08:16 +0000 "POST /mollycupcakes/baskets HTTP/1.1"
500 1992 "-" "curl/7.19.5 (i586-pc-mingw32msvc) libcurl/7.19.5
OpenSSL/0.9.8j zlib/1.2.3"
2010/1/29 Vidar Ramdal <vi...@idium.no>
> On Fri, Jan 29, 2010 at 2:51 PM, Ben Short <be...@benshort.co.uk> wrote:
> > Thank you for the response.
> >
> > I have the following rough class.
> >
> > package com.daisytechnologies.basket;
> >
> > import org.apache.sling.api.SlingHttpServletRequest;
> > import org.apache.sling.api.servlets.HtmlResponse;
> > import org.apache.sling.servlets.post.SlingPostOperation;
> > import org.apache.sling.servlets.post.SlingPostProcessor;
> >
> > import javax.jcr.Node;
> > import javax.jcr.RepositoryException;
> > import javax.jcr.Session;
> > import javax.jcr.SimpleCredentials;
> >
> > /**
> > *
> > * @scr.component metatype="no" immediate="true"
> > * @scr.service
> > interface="org.apache.sling.servlets.post.SlingPostOperation"
> > * @scr.property name="sling.post.operation" value="createBasket"
> > */
> > public class CreateBasketPostOperation implements SlingPostOperation {
> > public void run(SlingHttpServletRequest request, HtmlResponse
> > htmlResponse, SlingPostProcessor[] slingPostProcessors) {
> >
> > Session session =
> > request.getResourceResolver().adaptTo(Session.class);
> >
> > try {
> > Session superSession = session.getRepository().login(new
> > SimpleCredentials("admin", "admin".toCharArray()));
> >
> > final String path = request.getResource().getPath();
> >
> > Node basketsNode = (Node)superSession.getItem(path);
> >
> > Node basketNode =
> > basketsNode.addNode(Long.toString(System.currentTimeMillis()));
> >
> > htmlResponse.onCreated(basketNode.getPath());
> >
> > superSession.save();
> > superSession.logout();
> > } catch (RepositoryException e) {
> > e.printStackTrace();
> > }
> >
> > }
> > }
> >
> > And the following in my pom
> >
> > <plugin>
> > <groupId>org.apache.felix</groupId>
> > <artifactId>maven-bundle-plugin</artifactId>
> > <extensions>true</extensions>
> > <configuration>
> > <instructions>
> >
> >
> <Sling-Initial-Content>initial-content;overwrite:=true;uninstall=true</Sling-Initial-Content>
> > <Private-Package>>
> > com.daisytechnologies.basket
> > </Private-Package>>
> > </instructions>
> > </configuration>
> > </plugin>
> >
> > the bundle deploys ok but when I execute the following command
> >
> > curl -F":operation=addBasket"
> http://127.0.0.1:8080/mollycupcakes/baskets
> >
> > I get the following error..
> >
> > Status
> > 500
> > Message
> > Invalid operation specified for POST request
> > Location <http://127.0.0.1:8080/mollycupcakes/baskets>Parent
> > Location<http://127.0.0.1:8080/mollycupcakes/baskets>
> > Path
> > Refererhttp://127.0.0.1:8080/mollycupcakesChangeLog
> >
> >
> > any ideas where im going wrong?
>
> Check the Felix web console
> (http://localhost:8080/system/console/bundles) and see that your
> bundle is listed there, and is "active". If it is not, click to expand
> it and check if any of the imports are listed in red.
> You can also tail your sling.log, and see what is output when you
> start your bundle.
>
> > Also how do i inject the SlingRepository into the post operation?
>
> You just declare it with a /** @scr.reference */ annotation:
>
> /** @scr.reference */
> private SlingRepository repository;
>
> --
> Vidar S. Ramdal <vi...@idium.no> - http://www.idium.no
> Sommerrogata 13-15, N-0255 Oslo, Norway
> + 47 22 00 84 00 / +47 21 531941, ext 2070
>
Re: Storing anonymous user data
Posted by Felix Meschberger <fm...@gmail.com>.
Hi,
It works without the "<executions>" stuff, because we have the plugin in
the pluginManagement of the parent POM in Sling, while I assume you
don't have such a configuration.
Therefore you have to explicitly configure it in your project.
Regards
Felix
On 29.01.2010 15:43, Ben Short wrote:
> Altering the scr plugin config to the following makes it generate
> the Service Component Descriptors and add them to the pom.
>
> <plugin>
> <groupId>org.apache.felix</groupId>
> <artifactId>maven-scr-plugin</artifactId>
> <version>1.4.2</version>
> <executions>
> <execution>
> <id>generate-scr-scrdescriptor</id>
> <goals>
> <goal>scr</goal>
> </goals>
> </execution>
> </executions>
> </plugin>
>
> I'm not sure why it works with out the executions bit in the sample
> application.
>
> 2010/1/29 Ben Short <be...@benshort.co.uk>
>
>> OK I have been looking at the Sample Post Servlet Extensions and my pom is
>> pretty much the same.
>>
>> <build>
>> <plugins>
>> <plugin>
>> <groupId>org.apache.maven.plugins</groupId>
>> <artifactId>maven-compiler-plugin</artifactId>
>> <configuration>
>> <source>1.5</source>
>> <target>1.5</target>
>> </configuration>
>> </plugin>
>> <plugin>
>> <groupId>org.apache.maven.plugins</groupId>
>> <artifactId>maven-dependency-plugin</artifactId>
>> </plugin>
>> <plugin>
>> <groupId>org.apache.felix</groupId>
>> <artifactId>maven-scr-plugin</artifactId>
>> <version>1.4.2</version>
>> </plugin>
>> <plugin>
>> <groupId>org.apache.felix</groupId>
>> <artifactId>maven-bundle-plugin</artifactId>
>> <extensions>true</extensions>
>> <configuration>
>> <instructions>
>>
>> <Sling-Initial-Content>initial-content;overwrite:=true;uninstall=true</Sling-Initial-Content>
>> <Private-Package>
>> com.daisytechnologies.basket
>> </Private-Package>
>> </instructions>
>> </configuration>
>> </plugin>
>> </plugins>
>> </build>
>>
>> When I do a mvn package on the samples module I see that
>> a scr-plugin-generated directory is created. But when I do a mvn package on
>> my app it not created.
>>
>>
>> 2010/1/29 Bertrand Delacretaz <bd...@apache.org>
>>
>>> On Fri, Jan 29, 2010 at 2:59 PM, Vidar Ramdal <vi...@idium.no> wrote:
>>>
>>>> ...Check the Felix web console
>>>> (http://localhost:8080/system/console/bundles) and see that your
>>>> bundle is listed there, and is "active". If it is not, click to expand
>>>> it and check if any of the imports are listed in red.
>>>> You can also tail your sling.log, and see what is output when you
>>>> start your bundle....
>>>
>>> And the "components" page of the console should also show your
>>> CreateBasketPostOperation as active.
>>>
>>> -Bertrand
>>>
>>
>>
>
Re: Storing anonymous user data
Posted by Ben Short <be...@benshort.co.uk>.
Altering the scr plugin config to the following makes it generate
the Service Component Descriptors and add them to the pom.
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-scr-plugin</artifactId>
<version>1.4.2</version>
<executions>
<execution>
<id>generate-scr-scrdescriptor</id>
<goals>
<goal>scr</goal>
</goals>
</execution>
</executions>
</plugin>
I'm not sure why it works with out the executions bit in the sample
application.
2010/1/29 Ben Short <be...@benshort.co.uk>
> OK I have been looking at the Sample Post Servlet Extensions and my pom is
> pretty much the same.
>
> <build>
> <plugins>
> <plugin>
> <groupId>org.apache.maven.plugins</groupId>
> <artifactId>maven-compiler-plugin</artifactId>
> <configuration>
> <source>1.5</source>
> <target>1.5</target>
> </configuration>
> </plugin>
> <plugin>
> <groupId>org.apache.maven.plugins</groupId>
> <artifactId>maven-dependency-plugin</artifactId>
> </plugin>
> <plugin>
> <groupId>org.apache.felix</groupId>
> <artifactId>maven-scr-plugin</artifactId>
> <version>1.4.2</version>
> </plugin>
> <plugin>
> <groupId>org.apache.felix</groupId>
> <artifactId>maven-bundle-plugin</artifactId>
> <extensions>true</extensions>
> <configuration>
> <instructions>
>
> <Sling-Initial-Content>initial-content;overwrite:=true;uninstall=true</Sling-Initial-Content>
> <Private-Package>
> com.daisytechnologies.basket
> </Private-Package>
> </instructions>
> </configuration>
> </plugin>
> </plugins>
> </build>
>
> When I do a mvn package on the samples module I see that
> a scr-plugin-generated directory is created. But when I do a mvn package on
> my app it not created.
>
>
> 2010/1/29 Bertrand Delacretaz <bd...@apache.org>
>
>> On Fri, Jan 29, 2010 at 2:59 PM, Vidar Ramdal <vi...@idium.no> wrote:
>>
>> > ...Check the Felix web console
>> > (http://localhost:8080/system/console/bundles) and see that your
>> > bundle is listed there, and is "active". If it is not, click to expand
>> > it and check if any of the imports are listed in red.
>> > You can also tail your sling.log, and see what is output when you
>> > start your bundle....
>>
>> And the "components" page of the console should also show your
>> CreateBasketPostOperation as active.
>>
>> -Bertrand
>>
>
>
Re: Storing anonymous user data
Posted by Ben Short <be...@benshort.co.uk>.
OK I have been looking at the Sample Post Servlet Extensions and my pom is
pretty much the same.
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-scr-plugin</artifactId>
<version>1.4.2</version>
</plugin>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<extensions>true</extensions>
<configuration>
<instructions>
<Sling-Initial-Content>initial-content;overwrite:=true;uninstall=true</Sling-Initial-Content>
<Private-Package>
com.daisytechnologies.basket
</Private-Package>
</instructions>
</configuration>
</plugin>
</plugins>
</build>
When I do a mvn package on the samples module I see that
a scr-plugin-generated directory is created. But when I do a mvn package on
my app it not created.
2010/1/29 Bertrand Delacretaz <bd...@apache.org>
> On Fri, Jan 29, 2010 at 2:59 PM, Vidar Ramdal <vi...@idium.no> wrote:
> > ...Check the Felix web console
> > (http://localhost:8080/system/console/bundles) and see that your
> > bundle is listed there, and is "active". If it is not, click to expand
> > it and check if any of the imports are listed in red.
> > You can also tail your sling.log, and see what is output when you
> > start your bundle....
>
> And the "components" page of the console should also show your
> CreateBasketPostOperation as active.
>
> -Bertrand
>
Re: Storing anonymous user data
Posted by Bertrand Delacretaz <bd...@apache.org>.
On Fri, Jan 29, 2010 at 2:59 PM, Vidar Ramdal <vi...@idium.no> wrote:
> ...Check the Felix web console
> (http://localhost:8080/system/console/bundles) and see that your
> bundle is listed there, and is "active". If it is not, click to expand
> it and check if any of the imports are listed in red.
> You can also tail your sling.log, and see what is output when you
> start your bundle....
And the "components" page of the console should also show your
CreateBasketPostOperation as active.
-Bertrand
Re: Storing anonymous user data
Posted by Vidar Ramdal <vi...@idium.no>.
On Fri, Jan 29, 2010 at 2:51 PM, Ben Short <be...@benshort.co.uk> wrote:
> Thank you for the response.
>
> I have the following rough class.
>
> package com.daisytechnologies.basket;
>
> import org.apache.sling.api.SlingHttpServletRequest;
> import org.apache.sling.api.servlets.HtmlResponse;
> import org.apache.sling.servlets.post.SlingPostOperation;
> import org.apache.sling.servlets.post.SlingPostProcessor;
>
> import javax.jcr.Node;
> import javax.jcr.RepositoryException;
> import javax.jcr.Session;
> import javax.jcr.SimpleCredentials;
>
> /**
> *
> * @scr.component metatype="no" immediate="true"
> * @scr.service
> interface="org.apache.sling.servlets.post.SlingPostOperation"
> * @scr.property name="sling.post.operation" value="createBasket"
> */
> public class CreateBasketPostOperation implements SlingPostOperation {
> public void run(SlingHttpServletRequest request, HtmlResponse
> htmlResponse, SlingPostProcessor[] slingPostProcessors) {
>
> Session session =
> request.getResourceResolver().adaptTo(Session.class);
>
> try {
> Session superSession = session.getRepository().login(new
> SimpleCredentials("admin", "admin".toCharArray()));
>
> final String path = request.getResource().getPath();
>
> Node basketsNode = (Node)superSession.getItem(path);
>
> Node basketNode =
> basketsNode.addNode(Long.toString(System.currentTimeMillis()));
>
> htmlResponse.onCreated(basketNode.getPath());
>
> superSession.save();
> superSession.logout();
> } catch (RepositoryException e) {
> e.printStackTrace();
> }
>
> }
> }
>
> And the following in my pom
>
> <plugin>
> <groupId>org.apache.felix</groupId>
> <artifactId>maven-bundle-plugin</artifactId>
> <extensions>true</extensions>
> <configuration>
> <instructions>
>
> <Sling-Initial-Content>initial-content;overwrite:=true;uninstall=true</Sling-Initial-Content>
> <Private-Package>>
> com.daisytechnologies.basket
> </Private-Package>>
> </instructions>
> </configuration>
> </plugin>
>
> the bundle deploys ok but when I execute the following command
>
> curl -F":operation=addBasket" http://127.0.0.1:8080/mollycupcakes/baskets
>
> I get the following error..
>
> Status
> 500
> Message
> Invalid operation specified for POST request
> Location <http://127.0.0.1:8080/mollycupcakes/baskets>Parent
> Location<http://127.0.0.1:8080/mollycupcakes/baskets>
> Path
> Refererhttp://127.0.0.1:8080/mollycupcakesChangeLog
>
>
> any ideas where im going wrong?
Check the Felix web console
(http://localhost:8080/system/console/bundles) and see that your
bundle is listed there, and is "active". If it is not, click to expand
it and check if any of the imports are listed in red.
You can also tail your sling.log, and see what is output when you
start your bundle.
> Also how do i inject the SlingRepository into the post operation?
You just declare it with a /** @scr.reference */ annotation:
/** @scr.reference */
private SlingRepository repository;
--
Vidar S. Ramdal <vi...@idium.no> - http://www.idium.no
Sommerrogata 13-15, N-0255 Oslo, Norway
+ 47 22 00 84 00 / +47 21 531941, ext 2070
Re: Storing anonymous user data
Posted by Justin Edelson <ju...@gmail.com>.
On Jan 29, 2010, at 8:51 AM, Ben Short <be...@benshort.co.uk> wrote:
> Thank you for the response.
>
> I have the following rough class.
>
> package com.daisytechnologies.basket;
>
> import org.apache.sling.api.SlingHttpServletRequest;
> import org.apache.sling.api.servlets.HtmlResponse;
> import org.apache.sling.servlets.post.SlingPostOperation;
> import org.apache.sling.servlets.post.SlingPostProcessor;
>
> import javax.jcr.Node;
> import javax.jcr.RepositoryException;
> import javax.jcr.Session;
> import javax.jcr.SimpleCredentials;
>
> /**
> *
> * @scr.component metatype="no" immediate="true"
> * @scr.service
> interface="org.apache.sling.servlets.post.SlingPostOperation"
> * @scr.property name="sling.post.operation" value="createBasket"
> */
> public class CreateBasketPostOperation implements SlingPostOperation {
> public void run(SlingHttpServletRequest request, HtmlResponse
> htmlResponse, SlingPostProcessor[] slingPostProcessors) {
>
> Session session =
> request.getResourceResolver().adaptTo(Session.class);
>
> try {
> Session superSession = session.getRepository().login(new
> SimpleCredentials("admin", "admin".toCharArray()));
>
> final String path = request.getResource().getPath();
>
> Node basketsNode = (Node)superSession.getItem(path);
>
> Node basketNode =
> basketsNode.addNode(Long.toString(System.currentTimeMillis()));
>
> htmlResponse.onCreated(basketNode.getPath());
>
> superSession.save();
> superSession.logout();
> } catch (RepositoryException e) {
> e.printStackTrace();
> }
>
> }
> }
>
> And the following in my pom
>
> <plugin>
> <groupId>org.apache.felix</groupId>
> <artifactId>maven-bundle-plugin</artifactId>
> <extensions>true</extensions>
> <configuration>
> <instructions>
>
> <Sling-Initial-Content>initial-
> content;overwrite:=true;uninstall=true</Sling-Initial-Content>
> <Private-Package>>
> com.daisytechnologies.basket
> </Private-Package>>
> </instructions>
> </configuration>
> </plugin>
>
> the bundle deploys ok but when I execute the following command
>
> curl -F":operation=addBasket" http://127.0.0.1:8080/mollycupcakes/baskets
>
> I get the following error..
>
> Status
> 500
> Message
> Invalid operation specified for POST request
> Location <http://127.0.0.1:8080/mollycupcakes/baskets>Parent
> Location<http://127.0.0.1:8080/mollycupcakes/baskets>
> Path
> Refererhttp://127.0.0.1:8080/mollycupcakesChangeLog
>
>
> any ideas where im going wrong?
>
Is the SCR XML file being generated? Is the Service registered?
> Also how do i inject the SlingRepository into the post operation?
Take a look at the SCR documentation.
Justin
>
> 2010/1/29 Bertrand Delacretaz <bd...@apache.org>
>
>> On Fri, Jan 29, 2010 at 12:55 PM, Vidar Ramdal <vi...@idium.no>
>> wrote:
>>> .... Node basketsParent =
>> session.getItem(req.getResource().getPath());
>>> Node basket = basketsParent.addNode(/* generate a unique name
>>> */);
>>> ...
>>
>> To "unique name" I'd add "impossible to guess" to prevent people from
>> messing up with other people's baskets.
>>
>> -Bertrand
>>
Re: Storing anonymous user data
Posted by Ben Short <be...@benshort.co.uk>.
Thank you for the response.
I have the following rough class.
package com.daisytechnologies.basket;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.servlets.HtmlResponse;
import org.apache.sling.servlets.post.SlingPostOperation;
import org.apache.sling.servlets.post.SlingPostProcessor;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.SimpleCredentials;
/**
*
* @scr.component metatype="no" immediate="true"
* @scr.service
interface="org.apache.sling.servlets.post.SlingPostOperation"
* @scr.property name="sling.post.operation" value="createBasket"
*/
public class CreateBasketPostOperation implements SlingPostOperation {
public void run(SlingHttpServletRequest request, HtmlResponse
htmlResponse, SlingPostProcessor[] slingPostProcessors) {
Session session =
request.getResourceResolver().adaptTo(Session.class);
try {
Session superSession = session.getRepository().login(new
SimpleCredentials("admin", "admin".toCharArray()));
final String path = request.getResource().getPath();
Node basketsNode = (Node)superSession.getItem(path);
Node basketNode =
basketsNode.addNode(Long.toString(System.currentTimeMillis()));
htmlResponse.onCreated(basketNode.getPath());
superSession.save();
superSession.logout();
} catch (RepositoryException e) {
e.printStackTrace();
}
}
}
And the following in my pom
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<extensions>true</extensions>
<configuration>
<instructions>
<Sling-Initial-Content>initial-content;overwrite:=true;uninstall=true</Sling-Initial-Content>
<Private-Package>>
com.daisytechnologies.basket
</Private-Package>>
</instructions>
</configuration>
</plugin>
the bundle deploys ok but when I execute the following command
curl -F":operation=addBasket" http://127.0.0.1:8080/mollycupcakes/baskets
I get the following error..
Status
500
Message
Invalid operation specified for POST request
Location <http://127.0.0.1:8080/mollycupcakes/baskets>Parent
Location<http://127.0.0.1:8080/mollycupcakes/baskets>
Path
Refererhttp://127.0.0.1:8080/mollycupcakesChangeLog
any ideas where im going wrong?
Also how do i inject the SlingRepository into the post operation?
2010/1/29 Bertrand Delacretaz <bd...@apache.org>
> On Fri, Jan 29, 2010 at 12:55 PM, Vidar Ramdal <vi...@idium.no> wrote:
> > .... Node basketsParent =
> session.getItem(req.getResource().getPath());
> > Node basket = basketsParent.addNode(/* generate a unique name */);
> > ...
>
> To "unique name" I'd add "impossible to guess" to prevent people from
> messing up with other people's baskets.
>
> -Bertrand
>
Re: Storing anonymous user data
Posted by Bertrand Delacretaz <bd...@apache.org>.
On Fri, Jan 29, 2010 at 12:55 PM, Vidar Ramdal <vi...@idium.no> wrote:
> .... Node basketsParent = session.getItem(req.getResource().getPath());
> Node basket = basketsParent.addNode(/* generate a unique name */);
> ...
To "unique name" I'd add "impossible to guess" to prevent people from
messing up with other people's baskets.
-Bertrand
Re: Storing anonymous user data
Posted by Vidar Ramdal <vi...@idium.no>.
On Fri, Jan 29, 2010 at 12:25 PM, Ben Short <be...@benshort.co.uk> wrote:
> I want to store data for anonymous users, such as the contents of a shopping
> cart.
>
> When the user comes to the site a script will look for a path contained in a
> cookie. If this cookie present then any 'add to basket' forms will post to
> it and any requests to show the basket will pull content from this path.
>
> If the cookie is not present then a new unique path will be created for this
> user and the cookie will be returned in the response.
>
> Another way I can see it working is the basket contents is
> stored entirely on the client side in a cookie.
>
> Can anyone give me any pointers on best practices of how to
> store anonymous user data?
You could probably set ACLs so that a node (e.g. /shoppingbaskets) are
writable by anonymous users, and then send a simple POST request to
create new baskets. But it's not trivial to limit the type of content
your users can posts to /shoppingbaskets, so you might risk that
someone uploads a file to it etc.
Instead, I'd write a couple of SlingPostOperations [1] to
create/update baskets. The SlingPostOperations could log in as admin
(via SlingRepository.loginAdministrative()) and make sure the baskets
are created properly.
Some un-tested pseudo-code:
@Component
@Service
@Properties({@Property(name = "sling.post.operation", value = "createBasket")}
public class CreateBasketPostOp implements SlingPostOperation {
@Reference
private SlingRepository repository;
public void run(SlingHttpServletRequest req, HtmlResponse res,
SlingPostProcessors[] processors) {
Session session = repository.loginAdministrative(null);
Node basketsParent = session.getItem(req.getResource().getPath());
Node basket = basketsParent.addNode(/* generate a unique name */);
res.onCreated(basket.getPath());
session.save();
}
}
With this SlngPostOperation in place, you should be able to create a
shoppingbasket for a new, anonymous user by POSTing to
/shoppingbaskets, with the field ":operation" set to "createBasket".
You'll have to parse the resulting HTML to find the path of the
created basket node.
Then you need a PostOp to add products to a basket (I assume you're
getting the cookie from an ESP script or similar, and then include the
cookie value as part of the request URI):
@Component
@Service
@Properties({@Property(name = "sling.post.operation", value = "addToBasket")}
public class AddToBasketPostOp implements SlingPostOperation {
@Reference
private SlingRepository repository;
public void run(SlingHttpServletRequest req, HtmlResponse res,
SlingPostProcessors[] processors) {
Session session = repository.loginAdministrative(null);
Node basket = session.getItem(req.getResource().getPath());
Node product = basket.addNode(/* get ID of product added to
basket from query params */);
res.onCreated(product.getPath());
session.save();
}
}
You would add products to a shopping basket by posting to the basket's
node (e.g. /shoppingbaskets/basket1) with the field ":operation" set
to "addToBasket", and some field to specify the product ID that is to
be added.
You will also need some maintenance service to remove outdated baskets.
The strategy of storing the basket on the client (in a cookie) should
also be possible, but that would probably imply that the entire
shopping basket is sent with every request to your site (including
requests for images, css, javascript etc). This might not be a big
problem, but if the shopping basket is big, and the user has limited
upstream bandwith (a mobile user, for example), this could make
requests slower.
[1] http://sling.apache.org/site/manipulating-content-the-slingpostservlet-servletspost.html
- see "Extending the SlingPostSerlvet" at the bottom
--
Vidar S. Ramdal <vi...@idium.no> - http://www.idium.no
Sommerrogata 13-15, N-0255 Oslo, Norway
+ 47 22 00 84 00 / +47 21 531941, ext 2070