Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id 4C5FB200CAE for ; Wed, 21 Jun 2017 09:22:15 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id 4B555160BF8; Wed, 21 Jun 2017 07:22:15 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id EE185160BF3 for ; Wed, 21 Jun 2017 09:22:12 +0200 (CEST) Received: (qmail 43396 invoked by uid 500); 21 Jun 2017 07:22:11 -0000 Mailing-List: contact commits-help@sling.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@sling.apache.org Delivered-To: mailing list commits@sling.apache.org Received: (qmail 42718 invoked by uid 99); 21 Jun 2017 07:22:10 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 21 Jun 2017 07:22:10 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 72EBFE9669; Wed, 21 Jun 2017 07:22:08 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: bdelacretaz@apache.org To: commits@sling.apache.org Date: Wed, 21 Jun 2017 07:22:24 -0000 Message-Id: In-Reply-To: <9e1b8cb197984545acef77597d3ad19c@git.apache.org> References: <9e1b8cb197984545acef77597d3ad19c@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: [18/25] sling-site git commit: Testing with /ng/ context path for temporary https://sling.apache.org/ng/ setup archived-at: Wed, 21 Jun 2017 07:22:15 -0000 http://git-wip-us.apache.org/repos/asf/sling-site/blob/3a935d04/documentation/bundles/manipulating-content-the-slingpostservlet-servlets-post.html ---------------------------------------------------------------------- diff --git a/documentation/bundles/manipulating-content-the-slingpostservlet-servlets-post.html b/documentation/bundles/manipulating-content-the-slingpostservlet-servlets-post.html index 53be8bd..8cb6cce 100644 --- a/documentation/bundles/manipulating-content-the-slingpostservlet-servlets-post.html +++ b/documentation/bundles/manipulating-content-the-slingpostservlet-servlets-post.html @@ -2,22 +2,22 @@ Apache Sling on JBake - - + +

Manipulating Content - The SlingPostServlet (servlets.post)

[TOC]

Multiple Ways to Modify Content

@@ -25,17 +25,23 @@

What is Content anyway? In the following discussion, I use the terms Content and Item interchangeably. With Content I just mean some data to be stored in the JCR repository to be later used as the basis for some presentation. In this sense Content is a rather conceptual term. Item is the name of the parent interface of the JCR Node and Property interfaces. When speaking of Items we mean some actual data stored in the repository ignoring whether the data is actually stored as a Node with child nodes and properties or just a single Property.

Quickstart: Creating Content

To create content you simply send an HTTP POST request using the path of the node to store the content in and include the actual content as request parameters. So one possibility to do just that is by having an HTML Form like the following:

-
- - -
+
<form method="POST" action="http://host/some/new/content" enctype="multipart/form-data">
+   <input type="text" name="title" value="" />
+   <input type="text" name="text" value="" />
+</form>
+

This simple form will set the title and text properties on a node at /some/new/content. If this node does not exist it is just created otherwise the existing content would be modified.

Similarly, you can do this using the curl command line tool:

-

$ curl -Ftitle="some title text" -Ftext="some body text content" http://host/some/new/content

+
$ curl -Ftitle="some title text" -Ftext="some body text content" http://host/some/new/content
+

You might want to use a specific JCR node type for a newly created node. This is possible by simply setting a jcr:primaryType property on the request, e.g.

-

$ curl -F"jcr:primaryType=nt:unstructured" -Ftitle="some title text" -Ftext="some body text content" http://host/some/new/content

+
$ curl -F"jcr:primaryType=nt:unstructured" -Ftitle="some title text" \    
+    -Ftext="some body text content" http://host/some/new/content
+

Similarly, you may assign JCR mixin node types using the jcr:mixinTypes property and a Sling resource type using the sling:resourceType property. For example:

-

$ curl -F"sling:resourceType=sling:sample" -Ftitle="some title text" -Ftext="some body text content" http://host/some/new/content

+
$ curl -F"sling:resourceType=sling:sample" -Ftitle="some title text" \    
+    -Ftext="some body text content" http://host/some/new/content
+

Preface: multipart/form-data POSTs

Sometimes you might want to have the content modifications applied in a certain order. This is particularly interesting if you use fields to create child nodes and if you want to stipulate a certain child node order based on the form fields.

In this case, ensure you are submitting the POST request using multipart/form-data encoding. This preserves the order of parameter application according to the original HTML form. To this avail, ensure to always include the enctype="multipart/form-data" attribute with the <form> tag.

@@ -60,7 +66,7 @@ Note that the `launchpad/testing` module contains a number of Trailing star in the :applyTo parameter: -This applies to operations that use this parameter, since version 2.1.2 of the *org.apache.sling.servlets.post* bundle: If the last segment of the `:applyTo` value is '*' then the operation applies to all the children of the resolved parent resource. This can be used to act on all the children +This applies to operations that use this parameter, since version 2.1.2 of the *org.apache.sling.servlets.post* bundle: If the last segment of the `:applyTo` value is '*' then the operation applies to all the children of the resolved parent resource. This can be used to act on all the children of a resource without having to specify the path of each individual child resource.

Content Creation or Modification

@@ -114,14 +120,16 @@ of a resource without having to specify the path of each individual child resour
Setting Property Values

Setting property values is as simple as just adding a request parameter whose name is the name of the property to be set and whose value is the value to be assigned to the property. We already saw how to do this in the quick start examples above.

Here is another example show a simple HTML form to create a new node with an automatically created name:

-
- - - -
+
<form method="POST" action="/content/page/first" enctype="multipart/form-data">
+    <input type="text" name="title" />
+    <input type="text" name="text" />
+    <input type="Submit" />
+</form>
+

If this form is submitted with title and This is some Text as values for the title and text fields respectively, a new node is created at the path /content/page/first and the title and text properties set to the respective field values. If a node at /content/page/first already existed before submitting the form, the title and text properties are just updated to the new values from the form fields.

If a parameter has multiple values, the respective property will be created as a multi-value property. So for example the command line:

-

$ curl -Fmulti=one -Fmulti=two http://host/content/page

+
$ curl -Fmulti=one -Fmulti=two http://host/content/page
+

Would assign the /content/page/multi property the value [ "one", "two" ].

This is pretty much all there is to know about creating and modifying content. The following sections will now introduce more functionality which help you with more fine-grained control in your content management application.

Automatic property values: last modified and created by
@@ -152,16 +160,18 @@ of a resource without having to specify the path of each individual child resour

If the node to be created is nt:file, the actual file data will really be stored in the jcr:content child node of the new nt:file node whose primary node type is then set as nt:resource.

Example 1: Upload an image to a node named image below /content/page:

-
- - -
+
<form method="POST" action="/content/page" enctype="multipart/form-data">
+    <input type="file" name="image" />
+    <input type="Submit" />
+</form>
+

Example 2: Upload a file as a node of type nt:file below /content/folder:

-
- - - -
+
<form method="POST" action="/content/page" enctype="multipart/form-data">
+    <input type="file" name="*" />
+    <input type="hidden" name="*@TypeHint" value="nt:file" />
+    <input type="Submit" />
+</form>
+

Assuming the user selected a file named myImage.jpg the uploaded file would be stored in an nt:file node at /content/folder/myImage.jpg.

Date properties

Parameters providing date/time values to be stored in JCR properties of type Date require special handling. The problem is that there are a number of formats to represent such date/time values. To account for this open-ended list of formats, the Sling Post Servlet supports configurability of the process of parsing strings into Calendar objects.

@@ -181,13 +191,14 @@ of a resource without having to specify the path of each individual child resour

There may be times that you have forms which contain a lot of fields which you do not want to actually store in content. Such forms usually are created using some client-side GUI library which uses the fields for its own purposes. To be able to easily differentiate between real content to be actually stored and such control parameters, you may prefix the names of the fields destined for content with a dot-slash (./).

As soon as the SlingPostServlet encounters parameters prefixed with dot-slash, only those parameters are considered for content updates while all other parameters not prefixed are just ignored. In addition to dot-slash prefixed parameters, also parameters prefixed with dot-dot-slash (../) and slash (/) are considered in this situation.

For example, the following form only uses the first two fields for content update and ignores the rest:

-
- - - - - -
+
<form method="POST" action="/content/page/first" enctype="multipart/form-data">
+    <input type="text" name="./title" />
+    <input type="text" name="../first/text" />
+    <input type="hidden" name="control0" /><!-- ignored -->
+    <input type="hidden" name="control1" /><!-- ignored -->
+    <input type="Submit" />
+</form>
+

Because the SlingPostServlet encounters the ./title parameter, only parameters prefixed with dot-slash, dot-dot-slash and slash are considered for content update. In this case this would ./title and ../first/text while control0 and control1 are not prefixed and thus ignored.

Background: The name of the parameters used for content update are actually intended to be relative path names of the properties to modify. So in effect using the field name text is equivalent to ./text -- dot-slash meaning relative to the current node identified by the action attribute value for form tag -- or ../first/text if first is the name of the node to modify -- dot-dot-slash meaning relative to the parent node of the node identified by the action attribute value of the form tag.

In addition to the mechanism explained here, the following parameters are also ignored:

@@ -205,17 +216,18 @@ of a resource without having to specify the path of each individual child resour

Parameters with the @TypeHint suffix may be used to force storing the named parameter in a property with the given type. The value of the @TypeHint parameter, if applied to a parameter for a property, is the JCR property type name. If the @TypeHint parameter is applied to a field upload parameter, the value is used to indicate the JCR primary node type for the node into which the uploaded file is stored.

If the @TypeHint value ends with [], it indicates a multi-value property. A multi-value property is usually auto-detected if there are multiple values for the property (i.e. request parameter). But if only a single value is present in the request, the desired property type needs to be explicitly defined as multi-value by stating @TypeHint=<type>[].

Example: The following form sets the numeric width, the boolean checked, and the multi-valued hobbys (with 3 values to enter) properties:

-
- - - - - - - - - -
+
<form method="POST" action="/content/page/first" enctype="multipart/form-data">
+    <input type="text" name="width" />
+    <input type="hidden" name="width@TypeHint" value="Long" />
+    <input type="checkbox" name="checked" />
+    <input type="hidden" name="checked@TypeHint" value="Boolean" />
+    <input type="text" name="hobbys"/>
+    <input type="text" name="hobbys"/>
+    <input type="text" name="hobbys"/>
+    <input type="hidden" name="hobbys@TypeHint" value="String[]" />
+    <input type="Submit" />
+</form>
+

In real applications you would need some JavaScript that allows to add/remove values, ie. add/remove inputs with the name "hobbys". Or a pure JavaScript based form post would be used, that gathers the properties to update programmatically, but the additional parameter hobbys@TypeHint=String[] would be the same.

The @TypeHint suffixed parameter is assumed to be single-valued. If the parameter has multiple values, only the first is actually used.

For multi-value properties, see also the @Patch option.

@@ -223,45 +235,51 @@ of a resource without having to specify the path of each individual child resour
@DefaultValue

The @DefaultValue suffixed parameter may be provided to set a property to a default value should no value be provided in the actual parameters. Same as for normal parameters, the @DefaultValue parameter may have multiple values to create multi-valued properties.

Example: Set the text property to a default value if the user does not provide one:

-
- - - -
+
<form method="POST" action="/content/page/first" enctype="multipart/form-data">
+    <input type="text" name="text" />
+    <input type="hidden" name="text@DefaultValue" value="--- Default Value ---" />
+    <input type="Submit" />
+</form>
+
@UseDefaultWhenMissing

As described above, @DefaultValue only takes effect if no value is provided for a particular parameter. However, in some cases, such as HTML checkboxes, this isn't sufficient because the parameter isn't submitted at all. To handle this scenario, you can use the @UseDefaultWhenMissing suffixed parameter.

-
- - - -
+
<form method="POST" action="/content/page/first" enctype="multipart/form-data">
+    <input name="queryIgnoreNoise" class="input" type="checkbox" value="true"/>
+    <input type="hidden" name="queryIgnoreNoise@DefaultValue" value="false"/> 
+    <input type="hidden" name="queryIgnoreNoise@UseDefaultWhenMissing" value="true"/>
+</form>
+
@IgnoreBlanks

Sometimes a form client will supply empty parameter values resulting in content being created or modified. For example submitting this form:

-
- - - - -
+
<form method="POST" action="/content/page/first" enctype="multipart/form-data">
+    <input type="hidden" name="stringProperty@TypeHint" value="String[]"/>
+    <input type="text" name="stringProperty" value="foo"/>
+    <input type="text" name="stringProperty" value="bar"/>
+    <input type="text" name="stringProperty" value=""/>
+</form>
+

will result in multi-value String property being set to [ "foo", "bar", "" ]. Notice the blank value.

Likewise submitting this form without a value entered:

-
- - -
+
<form method="POST" action="/content/page/first" enctype="multipart/form-data">
+    <input type="hidden" name="stringProperty@TypeHint" value="String"/>
+    <input type="text" name="stringProperty" value=""/>
+</form>
+

will result in the single-value String property being set to an empty string.

To overcome this situation the @IgnoreBlanks suffix may be used to consider parameters with an empty string value to be ignored during processing. That is such parameter values would be treated as if they would not be supplied.

Adding

- +
<input type="hidden" name="stringProperty@IgnoreBlanks" value="true"/>
+

to the above forms will cause the multi-value property be set to the two-element value [ "foo", "bar" ] and to not modify the property at all in the second single-value example.

@ValueFrom

In some situations, an HTML form with parameters may be reused to update content. But one or more form parameters may not comply with the names expected to be used for properties. In this case a parameter suffixed with @ValueFrom may be set containing the name of the parameter providing the actual data to be used.

Example: To set the property text from a form element supplied_text, you might use the following form:

-
- - - -
+
<form method="POST" action="/content/page/first" enctype="multipart/form-data">
+    <input type="text" name="supplied_text" />
+    <input type="hidden" name="./text@ValueFrom" value="supplied_text" />
+    <input type="Submit" />
+</form>
+

To prevent storing the additional parameters in the repository you might want to use the prefixing mechanism as shown in the example above, where the @ValueFrom parameter is prefixed and thus the supplied_text parameter is not used for property setting.

The @ValueFrom suffixed parameter is assumed to be single-valued. If the parameter has multiple values it is ignored completely.

The @ValueFrom suffixed parameter is also special in that there must not be a correlated parameter without a suffix. Thus have parameters text and text@ValueFrom may have unexpected results.

@@ -269,26 +287,28 @@ of a resource without having to specify the path of each individual child resour

Sometimes it may be required to not set a property to a specific value but to just remove it while processing the content update request. One such situation is a property filled from one or more checkboxes in an HTML form. If none of the checkboxes are checked, no parameter is actually submitted for these checkboxes. Hence the SlingPostServlet will not touch this property and effectively leave it untouched, while the natural reaction would have been to remove the property.

Here comes the @Delete suffixed parameter. This simply causes the indicated property be removed if it exists. If the property does not exist, nothing more happens. The actual value of the @Delete suffixed parameter does not care as long as the parameter is submitted.

Example: To ensure the color property is actually removed if no color has been selected, you might use the following form:

-
- - - - - -
+
<form method="POST" action="/content/page/first" enctype="multipart/form-data">
+    <input type="checkbox" name="color" value="red" />
+    <input type="checkbox" name="color" value="green" />
+    <input type="checkbox" name="color" value="blue" />
+    <input type="hidden" name="color@Delete" value="delete text" /><!-- actual value is ignored -->
+    <input type="Submit" />
+</form>
+

The @Delete suffixed parameter is also special in that there need not be a correlated parameter without a suffix. If both -- a parameters text and text@Delete are set, the text property is first deleted and then filled with the new content.

The @Delete suffixed parameter in fact calls for a sub-operation, which is executed after the node addressed by the request URL is created (if needed) but before any other tasks of content creation and modification are done. Any item -- this may be a property or a node, actually -- addressed by the @Delete suffixed parameter is just removed if it exists. If the item does not exist, nothing happens.

@MoveFrom

Now, that your bright and shiny content management application has great Flash-based file upload feature you will want to be able to use the pre-uploaded files for your content with the same request as when you upload other content. For example you might have a node storing some text and an illustration you uploaded as an image file.

To support this kind of functionality, the @MoveFrom suffixed parameter may be set to the repository path of the node to where you uploaded the image file.

Example: Your Flash-based file upload stored the file on the server at /tmp/upload/123. You now want to store this file along with a title and a text in a newly created node. The following form will be your friend:

- -
- - - - -
+
<!-- trailing slash generates a name for the new node -->
+<form method="POST" action="/content/page/" enctype="multipart/form-data">
+    <input type="hidden" name="image@MoveFrom" value="/tmp/upload/123" />
+    <input type="text" name="title" />
+    <input type="text" name="text" />
+    <input type="Submit" />
+</form>
+

If there exists no repository item at the indicated path, nothing is done. If the item indicated by the @MoveFrom suffixed parameter already exists, it is replaced by the item addressed by the parameter value -- unless of course there is no item at the named location.

The @MoveFrom suffixed parameter is assumed to be single-valued. If the parameter has multiple values it is ignored completely.

The @MoveFrom suffixed parameter is also special in that there must not be a correlated parameter without a suffix. Thus have parameters text and text@MoveFrom may have unexpected results.

@@ -296,13 +316,14 @@ of a resource without having to specify the path of each individual child resour
@CopyFrom

Similar to the @MoveFrom suffix exists a @CopyFrom suffix. The latter works exactly the same as the former except that the item addressed by the parameter value is not moved but just copied.

Example: Your Flash-based file upload stored the file on the server at /tmp/upload/123. You now want to store this file along with a title and a text in a newly created node. The following form may be your friend:

- -
- - - - -
+
<!-- trailing slash generates a name for the new node -->
+<form method="POST" action="/content/page/" enctype="multipart/form-data">
+    <input type="hidden" name="image@CopyFrom" value="/tmp/upload/123" />
+    <input type="text" name="title" />
+    <input type="text" name="text" />
+    <input type="Submit" />
+</form>
+

If there exists no repository item at the indicated path, nothing is done. If the item indicated by the @CopyFrom suffixed parameter already exists, it is replaced by the item addressed by the parameter value -- unless of course there is no item at the named location.

The @CopyFrom suffixed parameter is assumed to be single-valued. If the parameter has multiple values it is ignored completely.

The @CopyFrom suffixed parameter is also special in that there must not be a correlated parameter without a suffix. Thus have parameters text and text@CopyFrom may have unexpected results.

@@ -310,13 +331,14 @@ of a resource without having to specify the path of each individual child resour
@Patch

When modifying multi-value properties, the @Patch suffix can be used to just add + or remove - individual values without overwriting the full array. This allows to change the array without knowing the current values.

For example, imagine a multi-value string property that stores tags or keywords. To both add a tag "cool" and remove "boring" from the list:

-
- - - - - -
+
<form method="POST" action="/content/page/first" enctype="multipart/form-data">
+    <input type="hidden" name="tags@TypeHint" value="String[]" />
+    <input type="hidden" name="tags@Patch"    value="true" />
+    <input type="text"   name="tags"          value="+cool"/>
+    <input type="text"   name="tags"          value="-boring"/>
+    <input type="Submit" />
+</form>
+

The array will be treated like a set: when adding a value, it will only be added once if it does not exist yet; when removing a value, all occurrences of it will be removed. For values not affected by the add or remove operations, nothing changes. An existing array with duplicate entries will not automatically be converted into a set.

The format for an individual parameter value is <operation><value>. If there is no or no valid operation given, this value will be ignored.

Operation + will add the <value> to the array if it is not part of it yet.

@@ -366,7 +388,8 @@ of a resource without having to specify the path of each individual child resour

Content Removal

To remove existing content just address the item to be removed and set the :operation parameter to delete. For example the following command line removes the /content/sample page:

-

$ curl -F":operation=delete" http://host/content/sample

+
$ curl -F":operation=delete" http://host/content/sample
+
Response Status

The delete operation has the following status responses:

@@ -395,9 +418,12 @@ of a resource without having to specify the path of each individual child resour

By using the :applyTo request parameter it is possible to remove multiple items in one single request. Deleting items in this way leaves you with less control, though. In addition, if a single item removal fails, no item at all is removed.

When specifying the item(s) to be removed with the :applyTo parameter, the request resource is left untouched (unless of course if listed in the :applyTo parameter) and only used to resolve any relative paths in the :applyTo parameter.

To remove the /content/page1 and /content/page2 nodes, for example, you might use the following command line:

-

$ curl -F":operation=delete" -F":applyTo=/content/page1" -F":applyTo=/content/page2" http://host/content/sample

+
$ curl -F":operation=delete" -F":applyTo=/content/page1" \    
+    -F":applyTo=/content/page2" http://host/content/sample
+

Using a trailing star in the :applyTo parameter (as mentioned before), you can remove all the children of the /content node, for example, as follows:

-

$ curl -F":operation=delete" -F":applyTo=/content/*" http://host/content/sample

+
$ curl -F":operation=delete" -F":applyTo=/content/*" http://host/content/sample
+

If any resource listed in the :applyTo parameter does not exist, it is silently ignored.

Response Status

The delete operation applied to multiple resources has the following status responses:

@@ -485,10 +511,14 @@ of a resource without having to specify the path of each individual child resour

By using the :applyTo request parameter it is possible to copy multiple items in one single request. Copying items in this way leaves you with less control, though. In addition, if a single item copy fails, no item at all is copied.

When specifying the item(s) to be copied with the :applyTo parameter, the request resource is left untouched (unless of course if listed in the :applyTo parameter) and only used to resolve any relative paths in the :applyTo parameter.

To copy the /content/page1 and /content/page2 nodes to /content/target, for example, use:

-

$ curl -F":operation=copy" -F":applyTo=/content/page1" -F":applyTo=/content/page2" -F":dest=/content/target/" http://host/content/sample

+
$ curl -F":operation=copy" -F":applyTo=/content/page1" -F":applyTo=/content/page2" \    
+    -F":dest=/content/target/" http://host/content/sample
+

Please note the trailing slash character (/) in the value of the :dest parameter. This is required for multi-item copy operations using the :applyTo parameter. The copied items are created below the node indicated by the :dest.

Using a trailing star in the :applyTo parameter (as mentioned before), you can copy all the children of the /content node, for example, as follows:

-

$ curl -F":operation=copy" -F":applyTo=/content/*" -F":dest=/content/target/" http://host/content/sample

+
$ curl -F":operation=copy" -F":applyTo=/content/*" -F":dest=/content/target/" \    
+    http://host/content/sample
+

If any resource listed in the :applyTo parameter does not exist, it is silently ignored. Any item already existing at the copy destination whose name is the same as the name of an item to be copied is silently overwritten with the source item.

Response Status

The copy operation applied to multiple resources has the following status responses:

@@ -580,10 +610,14 @@ of a resource without having to specify the path of each individual child resour

By using the :applyTo request parameter it is possible to move multiple items in one single request. Moving items in this way leaves you with less control, though. In addition, if a single item move fails, no item at all is moved.

When specifying the item(s) to be moved with the :applyTo parameter, the request resource is left untouched (unless of course if listed in the :applyTo parameter) and only used to resolve any relative paths in the :applyTo parameter.

To for example move the /content/page1 and /content/page2 nodes to /content/target, you might use the following command line:

-

$ curl -F":operation=move" -F":applyTo=/content/page1" -F":applyTo=/content/page2" -F":dest=/content/target/" http://host/content/sample

+
$ curl -F":operation=move" -F":applyTo=/content/page1" -F":applyTo=/content/page2" \    
+     -F":dest=/content/target/" http://host/content/sample
+

Please note the trailing slash character (/) in the value of the :dest parameter. This is required for multi-item move operations using the :applyTo parameter. The moved items are created below the node indicated by the :dest.

Using a trailing star in the :applyTo parameter (as mentioned before), you can move all the children of the /content node, for example, as follows:

-

$ curl -F":operation=move" -F":applyTo=/content/*" -F":dest=/content/target/" http://host/content/sample

+
$ curl -F":operation=move" -F":applyTo=/content/*" -F":dest=/content/target/" \    
+    http://host/content/sample
+

If any resource listed in the :applyTo parameter does not exist, it is silently ignored. Any item already existing at the move destination whose name is the same as the name of an item to be moved is silently overwritten with the source item.

Response Status

The move operation applied to multiple resources has the following status responses:

@@ -610,7 +644,7 @@ of a resource without having to specify the path of each individual child resour

Importing Content Structures

-

To import content structures just address the parent item to import into and set the :operation parameter to import.

+

To import content structures just address the parent item to import into and set the :operation parameter to import.

The optional name of the root node of the imported content may optionally be supplied using the Algorithm for Node Name Creation.

Other parameters for the import operation:

@@ -668,27 +702,35 @@ of a resource without having to specify the path of each individual child resour

For example the following command line imports the /content/sample page:

-

$ curl -F":operation=import" -F":contentType=json" -F":name=sample" -F':content={ "jcr:primaryType": "nt:unstructured", "propOne" : "propOneValue", "childOne" : { "childPropOne" : true } }' http://host/content

+
$ curl -F":operation=import" -F":contentType=json" -F":name=sample" \    
+   -F':content={ "jcr:primaryType": "nt:unstructured", "propOne" : "propOneValue", "childOne" : { "childPropOne" : true } }' \    
+   http://host/content
+

For example the following command line imports the /content/sample page without the optional name parameter:

-

$ curl -F":operation=import" -F":contentType=json" -F':content={ "sample" : {"propOne" : "propOneValue", "childOne" : { "childPropOne" : true } } }' http://host/content

+
$ curl -F":operation=import" -F":contentType=json"
+   -F':content={ "sample" : {"propOne" : "propOneValue", "childOne" : { "childPropOne" : true } } }' \    
+   http://host/content
+

For example the following form imports the /content/sample page:

-
- - - +
<form method="POST" action="/content" enctype="multipart/form-data">
+    <input type="hidden" name=":operation" value="import" />
+    <input type="hidden" name=":contentType" value="json" />
+    <input type="hidden" name=":nameHint" value="sample" />
 
-
-
-
+    <input type="text" name=":content" value="{ &quot;jcr:primaryType&quot; : &quot;nt:unstructured&quot; , &quot;propOne&quot; : &quot;propOneValue&quot;, &quot;childOne&quot; : { &quot;childPropOne&quot; : true } }" />
+    <input type="Submit" />
+</form>
+

For example the following form imports content from a file upload:

-
- - - +
<form method="POST" action="/content" enctype="multipart/form-data">
+    <input type="hidden" name=":operation" value="import" />
+    <input type="hidden" name=":contentType" value="json" />
+    <input type="hidden" name=":nameHint" value="sample" />
 
-
-
-
+    <input type="file" name=":contentFile" />
+    <input type="Submit" />
+</form>
+
Response Status

The move operation applied to multiple resources has the following status responses:

@@ -776,7 +818,8 @@ of a resource without having to specify the path of each individual child resour

Note that simple content reordering can be requested without applying any other operations. This is easiest done by placing a request to the resource to be reordered and just setting the :order parameter. For example to order the /content/sample/page5 resource above its sibling resource /content/sample/other a simple request

-

$ curl -F":order=before other" http://host/content/sample/page5

+
$ curl -F":order=before other" http://host/content/sample/page5
+

does the trick. To be redirected after the reodering, the :redirect parameter may optionally also be specified.

:redirect

Instructs the SlingPostServlet to redirect the client to the indicated location if the operation succeeds. That is the response status is set to 302/FOUND and the Location header is set to the value of the :redirect parameter.

http://git-wip-us.apache.org/repos/asf/sling-site/blob/3a935d04/documentation/bundles/metrics.html ---------------------------------------------------------------------- diff --git a/documentation/bundles/metrics.html b/documentation/bundles/metrics.html index 2b24803..4c8a3c3 100644 --- a/documentation/bundles/metrics.html +++ b/documentation/bundles/metrics.html @@ -2,50 +2,63 @@ Apache Sling on JBake - - + +
-

Sling Metrics

Sling Metrics bundle provides integration with Dropwizard Metrics library which provides a toolkit to capture runtime performance statistics in your application.

+

Sling Metrics

Sling Metrics bundle provides integration with Dropwizard Metrics library which provides a toolkit to capture runtime performance statistics in your application.

Features

    -
  • Registers a MetricsService which can be used to create various types of Metric instances
  • +
  • Registers a MetricsService which can be used to create various types of Metric instances
  • WebConsole Plugin which provides a HTML Reporter for the various Metric instances
  • Inventory Plugin which dumps the Metric state in plain text format

Basic Usage

-

:::java import org.apache.sling.metrics.Counter; import org.apache.sling.metrics.MetricsService;

-

@Reference private MetricsService metricsService;

-

private Counter counter;

-

@Activate private void activate(){ counter = metricsService.counter("sessionCounter"); }

-

public void onSessionCreation(){ counter.increment(); }

+
:::java
+import org.apache.sling.metrics.Counter;
+import org.apache.sling.metrics.MetricsService;
+
+@Reference
+private MetricsService metricsService;
+
+private Counter counter;
+
+@Activate
+private void activate(){
+    counter = metricsService.counter("sessionCounter");
+}
+
+public void onSessionCreation(){
+    counter.increment();
+}
+

To make use of MetricsService

  1. Get a reference to org.apache.sling.metrics.MetricsService
  2. -
  3. Initialize the metric e.g. Counter in above case. This avoids any potential lookup cost in critical code paths
  4. +
  5. Initialize the metric e.g. Counter in above case. This avoids any potential lookup cost in critical code paths
  6. Make use of metric instance to capture require stats

Refer to Metric Getting Started guide to see how various types of Metric instances can be used. Note that when using Sling Commons Metrics bundle class names belong to org.apache.sling.commons.metrics package

Best Practices

    -
  1. Use descriptive names - Qualify the name with class/package name where the metric is being used
  2. -
  3. Do not use the metrics for operation which take less than 1E-7s i.e. 1000 nano seconds otherwise timer overhead (Metrics makes use of System.nanoTime) would start affecting the performance.
  4. +
  5. Use descriptive names - Qualify the name with class/package name where the metric is being used
  6. +
  7. Do not use the metrics for operation which take less than 1E-7s i.e. 1000 nano seconds otherwise timer overhead (Metrics makes use of System.nanoTime) would start affecting the performance.

API

-

Sling Metrics bundle provides its own Metric classes which are modelled on Dropwizard Metrics library. The metric interfaces defined by Sling bundle only provides methods related to data collection.

+

Sling Metrics bundle provides its own Metric classes which are modelled on Dropwizard Metrics library. The metric interfaces defined by Sling bundle only provides methods related to data collection.

  • org.apache.sling.commons.metrics.Meter - Similar to Dropwizard Meter
  • org.apache.sling.commons.metrics.Timer - Similar to Dropwizard Timer
  • @@ -55,23 +68,36 @@

    Further it provides a MetricsService which enables creation of different type of Metrics like Meter, Timer, Counter and Histogram.

    Requirement of wrapper interfaces

      -
    • Abstraction - Provides an abstraction around how metrics are collected and how they are reported and consumed. Most of the code would only be concerned with collecting interesting data. How it gets consumed or reported is implementation detail.
    • -
    • Ability to turnoff stats collection - We can easily turn off data collection by switching to NOOP variant of MetricsService in case it starts adding appreciable overhead. Turning on and off can also be done on individual metric basis.
    • +
    • Abstraction - Provides an abstraction around how metrics are collected and how they are reported and consumed. Most of the code would only be concerned with collecting interesting data. How it gets consumed or reported is implementation detail.
    • +
    • Ability to turnoff stats collection - We can easily turn off data collection by switching to NOOP variant of MetricsService in case it starts adding appreciable overhead. Turning on and off can also be done on individual metric basis.

    It also allows us to later extend the type of data collected. For e.g. we can also collect TimerSeries type of data for each metric without modifying the caller logic.

    Access to Dropwizard Metrics API

    Sling Metrics bundle also registers the MetricRegistry instance with OSGi service registry. The instance registered has a service property name set to sling (so as allow distinguishing from any other registered MetricRegistry instance). It can be used to get direct access to Dropwizard Metric API if required.

    -

    :::java @Reference(target = "(name=sling)") private MetricRegistry registry;

    +
    :::java
    +@Reference(target = "(name=sling)")
    +private MetricRegistry registry;
    +

    Also the wrapper Metric instance can be converted to actual instance via adaptTo calls.

    -

    :::java import org.apache.sling.commons.metrics.Counter

    -

    Counter counter = metricService.counter("login"); com.codahale.metrics.Counter = counter.adaptTo(com.codahale.metrics.Counter.class)

    +
    :::java
    +import org.apache.sling.commons.metrics.Counter
    +
    +Counter counter = metricService.counter("login");
    +com.codahale.metrics.Counter = counter.adaptTo(com.codahale.metrics.Counter.class)
    +

    WebConsole Plugin

    -

    A Web Console plugin is also provided which is accessible at http://localhost:8080/system/console/slingmetrics. It lists down all registered Metric instances and their state.

    +

    A Web Console plugin is also provided which is accessible at http://localhost:8080/system/console/slingmetrics. It lists down all registered Metric instances and their state.

    Metric Plugin

    The plugin lists all Metric instances from any MetricRegistry instance found in the OSGi service registry. If the MetricRegistry service has a name property defined then that would be prefixed to the Metric names from that registry. This allows use of same name in different registry instances.

    Installation

    Add following Maven dependency to your pom.xml:

    -

    :::xml org.apache.sling org.apache.sling.commons.metrics 1.0.0

    +
    :::xml
    +<dependency>
    +    <groupId>org.apache.sling</groupId>
    +    <artifactId>org.apache.sling.commons.metrics</artifactId>
    +    <version>1.0.0</version>
    +</dependency>
    +

    Or download from here