incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From conflue...@apache.org
Subject [CONF] Apache Sling Website > Manipulating Content - The SlingPostServlet (servlets.post)
Date Wed, 19 May 2010 15:35:00 GMT
<html>
<head>
    <base href="http://cwiki.apache.org/confluence">
            <link rel="stylesheet" href="/confluence/s/1810/9/1/_/styles/combined.css?spaceKey=SLINGxSITE&amp;forWysiwyg=true" type="text/css">
    </head>
<body style="background: white;" bgcolor="white" class="email-body">
<div id="pageContent">
<div id="notificationFormat">
<div class="wiki-content">
<div class="email">
    <h2><a href="http://cwiki.apache.org/confluence/display/SLINGxSITE/Manipulating+Content+-+The+SlingPostServlet+%28servlets.post%29">Manipulating Content - The SlingPostServlet (servlets.post)</a></h2>
    <h4>Page <b>edited</b> by             <a href="http://cwiki.apache.org/confluence/display/~justinedelson">Justin Edelson</a>
    </h4>
        <br/>
                         <h4>Changes (3)</h4>
                                 
    
<div id="page-diffs">
            <table class="diff" cellpadding="0" cellspacing="0">
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" > <br> <br></td></tr>
            <tr><td class="diff-changed-lines" >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 <span class="diff-changed-words">param<span class="diff-deleted-chars"style="color:#999;background-color:#fdd;text-decoration:line-through;">a</span><span class="diff-added-chars"style="background-color: #dfd;">e</span>ters,</span> the {{@DefaultValue}} parameter may have multiple values to create multi-valued properties. <br></td></tr>
            <tr><td class="diff-unchanged" > <br>Example: Set the {{text}} property to a default value if the user does not provide one: <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" > <br> <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">h6. {{@UseDefaultWhenMissing}} <br></td></tr>
            <tr><td class="diff-unchanged" > <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">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&#39;t sufficient because the parameter isn&#39;t submitted at all. To handle this scenario, you can use the {{@UseDefaultWhenMissing}} suffixed parameter. <br> <br>{code:html} <br>&lt;form method=&quot;POST&quot; action=&quot;/content/page/first&quot; enctype=&quot;multipart/form-data&quot;&gt; <br>    &lt;input name=&quot;queryIgnoreNoise&quot; class=&quot;input&quot; type=&quot;checkbox&quot; value=&quot;true&quot;/&gt; <br>    &lt;input type=&quot;hidden&quot; name=&quot;queryIgnoreNoise@DefaultValue&quot; value=&quot;false&quot;/&gt;  <br>    &lt;input type=&quot;hidden&quot; name=&quot;queryIgnoreNoise@UseDefaultWhenMissing&quot; value=&quot;true&quot;/&gt; <br>&lt;/form&gt; <br>{code} <br> <br></td></tr>
            <tr><td class="diff-unchanged" >h6. {{@ValueFrom}} <br> <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
        </table>
</div>                            <h4>Full Content</h4>
                    <div class="notificationGreySide">
        <h1><a name="ManipulatingContent-TheSlingPostServlet%28servlets.post%29-ManipulatingContent%3ATheSlingPostServlet"></a>Manipulating Content: The SlingPostServlet</h1>


<div>
<ul>
    <li><a href='#ManipulatingContent-TheSlingPostServlet%28servlets.post%29-MultipleWaystoModifyContent'>Multiple Ways to Modify Content</a></li>
    <li><a href='#ManipulatingContent-TheSlingPostServlet%28servlets.post%29-Quickstart%3ACreatingContent'>Quickstart: Creating Content</a></li>
    <li><a href='#ManipulatingContent-TheSlingPostServlet%28servlets.post%29-Preface%3Amultipart%2FformdataPOSTs'>Preface: multipart/form-data POSTs</a></li>
    <li><a href='#ManipulatingContent-TheSlingPostServlet%28servlets.post%29-SlingPostServletOperations'>SlingPostServlet Operations</a></li>
<ul>
    <li><a href='#ManipulatingContent-TheSlingPostServlet%28servlets.post%29-ContentCreationorModification'>Content Creation or Modification</a></li>
    <li><a href='#ManipulatingContent-TheSlingPostServlet%28servlets.post%29-ContentRemoval'>Content Removal</a></li>
    <li><a href='#ManipulatingContent-TheSlingPostServlet%28servlets.post%29-CopyingContent'>Copying Content</a></li>
    <li><a href='#ManipulatingContent-TheSlingPostServlet%28servlets.post%29-MovingContent'>Moving Content</a></li>
    <li><a href='#ManipulatingContent-TheSlingPostServlet%28servlets.post%29-NullOperation'>Null Operation</a></li>
</ul>
    <li><a href='#ManipulatingContent-TheSlingPostServlet%28servlets.post%29-SpecialParameters'>Special Parameters</a></li>
<ul>
    <li><a href='#ManipulatingContent-TheSlingPostServlet%28servlets.post%29-%7B%7B%3Aorder%7D%7D'> <tt>:order</tt></a></li>
    <li><a href='#ManipulatingContent-TheSlingPostServlet%28servlets.post%29-%7B%7B%3Aredirect%7D%7D'> <tt>:redirect</tt></a></li>
    <li><a href='#ManipulatingContent-TheSlingPostServlet%28servlets.post%29-%7B%7B%3Astatus%7D%7D'> <tt>:status</tt></a></li>
</ul>
    <li><a href='#ManipulatingContent-TheSlingPostServlet%28servlets.post%29-Responseformat'>Response format</a></li>
    <li><a href='#ManipulatingContent-TheSlingPostServlet%28servlets.post%29-AdvancedTopic%3AExtendingtheSlingPostServlet'>Advanced Topic: Extending the SlingPostServlet</a></li>
</ul></div>



<h2><a name="ManipulatingContent-TheSlingPostServlet%28servlets.post%29-MultipleWaystoModifyContent"></a>Multiple Ways to Modify Content</h2>

<p>As always in life there is more than one way to do it. So to modify content in a JCR repository underlying Sling, you have multiple options, two of which are WebDAV and the Sling default POST Servlet also called the <em>SlingPostServlet</em>. This page is about how you can modify - create, modify, copy, move, delete - content through the <em>SlingPostServlet</em>. In addition it also explains how to extend the SlingPostServlet with new operations.</p>


<p>What is Content anyway ? In the following discussion, I use the terms <em>Content</em> and <em>Item</em> interchangeably. With <em>Content</em> 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 <em>Content</em> is a rather conceptual term. <em>Item</em> is the name of the parent interface of the JCR <em>Node</em> and <em>Property</em> interfaces. When speaking of <em>Items</em> we mean some actual data stored in the repository ignoring whether the data is actually stored as a <em>Node</em> with child nodes and properties or just a single <em>Property</em>.</p>


<h2><a name="ManipulatingContent-TheSlingPostServlet%28servlets.post%29-Quickstart%3ACreatingContent"></a>Quickstart: Creating Content</h2>

<p>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:</p>


<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
&lt;form method=<span class="code-quote">"POST"</span> action=<span class="code-quote">"http:<span class="code-comment">//host/some/<span class="code-keyword">new</span>/content"</span> enctype=<span class="code-quote">"multipart/form-data"</span>&gt;
</span>   &lt;input type=<span class="code-quote">"text"</span> name=<span class="code-quote">"title"</span> value="" /&gt;
   &lt;input type=<span class="code-quote">"text"</span> name=<span class="code-quote">"text"</span> value="" /&gt;
&lt;/form&gt;
</pre>
</div></div>


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

<p>Similarly you can do this using the <tt>curl</tt> command line tool:</p>

<div class="preformatted panel" style="border-width: 1px;"><div class="preformattedContent panelContent">
<pre>$ curl -Ftitle="some title text" -Ftext="some body text content" http://host/some/new/content
</pre>
</div></div>


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

<div class="preformatted panel" style="border-width: 1px;"><div class="preformattedContent panelContent">
<pre>$ curl -F"jcr:primaryType=nt:unstructured" -Ftitle="some title text" -Ftext="some body text content" http://host/some/new/content
</pre>
</div></div>

<p>Similary you may assing JCR mixin node types using the <tt>jcr:mixinTypes</tt> property and a Sling resource type using the <tt>sling:resourceType</tt> property. For example:</p>

<div class="preformatted panel" style="border-width: 1px;"><div class="preformattedContent panelContent">
<pre>$ curl -F"sling:resourceType=sling:sample" -Ftitle="some title text" -Ftext="some body text content" http://host/some/new/content
</pre>
</div></div>


<h2><a name="ManipulatingContent-TheSlingPostServlet%28servlets.post%29-Preface%3Amultipart%2FformdataPOSTs"></a>Preface: multipart/form-data POSTs</h2>

<p>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.</p>

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

<p>This support requires Sling Engine 2.1.0 and the Sling Default Post Servlet 2.0.6.</p>


<h2><a name="ManipulatingContent-TheSlingPostServlet%28servlets.post%29-SlingPostServletOperations"></a>SlingPostServlet Operations</h2>

<div class='panelMacro'><table class='infoMacro'><colgroup><col width='24'><col></colgroup><tr><td valign='top'><img src="/confluence/images/icons/emoticons/information.gif" width="16" height="16" align="absmiddle" alt="" border="0"></td><td>Note that the <tt>launchpad/testing</tt> module contains a number of <a href="http://svn.apache.org/repos/asf/sling/trunk/launchpad/testing/src/test/java/org/apache/sling/launchpad/webapp/integrationtest/servlets/post" class="external-link" rel="nofollow">integration tests</a> for the SlingPostServlet. Most of these tests are fairly readable, and can be used to find out more details about the servlet's behavior.</td></tr></table></div>

<p>The SlingPostServlet is actually just a frontend to the actual operations. To select the actual operation to execute, the <tt>:operation</tt> request parameter is used. Out of the box, the SlingPostServlet supports the following operations:</p>

<ul>
	<li>property not set or empty &#8211; Create new content or modify existing content</li>
	<li><tt>delete</tt> &#8211; Remove existing content</li>
	<li><tt>move</tt> &#8211; Move existing content to a new location</li>
	<li><tt>copy</tt> &#8211; Copy existing content to a new location</li>
	<li><tt>nop</tt> &#8211; Explicitly requests to do nothing and just sets the response status</li>
</ul>


<p>All these operations always operate on the resource of the request as returned by <tt>SlingHttpServletRequest.getResource()</tt>. Some operations require additional parameters to be set to operate completely.</p>

<p>Please note that operations are mutually exclusive. For a single POST request only one operation may be executed. Operations also only consume the request parameters as described below. Any excess parameters are silently ignored.</p>

<h3><a name="ManipulatingContent-TheSlingPostServlet%28servlets.post%29-ContentCreationorModification"></a>Content Creation or Modification</h3>


<p>The simplest and most common use case, probably, is content creation and modification. We already saw an example above in the quickstart section. In this section we elaborate more on the concrete stuff.</p>

<p>First, the request URL indicates the actual repository node to be handled. If the URL addresses an existing node, the request parameters just provide values for the properties to be set on the existing node.</p>

<p>If the resource of the request is a synthetic resource, e.g. <tt>NonExistingResource</tt> or <tt>StarResource</tt>, a new item is created. The path (including name) of the item to be created is derived from the resource path:</p>

<ul>
	<li>If the resource path ends with a <tt>/*</tt> or <tt>/</tt> the name of the item is automatically created using a name creation algorithm taking into account various request parameters.</li>
	<li>Otherwise the resource path is used as the path and name of the new item.</li>
</ul>


<p>In both cases the path may still include selectors and extensions, which are cut off the path before finding out, what to do.</p>

<p>To illustrate this algorithm, lets look at some examples:</p>

<div class='table-wrap'>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'> Resource Path </th>
<th class='confluenceTh'> Item path </th>
</tr>
<tr>
<td class='confluenceTd'> <tt>/content/new</tt> </td>
<td class='confluenceTd'> <tt>/content/new</tt> </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>/content/new.html</tt> </td>
<td class='confluenceTd'> <tt>/content/new</tt> </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>/content/new.print.a4.html</tt> </td>
<td class='confluenceTd'> <tt>/content/new</tt> </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>/content/</tt> </td>
<td class='confluenceTd'> <tt>/content/xxx</tt> where <tt>xxx</tt> is a generated name </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>/content/*</tt></td>
<td class='confluenceTd'> <tt>/content/xxx</tt> where <tt>xxx</tt> is a generated name </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>/content/*.html</tt></td>
<td class='confluenceTd'> <tt>/content/xxx</tt> where <tt>xxx</tt> is a generated name </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>/content/*.print.a4.html</tt></td>
<td class='confluenceTd'> <tt>/content/xxx</tt> where <tt>xxx</tt> is a generated name </td>
</tr>
</tbody></table>
</div>




<h5><a name="ManipulatingContent-TheSlingPostServlet%28servlets.post%29-SettingPropertyValues"></a>Setting Property Values</h5>

<p>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.</p>

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

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-html">
<span class="code-tag">&lt;form method=<span class="code-quote">"POST"</span> action=<span class="code-quote">"/content/page/first"</span> enctype=<span class="code-quote">"multipart/form-data"</span>&gt;</span>
    <span class="code-tag">&lt;input type=<span class="code-quote">"text"</span> name=<span class="code-quote">"title"</span> /&gt;</span>
    <span class="code-tag">&lt;input type=<span class="code-quote">"text"</span> name=<span class="code-quote">"text"</span> /&gt;</span>
    <span class="code-tag">&lt;input type=<span class="code-quote">"Submit"</span> /&gt;</span>
<span class="code-tag">&lt;/form&gt;</span>
</pre>
</div></div>

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

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

<div class="preformatted panel" style="border-width: 1px;"><div class="preformattedContent panelContent">
<pre>$ curl -Fmulti=one -Fmulti=two http://host/content/page
</pre>
</div></div>

<p>Would assign the <tt>/content/page/multi</tt> property the value <em>[ "one", "two" ]</em>.</p>

<p>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.</p>


<h5><a name="ManipulatingContent-TheSlingPostServlet%28servlets.post%29-FileUploads"></a>File Uploads</h5>

<p>File uploads are typically done using the <tt>&lt;input type="file""/&gt;</tt> element of an HTML form and ensuring the correct form encoding. The SlingPostServlet handles uploaded files specially, in that the file data is not simply written into a property, but a node is actually created with three properties:</p>

<ul>
	<li><tt>jcr:data</tt> &#8211; The actual file contents</li>
	<li><tt>jcr:lastModified</tt> &#8211; The time stamp of processing the uploaded file</li>
	<li><tt>jcr:mimeType</tt> &#8211; The MIME type from the original file submission (if contained in the file body part) or derived from the original file name</li>
</ul>


<p>The name of the node is either taken from the parameter name or if the name is <tt>*</tt> from the name of the uploaded file.</p>

<p>The primary node type of the uploaded file is selected using the following algorithm:</p>

<ol>
	<li>If a <tt>@TypeHint</tt> suffixed parameter (see below for a description) is present check whether the value is a known non-mixin node type. If so, the node is created with this primary node type.</li>
	<li>If a <tt>@TypeHint</tt> suffixed parameter is not present or the value does not denote an existing non-mixin node type, the node will be created as an <tt>nt:file</tt> node if the parent node is of type <tt>nt:folder</tt>. Otherwise the node will be created with primary node type <tt>nt:resource</tt>.</li>
</ol>


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

<p>Example 1: Upload an image to a node named <tt>image</tt> below <tt>/content/page</tt>:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-html">
<span class="code-tag">&lt;form method=<span class="code-quote">"POST"</span> action=<span class="code-quote">"/content/page"</span> enctype=<span class="code-quote">"multipart/form-data"</span>&gt;</span>
    <span class="code-tag">&lt;input type=<span class="code-quote">"file"</span> name=<span class="code-quote">"image"</span> /&gt;</span>
    <span class="code-tag">&lt;input type=<span class="code-quote">"Submit"</span> /&gt;</span>
<span class="code-tag">&lt;/form&gt;</span>
</pre>
</div></div>


<p>Example 2: Upload a file as a node of type <tt>nt:file</tt> below <tt>/content/folder</tt>:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-html">
<span class="code-tag">&lt;form method=<span class="code-quote">"POST"</span> action=<span class="code-quote">"/content/page"</span> enctype=<span class="code-quote">"multipart/form-data"</span>&gt;</span>
    <span class="code-tag">&lt;input type=<span class="code-quote">"file"</span> name=<span class="code-quote">"*"</span> /&gt;</span>
    <span class="code-tag">&lt;input type=<span class="code-quote">"hidden"</span> name=<span class="code-quote">"*@TypeHint"</span> value=<span class="code-quote">"nt:file"</span> /&gt;</span>
    <span class="code-tag">&lt;input type=<span class="code-quote">"Submit"</span> /&gt;</span>
<span class="code-tag">&lt;/form&gt;</span>
</pre>
</div></div>

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



<h5><a name="ManipulatingContent-TheSlingPostServlet%28servlets.post%29-Dateproperties"></a>Date properties</h5>

<p>Parameters providing date/time values to be stored in JCR properties of type <em>Date</em> 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 <tt>Calendar</tt> objects.</p>

<p>The Sling Post Servlet configuration property <tt>servlet.post.dateFormats</tt> takes a list of format strings which are used to setup <tt>java.text.SimpleDateFormat</tt> instances for parsing date/time string representations. A special format string <tt>ISO8601</tt> is supported to indicate the string to be parsed as a JCR standard string representation of a <em>Date</em> property.</p>

<p>The default list of configured date/time parse pattern is :</p>

<ul>
	<li>EEE MMM dd yyyy HH:mm:ss 'GMT'Z</li>
	<li>ISO8601</li>
	<li>yyyy-MM-dd'T'HH:mm:ss.SSSZ</li>
	<li>yyyy-MM-dd'T'HH:mm:ss</li>
	<li>yyyy-MM-dd</li>
	<li>dd.MM.yyyy HH:mm:ss</li>
	<li>dd.MM.yyyy</li>
</ul>


<p>Any date/time string parameter supplied is subject to the patterns in the configured order. The first pattern accepting the string and parsing it into a <tt>Date</tt> &#8211; and thus a <tt>Calendar</tt> &#8211; object is used. Therefore this list is best ordered in a most-stringent to least-stringent order.</p>



<h5><a name="ManipulatingContent-TheSlingPostServlet%28servlets.post%29-OmittingSomeParameters"></a>Omitting Some Parameters</h5>

<p>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 (<tt>./</tt>).</p>

<p>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 (<tt>../</tt>) and slash (<tt>/</tt>) are considered in this situation.</p>

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

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-html">
<span class="code-tag">&lt;form method=<span class="code-quote">"POST"</span> action=<span class="code-quote">"/content/page/first"</span> enctype=<span class="code-quote">"multipart/form-data"</span>&gt;</span>
    <span class="code-tag">&lt;input type=<span class="code-quote">"text"</span> name=<span class="code-quote">"./title"</span> /&gt;</span>
    <span class="code-tag">&lt;input type=<span class="code-quote">"text"</span> name=<span class="code-quote">"../first/text"</span> /&gt;</span>
    <span class="code-tag">&lt;input type=<span class="code-quote">"hidden"</span> name=<span class="code-quote">"control0"</span> /&gt;</span><span class="code-tag"><span class="code-comment">&lt;!-- ignored --&gt;</span></span>
    <span class="code-tag">&lt;input type=<span class="code-quote">"hidden"</span> name=<span class="code-quote">"control1"</span> /&gt;</span><span class="code-tag"><span class="code-comment">&lt;!-- ignored --&gt;</span></span>
    <span class="code-tag">&lt;input type=<span class="code-quote">"Submit"</span> /&gt;</span>
<span class="code-tag">&lt;/form&gt;</span>
</pre>
</div></div>

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

<p>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 <tt>text</tt> is equivalent to <tt>./text</tt> &#8211; dot-slash meaning relative to the current node identified by the <tt>action</tt> attribute value for <tt>form</tt> tag &#8211; or <tt>../first/text</tt> if <tt>first</tt> is the name of the node to modify &#8211; dot-dot-slash meaning relative to the parent node of the node identified by the <tt>action</tt> attribute value of the <tt>form</tt> tag.</p>

<p>Parameters whose name start with a colon (<tt>:</tt>) are always ignored by the SlingPostServlet with respect to content update. The reason is that the prefixing colon is intended as a marker for SlingPostServlet control parameters.</p>


<h5><a name="ManipulatingContent-TheSlingPostServlet%28servlets.post%29-ControllingContentUpdateswith%7B%7B@%7D%7DSuffixes"></a>Controlling Content Updates with <tt>@</tt> Suffixes</h5>

<p>Generally just creating forms with parameters and their values suffices it completely. Sometimes, though, you want to have more control on how the parameter values are actually stored in the properties. For example, you want to set a property to a default value if the user did provide an actual value. Or you might want to store a parameter explicitly with a given data type, such as numeric, boolean etc.</p>

<p>The SlingPostServlet provides such property control in the form of <tt>@</tt> suffixed parameters, which are now presented.</p>

<p>The <tt>@</tt> suffixed parameters are not used on their own but always in conjunction with a plain parameter. The part of the parameter name before the <tt>@</tt> suffix is used in this case for correlation and must match exactly the name of the parameter to which the <tt>@</tt> suffixed parameter belongs.</p>

<p>For example, the parameter <tt>width@TypeHint</tt> applies to the <tt>width</tt> parameter and the <tt>./height@TypeHint</tt> parameter applies to the <tt>./height</tt> parameter. As can be seen, the correlation between the parameters is a simple case-sensitive string comparison. That is the <tt>widht@TypeHint</tt> parameter would not apply to the <tt>./width</tt> even though both parameters address the same property but they do not have a string match.</p>


<h6><a name="ManipulatingContent-TheSlingPostServlet%28servlets.post%29-%7B%7B@TypeHint%7D%7D"></a><tt>@TypeHint</tt></h6>

<p>Parameters with the <tt>@TypeHint</tt> suffix may be used to force storing the named parameter in a property with the given type. The value of the <tt>@TypeHint</tt> parameter, if applied to a parameter for a property, is the JCR property type name. If the <tt>@TypeHint</tt> 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.</p>

<p>If the <tt>@TypeHint</tt> ends with "[]", it indicates a multi-value property. A multi-value property is usually auto-detected if there are mutliple values for the property (ie. 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 <tt>@TypeHint=&lt;type&gt;[]</tt>.</p>

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

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-html">
<span class="code-tag">&lt;form method=<span class="code-quote">"POST"</span> action=<span class="code-quote">"/content/page/first"</span> enctype=<span class="code-quote">"multipart/form-data"</span>&gt;</span>
    <span class="code-tag">&lt;input type=<span class="code-quote">"text"</span> name=<span class="code-quote">"width"</span> /&gt;</span>
    <span class="code-tag">&lt;input type=<span class="code-quote">"hidden"</span> name=<span class="code-quote">"width@TypeHint"</span> value=<span class="code-quote">"Long"</span> /&gt;</span>
    <span class="code-tag">&lt;input type=<span class="code-quote">"checkbox"</span> name=<span class="code-quote">"checked"</span> /&gt;</span>
    <span class="code-tag">&lt;input type=<span class="code-quote">"hidden"</span> name=<span class="code-quote">"checked@TypeHint"</span> value=<span class="code-quote">"Boolean"</span> /&gt;</span>
    <span class="code-tag">&lt;input type=<span class="code-quote">"text"</span> name=<span class="code-quote">"hobbys"</span>/&gt;</span>
    <span class="code-tag">&lt;input type=<span class="code-quote">"text"</span> name=<span class="code-quote">"hobbys"</span>/&gt;</span>
    <span class="code-tag">&lt;input type=<span class="code-quote">"text"</span> name=<span class="code-quote">"hobbys"</span>/&gt;</span>
    <span class="code-tag">&lt;input type=<span class="code-quote">"hidden"</span> name=<span class="code-quote">"hobbys@TypeHint"</span> value=<span class="code-quote">"String[]"</span> /&gt;</span>
    <span class="code-tag">&lt;input type=<span class="code-quote">"Submit"</span> /&gt;</span>
<span class="code-tag">&lt;/form&gt;</span>
</pre>
</div></div>

<p>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 <tt>hobbys@TypeHint=String[]</tt> would be the same.</p>

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

<p>For more information on applying <tt>@TypeHint</tt> to a file upload parameter see the section on File Uploads above.</p>


<h6><a name="ManipulatingContent-TheSlingPostServlet%28servlets.post%29-%7B%7B@DefaultValue%7D%7D"></a><tt>@DefaultValue</tt></h6>


<p>The <tt>@DefaultValue</tt> 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 <tt>@DefaultValue</tt> parameter may have multiple values to create multi-valued properties.</p>

<p>Example: Set the <tt>text</tt> property to a default value if the user does not provide one:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-html">
<span class="code-tag">&lt;form method=<span class="code-quote">"POST"</span> action=<span class="code-quote">"/content/page/first"</span> enctype=<span class="code-quote">"multipart/form-data"</span>&gt;</span>
    <span class="code-tag">&lt;input type=<span class="code-quote">"text"</span> name=<span class="code-quote">"text"</span> /&gt;</span>
    <span class="code-tag">&lt;input type=<span class="code-quote">"hidden"</span> name=<span class="code-quote">"text@DefaultValue"</span> value=<span class="code-quote">"--- Default Value ---"</span> /&gt;</span>
    <span class="code-tag">&lt;input type=<span class="code-quote">"Submit"</span> /&gt;</span>
<span class="code-tag">&lt;/form&gt;</span>
</pre>
</div></div>


<h6><a name="ManipulatingContent-TheSlingPostServlet%28servlets.post%29-%7B%7B@UseDefaultWhenMissing%7D%7D"></a><tt>@UseDefaultWhenMissing</tt></h6>

<p>As described above, <tt>@DefaultValue</tt> 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 <tt>@UseDefaultWhenMissing</tt> suffixed parameter.</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-html">
<span class="code-tag">&lt;form method=<span class="code-quote">"POST"</span> action=<span class="code-quote">"/content/page/first"</span> enctype=<span class="code-quote">"multipart/form-data"</span>&gt;</span>
    <span class="code-tag">&lt;input name=<span class="code-quote">"queryIgnoreNoise"</span> class=<span class="code-quote">"input"</span> type=<span class="code-quote">"checkbox"</span> value=<span class="code-quote">"true"</span>/&gt;</span>
    <span class="code-tag">&lt;input type=<span class="code-quote">"hidden"</span> name=<span class="code-quote">"queryIgnoreNoise@DefaultValue"</span> value=<span class="code-quote">"false"</span>/&gt;</span> 
    <span class="code-tag">&lt;input type=<span class="code-quote">"hidden"</span> name=<span class="code-quote">"queryIgnoreNoise@UseDefaultWhenMissing"</span> value=<span class="code-quote">"true"</span>/&gt;</span>
<span class="code-tag">&lt;/form&gt;</span>
</pre>
</div></div>

<h6><a name="ManipulatingContent-TheSlingPostServlet%28servlets.post%29-%7B%7B@ValueFrom%7D%7D"></a><tt>@ValueFrom</tt></h6>

<p>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 <tt>@ValueFrom</tt> may be set containing the name of the parameter providing the actual data to be used.</p>

<p>Example: To set the property <tt>text</tt> from a form element <tt>supplied_text</tt>, you might use the following form:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-html">
<span class="code-tag">&lt;form method=<span class="code-quote">"POST"</span> action=<span class="code-quote">"/content/page/first"</span> enctype=<span class="code-quote">"multipart/form-data"</span>&gt;</span>
    <span class="code-tag">&lt;input type=<span class="code-quote">"text"</span> name=<span class="code-quote">"supplied_text"</span> /&gt;</span>
    <span class="code-tag">&lt;input type=<span class="code-quote">"hidden"</span> name=<span class="code-quote">"./text@ValueFrom"</span> value=<span class="code-quote">"supplied_text"</span> /&gt;</span>
    <span class="code-tag">&lt;input type=<span class="code-quote">"Submit"</span> /&gt;</span>
<span class="code-tag">&lt;/form&gt;</span>
</pre>
</div></div>

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

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

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


<h6><a name="ManipulatingContent-TheSlingPostServlet%28servlets.post%29-%7B%7B@Delete%7D%7D"></a><tt>@Delete</tt></h6>

<p>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.</p>

<p>Here comes the <tt>@Delete</tt> 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 <tt>@Delete</tt> suffixed parameter does not care as long as the parameter is submitted.</p>

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

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-html">
<span class="code-tag">&lt;form method=<span class="code-quote">"POST"</span> action=<span class="code-quote">"/content/page/first"</span> enctype=<span class="code-quote">"multipart/form-data"</span>&gt;</span>
    <span class="code-tag">&lt;input type=<span class="code-quote">"checkbox"</span> name=<span class="code-quote">"color"</span> value=<span class="code-quote">"red"</span> /&gt;</span>
    <span class="code-tag">&lt;input type=<span class="code-quote">"checkbox"</span> name=<span class="code-quote">"color"</span> value=<span class="code-quote">"green"</span> /&gt;</span>
    <span class="code-tag">&lt;input type=<span class="code-quote">"checkbox"</span> name=<span class="code-quote">"color"</span> value=<span class="code-quote">"blue"</span> /&gt;</span>
    <span class="code-tag">&lt;input type=<span class="code-quote">"hidden"</span> name=<span class="code-quote">"color@Delete"</span> value=<span class="code-quote">"delete text"</span> /&gt;</span><span class="code-tag"><span class="code-comment">&lt;!-- actual value is ignored --&gt;</span></span>
    <span class="code-tag">&lt;input type=<span class="code-quote">"Submit"</span> /&gt;</span>
<span class="code-tag">&lt;/form&gt;</span>
</pre>
</div></div>


<p>The <tt>@Delete</tt> suffixed parameter is also special in that there need not be a correlated parameter without a suffix. If both &#8211; a parameters <tt>text</tt> and <tt>text@Delete</tt> are set, the <tt>text</tt> property is first deleted and then filled with the new content.</p>

<p>The <tt>@Delete</tt> 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 creattion and modification are done. Any item &#8211; this may be a property or a node, actually &#8211; addressed by the <tt>@Delete</tt> suffixed parameter is just removed if it exists. If the item does not exist, nothing happens.</p>



<h6><a name="ManipulatingContent-TheSlingPostServlet%28servlets.post%29-%7B%7B@MoveFrom%7D%7D"></a><tt>@MoveFrom</tt></h6>

<p>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.</p>

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

<p>Example: Your Flash-based file upload stored the file on the server at <tt>/tmp/upload/123</tt>. 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:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-html">
<span class="code-tag"><span class="code-comment">&lt;!-- trailing slash generates a name for the new node --&gt;</span></span>
<span class="code-tag">&lt;form method=<span class="code-quote">"POST"</span> action=<span class="code-quote">"/content/page/"</span> enctype=<span class="code-quote">"multipart/form-data"</span>&gt;</span>
    <span class="code-tag">&lt;input type=<span class="code-quote">"hidden"</span> name=<span class="code-quote">"image@MoveFrom"</span> value=<span class="code-quote">"/tmp/upload/123"</span> /&gt;</span>
    <span class="code-tag">&lt;input type=<span class="code-quote">"text"</span> name=<span class="code-quote">"title"</span> /&gt;</span>
    <span class="code-tag">&lt;input type=<span class="code-quote">"text"</span> name=<span class="code-quote">"text"</span> /&gt;</span>
    <span class="code-tag">&lt;input type=<span class="code-quote">"Submit"</span> /&gt;</span>
<span class="code-tag">&lt;/form&gt;</span>
</pre>
</div></div>

<p>If there exists no repository item at the indicated path, nothing is done. If the item indicated by the <tt>@MoveFrom</tt> suffixed parameter already exists, it is replaced by the item addressed by the parameter value &#8211; unless of course there is no item at the named location.</p>

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

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

<p>The <tt>@MoveFrom</tt> suffixed parameter in fact calls for a sub-operation, which is executed after the <tt>@Delete</tt> sub operation but before any other tasks of content creattion and modification are done.</p>


<h6><a name="ManipulatingContent-TheSlingPostServlet%28servlets.post%29-%7B%7B@CopyFrom%7D%7D"></a><tt>@CopyFrom</tt></h6>

<p>Similar to the <tt>@MoveFrom</tt> suffix exists a <tt>@CopyFrom</tt> 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.</p>

<p>Example: Your Flash-based file upload stored the file on the server at <tt>/tmp/upload/123</tt>. 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:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-html">
<span class="code-tag"><span class="code-comment">&lt;!-- trailing slash generates a name for the new node --&gt;</span></span>
<span class="code-tag">&lt;form method=<span class="code-quote">"POST"</span> action=<span class="code-quote">"/content/page/"</span> enctype=<span class="code-quote">"multipart/form-data"</span>&gt;</span>
    <span class="code-tag">&lt;input type=<span class="code-quote">"hidden"</span> name=<span class="code-quote">"image@CopyFrom"</span> value=<span class="code-quote">"/tmp/upload/123"</span> /&gt;</span>
    <span class="code-tag">&lt;input type=<span class="code-quote">"text"</span> name=<span class="code-quote">"title"</span> /&gt;</span>
    <span class="code-tag">&lt;input type=<span class="code-quote">"text"</span> name=<span class="code-quote">"text"</span> /&gt;</span>
    <span class="code-tag">&lt;input type=<span class="code-quote">"Submit"</span> /&gt;</span>
<span class="code-tag">&lt;/form&gt;</span>
</pre>
</div></div>

<p>If there exists no repository item at the indicated path, nothing is done. If the item indicated by the <tt>@CopyFrom</tt> suffixed parameter already exists, it is replaced by the item addressed by the parameter value &#8211; unless of course there is no item at the named location.</p>

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

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

<p>The <tt>@CopyFrom</tt> suffixed parameter in fact calls for a sub-operation, which is executed after the <tt>@MoveFrom</tt> sub operation but before any other tasks of content creattion and modification are done.</p>



<h5><a name="ManipulatingContent-TheSlingPostServlet%28servlets.post%29-AlgorithmforNodeNameCreation"></a>Algorithm for Node Name Creation</h5>

<p>If request is posted with an URL ending in slash <tt>/</tt> or slash-star <tt>/*</tt>, the SlingPostServlet derives a name for the node to be created upon the request applying the following algorithm:</p>


<ol>
	<li>If a <tt>:name</tt> parameter is supplied, the (first) value of this parameter is used unmodified as the name for the new node. If the name is illegally formed with respect to JCR name requirements, an exception will be thrown when trying to create the node. The assumption with the <tt>:name</tt> parameter is, that the caller knows what he (or she) is supplying and should get the exact result if possible.</li>
	<li>Otherwise if a <tt>:nameHint</tt> parameter is supplied, the (first) value of this parameter is used to generate the node name. A name filtering is applied to this hint to ensure a valid JCR node name.</li>
	<li>Otherwise a series of request paramaters supplied to set content is inspected for a possible name. The list of the names of these parameter is configurable with the SlingPostServlet and defaults ot <tt>[ title, jcr:title, name, description, jcr:description, abstract ]</tt>. The first request parameter with a non-empty value is used and filtered to get the valid JCR name.</li>
	<li>Otherwise an ever increasing auto generated number is used. Filtering is also applied to this numeric name.</li>
</ol>



<p>The filtering algorithm to create a valid name of the hints from above steps (except the first) works as follows:</p>

<ul>
	<li>Convert the proposed name to all lower case.</li>
	<li>Replace all characters not in the range [0..9a..z_] by a single underscore <tt>&#95;</tt>.</li>
	<li>If the name starts with a digit prepend an underscore. Technically names with leading digits are valid, but they present major issues when using such names in JCR XPath expressions. The algorithm takes care to not create names with two or more consecutive underscore characters.</li>
	<li>Finally the name is cut to a configurable maximum length (default is 20 characters).</li>
</ul>



<p>For example the <tt>:nameHint</tt> value <em>A quick brown Fox ...</em> is filtered to become <em>a&#95;quick&#95;brown&#95;fox&#95;</em></p>

<p>After generating and filtering the name it is further guaranteed that the name is unique: If a node of the same name as just generated from the algorithm already exists below the same parent node a numeric index is appended to the new node name to make it unique.</p>


<h5><a name="ManipulatingContent-TheSlingPostServlet%28servlets.post%29-ResponseStatus"></a>Response Status</h5>

<p>The modification operation has the following status responses:</p>

<div class='table-wrap'>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'> Status </th>
<th class='confluenceTh'> Explanation </th>
</tr>
<tr>
<td class='confluenceTd'> 200/OK </td>
<td class='confluenceTd'> An existing node has been updated with content </td>
</tr>
<tr>
<td class='confluenceTd'> 201/CREATED </td>
<td class='confluenceTd'> A new node has been created and filled with content </td>
</tr>
<tr>
<td class='confluenceTd'> 500/INTERNAL SERVER ERROR </td>
<td class='confluenceTd'> Some exception, for example a <tt>RepositoryException</tt>, occurred while processing the request </td>
</tr>
</tbody></table>
</div>



<h3><a name="ManipulatingContent-TheSlingPostServlet%28servlets.post%29-ContentRemoval"></a>Content Removal</h3>

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

<div class="preformatted panel" style="border-width: 1px;"><div class="preformattedContent panelContent">
<pre>$ curl -F":operation=delete" http://host/content/sample
</pre>
</div></div>


<h5><a name="ManipulatingContent-TheSlingPostServlet%28servlets.post%29-ResponseStatus"></a>Response Status</h5>

<p>The delete operation has the following status responses:</p>

<div class='table-wrap'>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'> Status </th>
<th class='confluenceTh'> Explanation </th>
</tr>
<tr>
<td class='confluenceTd'> 200/OK </td>
<td class='confluenceTd'> The resource (and all its descendents) has been removed </td>
</tr>
<tr>
<td class='confluenceTd'> 404/NOT FOUND </td>
<td class='confluenceTd'> The request URL does not address an existing repository item </td>
</tr>
<tr>
<td class='confluenceTd'> 500/INTERNAL SERVER ERROR </td>
<td class='confluenceTd'> Some exception, for example a <tt>RepositoryException</tt>, occurred while processing the request </td>
</tr>
</tbody></table>
</div>




<h5><a name="ManipulatingContent-TheSlingPostServlet%28servlets.post%29-DeletingMultipleItems"></a>Deleting Multiple Items</h5>

<p>By using the <tt>:applyTo</tt> 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.</p>

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

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

<div class="preformatted panel" style="border-width: 1px;"><div class="preformattedContent panelContent">
<pre>$ curl -F":operation=delete" -F":applyTo=/content/page1" -F":applyTo=/content/page2" http://host/content/sample
</pre>
</div></div>

<p>If any resource listed in the <tt>:applyTo</tt> parameter does not exist, it is silently ignored.</p>

<h6><a name="ManipulatingContent-TheSlingPostServlet%28servlets.post%29-ResponseStatus"></a>Response Status</h6>

<p>The delete operation applied to multiple resources has the following status responses:</p>

<div class='table-wrap'>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'> Status </th>
<th class='confluenceTh'> Explanation </th>
</tr>
<tr>
<td class='confluenceTd'> 200/OK </td>
<td class='confluenceTd'> All requested and existing resources have been removed </td>
</tr>
<tr>
<td class='confluenceTd'> 500/INTERNAL SERVER ERROR </td>
<td class='confluenceTd'> Some exception, for example a <tt>RepositoryException</tt>, occurred while processing the request </td>
</tr>
</tbody></table>
</div>




<h3><a name="ManipulatingContent-TheSlingPostServlet%28servlets.post%29-CopyingContent"></a>Copying Content</h3>


<p>To copy existing content to a new location, the <tt>copy</tt> operation is specified. This operation copies the item addressed by the request URL to a new location indicated by the <tt>:dest</tt> parameter. The <tt>:dest</tt> parameter is the absolute or relative path to which the resource is copied. If the path is relative it is assumed to be below the same parent as the request resource. If it is terminated with a <tt>/</tt> character the request resource is copied to an item of the same name under the destination path.</p>

<p>To illustrate the <tt>:dest</tt> parameter handling, lets look at a few examples. All examples are based on addressing the <tt>/content/sample</tt> item:</p>

<div class='table-wrap'>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'> <tt>:dest</tt> Parameter </th>
<th class='confluenceTh'> Destination Absolute Path </th>
</tr>
<tr>
<td class='confluenceTd'> <tt>/content/newSample</tt> </td>
<td class='confluenceTd'> <tt>/content/newSample</tt> </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>different/newSample</tt> </td>
<td class='confluenceTd'> <tt>/content/different/newSample</tt> </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>/content/different/</tt> </td>
<td class='confluenceTd'> <tt>/content/different/sample</tt> </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>different/</tt> </td>
<td class='confluenceTd'> <tt>/content/different/sample</tt> </td>
</tr>
</tbody></table>
</div>



<p>If an item already exists at the location derived from the <tt>:dest</tt> parameter, the copy operation fails unless the <tt>:replace</tt> parameter is set to <tt>true</tt> (case is ignored when checking the parameter value).</p>



<h5><a name="ManipulatingContent-TheSlingPostServlet%28servlets.post%29-ResponseStatus"></a>Response Status</h5>

<p>The copy operation has the following status responses:</p>

<div class='table-wrap'>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'> Status </th>
<th class='confluenceTh'> Explanation </th>
</tr>
<tr>
<td class='confluenceTd'> 200/OK </td>
<td class='confluenceTd'> The node has been copied to the new location replacing an existing item at the destination </td>
</tr>
<tr>
<td class='confluenceTd'> 201/CREATED </td>
<td class='confluenceTd'> The node has been copied to the new location creating a new item at the destination </td>
</tr>
<tr>
<td class='confluenceTd'> 404/NOT FOUND </td>
<td class='confluenceTd'> The request URL does not address an existing repository item </td>
</tr>
<tr>
<td class='confluenceTd'> 412/PRECONDITION FAILED </td>
<td class='confluenceTd'> An item already exists at the destination and the <tt>:replace</tt> parameter is not set to <tt>true</tt> </td>
</tr>
<tr>
<td class='confluenceTd'> 500/INTERNAL SERVER ERROR </td>
<td class='confluenceTd'> Some exception, for example a <tt>RepositoryException</tt>, occurred while processing the request </td>
</tr>
</tbody></table>
</div>




<h5><a name="ManipulatingContent-TheSlingPostServlet%28servlets.post%29-CopyingMultipleItems"></a>Copying Multiple Items</h5>

<p>By using the <tt>:applyTo</tt> 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.</p>

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

<p>To for example copy the <tt>/content/page1</tt> and <tt>/content/page2</tt> nodes to <tt>/content/target</tt>, you might use the following command line:</p>

<div class="preformatted panel" style="border-width: 1px;"><div class="preformattedContent panelContent">
<pre>$ curl -F":operation=copy" -F":applyTo=/content/page1" -F":applyTo=/content/page2" -F":dest=/content/target/" http://host/content/sample
</pre>
</div></div>

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

<p>If any resource listed in the <tt>:applyTo</tt> 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.</p>

<h6><a name="ManipulatingContent-TheSlingPostServlet%28servlets.post%29-ResponseStatus"></a>Response Status</h6>

<p>The copy operation applied to multiple resources has the following status responses:</p>

<div class='table-wrap'>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'> Status </th>
<th class='confluenceTh'> Explanation </th>
</tr>
<tr>
<td class='confluenceTd'> 200/OK </td>
<td class='confluenceTd'> All requested and existing resources have been copied </td>
</tr>
<tr>
<td class='confluenceTd'> 412/PRECONDITION FAILED </td>
<td class='confluenceTd'> The node indicated by the <tt>:dest</tt> parameter does not exist </td>
</tr>
<tr>
<td class='confluenceTd'> 500/INTERNAL SERVER ERROR </td>
<td class='confluenceTd'> Some exception, for example a <tt>RepositoryException</tt>, occurred while processing the request. This status is also set if the <tt>:dest</tt> parameter value does not have a trailing slash character. </td>
</tr>
</tbody></table>
</div>






<h3><a name="ManipulatingContent-TheSlingPostServlet%28servlets.post%29-MovingContent"></a>Moving Content</h3>


<p>To move existing content to a new location, the <tt>move</tt> operation is specified. This operation moves the item addressed by the request URL to a new location indicated by the <tt>:dest</tt> parameter. The <tt>:dest</tt> parameter is the absolute or relative path to which the resource is moved. If the path is relative it is assumed to be below the same parent as the request resource. If it is terminated with a <tt>/</tt> character the request resource is moved to an item of the same name under the destination path.</p>

<p>To illustrate the <tt>:dest</tt> parameter handling, lets look at a few examples. All examples are based on addressing the <tt>/content/sample</tt> item:</p>

<div class='table-wrap'>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'> <tt>:dest</tt> Parameter </th>
<th class='confluenceTh'> Destination Absolute Path </th>
</tr>
<tr>
<td class='confluenceTd'> <tt>/content/newSample</tt> </td>
<td class='confluenceTd'> <tt>/content/newSample</tt> </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>different/newSample</tt> </td>
<td class='confluenceTd'> <tt>/content/different/newSample</tt> </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>/content/different/</tt> </td>
<td class='confluenceTd'> <tt>/content/different/sample</tt> </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>different/</tt> </td>
<td class='confluenceTd'> <tt>/content/different/sample</tt> </td>
</tr>
</tbody></table>
</div>



<p>If an item already exists at the location derived from the <tt>:dest</tt> parameter, the move operation fails unless the <tt>:replace</tt> parameter is set to <tt>true</tt> (case is ignored when checking the parameter value).</p>



<h5><a name="ManipulatingContent-TheSlingPostServlet%28servlets.post%29-ResponseStatus"></a>Response Status</h5>

<p>The move operation has the following status responses:</p>

<div class='table-wrap'>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'> Status </th>
<th class='confluenceTh'> Explanation </th>
</tr>
<tr>
<td class='confluenceTd'> 200/OK </td>
<td class='confluenceTd'> The node has been moved to the new location replacing an existing item at the destination </td>
</tr>
<tr>
<td class='confluenceTd'> 201/CREATED </td>
<td class='confluenceTd'> The node has been moved to the new location creating a new item at the destination </td>
</tr>
<tr>
<td class='confluenceTd'> 404/NOT FOUND </td>
<td class='confluenceTd'> The request URL does not address an existing repository item </td>
</tr>
<tr>
<td class='confluenceTd'> 412/PRECONDITION FAILED </td>
<td class='confluenceTd'> An item already exists at the destination and the <tt>:replace</tt> parameter is not set to <tt>true</tt> </td>
</tr>
<tr>
<td class='confluenceTd'> 500/INTERNAL SERVER ERROR </td>
<td class='confluenceTd'> Some exception, for example a <tt>RepositoryException</tt>, occurred while processing the request </td>
</tr>
</tbody></table>
</div>




<h5><a name="ManipulatingContent-TheSlingPostServlet%28servlets.post%29-MovingMultipleItems"></a>Moving Multiple Items</h5>

<p>By using the <tt>:applyTo</tt> 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.</p>

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

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

<div class="preformatted panel" style="border-width: 1px;"><div class="preformattedContent panelContent">
<pre>$ curl -F":operation=move" -F":applyTo=/content/page1" -F":applyTo=/content/page2" -F":dest=/content/target/" http://host/content/sample
</pre>
</div></div>

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

<p>If any resource listed in the <tt>:applyTo</tt> 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.</p>

<h6><a name="ManipulatingContent-TheSlingPostServlet%28servlets.post%29-ResponseStatus"></a>Response Status</h6>

<p>The move operation applied to multiple resources has the following status responses:</p>

<div class='table-wrap'>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'> Status </th>
<th class='confluenceTh'> Explanation </th>
</tr>
<tr>
<td class='confluenceTd'> 200/OK </td>
<td class='confluenceTd'> All requested and existing resources have been moved </td>
</tr>
<tr>
<td class='confluenceTd'> 412/PRECONDITION FAILED </td>
<td class='confluenceTd'> The node indicated by the <tt>:dest</tt> parameter does not exist </td>
</tr>
<tr>
<td class='confluenceTd'> 500/INTERNAL SERVER ERROR </td>
<td class='confluenceTd'> Some exception, for example a <tt>RepositoryException</tt>, occurred while processing the request. This status is also set if the <tt>:dest</tt> parameter value does not have a trailing slash character. </td>
</tr>
</tbody></table>
</div>




<h3><a name="ManipulatingContent-TheSlingPostServlet%28servlets.post%29-NullOperation"></a>Null Operation</h3>

<p>Sometimes it is useful to explicitly request that nothing is to be done. The SlingPostServlet now provides such an operation under the name <tt>nop</tt>. Apart from doing nothing, the <tt>nop</tt> operations sets the response status to either the default <tt>200/OK</tt> or to any status requested by the <tt>:nopstatus</tt> request parameter.</p>

<p>The <tt>:nopstatus</tt> request parameter must be an integral number in the range [ 100 .. 999 ]. If the parameter value cannot be parsed to an integer or the value is outside of this range, the default status <tt>200/OK</tt> is still set.</p>


<h6><a name="ManipulatingContent-TheSlingPostServlet%28servlets.post%29-ResponseStatus"></a>Response Status</h6>

<p>The null operation sets a default status or the status requested by the <tt>:nopstatus</tt> request parameter.</p>

<div class='table-wrap'>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'> Status </th>
<th class='confluenceTh'> Explanation </th>
</tr>
<tr>
<td class='confluenceTd'> 200/OK </td>
<td class='confluenceTd'> Default status set if <tt>:nopstatus</tt> parameter is not set or does not have a valid value </td>
</tr>
<tr>
<td class='confluenceTd'> {:nopstatus} </td>
<td class='confluenceTd'> The status as requested by the <tt>:nopstatus</tt> parameter </td>
</tr>
</tbody></table>
</div>




<h2><a name="ManipulatingContent-TheSlingPostServlet%28servlets.post%29-SpecialParameters"></a>Special Parameters</h2>


<p>Some parameters have special significance for the complete processing of the SlingPostServlet or are used by multiple operations. This section summarizes these parameters:</p>


<h3><a name="ManipulatingContent-TheSlingPostServlet%28servlets.post%29-%7B%7B%3Aorder%7D%7D"></a><tt>:order</tt></h3>

<p>Child nodes may be ordered if the primary node type of their common parent node is defined as having orderable child nodes. To employ such ordering, the content creation/modification, move and copy operations support the <tt>:order</tt> parameter which apply child node ordering amongst its siblings of the target node.</p>

<p>The <tt>:order</tt> parameter may have the following values:</p>

<div class='table-wrap'>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'> Value </th>
<th class='confluenceTh'> Description </th>
</tr>
<tr>
<td class='confluenceTd'> <tt>first</tt> </td>
<td class='confluenceTd'> Place the target node as the first amongst its siblings </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>last</tt> </td>
<td class='confluenceTd'> Place the target node as the last amongst its siblings </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>before <em>xyz</em></tt> </td>
<td class='confluenceTd'> Place the target node immediately before the sibling whose name is <em>xyz</em> </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>after <em>xyz</em></tt> </td>
<td class='confluenceTd'> Place the target node immediately after the sibling whose name is <em>xyz</em> </td>
</tr>
<tr>
<td class='confluenceTd'> numeric </td>
<td class='confluenceTd'> Place the target node at the indicated numeric place amongst its siblings where <em>0</em> is equivalent to <tt>first</tt> and <em>1</em> means the second place </td>
</tr>
</tbody></table>
</div>



<p>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 <tt>:order</tt> parameter. For example to order the <tt>/content/sample/page5</tt> resource above its sibling resource <tt>/content/sample/other</tt> a simple request</p>

<div class="preformatted panel" style="border-width: 1px;"><div class="preformattedContent panelContent">
<pre>$ curl -F":order=before other" http://host/content/sample/page5
</pre>
</div></div>

<p>does the trick. To be redirected after the reodering, the <tt>:redirect</tt> parameter may optionally also be specified.</p>


<h3><a name="ManipulatingContent-TheSlingPostServlet%28servlets.post%29-%7B%7B%3Aredirect%7D%7D"></a><tt>:redirect</tt></h3>

<p>Instructs the SlingPostServlet to redirect the client to the indicated location if the operation succeeds. That is the reponse status is set to <em>302/FOUND</em> and the <tt>Location</tt> header is set to the value of the <tt>:redirect</tt> parameter.</p>


<h3><a name="ManipulatingContent-TheSlingPostServlet%28servlets.post%29-%7B%7B%3Astatus%7D%7D"></a><tt>:status</tt></h3>

<p>By default the SlingPostServlet sets response status according to the status of the operation executed. In some cases, it may be desirable to not have the real status codes (e.g. 404 or 505) but a normal <em>200/OK</em> to trick the client browser into displaying the response content generated by the SlingPostServlet.</p>

<p>To not send the actual response status back to the client, the <tt>:status</tt> request parameter should be set to <tt>browser</tt>. If this parameter is not set, is empty, is set to <tt>standard</tt> or to any other value, the actual status code is sent back to the client.</p>


<h2><a name="ManipulatingContent-TheSlingPostServlet%28servlets.post%29-Responseformat"></a>Response format</h2>

<p>The SlingPostServlet produces a basic HTTP response body, listing the response status, what changes have been made, and other meta-data about the result of the POST request.</p>

<p>The format of this response is either HTML or JSON (JSON support introduced with <a href="https://issues.apache.org/jira/browse/SLING-1336" class="external-link" rel="nofollow">SLING-1336</a>). SlingPostServlet determines which format to use by examining the Accept header of the incoming request. If the client has specified a preference for the media type "application/json", the JSON format is used, otherwise HTML is returned.<br/>
The Accept header can be overridden (and simulated) by posting a :http-equiv-accept field, which should have the same format as the Accept header.</p>

<p>Examples:</p>
<ol>
	<li>Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*<b>/</b>*;q=0.8</li>
	<li>Accept: application/json,*<b>/</b>*;q=0.9</li>
</ol>


<p>In example #1, SlingPostServlet will return HTML, since the client has specified a preference for text/html. In example #2, SlingPostServlet will return JSON.</p>

<p>See <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.1" class="external-link" rel="nofollow">RFC 2616, section 14.1</a> for information on the HTTP Accept header.</p>

<h2><a name="ManipulatingContent-TheSlingPostServlet%28servlets.post%29-AdvancedTopic%3AExtendingtheSlingPostServlet"></a>Advanced Topic: Extending the SlingPostServlet</h2>


<p>The supported operations of the SlingPostServlet may be extended by registering an OSGi service of type <tt>org.apache.sling.servlets.post.SlingPostOperation</tt> with the <tt>sling.post.operation</tt> service registration property set to the name of the operation. This name may then be used as the value of the <tt>:operation</tt> parameter to select the extended operation.</p>

<p>As a simple example, let us register an operation named <em>ping</em>. This operation actually does nothing actually. First let's create the operation. We are using the Apache Felix Maven SCR Plugin to create an Declarative Services descriptor for this operation, so apart from packaging this class and the descriptor in a bundle, there is nothing more to be done.</p>

<div class="code panel" style="border-width: 1px;"><div class="codeHeader panelHeader" style="border-bottom-width: 1px;"><b>PingOperation.java</b></div><div class="codeContent panelContent">
<pre class="code-java">
/**
 * @scr.component metatype=<span class="code-quote">"no"</span> immediate=<span class="code-quote">"<span class="code-keyword">true</span>"</span>
 * @scr.service
 * @scr.property name=<span class="code-quote">"sling.post.operation"</span> value=<span class="code-quote">"ping"</span>
 */
<span class="code-keyword">public</span> class PingOperation <span class="code-keyword">implements</span> SlingPostOperation {

<span class="code-keyword">public</span> void run(SlingHttpServletRequest request, HtmlResponse response, SlingPostProcessor[] processors) {
  }
}
</pre>
</div></div>

<p>That's all. Package this in a bundle and deploy to a running Sling instance. Then you POST to this using <tt>ping</tt> as the operation name:</p>

<div class="preformatted panel" style="border-width: 1px;"><div class="preformattedContent panelContent">
<pre>$ curl -F":operation=ping" http://host/content
</pre>
</div></div>


<p>For your convenience an abstract implementation of this interface exists as the <tt>org.apache.sling.servlets.post.AbstractSlingPostOperation</tt> abstract class, from which you may extend and implement the <tt>doRun(SlingHttpServletRequest, HtmlResponse,  java.util.List&lt;Modification&gt;)</tt> method.</p>
    </div>
        <div id="commentsSection" class="wiki-content pageSection">
        <div style="float: right;">
            <a href="http://cwiki.apache.org/confluence/users/viewnotifications.action" class="grey">Change Notification Preferences</a>
        </div>
        <a href="http://cwiki.apache.org/confluence/display/SLINGxSITE/Manipulating+Content+-+The+SlingPostServlet+%28servlets.post%29">View Online</a>
        |
        <a href="http://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=85794&revisedVersion=32&originalVersion=31">View Changes</a>
                |
        <a href="http://cwiki.apache.org/confluence/display/SLINGxSITE/Manipulating+Content+-+The+SlingPostServlet+%28servlets.post%29?showComments=true&amp;showCommentArea=true#addcomment">Add Comment</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message